学生成绩管理系统C(链表)语言
Posted stephen-jixing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学生成绩管理系统C(链表)语言相关的知识,希望对你有一定的参考价值。
#include"stdio.h" #include"stdlib.h" #include"string.h" //用于调用一些函数 struct person { char name[20]; int ID; int chinese; int english; int math; struct person *next; //连接处的指针 }; //由于create里面就有initialize所以先把initialize放在前面 void initialize(struct person *p, int num) { //初始化链表里面的值 printf("innitialize person %d name is:", num); //num用于计入输入的第几个同学 scanf_s("%s", &p->name, sizeof(p->name)); getchar(); printf("ID:"); scanf_s("%d", &p->ID); getchar(); printf("chinese:"); scanf_s("%d", &p->chinese); getchar(); printf("math:"); scanf_s("%d", &p->math); getchar(); printf("english:"); scanf_s("%d", &p->english); getchar(); } struct person *create(int len) { int num = 0; struct person *h = 0, *c, *pre = 0; while (num < len) { c = (struct person*)malloc(sizeof(struct person)); //malloc取内存 sizeof为这个内存的大小 然后转化成指针 if (num == 0) { h = c; pre = c; } // 如果num=0 h存下了首地址 为以后拿做准备 c->next = NULL; if (num) { pre->next = c; //pre为前一块内存的地址 把后一块的首地址赋给前一块的尾 pre = c; //收尾连接后 pre指前一块的功能就完成了 然后再指向现在的内存 为下一次拿内存、赋地址做准备 } initialize(c, num); //每取一块地址就去输入一次 ++num; }return h; } void traverse(struct person *head) { int index = 1; // 用于计数第几个学生 while (head != NULL) { //同样一直到后面没有地址结束 printf("name is: %s ID is:%d chinese is:%d englishi is:%d math is:%d ", head->name, head->ID, head->chinese, head->english, head->math); head = head->next; //前一个输完后就要指向下一块地址 ++index; } } int getlength(struct person *head) { int num = 0; while (head != NULL) { //当head指向后面没有了 它就是NULL 结束 ++num; head = head->next; //如果head 不是NULL ++num后要把head指针指向最后 }return num; } //增加学生信息 void append_node(struct person *h) { struct person *t = h, *p; while (t->next != NULL) { t = t->next; }p = (struct person *)malloc(sizeof(struct person)); initialize(p, 0); p->next = NULL; t->next = p; traverse(h); } //删除 struct person * delete_ID(struct person *head, int ID, int len) { struct person *t = head; struct person *temp; for (int i = 0; i < (len - 1); ++i) { if (i == 0) { if (head->ID == ID) { head = head->next; free(t); traverse(head); return head; } if ((t->next)->ID == ID) { temp = t->next; t->next = (t->next)->next; free(temp); traverse(head); return head; } } if (i != 0) { if ((t->next)->ID == ID) { temp = t->next; t->next = (t->next)->next; free(temp); traverse(head); return head; }t = t->next; } }return head; } //学号查询 void search_ID(struct person *head, int ID) { while (head != NULL) { if (head->ID == ID) { printf("name is: %s ID is:%d chinese is:%d englishi is:%d math is:%d ", head->name, head->ID, head->chinese, head->english, head->math); }head = head->next; } } //姓名查询 void search_name(struct person *head, char name[20]) { while (head != NULL) { if ((strcmp(head->name, name)) == 0) { printf("name is: %s ID is:%d chinese is:%d englishi is:%d math is:%d ", head->name, head->ID, head->chinese, head->english, head->math); } head = head->next; } } //指定学生修改 void change(struct person *head, char name[20]) { while (head != NULL) { if (strcmp(head->name, name) == 0) { printf(" name is:"); scanf_s("%s", &head->name, sizeof(head->name)); getchar(); printf("ID:"); scanf_s("%d", &head->ID); getchar(); printf("chinese:"); scanf_s("%d", &head->chinese); getchar(); printf("english:"); scanf_s("%d", &head->english); getchar(); printf("math:"); scanf_s("%d", &head->math); getchar(); } head = head->next; } traverse(head); } //数学分数平均数 int average_math(struct person *head, int len) { int i = 0, sum = 0, average; struct person *t = head; while (t != NULL) { sum += t->math; t = t->next; } average = (sum / len); return average; } //英语分数平均数 int average_english(struct person *head, int len) { int i = 0, sum = 0, average; struct person *t = head; while (t != NULL) { sum += t->english; t = t->next; } average = (sum / len); return average; } //语文分数平均数 int average_chinese(struct person *head, int len) { int sum = 0, average; struct person *t = head; while (t != NULL) { sum += t->chinese; t = t->next; } average = (sum / len); return average; } //成绩区间统计 int statistics_math(struct person*head, int min, int max) { int conter = 0; while (head!= NULL) { if (head->math >= min&&head->math <= max) { ++conter; printf("name:%s math score:%d ", head->name, head->math); }head = head->next; }return conter; } int statistics_chinese(struct person*head, int min, int max) { int conter = 0; while (head != NULL) { if (head->chinese >= min&&head->chinese <= max) { ++conter; printf("name:%s chinese score:%d ", head->name, head->chinese); }head = head->next; }return conter; } int statistics_english(struct person*head, int min, int max) { int conter = 0; while (head != NULL) { if (head->english >= min&&head->english <= max) { ++conter; printf("name:%s english score:%d ", head->name, head->english); }head = head->next; }return conter; } //某名学生的平均成绩 int average_name(struct person*head, char name[20]) { int av = 0; while (head != NULL) { if (strcmp(head->name, name) == 0) { av += head->math; av += head->chinese; av += head->english; } head = head->next; } av = av / 3; return av; } //排序数学 void rank_math(struct person *h, int len) { struct person *t = h, *pre = h; int i, math, chinese, english,ID; char name[20]; t = t->next; for (i = 0; i < (len - 1); ++i) { while (t != NULL) { if ((t->math) >(pre->math)) { ID = t->ID; t->ID = pre->ID; pre->ID = ID; math = t->math; t->math = pre->math; pre->math = math; strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name); chinese = t->chinese; t->chinese = pre->chinese; pre->chinese = chinese; english = t->english; t->english = pre->english; pre->english = english; } t = t->next; }pre = pre->next; t = pre->next; } traverse(h); } //排序语文 void rank_chinese(struct person *h, int len) { struct person *t = h, *pre = h; int i, math, chinese, english,ID; char name[20]; t = t->next; for (i = 0; i < (len - 1); ++i) { while (t != NULL) { if ((t->chinese) >(pre->chinese)) { ID = t->ID; t->ID = pre->ID; pre->ID = ID; math = t->math; t->math = pre->math; pre->math = math; strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name); chinese = t->chinese; t->chinese = pre->chinese; pre->chinese = chinese; english = t->english; t->english = pre->english; pre->english = english; } t = t->next; }pre = pre->next; t = pre->next; } traverse(h); } //排序英语 void rank_english(struct person *h, int len) { struct person *t = h, *pre = h; int i, math, chinese, english,ID; char name[20]; t = t->next; for (i = 0; i < (len - 1); ++i) { while (t != NULL) { if ((t->english) >(pre->english)) { ID = t->ID; t->ID = pre->ID; pre->ID = ID; math = t->math; t->math = pre->math; pre->math = math; strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name); chinese = t->chinese; t->chinese = pre->chinese; pre->chinese = chinese; english = t->english; t->english = pre->english; pre->english = english; } t = t->next; }pre = pre->next; t = pre->next; } traverse(h); } void release(struct person *head) { struct person *n; //需要一个指针存着下一个地址 while (head != NULL) { n = head->next; //把n指向下一块要释放的地址 free(head); head = n; //然后再把head从前一个地址移到下一个地址 } } //取长度 int getlen(struct person *head) { int conter = 0; struct person*t = head; while (t != NULL) { ++conter; t = t->next; } return conter; } //存入文件 void openfile(struct person *head, int len) { FILE *fp; struct person*t = head; errno_t err; int temp; char str[100]; char s[10]; if ((err = fopen_s(&fp, "D:\学生信息", "w")) != 0) { printf("文件打开错误 "); } else { printf("文件打开成功 "); } for (int i = 0; i < len; ++i) { strcpy_s(str, t->name); fputs("name:", fp); fputs(str, fp); fputs(": ", fp); fputs("ID:", fp); sprintf_s(s, "%d", t->ID); fputs(s, fp); fputs(" ", fp); sprintf_s(s, "%d", t->chinese); fputs("chinese:", fp); fputs(s, fp); fputs(" ", fp); sprintf_s(s, "%d", t->math); fputs("math:", fp); fputs(s, fp); fputs(" ", fp); sprintf_s(s, "%d", t->english); fputs("english:", fp); fputs(s, fp); fputs(" ", fp); t = t->next; } return; } void menu(struct person *head, int len) { int m = 0; int min, max; int ID = 0; int conter = 0; char name[20]; while (1) { printf(" 请选择您需要的操作: "); printf(" 0. 遍历学生信息 "); printf(" 1. 增加学生信息 "); printf(" 2. 删除学生信息 "); printf(" 3. 修改学生信息 "); printf(" 4. 按姓名查询 "); printf(" 5. 按学号查询 "); printf(" 6. 语文成绩在某个区间段的人数以及学生 "); printf(" 7. 数学成绩在某个区间段的人数以及学生 "); printf(" 8. 英语成绩在某个区间段的人数以及学生 "); printf(" 9. 语文平均分 "); printf(" 10. 数学平均分 "); printf(" 11. 英语平均分 "); printf(" 12. 某个学生的三科平均成绩 "); printf(" 13. 按语文成绩从高到低排序 "); printf(" 14. 按数学成绩从高到低排序 "); printf(" 15. 按英语成绩从高到底排序 "); printf(" 16. 结束功能并把信息写入文件中 "); scanf_s("%d", &m); switch (m) { case 0: traverse(head); break; case 1: append_node(head); break; case 2: {printf("要删除学生信息的学号:"); scanf_s("%d", &ID); head=delete_ID(head, ID, getlen(head)); }break; case 3: {printf("需要修改学生信息的同学姓名:"); scanf_s("%s", &name, sizeof(name)); change(head, name); }break; case 4: {printf("search by name:"); scanf_s("%s", &name, sizeof(name)); search_name(head, name); }break; case 5: {printf("学号查询:"); scanf_s("%d", &ID); search_ID(head, ID); }break; case 6: {printf("请输入语文成绩的区间:"); printf("min="); scanf_s("%d", &min); printf("max="); scanf_s("%d", &max); printf("语文成绩在区间%d到%d之间的学生:%d人 ", min, max, statistics_chinese(head, min, max)); }break; case 7: {printf("请输入数学成绩的区间:"); printf("min="); scanf_s("%d", &min); printf("max="); scanf_s("%d", &max); printf("数学成绩在区间%d到%d之间的学生:%d人 ", min, max, statistics_math(head, min, max)); }break; case 8: {printf("请输入英语成绩的区间:"); printf("min="); scanf_s("%d", &min); printf("max="); scanf_s("%d", &max); printf("英语成绩在区间%d到%d之间的学生:共有%d人 ", min, max, statistics_english(head, min, max)); }break; case 9: { printf("average of chinese is%d", average_chinese(head, getlen(head))); }break; case 10: { printf("average of math is%d", average_math(head, getlen(head))); }break; case 11: { printf("average of english is%d", average_english(head, getlen(head))); }break; case 12: {printf("请输入学生的姓名:"); scanf_s("%s", &name, sizeof(name)); printf("%s 的平均成绩为:%d", name, average_name(head, name)); }break; case 13: {printf("按照语文成绩从高到底排序:"); rank_chinese(head, getlen(head)); }break; case 14: {printf("按照数学成绩从高到底排序:"); rank_math(head, getlen(head)); }break; case 15: {printf("按照英语成绩从高到底排序:"); rank_english(head, getlen(head)); }break; case 16: openfile(head, getlen(head)); return; } } } int main() { struct person *head; int len; int min, max; int ID = 0; char name[20]; printf("请输入学生信息"); printf("学生人数:"); scanf_s("%d", &len); //输入要取的地址多少 head = create(len); // 创建地址 在create里面就有初始复制函数嵌套 traverse(head); //遍历 menu(head, getlen(head)); release(head); //释放内存 system("pause"); return 0; }
以上是关于学生成绩管理系统C(链表)语言的主要内容,如果未能解决你的问题,请参考以下文章