编写程序,求第5题中每个学生的总分和平均分,并给学生数据增加上总分和平均分,文件名为stu2.txt。
#include <stdio.h>
#include <stdlib.h>
#define N 5
struct student{
int num;
char name[15];
char sex;
int chinese;
int math;
int PE;
};
int main(void)
{
int i,sum;double pjf;struct student stu[N];
if((freopen("/Users/jiliangpeng/Desktop/stu1.txt",“r”,stdin))==NULL)
{
printf(“File open error!\n”);
exit(0);
}
if(freopen("/Users/jiliangpeng/Desktop/stu2.txt",“w”,stdout)==NULL)
{
printf(“File open 2 error!\n”);
exit(0);
}
for(i=0;i<N;i++)
{
scanf("%d %s\t%c\t%d %d %d",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].chinese,&stu[i].math,&stu[i].PE);
sum=stu[i].chinese+stu[i].math+stu[i].PE;
pjf=sum/3;
printf("%d %s\t%c\t%d %d %d %d %.2lf\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].chinese,stu[i].math,stu[i].PE,sum,pjf);
}
fclose(stdin);
fclose(stdout);
return 0;
}