Task4
1 #include <stdio.h>
2
3 int main() {
4 FILE* fp = fopen("data4.txt", "r");
5
6 int count = 0;
7 char c;
8 if (fp ==NULL) {
9 printf("fail to open the file\n");
10 return 1;
11 }
12 while ((c = fgetc(fp)) != EOF) {
13 if (c != ' ' && c != '\n' && c != '\t') {
14 count++;
15 }
16 }
17
18 fclose(fp);
19
20 printf("data4.txt中共包含字符数(不计空白符):%d\n", count);
21
22 return 0;
23 }
![]()
Task5
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_pass, cnt); // 输出到屏幕
35 foutput(stu_pass, cnt); // 输出到文件
36
37 pass_rate = 1.0 * cnt / N;
38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
39
40 system("pause");
41 return 0;
42 }
43
44 // 把通过考试的考生完整信息输出到屏幕上
45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
46 void output(STU st[], int n) {
47 int i;
48
49 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
50 for (i = 0; i < n; i++)
51 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);
52 }
53
54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
55 void finput(STU st[], int n) {
56 int i;
57 FILE *fin;
58
59 fin = fopen("examinee.txt", "r");
60 if (fin == NULL) {
61 printf("fail to open file\n");
62 exit(0);
63 }
64
65 while (!feof(fin)) {
66 for (i = 0; i < n; i++)
67 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
68 }
69
70 fclose(fin);
71 }
72
73 // 把通过考试的考生完整信息写入文件list_pass.txt
74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
75 void foutput(STU s[], int n) {
76 FILE *fout;
77 int i;
78
79 // 保存到文件
80 fout = fopen("list_pass.txt", "w");
81 if (!fout) {
82 printf("fail to open or create list_pass.txt\n");
83 exit(0);
84 }
85
86 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
87
88 for (i = 0; i < n; i++)
89 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);
90
91 fclose(fout);
92 }
93
94 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
95 int process(STU st[], int n, STU st_pass[]) {
96 int i;
97 int count = 0;
98 for (i = 0; i < n; i++) {
99 st[i].sum = st[i].objective + st[i].subjective;
100 if (st[i].sum > 60) {
101 strcpy(st[i].ans, "pass");
102 st_pass[count] = st[i];
103 count++;
104 }
105 else
106 strcpy(st[i].ans, "fail");
107 }
108 return count;
109 }
![]()
![]()
Task6
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include <time.h>
4 #define N 50
5
6 typedef struct {
7 long int id;
8 char name[N];
9 char cl[N];
10 } STU;
11
12 STU lucky[N],st[N];
13 void finput();
14 void foutput();
15 int main() {
16 int m, n;
17 finput();
18
19 srand(time(NULL));
20
21 for (m = 0; m < 5; m++) {
22 n = rand() % 80;
23 lucky[m] = st[n];
24 printf("%ld\t %s\t %s\t\n", lucky[m].id, lucky[m].name, lucky[m].cl);
25 }
26
27 foutput();
28
29 getchar();
30 system("pause");
31 return 0;
32 }
33
34
35 void finput() {
36 int i;
37 FILE *fin;
38 fin = fopen("list.txt", "r");
39 if (fin == NULL) {
40 printf("fail to open file\n");
41 return;
42 }
43 while (!feof(fin)) {
44 for (i = 0; i < 80; i++)
45 fscanf(fin, "%ld %s %s", &st[i].id, st[i].name,&st[i].cl);
46 }
47 fclose(fin);
48 }
49
50 void foutput() {
51 FILE *fout;
52 int i,n=5;
53 fout = fopen("lucky.txt", "w");
54 if (!fout) {
55 printf("fail to open or create lucky.txt\n");
56 return;
57 }
58
59 for (i = 0; i < n; i++)
60 fprintf(fout, "%ld\t\t%s\t\t\t%s\n", lucky[i].id,lucky[i].name,lucky[i].cl);
61
62 fclose(fout);
63 }
![]()