C Exercise Examples 100
Title:There are five students, each student has scores in 3 courses. Input the above data from the keyboard (including student ID, name, and scores of the three courses), calculate the average score, and store the original data and the calculated average scores in the disk file "stud".
Program analysis:None.
Program source code:
Example
// Created by www.example.com on 15/11/9.
// Copyright © 2015 Example. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int ID;
int math;
int English;
int C;
int avargrade;
char name[20];
}Stu;
int main()
{
FILE*fp;
Stu stu[5];
int i,avargrade=0;
printf("Please enter information for 5 students: student ID, name, and grades for 3 subjects:\n");
for(i=0;i<5;i++)
{
scanf("%d %s %d %d %d",&(stu[i].ID),stu[i].name,&(stu[i].math),&(stu[i].English),&(stu[i].C));
stu[i].avargrade=(stu[i].math+stu[i].English+stu[i].C)/3;
}
if((fp=fopen("stud","w"))==NULL)
{
printf("error :cannot open file!\n");
exit(0);
}
for(i=0;i<5;i++)
fprintf(fp,"%d %s %d %d %d %d\n",stu[i].ID,stu[i].name,stu[i].math,stu[i].English,
stu[i].C,stu[i].avargrade);
fclose(fp);
// system("pause");
return 0;
}
After running the above example, the output result is:
请输入5个同学的信息:学生号,姓名,3门成绩: 1 a 60 70 80 2 b 60 80 90 3 c 59 39 89 4 e 56 88 98 5 d 43 88 78
Open the stud file, the contents are as follows.
1 a 60 70 80 70 2 b 60 80 90 76 3 c 59 39 89 62 4 e 56 88 98 80 5 d 43 88 78 69other extensions