四、实验结论
4. 实验任务4
task4.c
1 #include <stdio.h>
2
3 int main() {
4 FILE *fp;
5
6 int count = 0;
7
8 fp = fopen("data4.txt", "r");
9
10 if(fp == NULL) {
11 printf("fail to open file\n");
12 return 1;
13 }
14 char ch;
15
16 ch = fgetc(fp);
17 do {
18 if (!isspace(ch)){
19 count++;
20 }
21 ch = fgetc(fp);
22 } while (ch != EOF);
23
24 printf("data4.txt中共包含字符数:%d", count);
25 fclose(fp);
26 return 0;
27 }
运行结果
![]()
5. 实验任务5
task5.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #define N 10
6
7 typedef struct {
8 long int id;
9 char name[20];
10 float objective; // 客观题得分
11 float subjective; // 操作题得分
12 float sum; // 总分
13 char ans[10]; // 考试结果
14 } STU;
15
16 // 函数声明
17 void finput(STU st[], int n);
18 void foutput(STU st[], int n);
19 void output(STU st[], int n);
20 int process(STU st[], int n, STU st_pass[]);
21
22 int main() {
23 STU stu[N], stu_pass[N];
24 int cnt;
25 double pass_rate;
26
27 printf("从文件读入%d个考生信息...\n", N);
28 finput(stu, N);
29
30 printf("\n对考生成绩进行统计...\n");
31 cnt = process(stu, N, stu_pass);
32
33 printf("\n通过考试的名单:\n");
34 output(stu, N); // 输出到屏幕
35 foutput(stu, N); // 输出到文件
36
37 pass_rate = 1.0 * cnt / N;
38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
39
40 return 0;
41 }
42
43 // 把通过考试的考生完整信息输出到屏幕上
44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
45 void output(STU st[], int n) {
46 int i;
47
48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
49 for (i = 0; i < n; i++)
50 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].ans);
51 }
52
53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
54 void finput(STU st[], int n) {
55 int i;
56 FILE *fin;
57
58 fin = fopen("examinee.txt", "r");
59 if (fin == NULL) {
60 printf("fail to open file\n");
61 exit(0);
62 }
63
64 while (!feof(fin)) {
65 for (i = 0; i < n; i++)
66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
67 }
68
69 fclose(fin);
70 }
71
72 // 把通过考试的考生完整信息写入文件list_pass.txt
73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
74 void foutput(STU s[], int n) {
75 FILE *fout;
76 int i;
77
78 // 保存到文件
79 fout = fopen("list_pass.txt", "w");
80 if (!fout) {
81 printf("fail to open or create list_pass.txt\n");
82 exit(0);
83 }
84
85 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
86
87 for (i = 0; i < n; i++)
88 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans);
89
90 fclose(fout);
91 }
92
93
94
95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
96 int process(STU st[], int n, STU st_pass[]) {
97 // 待补足
98 // ×××
99 int sum = 0;
100
101 for (int i = 0; i < n; i++) {
102 st[i].sum = st[i].subjective + st[i].objective;
103 if (st[i].sum >= 60.0) {
104 strcpy(st[i].ans, "pass");
105 st_pass[sum] = st[i];
106 sum++;
107 }
108 else {
109 strcpy(st[i].ans, "fail");
110 }
111 }
112 return sum;
113 }
运行结果
![]()
![]()
6. 实验任务6(已实现必做与选作)
task6.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 typedef struct {
6 char id[20];
7 char name[20];
8 char clazz[20];
9 } stu;
10
11 void swap(int *a, int *b) {
12 int temp = *a;
13 *a = *b;
14 *b = temp;
15 }
16
17 void bubbleSort(int arr[]) {
18 for (int i = 0; i < 5 - 1; i++) {
19 for (int j = 0; j < 5 - i - 1; j++) {
20 if (arr[j] > arr[j + 1]) {
21 swap(&arr[j], &arr[j + 1]);
22 }
23 }
24 }
25 }
26
27 int is_duplicate(int number, int *array, int size) {
28 for (int i = 0; i < size; ++i) {
29 if (array[i] == number) {
30 return 1; // 数字已经存在,是重复的
31 }
32 }
33 return 0; // 数字不存在,不是重复的
34 }
35
36 int main() {
37 srand(time(NULL));
38
39 int random_numbers[5];
40 int generated_numbers = 0;
41
42 while (generated_numbers < 5) {
43 int new_random = rand() % 80; // 假设生成 0 到 79 之间的随机数
44
45 if (!is_duplicate(new_random, random_numbers, generated_numbers)) {
46 random_numbers[generated_numbers] = new_random;
47 generated_numbers++;
48 }
49 }
50 bubbleSort(random_numbers);
51
52 FILE *fp;
53 fp = fopen("list.txt", "r");
54
55 if (fp == NULL) {
56 printf("fail to open file\n");
57 exit(0);
58 }
59
60 stu st[80];
61 while (!feof(fp)) {
62 for (int i = 0; i < 80; i++)
63 fscanf(fp, "%s %s %s", st[i].id, st[i].name, st[i].clazz);
64 }
65
66 fclose(fp);
67
68 time_t t;
69 struct tm *tm_info;
70 char filename[80];
71
72 time(&t);
73 tm_info = localtime(&t);
74
75 strftime(filename, sizeof(filename), "%Y%m%d.txt", tm_info);
76
77 fp = fopen(filename, "w");
78
79 for (int i = 0; i < 5; i++) {
80 printf("%s %s %s\n", st[random_numbers[i]].id, st[random_numbers[i]].name, st[random_numbers[i]].clazz);
81 fprintf(fp, "%s %s %s\n", st[random_numbers[i]].id, st[random_numbers[i]].name, st[random_numbers[i]].clazz);
82 }
83
84 fclose(fp);
85
86 return 0;
87 }
运行结果
![]()
![]()