C语言课程设计-学生成绩管理系统#include<stdio.h>#include<string.h>/* strcmp函数原型包含*/#define N 100/*定义最大学生人数为100*/struct student /*学生结构体定义*/{char no[11]; /*学号,10位*/char name[10];/*姓名,3个汉字*/float score[5];/*储存5门功课的成绩*/float ave;/*储存平均分*/int year;/*入学年*/}stu[N];int num;int panduani(int); /*通过i的值,判断信息要查找的信息是否存在*/void output(); /*输出学生成绩函数(公共函数)*/void init(); /*系统初始化函数*/void newfile(); /*建立新的数据文件*/ void read1(); /*从数据文件中读取数据*/void weihu(); /*维护界面函数*/void new1(); /*新增一个学生成绩函数*/void xiugai(); /*修改学生成绩*/int xgcj(int); /*修改成绩*/void delete1(); /*删除学生信息函数*/ void tongji(); /*统计界面函数*/void tongji1(); /*计算每个学生平均分*/ void tongji2(); /*计算每门课程的平均分*/ void paixu1(); /*排序函数*/void paino(); /*按学号排序*/void paiave(); /*按平均分排序*/void paidkcj(); /*按单科成绩排序*/void change(int,int); /*交换结构体数组中的两个记录*/void outputx(int); /*选择输出单科成绩徐修楼编*/void find1(); /*查找界面函数*/int find(char sno[]); /*按学号查找*/int findname(char cname[]);/*按姓名查找*/void findyear(int cyear); /*按入学年查找*/void save(); /*保存文件*/void stop(); /*停顿函数*/void stop() /*停顿函数(公共函数)*/{printf("Press Any Key To Continue...\n");getch();}void output() /*输出学生成绩函数(公共函数)*/ {int i,j;printf("==================================== ===============================\n");printf("XueHao XingMing RuXueNian C GaoShu DaWu DianLu MoDian\n");printf("==================================== ===============================\n");for(i=0;i<num;i++){printf("%-11s%-10s%-11d",stu[i].no,stu[i].name,stu[i].year);for(j=0;j<5;j++)printf("% -7.1f",stu[i].score[j]);printf("\n");}stop();}void init() /*系统初始化函数*/{if(access("stu.dat",0)) /*判断文件是否存在*/newfile(); /*若不存在,新建数据文件*/elseread1(); /*若存在,读取数据*/}void newfile() /*建立新的数据文件*/{int i;FILE *fp;if((fp=fopen("stu.dat","wb"))==NULL) /*创建二进制文件*/{printf("Chuang Jian Shi Bai! Qing Chong Xin YunXing Cheng Xu.\n"); /*显示“创建文件失败,请重新运行程序”*/exit(0); /*终止程序*/}printf(" Chu Ci Yun Xing, Jin Xing Shu Ju Chu Shi Hua...\n");/*显示“初次运行,进行数据初始化...”*/ stop();clrscr();printf(" Qing Shu Ru Xue Sheng Ren Shu:");/*显示“请输入学生人数:”*/scanf("%d",&num);for(i=0;i<num;i++){printf("Qing Shu Ru Di %d Ge Xue Sheng De Xue Hao:",i+1); /*显示“请输入第i+1个学生的学号:”*/ getchar(); /*吃回车*/gets(stu[i].no);printf("Xing Ming:"); /*显示“姓名:”*/gets(stu[i].name);printf("Ru Xue Nian:");/*显示“入学年”*/scanf("%d",&stu[i].year);printf("C-YuYan Cheng Ji:");/*显示“C语言成绩”*/scanf("%f",&stu[i].score[0]);printf("GaoShu Cheng Ji:");/*显示“高数成绩”*/ scanf("%f",&stu[i].score[1]);printf("DaWu Cheng Ji:");/*显示“大物成绩”*/ scanf("%f",&stu[i].score[2]);printf("DianLu Cheng Ji:");/*显示“电路成绩”*/ scanf("%f",&stu[i].score[3]);printf("MoDian Cheng Ji:");/*显示“模电成绩”*/ scanf("%f",&stu[i].score[4]);fwrite(&stu[i],1,sizeof(struct student),fp); /*把第i+1名学生成绩写入文件*/}fclose(fp);clrscr();printf("==================================== ==\n");printf(" Gung Cai Shu Ru De %d Ge Shu Ju Wei:\n",num); /*显示“刚才输入的num 个数据为:”*/printf("==================================== ==\n\n");output();}void read1() /*从数据文件中读取数据*/{FILE *fp;num=0; /*num为数据文件中实际存储的学生人数*/fp=fopen("stu.dat","r");if(fp==NULL) /*数据文件不存在*/{printf("Wen Jian Da Kai Shi Bai! Chong Xin Yun Xing Cheng Xu\n"); /*显示“文件打开失败!重新运行程序”*/exit(0);}while(!feof(fp)){fread(&stu[num],1,sizeof(struct student),fp);num++;}num--;fclose(fp);printf("==================================== ===\n");printf(" Xian You %d Ge Xue Sheng De Cheng Ji:\n",num); /*显示“现有NUM个学生的成绩:”*/printf("==================================== ===\n\n");output();}void weihu() /*维护界面函数*/{char c1;while(1){clrscr();printf("\n\n\n\t\t\t*****************************\n") ;printf("\t\t\t* Wei Hu *\n"); printf("\t\t\t* 1. Xin Zeng *\n"); /*新增*/printf("\t\t\t* 2. Shan Chu *\n"); /*删除(按学号)*/printf("\t\t\t* 3. Xiu Gai *\n"); /*修改*/printf("\t\t\t* ESC. Fan Hui *\n"); /*返回*/printf("\t\t\t*****************************\n"); printf("\t\t\tQing Xuan Ze:\n");/*请选择:*/c1=getch();if(c1==27)break;switch(c1){case '1':new1();break;case '2':delete1();case '3':xiugai();break;default:printf("Xuan Zen You Wu,Qing Chong Xuan!\n");/*“选择有误,请重选!”*/}}}void new1() /*新增一个学生成绩函数*/{printf("Qing Shu Ru Xue Hao:"); /*显示“请输入学号:”*/getchar(); /*吃回车*/gets(stu[num].no);printf("Xing Ming:"); /*显示“姓名:”*/gets(stu[num].name);printf("Ru Xue Nian:");/*显示“入学年”*/scanf("%d",&stu[num].year);printf("C-YuYan Cheng Ji:");/*显示“C语言成绩”*/scanf("%f",&stu[num].score[0]);printf("GaoShu Cheng Ji:");/*显示“高数成绩”*/ scanf("%f",&stu[num].score[1]);printf("DaWu Cheng Ji:");/*显示“大物成绩”*/ scanf("%f",&stu[num].score[2]);printf("DianLu Cheng Ji:");/*显示“电路成绩”*/ scanf("%f",&stu[num].score[3]);printf("MoDian Cheng Ji:");/*显示“模电成绩”*/ scanf("%f",&stu[num].score[4]);num++;printf("\n================================== ====\n");printf(" Xian You %d Ge Xue Sheng De Cheng Ji:\n",num); /*显示“现有NUM个学生的成绩:”*/printf("==================================== ==\n\n");output();}void xiugai() /*修改学生成绩*/{char xg[11];int i;printf("\nQing shu ru yao xiu gai xue sheng de xue hao:\n\n");gets(xg);for(i=0;i<num;i++)if(strcmp(stu[i].no,xg)==0)break;if(i==num){printf("\nShu ju wen jian wu gai sheng xi xi!\n\nQing chong xin shu ru xue hao.\n\n");stop();return;}xgcj(i);}int xgcj(int x) /*修改成绩*/{char xueke[5][10]={"Cyuyan"," Gaoshu","Wuli","Dianlu","Modian"};char (*p)[10],c;int j,k;p=xueke;for(;;){clrscr();printf("\n");printf("\t\t\t************************************\n\n");printf("\t\t\t Cheng Ji \n");printf("\t\t\t 1.C Yu Yan \n"); /*C语言*/printf("\t\t\t 2.Gao Shu \n"); /*高数*/printf("\t\t\t 3.Wu Li \n"); /*大学物理*/printf("\t\t\t 4.Dian Lu \n"); /*电路*/printf("\t\t\t 5.Mo Dian \n"); /*模电*/printf("\t\t\t Esc.Fan Hui \n\n"); /*返回上级*/printf("\t\t\t************************************\n \n");printf("\tQing xuan ze yao xiu gai de cheng ji(press 1.2.3.4.5.Esc):\n");c=getch();if(c==27)break;else if (c>='1'&&c<='5'){k=c-49;printf("\nQing shu ru %s cheng ji:\n",p+k);scanf("%f",&stu[x].score[k]);printf("\nXiu gai hou gai xue sheng xin xi wei:\n");printf("XueHao XingMing RuXueNian C GaoShu DaWu DianLu MoDian\n");printf("==================================== ===============================\n");printf("%-11s%-10s%-11d",stu[x].no,stu[x].name,stu[x ].year);for(j=0;j<5;j++)printf("% -7.1f",stu[x].score[j]);printf("\n");}else printf("\nXuan Zen You Wu,Qing Chong Xuan!\n\n");stop();}}int find(char sno[]) /*按学号查找*/{int i;for(i=0;i<num;i++)if(strcmp(stu[i].no,sno)==0) /*判断两个字符串相等*/return i;/*查找成功*/return -1;/*查找失败*/}void delete1() /*删除学生信息函数*/{char cno[11];int i,j;printf("Qing Shu Ru Yao Shan Chu De Xue Sheng Xue Hao:"); /*“请输入要删除的学生学号”*/gets(cno);i=find(cno);if(i!=-1){for(j=i;j<num;j++)stu[j]=stu[j+1];num--;printf("Xian You %d Ge Xue Sheng De Cheng Ji:\n",num); /*显示“现有NUM个学生的成绩:”*/ output();}else {printf("qing shu ru zheng que de xue hao. \n");stop();}}void tongji() /*统计界面函数*/{int i;char c2;while(1){clrscr();printf("\n\n\n\t\t\t******************************** ****\n");printf("\t\t\t* Tong Ji *\n");printf("\t\t\t* 1. Xue Sheng Ping Jun Fen*\n"); /*计算学生平均分*/printf("\t\t\t* 2. Ke Cheng Ping Jun Fen *\n"); /*计算每门课程平均分*/printf("\t\t\t* ESC. Fan Hui *\n"); /*返回*/printf("\t\t\t************************************\n ");printf("\t\t\tQing Xuan Ze:\n");/*请选择:*/c2=getch();if(c2==27) break;switch(c2){case '1':tongji1();printf("\n\nXue Sheng Ping Jun Fen Wei:\n");printf("==================================== =======\n");printf("XueHao XingMing RuXueNian PingJunFen\n");printf("===========================================\n");for(i=0;i<num;i++)printf("%-11s%-10s%-11d%-10.2f\n",stu[i].no,stu[i].name,stu[i].year,st u[i].ave);stop();break;case '2':tongji2();break;default:printf("Xuan Zen You Wu,Qing Chong Xuan!\n");/*“选择有误,请重选!”*/}}}void tongji1() /*计算每个学生平均分*/{int i,j;for(i=0;i<num;i++){stu[i].ave=0;for(j=0;j<5;j++)stu[i].ave+=stu[i].score[j];stu[i].ave=stu[i].ave/5;}}void tongji2() /*计算每门课程的平均分*/ {int i,j;float sum;float avec[5]; /*储存每门课的平均分*/for(j=0;j<5;j++){sum=0;for(i=0;i<num;i++)sum+=stu[i].score[j];avec[j]=sum/num;}printf("\n\nGe Ke Ping Jun Fen Wei:\n");/*“各科平均分:”*/printf("===================================\ n");printf("C GaoShu DaWu DianLu MoDian\n");printf("===================================\ n");for(i=0;i<5;i++)printf("%-7.1f",avec[i]);printf("\n");stop();}void paixu1() /*排序界面*/{char c3;for(;;){clrscr();printf("\n\n\n");printf("\t\t\t*************************************\ n");printf("\t\t\t* Pai Xu * \n");printf("\t\t\t* 1. An Xue Hao * \n"); /*按学号排序*/printf("\t\t\t* 2. An Ping Jun Fen * \n"); /*按平均分排序*/printf("\t\t\t* 3. An dan ke cheng ji * \n"); /*按单科成绩排序*/printf("\t\t\t* ESC. Fan Hui * \n"); /*返回*/printf("\t\t\t*************************************\ n\n");printf("\t\Qing Xuan Ze(press 1.2.3.Esc):\n");/*请选择:*/c3=getch();if(c3==27) break;switch(c3){case '1':paino();break;case '2':paiave();break;case '3':paidkcj();break;default:printf("\nXuan Zen You Wu,Qing Chong Xuan!\n\n");/*选择有误,请重选!*/stop();}}}void paino() /*按学号排序*/{ /*按学号从低到高冒泡法排序*/ int i,j,k;for(i=0;i<num;i++)for(j=0;j<num-1-i;j++)if(strcmp(stu[j].no,stu[j+1].no)>0)change(j,j+1);printf("\nPai Xu Hou Wei:\n"); /*“排序后为:”*/ output();}void paiave() /*按平均分排序*/{ /*按平均分从高到低冒泡法排序*/ int i,j;struct student temp;tongji1(); /*计算平均成绩*/for(i=0;i<num;i++)for(j=0;j<num-i-1;j++)if(stu[j].ave<stu[j+1].ave)change(j,j+1);printf("\n\tPai Xu Hou Wei:\n"); /*“排序后为:”*/printf("\t=================================== ============\n");printf("\tXueHao XingMing RuXueNian Ave Pai ming\n");printf("\t=================================== ============\n");for(i=0;i<num;i++)printf("\t%-11s%-10s%-11d%-6.2f %4d\n",stu[i].no,stu[i].name,stu[i].year,stu[i].ave,i+1);stop();}void paidkcj() /*按单科成绩排序*/{char c;int i,j,k;for(;;){clrscr();printf("\n");printf("\t\t\t************************************\n \n");printf("\t\t\t Dan Ke Cheng Ji \n");printf("\t\t\t 1.C Yu Yan \n"); /*C语言*/printf("\t\t\t 2.Gao Shu \n"); /*高数*/printf("\t\t\t 3.Wu Li \n"); /*大学物理*/printf("\t\t\t 4.Dian Lu \n"); /*电路*/printf("\t\t\t 5.Mo Dian \n"); /*模电*/printf("\t\t\t Esc.Fan Hui \n\n"); /*返回上级*/printf("\t\t\t************************************\n \n");printf("\tQing xuan ze(press 1.2.3.4.5.Esc):\n");c=getch();if(c==27)break;else if (c>='1'&&c<='5'){k=c-49;for(i=0;i<num;i++)for(j=0;j<num-1-i;j++)if(stu[j].score[k]<stu[j+1].score[k])change(j,j+1);outputx(k);}else printf("\nXuan Zen You Wu,Qing Chong Xuan!\n\n");stop();}}void change(int i,int j) /*交换结构体数组中的两个记录*/{struct student temp;temp=stu[i];stu[i]=stu[j];stu[j]=temp;}void outputx(int k) /*选择输出单科成绩*/{charxueke[5][10]={"Cyuyan","Gaoshu","Wuli","Dianlu"," Modian"};char (*p)[10]; /*定义一个指向包含10个字符型元素的一维数组的指针变量*/int i;p=xueke;printf("\n\tPai Xu Hou Wei:\n");printf("\t=================================== ============\n");printf("\tXueHao XingMing RuXueNian %s Pai Ming\n",p+k);printf("\t=================================== ============\n");for(i=0;i<num;i++)printf("\t%-11s%-10s%-11d%-6.1f%5d\n",stu[i].no,stu[i].name,stu[i].year,stu[i].score[k],i+1);}void find1() /*查找界面函数*/{char c4;char cno[11],cname[10];int i,j,cyear;while(1){clrscr();printf("\t\t\t************************************\n ");printf("\t\t\t* Cha Zhao *\n");printf("\t\t\t* 1. An Xue Hao *\n"); /*按学号查找*/printf("\t\t\t* 2. An Xing Ming *\n"); /*按姓名查找*/printf("\t\t\t* 3. An Ru Xue Nian *\n"); /*按入学年查找*/printf("\t\t\t* ESC. Fan Hui *\n"); /*返回*/printf("\t\t\t************************************\n ");printf("\t\t\tQing Xuan Ze:\n");/*请选择:*/c4=getch();if(c4==27) break;switch(c4){case '1':printf("Shu Ru Xue Hao:\n");/*“输入学号:”*/gets(cno);i=find(cno);panduani( i);break;case '2':printf("Shu Ru Xing Ming:\n");/*“输入姓名:”*/ gets(cname);i=findname(cname);panduani(i);break;case '3':printf("Shu Ru RU Xue Nian:\n");/*“输入入学年”*/scanf("%d",&cyear);findyear(cyear);stop();break;default:printf("Xuan Zen You Wu,Qing Chong Xuan!\n");/*“选择有误,请重选!”*/}}}int findname(char cname[]) /*按姓名查找*/{int i;for(i=0;i<num;i++)if(strcmp(stu[i].name,cname)==0) /*判断两个字符串相等*/return i; /*查找成功*/return -1; /*查找失败*/}void findyear(int cyear){int i,j,flag=0;for(i=0;i<num;i++)if(stu[i].year==cyear){ flag=1;break;}if(flag==0){printf("Yao Cha Zhao De Xue Sheng Bu Cun Zai\n");stop(); }else{printf("Cha Zhao Dao De Xue Sheng Xin Xi Wei:\n");/*“查找到的学生信息为:”*/printf("XueHao XingMing RuXueNian C GaoShu DaWu DianLu MoDian\n");printf("==================================== ===============================\n");for(i=0;i<num;i++){if(stu[i].year==cyear) /*判断两个数值相等*/{printf("%-11s%-10s%-11d",stu[i].no,stu[i].name,stu[i].year);for(j=0;j<5;j++)printf("% -7.1f",stu[i].score[j]);printf("\n");}}}}int panduani(int i) /*通过i的值,判断信息要查找的信息是否存在*/{int j;if(i==-1){printf("Yao Cha Zhao De Xue Sheng Bu Cun Zai\n");/*要查找的学生不存在*/stop();}else{printf("Cha Zhao Dao De Xue Sheng Xin Xi Wei:\n");/*“查找到的学生信息为:”*/printf("XueHao XingMing RuXueNian C GaoShu DaWu DianLu MoDian\n");printf("==================================== ===============================\n");printf("%-11s%-10s%-11d",stu[i].no,stu[i].name,stu[i].year);for(j=0;j<5;j++)printf("% -7.1f",stu[i].score[j]);printf("\n");stop();}}void save() /*保存文件*/{int i;FILE *fp;if((fp=fopen("stu.dat","wb"))==NULL) /*创建二进制文件*/{printf("Chuang Jian Shi Bai! Qing Chong Xin Yun Xing Cheng Xu.\n"); /*显示“创建文件失败,请重新运行程序”*/exit(0); /*终止程序*/}for(i=0;i<num;i++)fwrite(&stu[i],1,sizeof(struct student),fp); /*将I+1名学生成绩写入文件*/fclose(fp);}void main() /*主控菜单*/{char c;init();while(1){clrscr();printf("\n\n\n\t\t\t*****************************\n") ;printf("\t\t\t* MAIN MENU *\n");printf("\t\t\t* 1. Wei Hu *\n"); /*维护*/printf("\t\t\t* 2. Tong Ji *\n"); /*统计*/printf("\t\t\t* 3. Pai Xu *\n"); /*排序*/printf("\t\t\t* 4. Cha Zhao *\n"); /*查找*/printf("\t\t\t* ESC. SA VE & EXIT *\n"); /*存盘退出*/printf("\t\t\t*****************************\n"); printf("\t\t\tQing Xuan Ze:\n");/*请选择:*/c=getch();switch(c){case '1':weihu();break;case '2':tongji();break;case '3':paixu1();break;case '4':find1();break;case 27:save();exit(0);break;default:printf("Xuan Zen You Wu,Qing Chong Xuan!\n");/*“选择有误,请重选!”*/}}}。