#include<stdio.h> #include<stdlib.h> #include<string.h> struct student { int num; char name[20]; char sex[10]; int year; float score; struct student* next; }; int n; struct student* h; int main() { void log();//登录函数 void record();//目录 struct student* creat();//输入函数 void print(struct student* head); //输出函数 void dels(); //删除全操作 void adds(); //添加全操作 struct student *refers(struct student *h1);//查询操作 struct student *change(struct student *hl); //更改操作 int in1, in2; log(); while (1) { printf("\n请输入你的操作命令:"); do { scanf("%d", &in1); } while (in1 != 1 && in1 != 2 && in1 != 3 && in1 != 4 && in1 != 5 && in1 != 6); if (in1 == 1 || in1 == 2 || in1 == 3 || in1 == 4 || in1 == 5) { printf("\n未储存有数据!\n请输入数据!"); break; } else { printf("你已退出!欢迎使用!"); break; } } h = creat(); print(h); A: { printf("\n**********************************************************"); printf("\n1、返回界面 2、添加 3、删除 4、更改 5、查询 6、退出"); printf("\n请输入你的操作命令:"); scanf("%d", &in2); switch (in2) { case 1: {log(); break; } case 2: {adds(); goto A; break; } case 3: {dels(); goto A; break; } case 4: {change(h); goto A; break; } case 5: {refers(h); goto A; break; } case 6: {printf("\n你已退出!欢迎使用!"); break; } default:printf("\n输入错误!"); printf("\n你已退出!欢迎使用!"); break; } } } void log() //登录函数 { int i; void record(); printf(" 学生管理系统 \t"); printf("\n\t1、登录\n\t2、退出\t"); while (1) //循环输入,判断 { printf("\n\n请输入你要执行的操作:"); do { scanf("%d", &i); } while (i != 1 && i != 2); if (i == 1) { record(); break; } else { printf("\n你已退出,欢迎使用!"); break; } } } void record() //显示目录 { printf("\n\t1、显示学生信息"); printf("\n\t2、添加学生信息"); printf("\n\t3、删除学生信息"); printf("\n\t4、更改学生信息"); printf("\n\t5、查询学生信息"); printf("\n\t6、退出学生系统"); } struct student* creat() //输入数据 { struct student* head; struct student* p1, * p2; p1 = (struct student*)malloc(sizeof(struct student)); p2 = p1; printf("\n请输入学生学号、姓名、性别、年龄、成绩 :"); scanf("%d%s%s%d%f", &p1->num, &p1->name, &p1->sex, &p1->year, &p1->score); head = NULL; n = 0; while (p1->num != 0) { n++; if (n == 1) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (struct student*)malloc(sizeof(struct student)); printf("请输入学生学号、姓名、性别、年龄、成绩 :"); scanf("%d%s%s%d%f", &p1->num, &p1->name, &p1->sex, &p1->year, &p1->score); } p2->next = NULL; return head; } void print(struct student* hl) //输出数据 { struct student* p; p = hl; if (hl != NULL) { printf("\n\t学号:\t姓名:\t性别:\t年龄:\t成绩:"); do { printf("\n\t%d\t%s\t%s\t%d\t%.2f", p->num, p->name, p->sex, p->year, p->score); p = p->next; } while (p != NULL); } } void dels() //删除全操作 { struct student* del(struct student* h, int sum); void print(struct student* head); int sum; int ch; while (1) { printf("\n你是否需要删除数据(Y/N):"); do { ch = getchar(); } while (ch != 'Y' && ch != 'N'); if (ch == 'Y') { printf("请输入你要删除的数据:"); scanf("%d", &sum); h = del(h, sum); print(h); } else { break; } } printf("删除完毕!最终数据为:\n"); print(h); } struct student* del(struct student* hl, int sum) //删除关键操作 { struct student* p1, * p2; p2 = p1 = hl; while (p1->num != sum && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (p1->num == sum) { if (p1 == h) { hl = p1->next; } else { p2->next = p1->next; } } else { printf("未找到数据!"); } return h; } void adds() //添加全操作 { struct student* add(struct student** h, int num, char name[20], char sex[10], int year, float score); void print(struct student* h); int num; char name[20]; char sex[10]; int year; float score; int ch; while (1) { printf("\n你是否需要添加数据:"); do { ch = getchar(); } while (ch != 'Y' && ch != 'N'); if (ch == 'Y') { printf("\n请输入学生学号、姓名、性别、年龄、成绩 :"); scanf("%d%s%s%d%f", &num, &name, &sex, &year, &score); h = add(&h, num, name, sex, year, score); } else { break; } } printf("添加完毕!"); printf("最终数据为:\n"); print(h); } struct student* add(struct student** hl, int num, char name[20], char sex[10], int year, float score) //添加关键操作 { struct student* p1, * p2, * New; p1 = *hl; p2 = NULL; while (p1 != NULL && p1->num < num) { p2 = p1; p1 = p1->next; } New = (struct student*)malloc(sizeof(struct student)); New->num = num; strcpy(New->name, name); strcpy(New->sex, sex); New->year = year; New->score = score; New->next = p1; if (p2 == NULL) { *hl = New; } else { p2->next = New; } return *hl; } struct student *refers(struct student *hl) //查询全操作 { struct student* p1, * p2, * p0; int num; int ch; p0 = NULL; while (1) { p1 = p2 = hl; printf("\n你是否需要查询(Y/N):"); do { ch = getchar(); } while (ch != 'Y' && ch != 'N'); if (ch == 'Y') { printf("\n请输入你要查询的学号:"); scanf("%d", &num); while (p1->num != num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (p1->num == num) { p0 = p1; printf("\n你的查询结果:"); printf("\n\t学号:\t姓名:\t性别:\t年龄:\t成绩:"); printf("\n\t%d\t%s\t%s\t%d\t%.2f", p0->num, p0->name, p0->sex, p0->year, p0->score); } else { printf("未找到相关数据!"); } } else { printf("\n你已退出查询!"); break; } } return h; } struct student *change(struct student *hl) //更改全操作 { struct student *xue(struct student *d); struct student *xing(struct student *d); struct student *bie(struct student *d); struct student *nian(struct student *d); struct student *cheng(struct student *d); struct student* p1, * p2, * p0; int num; int ch; int chan; p0 = NULL; while (1) { p1 = p2 = hl; printf("\n你是否需要更改(Y/N):"); do { ch = getchar(); } while (ch != 'Y' && ch != 'N'); if (ch == 'Y') { printf("\n请输入你要更改的学号:"); scanf("%d", &num); while (p1->num != num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if (p1->num == num) { p0 = p1; printf("\n1、学号 2、姓名 3、性别 4、年龄 5、成绩"); printf("\n请输入你的操作:"); scanf("%d",&chan); switch(chan) { case 1:xue(p0);break; case 2:xing(p0);break; case 3:bie(p0);break; case 4:nian(p0);break; case 5:cheng(p0);break; default :break; } printf("\n你更改后的结果:"); printf("\n\t学号:\t姓名:\t性别:\t年龄:\t成绩:"); printf("\n\t%d\t%s\t%s\t%d\t%.2f", p0->num, p0->name, p0->sex, p0->year, p0->score); } else { printf("未找到相关数据!"); } } else { printf("\n你已退出更改!"); break; } } return h; } struct student *xue(struct student *d) //更改学号 { struct student *p; p=d; printf("\n请输入新的学号:"); scanf("%d",&p->num); return p; } struct student *xing(struct student *d)//更改姓名 { struct student *p; char s[20]; p=d; printf("\n请输入新的姓名:"); scanf("%s",&s); strcpy(p->name,s); return p; } struct student *bie(struct student *d)//更改性别 { struct student *p; char s[10]; p=d; printf("\n请输入新的性别:"); scanf("%s",&s); strcpy(p->sex,s); return p; } struct student *nian(struct student *d)//更改年龄 { struct student *p; p=d; printf("\n请输入新的年龄:"); scanf("%d",&p->year); return p; } struct student *cheng(struct student *d)//更改成绩 { struct student *p; p=d; printf("\n请输入新的成绩:"); scanf("%f",&p->score); return p; }