本人是新人,请教高手!!
题目:有5个学生,每个学生有3门课的成绩,从键盘输入数据(包括学生号,姓名、3门课程的成绩),计算出平均成绩,将原有数据和计算出的平均分数放在磁盘文件stud中。#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
struct student
{int num;
char name[20];
float score[3];
float ave;
}a[5];
void input(struct student *p)
{int i;
struct student *s=p;
printf("please input:\n");
for(;p<(s+5);p++)
{printf("number:");
scanf("%d",&p->num);
printf("name:");
scanf("%s",p->name);
printf("score:");
scanf("1=%f,2=%f,3=%f",&p->score[0],&p->score[1],&p->score[2]);}
}
void main()
{FILE *fp;
int i;
if((fp=fopen("stud.txt","w"))==NULL)
{printf("Can't open file\n");
exit(0);}
input(a);
for(i=0;i<5;i++)
{a[i].ave=(a[i].score[0]+a[i].score[1]+a[i].score[2])/3;
fwrite(a,sizeof(a),1,fp);}
fclose(fp);
printf("num name score1 score2 score3 average \n");
printf("------------------------------------------------");
fp=fopen("test.txt","r");
for(i=0;i<5;i++)
{fread(a,sizeof(a),1,fp);
printf("%d %s %d %d %d %4.1f",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].score[2],a[i].ave);
}
fclose(fp);
}
逻辑应该没错吧···我就是不知道怎么改!