C程序题解答

来源:百度知道 编辑:UC知道 时间:2024/05/03 04:30:39
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

#include<stdio.h>
#include<conio.h>
struct stu //创建学生所需的结构体
{
char stuname[10]; //存储学生姓名
int stunum; //存储学生编号
float fir,sec,thi,ave; //存储学生成绩
};
add() //写入文件所需的函数
{
FILE *fp;
struct stu addstu;
float flag='y';
if((fp=fopen("stud.txt","a+f"))==NULL) //判断文件是否存在,a+f追加或创建文件读或写操作
{
printf("文件未找到.");
getch();
return;
}
printf("请输入学生信息\n");
while(flag=='y') //循环接收学生信息
{
printf("请输入学生编号:");
scanf("%d",&addstu.stunum);

printf("学生姓名:");
scanf("%s",addstu.stuname);

printf("第一门成绩:");
scanf("%f",&addstu.fir);
printf("第二门成绩:");
scanf("%f",&addstu.sec);
printf(&qu