请哪位C程序高手,帮我编我一个小程序,谢谢~急!!!
输入课程成绩:高数、英语、程序、线代、体育、管理学,输出以下表格:4)某个学生本学期的成绩单5)某门课程每个班的总平均粉(从高到低排列)6)某门课程某班的成绩单(按学号排...
输入课程成绩:高数、英语、程序、线代、体育、管理学,输出以下表格:
4)某个学生本学期的成绩单
5)某门课程每个班的总平均粉(从高到低排列)
6)某门课程某班的成绩单(按学号排列) 展开
4)某个学生本学期的成绩单
5)某门课程每个班的总平均粉(从高到低排列)
6)某门课程某班的成绩单(按学号排列) 展开
2个回答
展开全部
我写了一个,大体按照你的题意,主程序和一些数据结构需要你自己改动。
两个文件,以下是程序清单:
/* student.h */
#ifndef ST_H
#define ST_H
/* execute status, use to detect return value of function. */
#define ERROR 0
#define SUCCES 1
/* we assume insert order is big to small. */
#define ORDER BIG_TO_SMALL
enum INSERT_ORDER
{
BIG_TO_SMALL = 0,
SMALL_TO_BIG = 1
};
/* student ADS */
typedef struct STUDENT
{
/* basic info, require user input */
char *name;
char *id;
char *class_id;
float math;
float english;
float sports;
/* list in chain, hide for user */
struct STUDENT *next;
}STUDENT, *pST;
size_t ST_LEN = sizeof (STUDENT);
/* NODE must be a pointer type */
#define NEW(NODE) ( (NODE*)malloc( sizeof(NODE) ) )
/* is there any effect? never used! */
enum CLASS_ID
{
B1 = 0,
B2, B3, B4,
B5, B6, B7,
B8, B9, B10,
LAST_CLASS
};
char *class_name[LAST_CLASS] = {
"class1", "class2", "class3", "class4",
"class5", "class6", "class7", "class8",
"class9", "class10"
};
#define CLASS_NAME(id) (class_name[id])
/* suppose number of ban is 10. */
pST bucket[LAST_CLASS];
/* class_id must be char*. id is an integer type node. */
#define BUCKET_ID(class_id) ((15U & *class_id) - 1)
enum COURSE_ID
{
MATH,
ENGLISH,
SPORTS,
LAST_COURSE
};
/* I don't ensure it can execute right all the time! */
#define COURSE_SCORE(student, course_id) \
*( (float*)((char *)student + (sizeof(char *) * 3)) + course_id)
/* course name array. */
char *course_name[LAST_COURSE] = {"math", "english", "sports"};
#define COURSE_NAME(course_id) (course_name[course_id])
/* this struct just use to statistics the average score. */
typedef struct class_average
{
char *class_name;
float average;
} CLASS_AVERAGE;
/* access the average and class_name */
#define AVERAGE_SCORE(NODE) (NODE.average)
#define AVERAGE_NAME(NODE) (NODE.class_name)
/* function def. */
void init_bucket(pST bucket[]);
int insert (pST student);
void print_student_score (char *name, char *class_id);
void print_score_list (enum COURSE_ID course, char *class_id);
void print_average (enum COURSE_ID course, enum INSERT_ORDER order);
#endif /* student.h end */
/* student.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"
/* main function, test the function */
int main()
{
/* we have 4 student info
caution: the name, id and class must be the type of char *, and class just like
"1" or "4" is correct, else is incorrect. */
STUDENT st0 = {"jack", "620", "1", 92, 12, 50, NULL};
STUDENT st1 = {"w", "621", "2", 40, 40, 40, NULL};
STUDENT st2 = {"x", "59", "1", 10, 0, 50, NULL};
STUDENT st3 = {"y", "700", "4", 20, 30, 70, NULL};
/* initial bucket, maybe it's unnecessary. */
init_bucket (bucket);
/* add student info into bucket(list). */
insert (&st0);
insert (&st1);
insert (&st2);
insert (&st3);
/* print the score list of student "jack" in this term. */
print_student_score ("jack", "1");
/* print the score of courese MATH in class "1". */
print_score_list (MATH, "1");
/* print the average score of course MATH in each classes of all. */
print_average (MATH, BIG_TO_SMALL);
return 0;
}
/* maybe it is unnecessarily. */
void init_bucket(pST bucket[])
{
int i = 0;
while (i<LAST_CLASS)
bucket[i++] = NULL;
}
/* create and modify bucket */
int insert (pST student)
{
pST *head;
pST prev, cur;
pST st;
/* find the bucket entry. */
head = &bucket[BUCKET_ID (student->class_id)];
/* we never want to insert null_value in bucket,
although it is easy to achieve in this function. */
if (!student)
{
printf ("forbidden insert NULL element!\n");
return ERROR;
}
/* although student is an pointer type, but we must realloc
mem for st, because student maybe an address of auto varibal's. */
st = (pST)malloc(ST_LEN);
if (!st)
{
printf ("memory alloc failed!\n");
return ERROR;
}
/* we should be copied the content to st. */
memcpy (st, student, ST_LEN);
/*may be a NULL_LIST, we insert to the head*/
if (!*head)
{
*head = st;
st->next = NULL;
return SUCCES;
}
/*if not a NULL_LIST, we should find a slot to insert*/
cur = *head;
prev = NULL;
while (cur)
{
#if ORDER
if (strcmp(st->id, cur->id) > 0)
#else
if (strcmp(st->id, cur->id) < 0)
#endif
{
prev = cur;
cur = cur->next;
}
else
break;
}
/* we find it, then insert in list */
if (!prev)
{
*head = st;
st->next = cur;
}
else
{
prev->next = st;
st->next = cur;
}
return SUCCES;
}
/* print the score list of a student */
void print_student_score (char *name, char *class_id)
{
pST head = bucket[BUCKET_ID (class_id)];
while (head)
{
if (strcmp (head->name, name) == 0)
break;
head = head->next;
}
/* if head is NULL, so we known there isn't this man */
if (!head)
{
printf ("we can't find %s in %s!\n", name, CLASS_NAME (BUCKET_ID(class_id)));
return;
}
printf ("score list of student %s:\n", name);
printf (" MATH %.1f\n", head->math);
printf (" ENGLISH %.1f\n", head->english);
printf (" SPORTS %.1f\n", head->sports);
}
/* print the score of one course in one classes */
void print_score_list (enum COURSE_ID course, char *class_id)
{
pST head = bucket[BUCKET_ID (class_id)];
printf ("score list of %s in %s\n", COURSE_NAME (course), CLASS_NAME (BUCKET_ID(class_id)));
while (head)
{
printf ("%s\t%.1f\n", head->name, COURSE_SCORE (head, course));
head = head->next;
}
}
/* print one course average of all classes, ordered by order we set */
void print_average (enum COURSE_ID course, enum INSERT_ORDER order)
{
CLASS_AVERAGE average[LAST_CLASS];
pST head;
int i, cnt = 0;
/* we must initial the array before we use it. */
memset(average, 0, sizeof(average));
/* compute the average score and sotre it in the array average. */
for (i=0; i<LAST_CLASS; i++)
{
head = bucket[i];
AVERAGE_NAME(average[i]) = CLASS_NAME (i);
while (head)
{
AVERAGE_SCORE (average[i]) += COURSE_SCORE (head, course);
head = head->next;
cnt++;
}
if (cnt != 0)
AVERAGE_SCORE(average[i]) /= cnt;
cnt = 0;
}
/* sort the array, and ordered by order we set. */
for (i=0; i<LAST_CLASS; i++)
{ for (cnt=i+1; cnt<LAST_CLASS; cnt++)
{
#if order /* SMALL_TO_BIG */
if (AVERAGE_SCORE(average[i]) > AVERAGE_SCORE(average[cnt]))
#else /* BIG_TO_SMALL */
if (AVERAGE_SCORE(average[i]) < AVERAGE_SCORE(average[cnt]))
#endif
{
CLASS_AVERAGE tmp = average[i];
average[i] = average[cnt];
average[cnt] = tmp;
}
}
}
/* we have order the array by order we set, so print it */
printf ("average score of course %s in each class of all\n", COURSE_NAME (course));
for (i=0; i<LAST_CLASS; i++)
printf ("%s %s average score: %.1f\n", AVERAGE_NAME(average[i]), COURSE_NAME (course), AVERAGE_SCORE(average[i]));
}
两个文件,以下是程序清单:
/* student.h */
#ifndef ST_H
#define ST_H
/* execute status, use to detect return value of function. */
#define ERROR 0
#define SUCCES 1
/* we assume insert order is big to small. */
#define ORDER BIG_TO_SMALL
enum INSERT_ORDER
{
BIG_TO_SMALL = 0,
SMALL_TO_BIG = 1
};
/* student ADS */
typedef struct STUDENT
{
/* basic info, require user input */
char *name;
char *id;
char *class_id;
float math;
float english;
float sports;
/* list in chain, hide for user */
struct STUDENT *next;
}STUDENT, *pST;
size_t ST_LEN = sizeof (STUDENT);
/* NODE must be a pointer type */
#define NEW(NODE) ( (NODE*)malloc( sizeof(NODE) ) )
/* is there any effect? never used! */
enum CLASS_ID
{
B1 = 0,
B2, B3, B4,
B5, B6, B7,
B8, B9, B10,
LAST_CLASS
};
char *class_name[LAST_CLASS] = {
"class1", "class2", "class3", "class4",
"class5", "class6", "class7", "class8",
"class9", "class10"
};
#define CLASS_NAME(id) (class_name[id])
/* suppose number of ban is 10. */
pST bucket[LAST_CLASS];
/* class_id must be char*. id is an integer type node. */
#define BUCKET_ID(class_id) ((15U & *class_id) - 1)
enum COURSE_ID
{
MATH,
ENGLISH,
SPORTS,
LAST_COURSE
};
/* I don't ensure it can execute right all the time! */
#define COURSE_SCORE(student, course_id) \
*( (float*)((char *)student + (sizeof(char *) * 3)) + course_id)
/* course name array. */
char *course_name[LAST_COURSE] = {"math", "english", "sports"};
#define COURSE_NAME(course_id) (course_name[course_id])
/* this struct just use to statistics the average score. */
typedef struct class_average
{
char *class_name;
float average;
} CLASS_AVERAGE;
/* access the average and class_name */
#define AVERAGE_SCORE(NODE) (NODE.average)
#define AVERAGE_NAME(NODE) (NODE.class_name)
/* function def. */
void init_bucket(pST bucket[]);
int insert (pST student);
void print_student_score (char *name, char *class_id);
void print_score_list (enum COURSE_ID course, char *class_id);
void print_average (enum COURSE_ID course, enum INSERT_ORDER order);
#endif /* student.h end */
/* student.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"
/* main function, test the function */
int main()
{
/* we have 4 student info
caution: the name, id and class must be the type of char *, and class just like
"1" or "4" is correct, else is incorrect. */
STUDENT st0 = {"jack", "620", "1", 92, 12, 50, NULL};
STUDENT st1 = {"w", "621", "2", 40, 40, 40, NULL};
STUDENT st2 = {"x", "59", "1", 10, 0, 50, NULL};
STUDENT st3 = {"y", "700", "4", 20, 30, 70, NULL};
/* initial bucket, maybe it's unnecessary. */
init_bucket (bucket);
/* add student info into bucket(list). */
insert (&st0);
insert (&st1);
insert (&st2);
insert (&st3);
/* print the score list of student "jack" in this term. */
print_student_score ("jack", "1");
/* print the score of courese MATH in class "1". */
print_score_list (MATH, "1");
/* print the average score of course MATH in each classes of all. */
print_average (MATH, BIG_TO_SMALL);
return 0;
}
/* maybe it is unnecessarily. */
void init_bucket(pST bucket[])
{
int i = 0;
while (i<LAST_CLASS)
bucket[i++] = NULL;
}
/* create and modify bucket */
int insert (pST student)
{
pST *head;
pST prev, cur;
pST st;
/* find the bucket entry. */
head = &bucket[BUCKET_ID (student->class_id)];
/* we never want to insert null_value in bucket,
although it is easy to achieve in this function. */
if (!student)
{
printf ("forbidden insert NULL element!\n");
return ERROR;
}
/* although student is an pointer type, but we must realloc
mem for st, because student maybe an address of auto varibal's. */
st = (pST)malloc(ST_LEN);
if (!st)
{
printf ("memory alloc failed!\n");
return ERROR;
}
/* we should be copied the content to st. */
memcpy (st, student, ST_LEN);
/*may be a NULL_LIST, we insert to the head*/
if (!*head)
{
*head = st;
st->next = NULL;
return SUCCES;
}
/*if not a NULL_LIST, we should find a slot to insert*/
cur = *head;
prev = NULL;
while (cur)
{
#if ORDER
if (strcmp(st->id, cur->id) > 0)
#else
if (strcmp(st->id, cur->id) < 0)
#endif
{
prev = cur;
cur = cur->next;
}
else
break;
}
/* we find it, then insert in list */
if (!prev)
{
*head = st;
st->next = cur;
}
else
{
prev->next = st;
st->next = cur;
}
return SUCCES;
}
/* print the score list of a student */
void print_student_score (char *name, char *class_id)
{
pST head = bucket[BUCKET_ID (class_id)];
while (head)
{
if (strcmp (head->name, name) == 0)
break;
head = head->next;
}
/* if head is NULL, so we known there isn't this man */
if (!head)
{
printf ("we can't find %s in %s!\n", name, CLASS_NAME (BUCKET_ID(class_id)));
return;
}
printf ("score list of student %s:\n", name);
printf (" MATH %.1f\n", head->math);
printf (" ENGLISH %.1f\n", head->english);
printf (" SPORTS %.1f\n", head->sports);
}
/* print the score of one course in one classes */
void print_score_list (enum COURSE_ID course, char *class_id)
{
pST head = bucket[BUCKET_ID (class_id)];
printf ("score list of %s in %s\n", COURSE_NAME (course), CLASS_NAME (BUCKET_ID(class_id)));
while (head)
{
printf ("%s\t%.1f\n", head->name, COURSE_SCORE (head, course));
head = head->next;
}
}
/* print one course average of all classes, ordered by order we set */
void print_average (enum COURSE_ID course, enum INSERT_ORDER order)
{
CLASS_AVERAGE average[LAST_CLASS];
pST head;
int i, cnt = 0;
/* we must initial the array before we use it. */
memset(average, 0, sizeof(average));
/* compute the average score and sotre it in the array average. */
for (i=0; i<LAST_CLASS; i++)
{
head = bucket[i];
AVERAGE_NAME(average[i]) = CLASS_NAME (i);
while (head)
{
AVERAGE_SCORE (average[i]) += COURSE_SCORE (head, course);
head = head->next;
cnt++;
}
if (cnt != 0)
AVERAGE_SCORE(average[i]) /= cnt;
cnt = 0;
}
/* sort the array, and ordered by order we set. */
for (i=0; i<LAST_CLASS; i++)
{ for (cnt=i+1; cnt<LAST_CLASS; cnt++)
{
#if order /* SMALL_TO_BIG */
if (AVERAGE_SCORE(average[i]) > AVERAGE_SCORE(average[cnt]))
#else /* BIG_TO_SMALL */
if (AVERAGE_SCORE(average[i]) < AVERAGE_SCORE(average[cnt]))
#endif
{
CLASS_AVERAGE tmp = average[i];
average[i] = average[cnt];
average[cnt] = tmp;
}
}
}
/* we have order the array by order we set, so print it */
printf ("average score of course %s in each class of all\n", COURSE_NAME (course));
for (i=0; i<LAST_CLASS; i++)
printf ("%s %s average score: %.1f\n", AVERAGE_NAME(average[i]), COURSE_NAME (course), AVERAGE_SCORE(average[i]));
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询