首页 > 其他 > 详细

5个学生,3门成绩,输入信息,保存到文件

时间:2018-09-23 23:26:08      阅读:183      评论:0      收藏:0      [点我收藏+]

有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

#include <stdlib.h>
#include <stdio.h>

typedef struct
{
int id;
char name[20];
float math;
float chinese;
float english;
float average;
}Stu;

int main(void)
{
Stu stu[5];
int i;
for(i=0; i < 5; i++)
{
printf("Please input the ID, Name and scores of three courses\n");
scanf("%d %s %f %f %f",&(stu[i].id),&(stu[i].name),&(stu[i].math),&(stu[i].chinese),&(stu[i].english));//注意scanf的""中不能加\n
stu[i].average = (stu[i].math + stu[i].chinese + stu[i].english)/3;
printf("average is %f\n",stu[i].average);
}

FILE *fp;
if((fp = fopen("stud","w"))== NULL)
{
printf("error:cannot open file!\n");
exit(0);
}
for(i=0; i<5; i++)
{
fprintf(fp, "%d %s %f %f %f %f",stu[i].id,stu[i].name,stu[i].english,stu[i].chinese,stu[i].math,stu[i].average);
}
fclose(fp);

return 0;
}

 

5个学生,3门成绩,输入信息,保存到文件

原文:https://www.cnblogs.com/embeddedking/p/9693777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!