#include #include #include #include #include #include #include #include unsigned int counts = 0; /*结构体定义*/ typedef struct student { char no[21]; char name[11]; bool fame; unsigned short int age; char oth[151]; struct student * next; }student; struct student * first = NULL;//第一个 struct student * loc = NULL;//当前(一般为NULL) void input(void);//输入 void print(void);//打印所有 void print_1(const student *);//打印单个 void creat(const student);//创造单个 void save(void);//全部保存 void search_by_sex(bool);//按性别搜索 void search_by_age(unsigned short int);//按年龄搜索 void show_menu(void);//显示菜单 void exith(void);//退出(清理程序内容) int main() { run(); system("pause"); return 0; } void show_menu(void) { printf("a.学生基本信息录入\n"); printf("b.学生基本信息显示\n"); printf("c.学生基本信息保存\n"); printf("d.学生基本信息删除\n"); printf("e.学生基本信息修改\n"); printf("f.学生基本信息查询\n"); printf("g.退出系统\n"); char t = getch(); switch (t) { case 'a': input(); show_menu(); break; case 'b': system("cls"); print(); system("pause"); system("cls"); show_menu(); break; case 'c': save(); system("cls"); printf("保存完成!\n"); show_menu(); break; case 'd': { char no_t[21]; system("cls"); print(); printf("\n请输入要删除的学生的学号:\n"); while (1) { fgets_nn(no_t, 21, stdin); if (!strcmp(no_t, "\0")) { printf("学号不能为空:\n"); continue; } break; } if (del(no_t)) { system("cls"); printf("删除成功!\n\n"); } else { system("cls"); printf("删除失败!不存在该学号的学生!\n\n"); } show_menu(); break; } case 'e': { char no_t[21]; system("cls"); if (!check_password()) { system("cls"); printf("密码验证失败!\n"); show_menu(); } system("cls"); print(); printf("\n请输入要修改的学生的学号:\n"); while (1) { fgets_nn(no_t, 21, stdin); if (!strcmp(no_t, "\0")) { printf("学号不能为空:\n"); continue; } break; } if (edit(no_t)) { system("cls"); printf("修改成功!\n\n"); } else { system("cls"); printf("修改失败!不存在该学号的学生\n\n"); } show_menu(); break; } case 'f': { system("cls"); char mod; while (1) { printf("请输入模式:\n"); printf("(1)按学号查询\n"); printf("(2)按姓名查询\n"); printf("(3)按性别查询\n"); printf("(4)按年龄查询\n"); mod = getch(); if (mod - '0' < 1 || mod - '0'>4) { system("cls"); printf("输入错误\n\n"); continue; } system("cls"); break; } switch (mod - '0') { case 1: {//建立一个块,防止case内定义被编译器报错 char tem[21]; printf("\n请输入要查询的学生的学号:\n"); while (1) { fgets_nn(tem, 21, stdin); if (!strcmp(tem, "\0")) { system("cls"); printf("学号不能为空,请输入要查询的学生的学号:\n"); continue; } break; } /*student *ge = search_by_no(tem); if (ge == NULL) { printf("不存在该学号的学生!请检查输入的内容!\n"); } else { system("cls"); printf("查询到学号为%s的学生如下:\n\n", tem); print_1(ge); } break; }*/ case 2: { char name[11]; printf("请输入学生姓名:\n"); do { fgets_nn(name, 11, stdin); } while (!strcmp(name, "\0")); search_by_name(name); break; } case 3: { bool fame; while (1) { printf("请输入学生性别(1.男 2.女):\n"); char t = getch(); if (t == '1') { fame = true; break; } else if (t == '2') { fame = false; break; } else { system("cls"); printf("输入不正确,请重新输入学生性别(1.男 2.女):\n"); continue; } } search_by_sex(fame); break; } case 4: { unsigned short int age; while (1) { printf("请输入学生年龄:\n"); if (scanf("%hd", &age) != 1) { system("cls"); printf("输入不正确,请重新输入学生年龄:\n"); while (getchar() != '\n')continue; continue; } break; } search_by_age(age); break; } } system("pause"); system("cls"); show_menu(); break; } case 'g': save(); system("cls"); printf("数据已保存,正在退出系统!\n"); exith(); break; default: system("cls"); printf("输入错误!\n\n"); show_menu(); break; } } void input(void) { student temp; char t; while (1) { system("cls"); printf("请输入学生学号,退出输入请直接回车:\n"); while (1) { fgets_nn(temp.no, 21, stdin); if (!check_no(temp.no)) printf("学号已存在!\n"); else break; } if (!strcmp(temp.no, "\0")) { return; } printf("请输入学生姓名:\n"); do { fgets_nn(temp.name, 11, stdin); } while (!strcmp(temp.name, "\0")); while (1) { printf("请输入学生性别(1.男 2.女):\n"); t = getch(); if (t == '1') { temp.fame = true; printf("男\n"); break; } else if (t == '2') { temp.fame = false; printf("女\n"); break; } else { printf("输入不正确\n"); continue; } } while (1) { printf("请输入学生年龄:\n"); if (scanf("%hd", &temp.age) != 1) { printf("输入不正确\n"); while (getchar() != '\n')continue; continue; } break; } printf("备注:\n"); while (getchar() != '\n')continue; fgets_nn(temp.oth, 151, stdin); creat(temp); system("pause"); counts++; save(); } } void creat(const student t) { if (first == NULL) { first = (student*)malloc(sizeof(student)); loc = first; first->next = NULL; } else { loc->next= (student*)malloc(sizeof(student)); loc = loc->next; loc->next = NULL; } loc->age = t.age; loc->fame = t.fame; int i=0; for(i=0;i<11&&t.name[i>0?i-1:0]!='\0';i++) loc->name[i] = t.name[i]; for (i = 0; i < 21&&t.no[i > 0 ? i - 1 : 0] != '\0'; i++) loc->no[i] = t.no[i]; for (i = 0; i < 151; i++) loc->oth[i] = t.oth[i]; } void print(void) { student * l = first; printf("系统内共有%d名学生数据:\n\n", counts); while (l != NULL) { print_1(l); printf("\n"); l = l->next; } } void print_1(const student * t) { printf("学号:%s\n", t->no); printf("姓名:%s\n", t->name); printf("性别:%s\n", t->fame ? "男": "女"); printf("年龄:%d\n", t->age); printf("备注:%s\n", t->oth); } void exith(void) { student *l = first; student n; while (l != NULL) { n = *l; free(l); l = n.next; } printf("系统已退出!\n"); system("pause"); exit(1); } void save(void) { FILE * data; data = fopen("data", "w"); student * l = first; while (l != NULL) { fprintf(data, "%s\n", l->no); fprintf(data, "%s\n", l->name); fprintf(data, "%d\n", l->fame); fprintf(data, "%d\n", l->age); fprintf(data, "%s\n", l->oth); l = l->next; } fclose(data); } void search_by_sex(bool fame) { student *l = first; unsigned int c = 0; system("cls"); while (l != NULL) { if (l->fame == fame) { print_1(l); printf("\n"); c++; } l = l->next; } if (c == 0) { printf("未检索到内容!\n"); } else { printf("\n已检索到%d条性别为%s的学生\n\n", c, fame ? "男" : "女"); } } void search_by_age(unsigned short int age) { student *l = first; unsigned int c = 0; system("cls"); while (l != NULL) { if (l->age == age) { print_1(l); printf("\n"); c++; } l = l->next; } if (c == 0) { printf("未检索到内容!\n"); } else { printf("\n已检索到%d条年龄为%d的学生\n\n", c, age); } } }