![]() |
#2
fulltimelink2020-06-14 19:24
|
【输入形式】
【输出形式】
【样例输入】假设score.txt中的数据为:90 80 70 60
【样例输出】result.txt中的数据为:75.00 90 60
麻烦大神们帮我改一下这个程序 谢谢谢谢 总是测试的数据不对

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
int max=0,min=100,score,n=0;
double avg=0;
if ((fp1=fopen("score.txt","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
fp2=fopen("result.txt","w");
while(fscanf(fp1,"%d",&score)!=EOF)
{
fscanf(fp1,"%d",&score);
if(max<score) max=score;
if(min>score) min=score;
avg+=score;
n++;
}
avg=avg/n;
fprintf(fp2,"%.2f %d %d",avg,max,min);
if(fclose(fp1)){
printf("Cannot close this file!\n");
exit(0);
}
if(fclose(fp2)){
printf("Cannot close this file!");
exit(0);
}
return 0;
}
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
int max=0,min=100,score,n=0;
double avg=0;
if ((fp1=fopen("score.txt","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
fp2=fopen("result.txt","w");
while(fscanf(fp1,"%d",&score)!=EOF)
{
fscanf(fp1,"%d",&score);
if(max<score) max=score;
if(min>score) min=score;
avg+=score;
n++;
}
avg=avg/n;
fprintf(fp2,"%.2f %d %d",avg,max,min);
if(fclose(fp1)){
printf("Cannot close this file!\n");
exit(0);
}
if(fclose(fp2)){
printf("Cannot close this file!");
exit(0);
}
return 0;
}