Posted in 面试题 onJune 06, 2014
已知文件中存有10 个学生的数学、政治、和英语三门课的成绩,试统计每一学生的平均成绩,并存入该文件中。
解:#include
struct student{ int number;
char cname[10];
int shuxue;
int zhengzhi;
int yingyu;
double ave;};
main()
{
FILE *fp;
struct student s[10];
int size;
int i;
size=sizeof(struct student);
if((fp=fopen(“student”,”ab+”))==NULL)
{
printf(“cannot open the file!”);
exit(0);
}
for(i=0;i {
fread(&s[i],size,1,fp);
s[i].ave=(s[i].shuxue+s[i].zhengzhi+s[i].yingyu)/3;
}
rewind(fp);
for(i=0;i fwrite(&s[i],size,i,fp);
fclose(fp);
}
解:#include
struct student{ int number;
char cname[10];
int shuxue;
int zhengzhi;
int yingyu;
double ave;};
main()
{
FILE *fp;
struct student s[10];
int size;
int i;
size=sizeof(struct student);
if((fp=fopen(“student”,”ab+”))==NULL)
{
printf(“cannot open the file!”);
exit(0);
}
for(i=0;i {
fread(&s[i],size,1,fp);
s[i].ave=(s[i].shuxue+s[i].zhengzhi+s[i].yingyu)/3;
}
rewind(fp);
for(i=0;i fwrite(&s[i],size,i,fp);
fclose(fp);
}
统计每一学生的平均成绩
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@