#include<stdio.h>
#include<stdlib.h>
#define N 5
void enroll();
void logon();
void teacher();
void enter();
void fix();
int seek(int m);
struct score
{
int num;
char name[20];
float score[3];
float aver;
}score[N];
struct ID
{
int num;
int pw;
}ID[N];
int main()
{
int n,i;
printf("欢迎使用成绩查找系统\n(老师的账号为0,学生账学号)\n");
printf("注册账号输入1\n登录请输入2\n");
FILE*fp1;
if((fp1=fopen("ID","wb"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
for(i=0;i<N;i++)
{ ID[i].num=i;
if(fwrite(&ID[i],sizeof(struct ID),1,fp1)!=1)
printf("file write error\n");
}
fclose(fp1);
scanf("%d",n);
if(n==1)
{logon();
enroll();
}
else enroll();
return 0;
}
void logon()
{ int m,n;
FILEfp1;
if((fp1=fopen("ID","wb"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
scanf("%d %d",m,n);
ID[m].num=m;
ID[m].pw=n;
fseek(fp1,msizeof(struct ID),0);
fwrite(&ID[m],sizeof(struct ID),1,fp1);
fclose(fp1);
}
void enroll()
{ int m,n;
printf("请输入账号:\n");
scanf("%d",m);
printf("请输入密码:\n");
scanf("%d",n);
FILEfp2;
if((fp2=fopen("ID","rb"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
fseek(fp2,msizeof(struct ID),0);
fread(&ID[m],sizeof(struct ID),1,fp2);
if(m=0)
{if(m==ID[m].num&&n==ID[m].pw)teacher();
else{printf("密码错误");
enroll();}
}
else if(m==ID[m].num&&n==ID[m].pw)seek(m);
else{printf("密码错误");
enroll();
}
fclose(fp2);
}
void teacher()
{ int t;
printf("录入成绩输入1\n修改成绩输入2\n");
scanf("%d",&t);
if(t==1)enter();
else if(t==2)fix();
else teacher();
}
void enter()
{ printf("请按学号顺序依此输入\n");
int i,m;
FILE*fp3;
if((fp3=fopen("score","wb"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
for(i=0;i<N;i++)
{
scanf("%d %s %f %f %f",&score[i].num,&score[i].name,&score[i].score[0],&score[i].score[1],&score[i].score[2]);
if(fwrite(&score[i],sizeof(struct score),1,fp3)!=1)
printf("file write error\n");
for(i=0;i<N;i++)
score[i].aver=(score[i].score[0]+score[i].score[1]+score[i].score[2])/3.0;
}
printf("已输入完成\n显示平均分请输入1\n修改请输入2\n");
scanf("%d",&m);
if(m==1)aver();
else(m==2)fix();
fclose(fp3);
}
void aver()
{ FILE*fp4;
if((fp4=fopen("score","rb"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
for(i=0;i<N;i++)
{fread(&score[i],sizeof(struct score),1,fp);
printf("%4d%-10s%4f%4f%4f%4f",score[i].num,score[i].name,score[i].score[0],score[i].score[1],score[i].score[2],score[i].aver);
}
fclose(fp4);
}
void fix()
{ int m;
FILEfp5;
if((fp5=fopen("score","rb+"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
printf("输入需修改成绩的学号:\n");
scanf("%d",&m);
fseek(fp5,msizeof(struct score),0);
fread(&score[m],sizeof(struct score),1,fp);
printf("%4d%-10s%4f%4f%4f%4f",score[i].num,score[i].name,score[i].score[0],score[i].score[1],score[i].score[2],score[i].aver);
printf("重新输入");
scanf("%d %s %f %f %f %f",&score[m].num,&score[m].name,&score[m].score[0],&score[m].score[1],&score[m].score[2],&score[m].aver);
fwrite(&score[m],sizeof(struct score),1,fp);
fclose(fp5);
}
int seek(int m);
{ FILEfp6;
if((fp6=fopen("score","rb"))==NULL)
{printf("cannot not open file\n");
exit(0);
}
fseek(fp6,msizeof(struct score),0);
fread(&score[m],sizeof(struct score),1,fp);
printf("%4d%-10s%4f%4f%4f%4f",score[i].num,score[i].name,score[i].score[0],score[i].score[1],score[i].score[2],score[i].aver);
fclose(fp6);
}