C语言大作业(学生信息管理系统)

it2025-02-10  9

C语言大作业 学生信息管理系统 最最最基础带头节点的链表模板

#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<string.h> #define setup (StuList)malloc(sizeof(Student)) struct Grade { float score; }; typedef struct Student { char sno[20]; char sname[20]; struct Grade Chinese, English, Math; struct Student *next; }Student, *StuList; StuList CreateStuList(StuList head) { //传参头节点指针 int tmp; StuList p, tail; head = (StuList)malloc(sizeof(Student)); head->sno[0] = '\0'; head->sname[0] = '\0'; (head->Chinese).score = 0; (head->English).score = 0; (head->Math).score = 0; head->next = NULL; tail = head; //链表为空时,头节点等于尾结点 int count; printf("输入要加入的学生的人数:\n"); tmp = scanf("%d", &count); if(tmp == 0) { printf("输入错误!\n"); } for(int i = 0; i < count; i++) { printf("请输入第%d个学生信息:学号 姓名 语文成绩 英语成绩 数学成绩:\n", i + 1); p = (StuList)malloc(sizeof(Student)); scanf("%s %s %f %f %f", &p->sno, &p->sname, &(p->Chinese).score, &(p->English).score, &(p->Math).score); p->next = NULL; tail->next = p; tail = p; } return head; } StuList AddStuList(StuList student) { printf("请输入要在哪个学号后面插入学生信息:\n"); char sno[20]; scanf("%s", sno); StuList p = setup; printf("请输入要插入的学生信息:学号 姓名 语文成绩 英语成绩 数学成绩:\n"); scanf("%s %s %f %f %f", &p->sno, &p->sname, &(p->Chinese).score, &(p->English).score, &(p->Math).score); p->next = NULL; StuList head = student, List = student->next, pre = student; while(List != NULL) { if(strcmp(List->sno, p->sno) == 0) { printf("该生信息已经存在!\n"); return head; } List = List->next; } List = student->next; while(List != NULL) { pre = List; if(strcmp(List->sno, sno) == 0) { p->next = pre->next; pre->next = p; } List = List->next; } return head; } StuList DelStuList(StuList head) { printf("请输入要删除的学生的学号:\n"); char sno[20]; scanf("%s", sno); StuList p = head->next, pre = head; while(p != NULL) { if(strcmp(p->sno, sno) == 0) { pre->next = p->next; free(p); p = NULL; printf("删除成功!\n"); return head; } pre = p; p = p->next; } printf("未找到该生,删除失败!\n"); return head; } void SearchStuList(StuList head) { char sno[20]; printf("请输入要查找的学生的学号:\n"); scanf("%s", sno); StuList p = head->next; while(p != NULL) { if(strcmp(p->sno, sno) == 0) { printf("该学生信息如下:\n"); printf("%s %s %.2f %.2f %.2f\n", p->sno, p->sname, (p->Chinese).score, (p->English).score, (p->Math).score); return ; } p = p->next; } printf("未找到该生信息!\n"); } void PrintStuList(StuList head) { StuList p = head->next; if(p == NULL) { printf("学生信息为空。\n"); return ; } while(p != NULL) { printf("学生信息:学号%s 姓名%s 语文成绩%.2f 英语成绩%.2f 数学成绩%.2f:\n", p->sno, p->sname, (p->Chinese).score, (p->English).score, (p->Math).score); p = p->next; } } StuList AlterStuList(StuList head) { StuList p = head->next; printf("请输入想要更改的学生的学号:\n"); char sno[20]; scanf("%s", sno); while(p != NULL) { if(strcmp(p->sno, sno) == 0) { printf("已找到该生信息,请输入修改后该生的姓名 语文成绩 英语成绩 数学成绩\n"); scanf("%s %f %f %f", &p->sname, &(p->Chinese).score, &(p->English).score, &(p->Math).score); return head; } p = p->next; } return head; } StuList SortStuList(StuList head) { StuList p = head, q = head->next, tail = NULL, p1 = head->next; while(p1->next != NULL) { p1 = p1->next; tail = p1; } while(head->next != tail) { p = head; q = head->next; while(p->next != tail) { if(strcmp(p->next->sno, q->next->sno) > 0) { char snot[20], snametmp[20]; float EnglishScoreTmp, ChineseScoreTmp, MathScoreTmp; strcpy(snot, p->next->sno); strcpy(p->next->sno, q->next->sno); strcpy(q->next->sno, snot); strcpy(snametmp, p->next->sname); strcpy(p->next->sname, q->next->sname); strcpy(q->next->sname, snametmp); ChineseScoreTmp = (p->next->Chinese).score, (p->next->Chinese).score = (q->next->Chinese).score, (q->next->Chinese).score = ChineseScoreTmp; MathScoreTmp = (p->next->Math).score, (p->next->Math).score = (q->next->Math).score, (q->next->Math).score = MathScoreTmp; EnglishScoreTmp = (p->next->English).score, (p->next->English).score = (q->next->English).score, (q->next->English).score = EnglishScoreTmp; } p = p->next; q = q->next; } tail = p; } return head; } int main() { void welcome(); void menu(); welcome(); // Sleep(3000); StuList student; //链表头结点 int choose; int test; while(1) { menu(); printf("请输入您要选择的功能键:\n"); test = scanf("%d", &choose); if(test == 0) { printf("输入错误!\n"); getchar(); //吸收输入非法字符后的回车键 continue; } switch(choose) { case 0: printf("谢谢使用!\n"); exit(0); case 1: student = CreateStuList(student); //创建学生链表 break; case 2: student = AddStuList(student); //增 break; case 3: student = DelStuList(student); //删 break; case 4: SearchStuList(student); //查 break; case 5: student = AlterStuList(student); //改 break; case 6: PrintStuList(student); //打印全部学生信息 break; case 7: student = SortStuList(student); break; default: printf("无效输入!\n"); } } } void welcome() { system("color 8B"); printf("\n"); printf("````````````````````````````````````````````````````````````````````````````````"); printf("\n\n\n"); printf(" *********************** 欢迎登录学生信息管理平台 ************************* \n"); printf("\n\n\n"); printf("````````````````````````````````````````````````````````````````````````````````\n"); } void menu() { system("color 8B"); printf(" |________________________________________________|\n"); printf(" | |\n"); printf(" | 学生信息管理系统 |\n"); printf(" | |\n"); printf(" | 0、退出系统 |\n"); printf(" | 1、增加学生信息 |\n"); printf(" | 2、插入学生信息 |\n"); printf(" | 3、删除学生信息 |\n"); printf(" | 4、查找学生信息 |\n"); printf(" | 5、更改学生信息 |\n"); printf(" | 6、浏览全部学生信息 |\n"); printf(" | 7、按学号从小到大排序 |\n"); printf(" | |\n"); printf(" |________________________________________________|\n"); return ; }
最新回复(0)