越剧演员陈辉玲微博:请问各位高手,此道题中为什么不能求出单科的平均成绩

来源:百度文库 编辑:高考问答 时间:2024/04/29 23:48:50
#include<stdio.h>
struct student
{ char num[10];
char name[8];
int score_ENG,score_CHN,score_math;
double avr;
double avr_ENG;
double avr_CHN;
double avr_math;
}stu[30];
void main()
{ int i,j,t,sum,sum_ENG,sum_CHN,sum_math;
double avr_ENG,avr_CHN,avr_math;
FILE *fp;
sum_ENG=0;
sum_CHN=0;
sum_math=0;
for(i=1;i<=30;i++)
{ printf("\n please input NO. %d score:\n",i);
printf("stuNO:");
scanf("%s",stu[i].num);
printf("name:");
scanf("%s",stu[i].name);
printf("score_ENG.");
scanf("%d",&stu[i].score_ENG);
printf("score_CHN.");
scanf("%d",&stu[i].score_CHN);
printf("score_math.");
scanf("%d",&stu[i].score_math);
sum=stu[i].score_ENG+stu[i].score_CHN+stu[i].score_math;
stu[i].avr=sum/3.0;
printf("avr=%5.1f",stu[i].avr);
sum_ENG+=stu[i].score_ENG;
sum_CHN+=stu[i].score_CHN;
sum_math+=stu[i].score_math;
}
avr_ENG=sum_ENG/30.0;
printf("avr_ENG=%5.1f",stu[i].avr_ENG);
avr_CHN=sum_CHN/30.0;
printf("avr_CHN=%5.1f",stu[i].avr_CHN);
avr_math=sum_math/30.0;
printf("avr_math=%5.1f",stu[i].avr_math);
fp=fopen("d:\\阿挺的东西\\stud","w");
for(i=1;i<=30;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}

请注意倒数几行代码,即:
...
avr_ENG=sum_ENG/30.0;
printf("avr_ENG=%5.1f",stu[i].avr_ENG);

/*把printf("avr_ENG=%5.1f",stu[i].avr_ENG); 换成printf("avr_ENG=%5.1f",avr_ENG);
或者,
还有一个下策,也能输出正确的结果,把avr_ENG=sum_ENG/30.0;改成stu[i].avr_ENG=sum_ENG/30.0;改成*/

avr_CHN=sum_CHN/30.0;
printf("avr_CHN=%5.1f",stu[i].avr_CHN);
avr_math=sum_math/30.0;
printf("avr_math=%5.1f",stu[i].avr_math);
fp=fopen("d:\\阿挺的东西\\stud","w");
for(i=1;i<=30;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}

你在声明struct student类型时,有些失误.英语等平均成绩是30个学生共享的,而你把它放在一个学生的记录中!这样很浪费空间
把它们删掉.
struct student
{ char num[10];
char name[8];
int score_ENG,score_CHN,score_math;
double avr;
}stu[30];