大神能帮我改下错么。。刚学文件读写不太熟
#include<stdio.h>#define N 30
typedef struct student
{
long studentID;
int score[3];
}STUDENT;
void readscore(STUDENT stu[],int n,int m);
void averscore(STUDENT stu[],float aver[],int n,int m);
void addscore(STUDENT stu[],int add[],float aver[],int n,int m);
void writetofile(STUDENT stu[],int add[],float aver[],int n,int m)
int main()
{
float aver[N];
STUDENT stu[N];
int add[N];
int n;
scanf("%d",&n);
readscore(stu,n,3);
averscore(stu,aver,n,3);
addscore(stu,add,aver,n,3);
writetofile(stu,add,aver,n,3);
return 0;
}
void readscore(STUDENT stu[],int n,int m)
{
FILE *op;
int i,j;
op=fopen("data.in.txt","r");
fscanf(op,"%d",&n);
for(i=0;i<n;i++)
{
fscanf(op,"%ld",&stu[i].studentID);
for(j=0;j<m;j++)
{
fscanf(op,"%d",&stu[i].score[j]);
}
}
}
void averscore(STUDENT stu[],float aver[],int n,int m)
{
int i,j;
int sum[N];
for(i=0;i<n;i++)
{
sum[i]=0;
for(j=0;j<m;j++)
{
sum[i]=sum[i]+stu[i].score[j];
}
aver[i]=(float)sum[i]/m;
}
}
void addscore(STUDENT stu[],int add[],float aver[],int n,int m)
{
int i,j,add1;
float aver1;
STUDENT change;
for(i=0;i<n;i++)
{
add[i]=0;
for(j=0;j<m;j++)
{
add[i]=add[i]+stu[i].score[j];
}
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
if(add[i]<add[j])
{
add1=add[j];
change=stu[j];
aver1=aver[j];
add[j]=add[i];
stu[j]=stu[i];
aver[j]=aver[i];
add[i]=add1;
stu[i]=change;
aver[i]=aver1;
}
}
}
void writetofile(STUDENT stu[],int add[],float aver[], int n,int m)
{
FILE *fp;
int i,j;
fp=fopen("data.out.txt","w");
fprintf(fp,"\tNO\tMT\tEN\tPH\tSUM\tAVER\n");
for(i=0;i<n;i++)
{
fprintf(fp,"%ld\t",stu[i].studentID);
for(j=0;j<m;j++)
{
fprintf(fp,"%d\t",stu[i].score[j]);
}
fprintf(fp,"%d\t",add[i]);
fprintf(fp,"%.0f\n",aver[i]);
}
}