求解答,问题描述附件一,出问题的代码附件二,学生成绩管理
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef struct student
{
double id;
char name[50];
int score[3];
int ave;
int sum;
}STU;
STU s[48];
STU t;
int main()
{
FILE * fp;
int i,j,max;
if((fp=fopen("stud.txt","r"))==NULL) //读取原始数据
{
printf("Can not open the file !!!\n");
exit(0);
}
for(i=0;i<48;i++) //原始数据暂存结构体
{
fscanf(fp,"%lf%s%d%d%d",&s[i].id,s[i].name,&s[i].score[0],&s[i].score[1],&s[i].score[2]);
s[i].ave=(s[i].score[0]+s[i].score[1]+s[i].score[2])/3;
s[i].sum=s[i].score[0]+s[i].score[1]+s[i].score[2];
}
fclose(fp);
for(i=0;i<48;i++)
{
printf("%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
}
if((fp=fopen("stud.dat","wb"))==NULL) //数据存储二进制文件
{
printf("Can not open the file stud.dat!!\n");
exit(0);
}
fwrite(s,sizeof(STU),48,fp);
fclose(fp);
for(i=0;i<47;i++)
{
max=i;
for(j=i+1;j<48;j++)
{
if(s[max].sum<s[j].sum)
max=j;
}
t=s[i];
s[i]=s[max];
s[max]=t;
}
if((fp=fopen("score_sorted.txt","w"))==NULL) // 总成绩排序存储文件
{
printf("Can not open the file score_sorted.txt!!!\n");
exit(0);
}
for(i=0,j=1;i<48;i++,j++)
{
fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
}
if((fp=fopen("score_sorted.dat","wb"))==NULL) //二进制文件
{
printf("Can not open the file score_sorted.dat!!\n");
exit(0);
}
fwrite(s,sizeof(STU),48,fp);
fclose(fp);
if((fp=fopen("Chinese_sorted.txt","w"))==NULL) //语文成绩排序存储文件
{
printf("Can not open the file Chinese_sorted.txt!!!\n");
exit(0);
}
for(i=0;i<47;i++)
{
max=i;
for(j=i+1;j<48;j++)
{
if(s[max].score[0]<s[j].score[0])
max=j;
}
t=s[i];
s[i]=s[max];
s[max]=t;
}
for(i=0,j=1;i<48;i++,j++)
{
fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
}
if((fp=fopen("Chinese_sorted.dat","wb"))==NULL) //二进制文件
{
printf("Can not open the file Chinese_sorted.dat!!\n");
exit(0);
}
fwrite(s,sizeof(STU),48,fp);
fclose(fp);
if((fp=fopen("Math_sorted.txt","w"))==NULL) //数学成绩排序存储文件
{
printf("Can not open the file Math_sorted.txt!!!\n");
exit(0);
}
for(i=0;i<47;i++)
{
max=i;
for(j=i+1;j<48;j++)
{
if(s[max].score[1]<s[j].score[1])
max=j;
}
t=s[i];
s[i]=s[max];
s[max]=t;
}
for(i=0,j=1;i<48;i++,j++)
{
fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
}
if((fp=fopen("Math_sorted.dat","wb"))==NULL) //二进制文件
{
printf("Can not open the file Math_sorted.dat!!\n");
exit(0);
}
fwrite(s,sizeof(STU),48,fp);
fclose(fp);
if((fp=fopen("English_sorted.txt","w"))==NULL) //英语成绩排序存储文件
{
printf("Can not open the file English_sorted.txt!!!\n");
exit(0);
}
for(i=0;i<47;i++)
{
max=i;
for(j=i+1;j<48;j++)
{
if(s[max].score[2]<s[j].score[2])
max=j;
}
t=s[i];
s[i]=s[max];
s[max]=t;
}
for(i=0,j=1;i<48;i++,j++)
{
fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
}
if((fp=fopen("English_sorted.dat","wb"))==NULL) //二进制文件
{
printf("Can not open the file English_sorted.dat!!\n");
exit(0);
}
fwrite(s,sizeof(STU),48,fp);
fclose(fp);
double N;
char M[20];
int a;
printf("Please put in the the student's ID and the subject(Chinese or Math or English or Total) that you want to have a look:\n");
scanf("%lf",&N);
scanf("%s",M);
getchar();
if((fp=fopen(M,"r"))==NULL)
{
printf("Can not open the file %s!!!\n",M);
exit(0);
}
for(i=0;i<48;i++)
{
fscanf(fp,"%d\t\t%lf%s%d%d%d%d%d",&a,&t.id,t.name,&t.score[0],&t.score[1],&t.score[2],&t.ave,&t.sum);
if(N==t.id)
{
printf("%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",a,t.id,t.name,t.score[0],t.score[1],t.score[2],t.ave,t.sum);
break;
}
}
fclose(fp);
return 0;
}