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