#include<stdio.h> #include<stdlib.h> #include<string.h> #define NUM 15 #define NAME 25 struct student { char num[NUM]; char name[NAME]; float mathscore;//数学成绩 int mathmici; float engscore;//英语成绩 int engmici; float cyyscore;//C语言成绩 int cyymici; float aver; int minci; }; void ShuRu(struct student *pi,int n); //void PaiXu(struct student *pi,int n); //void MathPaiXu(struct student *pi,int n); //void EngPai(struct student *pi,int n); //void CyyPai(struct student *pi,int n); void Danke(); void main() { puts("***********欢迎进入学生成绩管理系统***********"); puts("***************学生访问请按 1 **************"); puts("***************管理员访问请按 2 **************"); puts("**********************************************"); int d; printf("请选择:\n"); scanf("%d",&d); if(d==2) //管理员访问************************************************* { puts("请输入密码:"); char key[10]; char mm[10]="6666"; scanf("%s",key); if(strcmp(key,mm)==0) //选择是修改数据还是重新写入数据************* { puts("*****重新输入数据请按 A *****"); puts("*******修改数据请按 B *******"); char x; getchar(); x=getchar(); if(x=='a'||x=='A') //重新写入数据 { int n; puts("请输入学生总人数"); scanf("%d",&n); struct student *pi=NULL; pi=(struct student *)malloc(n*sizeof(struct student)); puts("请输入学号 姓名 高数成绩 英语成绩 C语言成绩"); ShuRu(pi,n);//输入数据 FILE *fp=fopen("chengji","w"); fwrite(&pi[0],sizeof(student),n,fp);//输入的数据储存 fflush(fp); fclose(fp); //****************************** } else if(x=='b'||x=='B') //修改数据*************************** { FILE *yp=fopen("chengji","r+"); puts("请输入要修改的学生的学号"); char xh[NUM]; scanf("%s",xh); struct student ptr;int v,count=0; do{ v=fread(&ptr,sizeof(student),1,yp); count++; }while(strcmp(ptr.num,xh)!=0&&v==1); if(strcmp(ptr.num,xh)==0) { printf("姓名 %s\n",ptr.name); puts("请输入要修改的科目"); puts("修改数学请按 1 "); puts("修改英语请按 2 "); puts("修改C语言请按 3 "); int xuan; scanf("%d",&xuan); switch (xuan) { case 1: puts("请输入数学成绩:"); float shu; scanf("%f",&shu); ptr.mathscore=shu; printf("高数成绩=%.2f ",ptr.mathscore); break; case 2: puts("请输入英语成绩:"); float ying; scanf("%f",&ying); ptr.engscore=ying; printf("英语成绩=%.2f ",ptr.mathscore); break; case 3: puts("请输入C语言成绩:"); float YY; scanf("%f",&YY); ptr.cyyscore=YY; printf("C语言成绩=%.2f ",ptr.mathscore); break; } fseek(yp,(count-1)*sizeof(student),SEEK_SET);//查找位置 fwrite(&ptr,sizeof(student),1,yp);//输入的数据储存 fflush(yp); fclose(yp); } else puts("无此学号!"); } else puts("选择错误!"); } else puts("密码错误!"); } else if(d==1) //学生访问******************************************************* { FILE *yp=fopen("chengji","r"); puts("请输入学号"); char xh[NUM]; scanf("%s",xh); struct student ptr;int v; do{ v=fread(&ptr,sizeof(student),1,yp); }while(strcmp(ptr.num,xh)!=0&&v==1); if(strcmp(ptr.num,xh)==0) { puts("请输入姓名"); char xm[NAME]; scanf("%s",xm); if(strcmp(ptr.name,xm)==0) { printf("学号 %s ",ptr.num); printf("姓名 %s ",ptr.name); printf("您的平均分=%.2f ",ptr.aver); printf("您的名次是 %d\n",ptr.minci); printf("高数成绩=%.2f ",ptr.mathscore);//printf("高数名次是%d\n",ptr.mathmici); printf("英语成绩=%.2f ",ptr.engscore);//printf("英语名次是%d\n",ptr.engmici); printf("C语言成绩=%.2f ",ptr.cyyscore);//printf("C语言名次是%d\n",ptr.cyymici); fclose(yp); puts("欢迎下次再来!"); } else puts("学号和姓名不符,不能访问!"); } else puts("无此学号!"); } else puts("选择错误!"); } //********************************************************************************************************************** /*void Danke() { FILE *yp=fopen("chengji","r+"); struct student Pi[5]; int i=0; //pi=(struct student *)malloc(4*sizeof(struct student)); while(feof(yp)==0) { fread(&Pi[0],sizeof(student),1,yp); } struct student *pi; pi=Pi; int n=4; puts("******平均分和单科第一名排列如下*******"); int q; PaiXu(pi,n);//平均分排序 for(q=0;q<n;q++) (pi+q)->minci=q+1;//把平均分名次写入到 minci printf("平均分第一名 %s\n",(pi+0)->name); int x; MathPaiXu(pi,n); for(x=0;x<n;x++) (pi+x)->mathmici=x+1; printf("数学第一名%s\n",(pi+0)->name); int y; EngPai(pi,n); for(y=0;y<n;y++) (pi+y)->engmici=y+1; printf("英语第一名%s\n",(pi+0)->name); int z; CyyPai(pi,n); for(z=0;z<n;z++) (pi+z)->cyymici=z+1; printf("C语言第一名%s\n",(pi+0)->name); puts("***************"); fflush(yp); fclose(yp); }*/ void ShuRu(struct student *pi,int n)//输入数据函数 { int i; for(i=0;i<n;i++) { printf("第 %d 个学生的信息 ",i+1); scanf("%s",(pi+i)->num); scanf("%s",(pi+i)->name); scanf("%f",&(pi+i)->mathscore); scanf("%f",&(pi+i)->engscore); scanf("%f",&(pi+i)->cyyscore); (pi+i)->aver=(((pi+i)->mathscore)+((pi+i)->engscore)+((pi+i)->cyyscore))/3; (pi+i)->aver=(float)((int)((pi+i)->aver*100))/100; printf("平均分是 %f\n",(pi+i)->aver); } } /* //排序函数********************************************* void PaiXu(struct student *pi,int n) { int i,j; struct student t; int maxpos; for(i=1;i<=n;i++) { maxpos=i-1; for(j=i;j<n;j++) { if((pi+j)->aver>(pi+maxpos)->aver) maxpos=j; } t=*(pi+maxpos); *(pi+maxpos)=*(pi+(i-1)); *(pi+(i-1))=t; } } void MathPaiXu(struct student *pi,int n) { int i,j; struct student t; int maxpos; for(i=1;i<=n;i++) { maxpos=i-1; for(j=i;j<n;j++) { if((pi+j)->mathscore>(pi+maxpos)->mathscore) maxpos=j; } t=*(pi+maxpos); *(pi+maxpos)=*(pi+(i-1)); *(pi+(i-1))=t; } } void EngPai(struct student *pi,int n) { int i,j; struct student t; int maxpos; for(i=1;i<=n;i++) { maxpos=i-1; for(j=i;j<n;j++) { if((pi+j)->engscore>(pi+maxpos)->engscore) maxpos=j; } t=*(pi+maxpos); *(pi+maxpos)=*(pi+(i-1)); *(pi+(i-1))=t; } } void CyyPai(struct student *pi,int n) { int i,j;float max; struct student t; int maxpos; for(i=1;i<=n;i++) { max=(pi+i-1)->cyyscore; maxpos=i-1; for(j=i;j<n;j++) { if((pi+j)->cyyscore>(pi+maxpos)->cyyscore) maxpos=j; } t=*(pi+maxpos); *(pi+maxpos)=*(pi+i-1); *(pi+i-1)=t; } }*/