diff --git "a/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.c" "b/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.c" index 00b9f57..8961806 100644 --- "a/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.c" +++ "b/\346\210\220\347\273\251\347\256\241\347\220\206\347\263\273\347\273\237.c" @@ -1,55 +1,53 @@ #include #include #include -#include "malloc.h" #include -#define MAX_LEN 10 /* 字符串最大长度 */ -#define STU_NUM 30 /* 最多的学生人数 */ -#define COURSE_NUM 6 /* 最多的考试科目数 */ +#define MAX_LEN 10 /* 瀛楃涓叉渶澶ч暱搴 */ +#define STU_NUM 30 /* 鏈澶氱殑瀛︾敓浜烘暟 */ +#define COURSE_NUM 6 /* 鏈澶氱殑鑰冭瘯绉戠洰鏁 */ #define LEN sizeof(struct Student) typedef struct Student { - long num; /* 每个学生的学号 */ - char name[MAX_LEN]; /* 每个学生的姓名 */ - float score[COURSE_NUM]; /* 每个学生COURSE_NUM门功课的成绩 */ - float sum; /* 每个学生的总成绩 */ - float aver; /* 每个学生的平均成绩 */ + long num; /* 姣忎釜瀛︾敓鐨勫鍙 */ + char name[MAX_LEN]; /* 姣忎釜瀛︾敓鐨勫鍚 */ + float score[COURSE_NUM]; /* 姣忎釜瀛︾敓COURSE_NUM闂ㄥ姛璇剧殑鎴愮哗 */ + float sum; /* 姣忎釜瀛︾敓鐨勬绘垚缁 */ + float aver; /* 姣忎釜瀛︾敓鐨勫钩鍧囨垚缁 */ struct Student *next; }STU; - -int Menu(void); //创建菜单 -void Print(STU *head, int n, int m); //打印函数 -void AverSumofEveryStudent(STU *head, int n, int m); //计算每门课程的总分和平均分 -void AverSumofEveryCourse(STU *head, int n, int m); //计算每个学生的总分和平均分 -STU *SortbyScore(STU *head, int n); //按每个学生的总分由高到低排出名次表 -STU *Creat(int n, int m); //创建链表并录入信息 +int Menu(void); //鍒涘缓鑿滃崟 +void Print(STU *head, int n, int m); //鎵撳嵃鍑芥暟 +void AverSumofEveryStudent(STU *head, int n, int m); //璁$畻姣忛棬璇剧▼鐨勬诲垎鍜屽钩鍧囧垎 +void AverSumofEveryCourse(STU *head, int n, int m); //璁$畻姣忎釜瀛︾敓鐨勬诲垎鍜屽钩鍧囧垎 +STU *SortbyScore(STU *head, int n); //鎸夋瘡涓鐢熺殑鎬诲垎鐢遍珮鍒颁綆鎺掑嚭鍚嶆琛 +STU *Creat(int n, int m); //鍒涘缓閾捐〃骞跺綍鍏ヤ俊鎭 STU *Creat1(int n, int m); -STU *SortbyScore1(STU *head, int n); //按每个学生的总分由低到高排出名次表 -STU *SortbyNum(STU *head); //按学号由小到大排出成绩表 -STU *SortbyName(STU *head, int n); //按姓名的字典顺序排出成绩表 -void SearchbyNum(STU *head, int n, int m); //按学号查询学生排名及其考试成绩 -void SearchbyName(STU *head, int n, int m); //按姓名查询学生排名及其考试成绩 -void StatisticAnalysis(STU *head, int n, int m); //按类别及比例输出 -void WritetoFile(STU *head, int n, int m); //将每个学生的纪录信息写入文件 -STU *ReadfromFile(STU *head, int *n, int *m); //从文件中读出每个学生的纪录信息并显示 +STU *SortbyScore1(STU *head, int n); //鎸夋瘡涓鐢熺殑鎬诲垎鐢变綆鍒伴珮鎺掑嚭鍚嶆琛 +STU *SortbyNum(STU *head); //鎸夊鍙风敱灏忓埌澶ф帓鍑烘垚缁╄〃 +STU *SortbyName(STU *head, int n); //鎸夊鍚嶇殑瀛楀吀椤哄簭鎺掑嚭鎴愮哗琛 +void SearchbyNum(STU *head, int n, int m); //鎸夊鍙锋煡璇㈠鐢熸帓鍚嶅強鍏惰冭瘯鎴愮哗 +void SearchbyName(STU *head, int n, int m); //鎸夊鍚嶆煡璇㈠鐢熸帓鍚嶅強鍏惰冭瘯鎴愮哗 +void StatisticAnalysis(STU *head, int n, int m); //鎸夌被鍒強姣斾緥杈撳嚭 +void WritetoFile(STU *head, int n, int m); //灏嗘瘡涓鐢熺殑绾綍淇℃伅鍐欏叆鏂囦欢 +STU *ReadfromFile(STU *head, int *n, int *m); //浠庢枃浠朵腑璇诲嚭姣忎釜瀛︾敓鐨勭邯褰曚俊鎭苟鏄剧ず int main() { int n, m; int i; - STU *head; //定义头节点 + STU *head; //瀹氫箟澶磋妭鐐 head = (STU *)malloc(LEN); while (1) { i = Menu(); if (i == 1) { - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\t\t\t******************************************************************************\n"); - printf("\t\t\tInput student number(n<30):\n"); //输入学生数 + printf("\t\t\tInput student number(n<30):\n"); //杈撳叆瀛︾敓鏁 printf("\t\t\t"); scanf("%d", &n); printf("\t\t\tInput course number(m<=6):\n"); @@ -61,84 +59,84 @@ int main() case 1: printf("\t\t\tInput student's ID, name and score:\n"); head = Creat(n, m); - system("cls"); //清屏 + system("cls"); //娓呭睆 break; case 2: - system("cls"); //清屏 + system("cls"); //娓呭睆 AverSumofEveryStudent(head, n, m); break; case 3: - system("cls"); //清屏 + system("cls"); //娓呭睆 AverSumofEveryCourse(head, n, m); break; case 4: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\tSort in ascending order by score:\n"); head = SortbyScore(head, n); Print(head, n, m); break; case 5: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\tSort in ascending order by score:\n"); head = SortbyScore1(head, n); Print(head, n, m); break; case 6: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\tSort in ascending order by number:\n"); head = SortbyNum(head); Print(head, n, m); break; case 7: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\tSort in dictionary order by name:\n"); head = SortbyName(head, n); Print(head, n, m); break; case 8: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\t******************************************************************************\n"); printf("\t\t\tInput the number you want to search:\n"); SearchbyNum(head, n, m); break; case 9: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\t******************************************************************************\n"); printf("\t\t\tInput the name you want to search:\n"); SearchbyName(head, n, m); break; case 10: - system("cls"); //清屏 + system("cls"); //娓呭睆 StatisticAnalysis(head, n, m); break; case 11: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); Print(head, n, m); break; case 12: - system("cls"); //清屏 + system("cls"); //娓呭睆 WritetoFile(head, n, m); break; case 13: - system("cls"); //清屏 + system("cls"); //娓呭睆 head = ReadfromFile(head, &n, &m); break; case 0: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\t******************************************************************************\n"); printf("\t\t\tEnd of program!\n"); printf("\t\t\t******************************************************************************\n"); return 0; default: - system("cls"); //清屏 + system("cls"); //娓呭睆 printf("\n\n\n"); printf("\t\t\t******************************************************************************\n"); printf("\t\t\tInput error!\n"); @@ -152,41 +150,40 @@ int main() } -// 创建菜单 +// 鍒涘缓鑿滃崟 int Menu(void) { int i; - system("title 学生成绩管理系统V7.0 By wyxiang"); - printf("\n\n"); - printf("\t\t\t 学生成绩管理系统V7.0\n"); - printf("\t\t\t******************************************************************************\n"); - printf("\t\t\t* Management for Students' scores *\n"); - printf("\t\t\t* 1. Input record *\n"); - printf("\t\t\t* 2. Caculate total and average score of every course *\n"); - printf("\t\t\t* 3. Caculate total and average score of every student *\n"); - printf("\t\t\t* 4. Sort in descending order by score *\n"); - printf("\t\t\t* 5. Sort in ascending order by score *\n"); - printf("\t\t\t* 6. Sort in ascending order by number *\n"); - printf("\t\t\t* 7. Sort in dictionary order by name *\n"); - printf("\t\t\t* 8. Search by number *\n"); - printf("\t\t\t* 9. Search by name *\n"); - printf("\t\t\t* 10. Statistic analysis *\n"); - printf("\t\t\t* 11. List record *\n"); - printf("\t\t\t* 12. Write to a file *\n"); - printf("\t\t\t* 13. Read from a file *\n"); - printf("\t\t\t* 0. Exit *\n"); - printf("\t\t\t******************************************************************************\n"); - printf("\n"); - printf("\n"); - printf("\n"); - printf("\n"); - printf("\t\t\t*****************Please Input your choice:"); + system("title 瀛︾敓鎴愮哗绠$悊绯荤粺V7.0 By wyxiang"); + printf( "\n\n" + "\t\t\t 瀛︾敓鎴愮哗绠$悊绯荤粺V7.0\n" + "\t\t\t******************************************************************************\n" + "\t\t\t* Management for Students' scores *\n" + "\t\t\t* 1. Input record *\n" + "\t\t\t* 2. Caculate total and average score of every course *\n" + "\t\t\t* 3. Caculate total and average score of every student *\n" + "\t\t\t* 4. Sort in descending order by score *\n" + "\t\t\t* 5. Sort in ascending order by score *\n" + "\t\t\t* 6. Sort in ascending order by number *\n" + "\t\t\t* 7. Sort in dictionary order by name *\n" + "\t\t\t* 8. Search by number *\n" + "\t\t\t* 9. Search by name *\n" + "\t\t\t* 10. Statistic analysis *\n" + "\t\t\t* 11. List record *\n" + "\t\t\t* 12. Write to a file *\n" + "\t\t\t* 13. Read from a file *\n" + "\t\t\t* 0. Exit *\n" + "\t\t\t******************************************************************************\n" + "\n" + "\n" + "\n" + "\n" + "\t\t\t*****************Please Input your choice:"); // 鍙渶璋冪敤涓娆 printf()锛岄熷害鏇村揩 scanf("%d", &i); return i; - } -// 创建链表 +// 鍒涘缓閾捐〃 STU *Creat(int n, int m) { STU *head; STU *p1, *p2; @@ -293,19 +290,19 @@ void AverSumofEveryCourse(STU *head, int n, int m) STU *SortbyScore(STU *head, int n) { - STU *endpt; //控制循环比较 - STU *p; //临时指针变量 + STU *endpt; //鎺у埗寰幆姣旇緝 + STU *p; //涓存椂鎸囬拡鍙橀噺 STU *p1, *p2; p1 = (STU *)malloc(LEN); - p1->next = head; //注意理解:我们增加一个节点,放在第一个节点的前面,主要是为了便于比较。因为第一个节点没有前驱,我们不能交换地址 - head = p1; //让head指向p1节点,排序完成后,我们再把p1节点释放掉 + p1->next = head; //娉ㄦ剰鐞嗚В锛氭垜浠鍔犱竴涓妭鐐癸紝鏀惧湪绗竴涓妭鐐圭殑鍓嶉潰锛屼富瑕佹槸涓轰簡渚夸簬姣旇緝銆傚洜涓虹涓涓妭鐐规病鏈夊墠椹憋紝鎴戜滑涓嶈兘浜ゆ崲鍦板潃 + head = p1; //璁﹉ead鎸囧悜p1鑺傜偣锛屾帓搴忓畬鎴愬悗锛屾垜浠啀鎶妏1鑺傜偣閲婃斁鎺 for (endpt = NULL; endpt != head; endpt = p) { for (p = p1 = head; p1->next->next != endpt; p1 = p1->next) { - if (p1->next->sum < p1->next->next->sum) //如果前面的节点键值比后面节点的键值大,则交换 + if (p1->next->sum < p1->next->next->sum) //濡傛灉鍓嶉潰鐨勮妭鐐归敭鍊兼瘮鍚庨潰鑺傜偣鐨勯敭鍊煎ぇ锛屽垯浜ゆ崲 { p2 = p1->next->next; p1->next->next = p2->next; @@ -316,29 +313,29 @@ STU *SortbyScore(STU *head, int n) } } - p1 = head; //把p1的信息去掉 - head = head->next; //让head指向排序后的第一个节点 - free(p1); //释放p1 - p1 = NULL; //p1置为NULL,保证不产生“野指针”,即地址不确定的指针变量 + p1 = head; //鎶妏1鐨勪俊鎭幓鎺 + head = head->next; //璁﹉ead鎸囧悜鎺掑簭鍚庣殑绗竴涓妭鐐 + free(p1); //閲婃斁p1 + p1 = NULL; //p1缃负NULL锛屼繚璇佷笉浜х敓鈥滈噹鎸囬拡鈥濓紝鍗冲湴鍧涓嶇‘瀹氱殑鎸囬拡鍙橀噺 return head; } STU *SortbyScore1(STU *head, int n) { - STU *endpt; //控制循环比较 - STU *p; //临时指针变量 + STU *endpt; //鎺у埗寰幆姣旇緝 + STU *p; //涓存椂鎸囬拡鍙橀噺 STU *p1, *p2; p1 = (STU *)malloc(LEN); - p1->next = head; //注意理解:我们增加一个节点,放在第一个节点的前面,主要是为了便于比较。因为第一个节点没有前驱,我们不能交换地址 - head = p1; //让head指向p1节点,排序完成后,我们再把p1节点释放掉 + p1->next = head; //娉ㄦ剰鐞嗚В锛氭垜浠鍔犱竴涓妭鐐癸紝鏀惧湪绗竴涓妭鐐圭殑鍓嶉潰锛屼富瑕佹槸涓轰簡渚夸簬姣旇緝銆傚洜涓虹涓涓妭鐐规病鏈夊墠椹憋紝鎴戜滑涓嶈兘浜ゆ崲鍦板潃 + head = p1; //璁﹉ead鎸囧悜p1鑺傜偣锛屾帓搴忓畬鎴愬悗锛屾垜浠啀鎶妏1鑺傜偣閲婃斁鎺 for (endpt = NULL; endpt != head; endpt = p) { for (p = p1 = head; p1->next->next != endpt; p1 = p1->next) { - if (p1->next->sum > p1->next->next->sum) //如果前面的节点键值比后面节点的键值大,则交换 + if (p1->next->sum > p1->next->next->sum) //濡傛灉鍓嶉潰鐨勮妭鐐归敭鍊兼瘮鍚庨潰鑺傜偣鐨勯敭鍊煎ぇ锛屽垯浜ゆ崲 { p2 = p1->next->next; p1->next->next = p2->next; @@ -349,39 +346,39 @@ STU *SortbyScore1(STU *head, int n) } } - p1 = head; //把p1的信息去掉 - head = head->next; //让head指向排序后的第一个节点 - free(p1); //释放p1 - p1 = NULL; //p1置为NULL,保证不产生“野指针”,即地址不确定的指针变量 + p1 = head; //鎶妏1鐨勪俊鎭幓鎺 + head = head->next; //璁﹉ead鎸囧悜鎺掑簭鍚庣殑绗竴涓妭鐐 + free(p1); //閲婃斁p1 + p1 = NULL; //p1缃负NULL锛屼繚璇佷笉浜х敓鈥滈噹鎸囬拡鈥濓紝鍗冲湴鍧涓嶇‘瀹氱殑鎸囬拡鍙橀噺 return head; } STU *SortbyNum(STU *head) { - STU *first; //为原链表剩下用于直接插入排序的节点头指针 - STU *t; //临时指针变量:插入节点 - STU *p, *q; //临时指针变量 + STU *first; //涓哄師閾捐〃鍓╀笅鐢ㄤ簬鐩存帴鎻掑叆鎺掑簭鐨勮妭鐐瑰ご鎸囬拡 + STU *t; //涓存椂鎸囬拡鍙橀噺锛氭彃鍏ヨ妭鐐 + STU *p, *q; //涓存椂鎸囬拡鍙橀噺 - first = head->next; //原链表剩下用于直接插入排序的节点链表:可根据图12来理解 - head->next = NULL; //只含有一个节点的链表的有序链表:可根据图11来理解 + first = head->next; //鍘熼摼琛ㄥ墿涓嬬敤浜庣洿鎺ユ彃鍏ユ帓搴忕殑鑺傜偣閾捐〃锛氬彲鏍规嵁鍥12鏉ョ悊瑙 + head->next = NULL; //鍙惈鏈変竴涓妭鐐圭殑閾捐〃鐨勬湁搴忛摼琛細鍙牴鎹浘11鏉ョ悊瑙 - while (first != NULL) //遍历剩下无序的链表 + while (first != NULL) //閬嶅巻鍓╀笅鏃犲簭鐨勯摼琛 { - //注意:这里for语句就是体现直接插入排序思想的地方 - for (t = first, q = head; ((q != NULL) && (q->num < t->num)); p = q, q = q->next); //无序节点在有序链表中找插入的位置 + //娉ㄦ剰锛氳繖閲宖or璇彞灏辨槸浣撶幇鐩存帴鎻掑叆鎺掑簭鎬濇兂鐨勫湴鏂 + for (t = first, q = head; ((q != NULL) && (q->num < t->num)); p = q, q = q->next); //鏃犲簭鑺傜偣鍦ㄦ湁搴忛摼琛ㄤ腑鎵炬彃鍏ョ殑浣嶇疆 - first = first->next; //无序链表中的节点离开,以便它插入到有序链表中 + first = first->next; //鏃犲簭閾捐〃涓殑鑺傜偣绂诲紑锛屼互渚垮畠鎻掑叆鍒版湁搴忛摼琛ㄤ腑 - if (q == head) //插在第一个节点之前 + if (q == head) //鎻掑湪绗竴涓妭鐐逛箣鍓 { head = t; } - else //p是q的前驱 + else //p鏄痲鐨勫墠椹 { p->next = t; } - t->next = q; //完成插入动作 + t->next = q; //瀹屾垚鎻掑叆鍔ㄤ綔 //first = first->next; } return head; @@ -389,33 +386,33 @@ STU *SortbyNum(STU *head) STU *SortbyName(STU *head, int n) { - STU *endpt; //控制循环比较 - STU *p; //临时指针变量 + STU *endpt; //鎺у埗寰幆姣旇緝 + STU *p; //涓存椂鎸囬拡鍙橀噺 STU *p1, *p2; p1 = (STU *)malloc(LEN); - p1->next = head; //注意理解:我们增加一个节点,放在第一个节点的前面,主要是为了便于比较。因为第一个节点没有前驱,我们不能交换地址 - head = p1; //让head指向p1节点,排序完成后,我们再把p1节点释放掉 + p1->next = head; //娉ㄦ剰鐞嗚В锛氭垜浠鍔犱竴涓妭鐐癸紝鏀惧湪绗竴涓妭鐐圭殑鍓嶉潰锛屼富瑕佹槸涓轰簡渚夸簬姣旇緝銆傚洜涓虹涓涓妭鐐规病鏈夊墠椹憋紝鎴戜滑涓嶈兘浜ゆ崲鍦板潃 + head = p1; //璁﹉ead鎸囧悜p1鑺傜偣锛屾帓搴忓畬鎴愬悗锛屾垜浠啀鎶妏1鑺傜偣閲婃斁鎺 for (endpt = NULL; endpt != head; endpt = p) { for (p = p1 = head; p1->next->next != endpt; p1 = p1->next) { - if (strcmp(p1->next->name, p1->next->next->name)>0) //如果前面的节点键值比后面节点的键值大,则交换 + if (strcmp(p1->next->name, p1->next->next->name)>0) //濡傛灉鍓嶉潰鐨勮妭鐐归敭鍊兼瘮鍚庨潰鑺傜偣鐨勯敭鍊煎ぇ锛屽垯浜ゆ崲 { p2 = p1->next->next; p1->next->next = p2->next; p2->next = p1->next; - p1->next = p2; //结合第4点理解 - p = p1->next->next; //结合第6点理解 + p1->next = p2; //缁撳悎绗4鐐圭悊瑙 + p = p1->next->next; //缁撳悎绗6鐐圭悊瑙 } } } - p1 = head; //把p1的信息去掉 - head = head->next; //让head指向排序后的第一个节点 - free(p1); //释放p1 - p1 = NULL; //p1置为NULL,保证不产生“野指针”,即地址不确定的指针变量 + p1 = head; //鎶妏1鐨勪俊鎭幓鎺 + head = head->next; //璁﹉ead鎸囧悜鎺掑簭鍚庣殑绗竴涓妭鐐 + free(p1); //閲婃斁p1 + p1 = NULL; //p1缃负NULL锛屼繚璇佷笉浜х敓鈥滈噹鎸囬拡鈥濓紝鍗冲湴鍧涓嶇‘瀹氱殑鎸囬拡鍙橀噺 return head; } @@ -427,7 +424,7 @@ void Print(STU *head, int n, int m) int i; p = head; - if (head != NULL) //只要不是空链表,就输出链表中所有节点 + if (head != NULL) //鍙涓嶆槸绌洪摼琛紝灏辫緭鍑洪摼琛ㄤ腑鎵鏈夎妭鐐 { printf("\t\t\t******************************************************************************\n"); do @@ -439,7 +436,7 @@ void Print(STU *head, int n, int m) printf("%.0f\t", p->score[i]); } printf("%.0f\t%.0f\n", p->sum, p->sum / m); - p = p->next; //移到下一个节点 + p = p->next; //绉诲埌涓嬩竴涓妭鐐 } while (p != NULL); printf("\t\t\t******************************************************************************\n"); } @@ -526,8 +523,8 @@ void StatisticAnalysis(STU *head, int n, int m) int i, j; for (i = 0; i