#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #include <unistd.h> #include<windows.h> struct student{ int age; float math; struct student *pnext; char stu_name[20]; int code;//学生学号 }; void menu(){//操作界面 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(" | [8]按照学生学号排排序 |\n"); printf(" | [9]打印链表信息 |\n"); printf(" |________________________________________________|\n"); } void welcome(){ system("color a0"); printf("***********************************************\n"); printf("\n"); printf("\n"); printf("\n"); printf("*************欢迎进入学生信息管理系统**********"); printf("\n"); printf("\n"); printf("\n"); printf("************************************************\n"); } void free_link_list(struct student *phead){ struct student *pre=phead; struct student *p=phead->pnext; while(p!=NULL){ free(pre); pre=p; p=p->pnext; } } void save_student(student *phead) {//保存学生信息到文件 FILE *fp=NULL; if(!(fp=fopen("student.txt","w"))){ printf("打开文件失败 \n"); exit(0); } struct student *p=phead->pnext; while(p) { fprintf(fp,"%d %s %d %f\n",p->code,p->stu_name,p->age,p->math);//写入文件 p=p->pnext; } fclose(fp); free_link_list(phead); } void add_student(student *phead) { system("cls"); menu(); student *pnew=(student*)malloc(sizeof(student)); pnew->pnext=phead->pnext; phead->pnext=pnew; printf("请输入学生的年龄:"); scanf("%d",&pnew->age); printf("\n请输入学生的姓名:"); scanf("%s",pnew->stu_name); printf("\n请输入学生的数学成绩:"); scanf("%f",&pnew->math); printf("\n请您输入学生的学号:\n"); scanf("%d",&pnew->code); printf("\n添加成功!\n"); sleep(1); } void del_student(student *phead){//删除学生信息 system("cls"); menu(); printf("\n请您输入该生的学号 :\n"); student *p=phead->pnext; int CODE; scanf("%d",&CODE); student *pre=phead; while(p!=NULL) { if(p->code==CODE){ pre->pnext=p->pnext; free(p); printf("\n**!!删除成功 !!**\n"); break; } else{ p=p->pnext; pre=pre->pnext; } } } void change_student(student *phead) {//修改学生信息 student *p=phead->pnext; printf("请输入学生的学号:\n"); int code; scanf("%d",&code); int i=0; while(p!=NULL) { if(p->code==code) { i=1; printf("\n*****找到该生*****\n"); printf("学生学号\t 学生姓名\t 年龄 \t 高等数学\n"); printf("---------------------------------------------\n"); printf("%d \t %s \t %d \t %f",p->code,p->stu_name,p->age,p->math); int a; printf("\n**请输入您的操作**\n"); printf("---------------------------"); printf("[1]修改学生数学成绩\n"); scanf("%d",&a); printf("请输入修改的数学成绩为:\n"); float Math; scanf("%f",&Math); switch(a) { case 1: p->math=Math; printf("修改成功!\n"); printf("学生学号\t 学生姓名\t 年龄 \t 高等数学\n"); printf("---------------------------------------------\n"); printf("%d \t %s \t %d \t %f",p->code,p->stu_name,p->age,p->math); break; default:printf("\n**输入错误!请重新输入....**\n");break; } } p=p->pnext; } if(!i)printf("**未找到该生**"); } void print_linklist(student *phead){ system("cls"); menu(); struct student *p=phead->pnext; if(phead->pnext==NULL){ printf("当前链表还没任何学生\n"); } while(p!=NULL){ printf("学生姓名:%s\n",p->stu_name); p=p->pnext; } } struct student *seek_student(struct student *phead){//查找学生 system("cls"); menu(); struct student *p=phead->pnext; printf("\n请输入学生学号:\n"); int CODE; int index=1; scanf("%d",&CODE); while(p!=NULL){ if(p->code==CODE){ printf("**找到该学生**"); printf("****************************************\n"); printf("学号\t姓名\t年龄\t高等数学\n"); printf("%d\t%s\t%d\t%.2f\n",p->code,p->stu_name,p->age,p->math); index=0; break; } p=p->pnext; } if(index){ printf("\n**************未找到该学生***************\n"); } } struct student *find_pos_score(struct student *phead,student *pend){ float key=phead->math; struct student *p=phead; struct student *q=phead->pnext; float item=0; while(q!=pend){ if(q->math>key){ p=p->pnext; item=q->math; q->math=p->math; p->math=item; int CODE=q->code; q->code=p->code; p->code=CODE; char str[20]; strcpy(str,q->stu_name); strcpy(q->stu_name,p->stu_name); strcpy(p->stu_name,str); int a=q->age; q->age=p->age; p->age=a; } q=q->pnext; } float temp=phead->math; phead->math=p->math; p->math=temp; int item2 =phead->code; phead->code=p->code; p->code=item2; int index=phead->age; phead->age=p->age; p->age=index; char str[20]; strcpy(str,phead->stu_name); strcpy(phead->stu_name,p->stu_name); strcpy(p->stu_name,str); return p; } void quick_sort_score(student *phead,student *pend){ if(phead!=pend){ student *pnode=find_pos_score(phead,pend); quick_sort_score(phead,pnode); quick_sort_score(pnode->pnext,pend); } } void rank_student_by_score(struct student *phead,student *pend) { system("cls"); menu(); printf("****************************************\n"); printf("学号\t姓名\t年龄\t高等数学\n"); quick_sort_score(phead,pend); struct student *p=phead; while(p!=NULL){ printf("%d\t%s\t%d\t%.2f\n",p->code,p->stu_name,p->age,p->math); p=p->pnext; } } struct student *find_pos_code(student *phead,student *pend){ float key=phead->code; struct student *p=phead; struct student *q=phead->pnext; while(q!=pend){ if(q->code<key){ p=p->pnext; int item=q->code; q->code=p->code; p->code=item; int AGE=q->age; q->age=p->age; p->age=AGE; float MATH=q->math; q->math=p->math; p->math=MATH; char str[20]; strcpy(str,q->stu_name); strcpy(q->stu_name,p->stu_name); strcpy(p->stu_name,str); } q=q->pnext; } int pig=phead->code; phead->code=p->code; p->code=pig; int AGE2=phead->age; phead->age=p->age; p->age=AGE2; float MATH2=phead->math; phead->math=p->math; p->math=MATH2; char str1[20]; strcpy(str1,phead->stu_name); strcpy(phead->stu_name,p->stu_name); strcpy(p->stu_name,str1); return p; } void quick_sort_code(struct student *phead,struct student *pend){ if(phead!=pend){ struct student *pnode=find_pos_code(phead,pend); quick_sort_code(phead,pnode); quick_sort_code(pnode->pnext,pend); } } void rank_student_by_code(struct student *phead,student *pend){//快排 system("cls"); menu(); printf("****************************************\n"); printf("学号\t姓名\t年龄\t高等数学\n"); quick_sort_code(phead,pend); struct student *p=phead; while(p!=NULL){ printf("%d\t%s\t%d\t%.2f\n",p->code,p->stu_name,p->age,p->math); p=p->pnext; } } void load_all_student(struct student *phead){//将文件信息读入链表 FILE *fp=fopen("student.txt","r+"); char stu_name[20]; if(fp==NULL){ printf("不能打开文件\n"); } struct student *p=phead->pnext; int c; rewind(fp); fseek(fp, 0, 2); c = ftell(fp); rewind(fp); while((c-2) != ftell(fp)){ struct student *pnew=(student*)malloc(sizeof(student)); fscanf(fp,"%d %s %d %f",&pnew->code,stu_name,&pnew->age,&pnew->math); strcpy(pnew->stu_name,stu_name); pnew->pnext=phead->pnext; phead->pnext=pnew; } fclose(fp); printf("\n加载成功!\n"); } void show(){ system("cls"); menu(); printf("请输入数字编号————以完成功能\n"); int number; struct student *phead=(student *)malloc(sizeof(student)); phead->pnext=NULL; load_all_student(phead); while(1) { printf("**请输入数字**"); scanf("%d",&number); switch(number) { case 0: save_student(phead); printf("**************谢谢使用再见!**"); exit(0); break; case 1:add_student(phead); break; case 2:del_student(phead); break; case 3:change_student(phead); break; case 4:seek_student(phead); break; case 5:rank_student_by_score(phead->pnext,NULL); break; case 6: break;printf("\n功能尚未开发敬请期待\n");break; case 7:save_student(phead);break; case 8:rank_student_by_code(phead->pnext,NULL);break; case 9:print_linklist(phead);break; default : printf("*输入有误,请重新输入*"); break; } } } int main() { welcome();//欢迎 sleep(2);//延迟 show();//展示操作功能 return 0; }