2014年计算机二级C语言上机模拟试题4
程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是从形参filename所指的文件中读入学生数据,并按照学号从小到大排序后,再用二进制方式把排序后的学生数据输出到filename所指的文件中,覆盖原来的文件内容。 请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。 注意:源程序存放在考生文件夹下的BLANK1.C中。 不得增行或删行,也不得更改程序的结构! 给定源程序: #include #define N 5 typedef struct student { long sno; char name[10]; float score[3]; } STU; void fun(char *filename) { FILE *fp; int i, j; STU s[N], t; fp = fopen(filename, __1__); fread(s, sizeof(STU), N, fp); fclose(fp); for (i=0; i for (j=i+1; j if (s[i].sno __2__ s[j].sno) { t = s[i]; s[i] = s[j]; s[j] = t; } fp = fopen(filename, "wb"); __3__(s, sizeof(STU), N, fp); /* 二进制输出 */ fclose(fp); } main() { STU t[N]={ {10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78}, {10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}, {10001,"MaChao", 91, 92, 77}}, ss[N]; int i,j; FILE *fp; fp = fopen("student.dat", "wb"); fwrite(t, sizeof(STU), 5, fp); fclose(fp); printf("
The original data :
"); for (j=0; j { printf("
No: %ld Name: %-8s Scores: ",t[j].sno, t[j].name); for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]); printf("
"); } fun("student.dat"); printf("
The data after sorting :
"); fp = fopen("student.dat", "rb"); fread(ss, sizeof(STU), 5, fp); fclose(fp); for (j=0; j { printf("
No: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name); for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]); printf("
"); } } |