首页 > 试题广场 >

有5个学生,每个学生有3门课的成绩,从键盘输人学生数据(包括

[问答题]
有5个学生,每个学生有3门课的成绩,从键盘输人学生数据(包括学号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件“stud”中。
推荐
解法一: N-S图如下图所示。

#include<stdio. h>
struct student
{char num[10];
 char name[8];
 float ave;
} stu[5];
main()
{int i,j,sum;
FILE *fp;
for(i=0;i<5;i++ )
    {printf("\ hInput score of student %d:\n",i+1);
    printf("NO.:");
    scanf("%s",stu[i].num);
    printf("name:");
    scanf("%s",stu[i].name);
    sum=0;
    for(j=0;j<3;j++)
       {printf(" score %d:",j+1);
       scanf("%d",&stu[i].score[j])
       sum+ =stufi].score[j];
        }
    stu[i].ave=sum/3.0;
}
/*将数据写人文件*/
fp= fopen("stud","w");
for (i=0;i<5;i++ )
    if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
        printf("File white error\n”);
fclose(fp);
/*检查文件内容*/
fp= fopen(”stud"."r");
for (i=0;i<5;i++ )
    if(fread(&stu[i],sizof(struct student),1,fp))
printf("%s,%s,%d,%d,%d%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],
stu[i].ave);}
}
运行结果如下:
Input score of student 1:
NO.: 110
name: Li
score 1: 90
score 2: 89
score 3: 88

Input score of student 2:
NO.: 120
name:Wang
score 1: 80
score 2: 79
score 3: 78

Input score of student 3:
NO.: 130
name: Chen
score 1: 70
score 2: 69
score 3: 68

Input score of student 4:
NO.: 140
name: Ma
score 1: 100
score 2: 99
score 3: 98
Input score of student 5:
NO.: 150
name: Wei
score 1: 60
score 2: 59
score 3: 58

110,Li,90,89,88,89.00
120,Wang,80,79,78,79.00
130,Chen,70,69,68,69.00
140,Ma,100,99,98,99.00
150,Wei,60,59,58,59.00
MO.A 140
ingu Koeeod mdkmns,
NO.150
manet We
core1a 60
sore2159
score3i 58
10.1.90.89.68.89.00
120.Wang.80.79.78.79.00
130.0mem7.69.68.69.00
140.M10.09.99.00
150.We,60,59.58.59 (00
说明:在程序的第一个for循环中,有两个printf函数语句用来提示用户输人数据,即"printf ("\ninput score of student %d:\n",i+1);和"printf("score %d:",j+1);"其中用“i+ 1”和“j+1”而不用i和j的用意是使显示提示时,序号从1起,即学生1和成绩1 (而不是学生0和成绩0),以符合人们习惯,但在内存中,数组元素下标仍从0算起。
程序最后5行用来检查文件stud中的内容是否正确,从结果来看,是正确的。请注意:用fwrite函数向文件输出数据时不是按ASCII码方式输出的,而是按内存中存储数据的方式输出的(例如一个整数占2个字书,一个实数占4个字节),因此不能用type命令输出该文件中的数据。
解法二:
#include<stdio. h>
#define SIZE 5
struct student
{char name[10];
 int num;
 int score[3];
 float ave;
} stud[SIZE];
main()
{ void save( void);    /*函数声明*/
 int i;
 float sum[SIZE];
 FILE * fp1;
 for(i=0;i< SIZE;i++ )    /*输入数据,并求每个学生的平均分*/
 {scanf(”%s%d%d%d%d”,stud[i].name,&stud[i].num,&stud[i].score[0],&stud[i].score[1],&stu[i].score[2]);
        sum[i] = stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
        stud[i].ave= sum[i]/3;
    }
    save();    /*调用save函数,向文件stu.dat输出数据*/
    fpl= fopen("stu.dat","rb");    /*用只读方式打开stu.dat文件*/
    printf(”\ n name NO. score1 score2 score3 ave\n")    
      for (i=0;i< SIZE;i++ )    /*从文件读入数据并在屏幕输出*/
       {
       fread(&stud[i],sizeof(struct student),1,fp1);
printf("%-10s%3d%7d%7d%7d%8.2f\n'"stud{i].name,stud[i].num,stud[i].score[0],stud[i].
score[1],
stud[i].score[2],stud[i].ave);
        }
    fclose (fpl);
}
void save( void)    /*向文件输出数据的函数*/
    {
    FILE *fp;
    int 1;
    if((fp= fopen(”stu.dat",”wb"))==NULL)
        {
        printf("Can not open the file\n");
        return;
        }
    for(i=0;i<SIZE;i++ )
        if(fwrite( &stud[i] ,sizeof(struct student),1,fp)! =1)
           {printf("File write error\n");
           return;
            }
    fclose(fp);
}

运行结果如下:
Zhang 101 77 78 98
Li 102 67 78 88
Wang 103 89 99 97
Wei 104 77 76 98
Tan 105 78 89 97

name     NO.    score1    score2    score3    ave
Zhang    101    77            78            98        84.33
Li            102    67            78            88         77.67
Wang      103    89            99            97         95.00
Wei          104    77            76            98         83.67
Tan         105     78            89            97        88.00
本程序用save函数将数据写到磁盘文件上,再从文件读回,然后用printf函数输出,从运行结果可以看到文件中的数据是正确的。

发表于 2018-08-06 19:58:12 回复(0)
s
发表于 2020-05-12 09:02:45 回复(0)