#include void studentinit(student t) { t.num = 0; t.math = 0; t.english = 0; t.sum_score = 0; } int student_show_menu() //显示菜单 { int i = 0; printf("\n"); for (i = 1; i < 50; i++) printf("*"); printf("\n"); printf("欢迎使用学生信息管理系统\n\n"); printf("1.输入全部学生信息\n"); printf("2.读取全部学生信息\n"); printf("3.按姓名查询学生\n"); printf("4.按学号查询学生\n"); printf("5.按学号排序学生\n"); printf("6.按总成绩排序学生\n"); printf("7.添加学生信息\n"); printf("8.删除学生信息\n"); printf("0.退出系统\n\n"); for ( i = 1; i < 50; i++) printf("*"); printf("\n"); return 0; } void student_input(LNode* head) //输入学生信息 { int n=0; int i = 0; printf("\n【1】输入全部学生信息\n"); printf("输入学生个数:"); scanf_s("%d", &n); LNode* p; LNode* r = head; printf("请输入学生信息:\n"); for (i = 0; i < n; i++) { p = (LNode*)malloc(sizeof(LNode)); p->next = NULL; printf("请输入学生姓名:"); scanf_s("%s", p->data.names, maxsize); printf("请输入学号:"); scanf_s("%d", &p->data.num); printf("请输入学生数学成绩:"); scanf_s("%d", &p->data.math); printf("请输入学生英语成绩:"); scanf_s("%d", &p->data.english); //int *sum = p->data.sum_score; p->data.sum_score = p->data.math + p->data.english; r->next = p; r = r->next; //free(p); //若使用了此句,后续输出列表会出错。因为每个空间的数据都是有用的,所以不用free。 printf("\n"); } printf("\n添加学生信息成功!\n"); //return head; } void student_show(LNode* head) //读取全部学生信息 { printf("\n【2】读取全部学生信息\n"); LNode* p; p = head; if (p->next == NULL) { printf("\n当前没有任何学生信息记录! 请输入学生信息。\n"); return; } printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n"); while (p->next != NULL) { p = p->next; printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", p->data.names, p->data.num, p->data.math, p->data.english, p->data.sum_score); } } int name_find(LNode* head, student f) //按姓名查询学生 { printf("【3】按姓名查询学生\n"); LNode* p; p = head; printf("请输入查找学生姓名:"); scanf_s("%s", f.names, maxsize); while ((p->next != NULL) && strcmp(f.names, p->data.names)) //str1=str2,则返回零;若str1!=str2,则返回正数 { p = p->next; } if ((p->next == NULL) && strcmp(f.names, p->data.names)) { printf("\n抱歉,没有找到该学生的信息! \n"); return 0; } else { printf("\n查找学生信息成功!\n"); printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n"); printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", p->data.names, p->data.num, p->data.math, p->data.english, p->data.sum_score); return 1; } } int num_find(LNode* head, student f2) //按学号查找学生 { printf("【4】按学号查询学生\n"); LNode* p; p = head; printf("请输入查找学生学号:"); scanf_s("%d", &f2.num); while ((p->next != NULL) && (p->data.num != f2.num)) { p = p->next; } if ((p->next == NULL) && (p->data.num != f2.num)) { printf("\n抱歉,没有找到该学生的信息! \n"); return 0; } else { printf("\n查找学生信息成功!\n"); printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n"); printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", p->data.names, p->data.num, p->data.math, p->data.english, p->data.sum_score); return 1; } } void num_seq(LNode* head) //按学号排序 { printf("\n【5】按学号排序学生\n"); LNode* newhead, * rear; newhead = head->next; rear = NULL; if (newhead == NULL || newhead->next == NULL) { printf("\n当前没有任何学生信息记录! 请输入学生信息。\n"); return; } while (newhead != rear) { while (newhead->next != rear) { if (newhead->data.num > newhead->next->data.num) { int temp = newhead->data.num; newhead->data.num = newhead->next->data.num; newhead->next->data.num = temp; int temp1 = newhead->data.math; newhead->data.math = newhead->next->data.math; newhead->next->data.math = temp1; int temp2 = newhead->data.english; newhead->data.english = newhead->next->data.english; newhead->next->data.english = temp2; int temp3 = newhead->data.sum_score; newhead->data.sum_score = newhead->next->data.sum_score; newhead->next->data.sum_score = temp3; //char temp4_str = gets(newhead->data.names, sizeof(char)); //char *temp4 = newhead->data.names; //先替换了此值,故第二个strcpy使用*temp4已经是新值了。 student str; strcpy_s(str.names, maxsize, newhead->data.names); //gets_s(newhead->data.names,maxsize); //newhead->data.names = newhead->next->data.names; strcpy_s(newhead->data.names, maxsize, newhead->next->data.names); //newhead->next->data.names = temp4; strcpy_s(newhead->next->data.names, maxsize, str.names); } newhead = newhead->next; } rear = newhead; newhead = head->next; } printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n"); while (head->next != NULL) { head = head->next; printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", head->data.names, head->data.num, head->data.math, head->data.english, head->data.sum_score); } } void sum_seq(LNode* head) //按总成绩排序 { printf("\n【6】按总成绩排序学生\n"); LNode* newhead, * rear; newhead = head->next; rear = NULL; if (newhead == NULL || newhead->next == NULL) { printf("\n当前没有任何学生信息记录! 请输入学生信息。\n"); return; } while (newhead != rear) { while (newhead->next != rear) { if (newhead->data.sum_score < newhead->next->data.sum_score) { int temp = newhead->data.num; newhead->data.num = newhead->next->data.num; newhead->next->data.num = temp; int temp1 = newhead->data.math; newhead->data.math = newhead->next->data.math; newhead->next->data.math = temp1; int temp2 = newhead->data.english; newhead->data.english = newhead->next->data.english; newhead->next->data.english = temp2; int temp3 = newhead->data.sum_score; newhead->data.sum_score = newhead->next->data.sum_score; newhead->next->data.sum_score = temp3; student str; strcpy_s(str.names, strlen(newhead->data.names) + 1, newhead->data.names); strcpy_s(newhead->data.names, strlen(newhead->next->data.names) + 1, newhead->next->data.names); //注意缓冲区长度,strlen()函数计算的字符串长度是不包括'\0’ strcpy_s(newhead->next->data.names, strlen(str.names) + 1, str.names); } newhead = newhead->next; } rear = newhead; newhead = head->next; } printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n"); while (head->next != NULL) { head = head->next; printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", head->data.names, head->data.num, head->data.math, head->data.english, head->data.sum_score); } } void stu_insert(LNode* head, student in) //增加学生信息 { printf("\n【7】添加学生信息\n"); printf("请输入学生信息:\n"); printf("请输入学生姓名:"); scanf_s("%s", in.names, maxsize); printf("请输入学号:"); scanf_s("%d", &in.num); printf("请输入学生数学成绩:"); scanf_s("%d", &in.math); printf("请输入学生英语成绩:"); scanf_s("%d", &in.english); in.sum_score = in.math + in.english; LNode* stu, * r; r = head; int d, dp = 0; printf("请输入要插入的位置:"); scanf_s("%d", &d); while ((r != NULL) && (dp < d - 1)) { r = r->next; dp = dp + 1; } if (r == NULL) { printf("\n插入错误,请重新输入。\n"); return; } stu = (LNode*)malloc(sizeof(LNode)); stu->data = in; stu->next = r->next; r->next = stu; printf("\n插入信息成功!\n"); } void stu_del(LNode* head, student* dl) //删除学生信息 { printf("\n【8】删除学生信息\n"); LNode* stu, * r; r = head; int d, dp = 0; printf("请输入要删除的位置:"); scanf_s("%d", &d); while ((r->next != NULL) && (dp < d - 1)) { r = r->next; dp++; } if (r->next == NULL) { printf("\n删除错误,请重新输入。\n"); return; } stu = r->next; r->next = stu->next; *dl = stu->data; //此时应为*dl ,下面printf才能顺利输出 free(stu); printf("删除%s成功!\n", dl->names); //return; }