1 #include <stdio.h>
2 #include <string.h>
3
4 #define N 10
5
6 typedef struct {
7 long id;
8 char name[20];
9 float objective;
10 float subjective;
11 float sum;
12 char result[10];
13 } STU;
14
15 void read(STU st[], int n);
16 void write(STU st[], int n);
17 void output(STU st[], int n);
18 int process(STU st[], int n, STU st_pass[]);
19
20 int main() {
21 STU stu[N], stu_pass[N];
22 int cnt;
23 double pass_rate;
24
25 printf("从文件读入%d个考生信息: 已完成\n", N);
26 read(stu, N);
27
28 printf("\n对考生成绩进行统计: 已完成\n");
29 cnt = process(stu, N, stu_pass);
30
31 printf("\n所有考生完整信息:\n");
32 output(stu, N);
33
34 printf("\n通过考试的名单写入文件: 已完成!\n");
35 write(stu_pass, cnt);
36
37 pass_rate = 1.0 * cnt / N;
38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
39
40 return 0;
41 }
42
43 void output(STU st[], int n) {
44 int i;
45
46 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
47 for (i = 0; i < n; i++)
48 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
49 }
50
51 void read(STU st[], int n) {
52 int i;
53 FILE *fin;
54
55 fin = fopen("examinee.txt", "r");
56 if (!fin) {
57 printf("fail to open file\n");
58 return;
59 }
60
61 for (i = 0; i < n; i++)
62 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
63
64 fclose(fin);
65 }
66
67 int process(STU st[], int n, STU st_pass[]) {
68 int i,num=0;
69 for(i=0;i<n;i++){
70 st[i].sum=st[i].objective+st[i].subjective;
71 if(st[i].sum>60){
72 strcpy(st[i].result,"通过");
73 st_pass[num]=st[i];
74 num++;
75 }else
76 strcpy(st[i].result,"不通过");
77 }
78 return num;
79 }
80
81 void write(STU st[], int n) {
82 int i;
83 FILE *fout;
84 fout=fopen("list_pass.txt","w");
85 fprintf(fout,"准考证号\t姓名\t客观题\t操作题\t总分\t考试结果\n");
86 for(i=0;i<n;i++)
87 fprintf(fout,"%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result);
88 fclose(fout);
89 }