实验任务4
代码
1 #include <stdio.h>
2
3 int main()
4 {
5 FILE *fp;
6 char line[1024];
7 int lines = 0;
8 int chars = 0;
9 int i;
10
11 fp = fopen("C:\\Users\\lenovo\\Downloads\\实验7数据文件及部分代码_gbk\\data4.txt", "r");
12
13 while (fgets(line, sizeof(line), fp) != NULL) {
14 lines++;
15 for (i = 0; line[i] != '\0'; i++) {
16 char c = line[i];
17 if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
18 chars++;
19 }
20 }
21 }
22
23 fclose(fp);
24
25 printf("data4.txt统计结果:\n");
26 printf("行数: %d\n", lines);
27 printf("字符数(不统计空白符): %d\n", chars);
28
29 getchar();
30 return 0;
31 }
运行结果
![image]()
实验任务5
代码
1 #include <stdio.h>
2 #include <string.h>
3 #include<stdlib.h>
4
5 #define N 10
6
7 typedef struct {
8 long id; // 准考证号
9 char name[20]; // 姓名
10 float objective; // 客观题得分
11 float subjective; // 操作题得分
12 float sum; // 总分
13 char result[10]; // 考试结果
14 } STU;
15
16 // 函数声明
17 void read(STU st[], int n);
18 void write(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 read(stu, N);
29
30 printf("\n对考生成绩进行统计: 已完成\n");
31 cnt = process(stu, N, stu_pass);
32
33 printf("\n所有考生完整信息:\n");
34 output(stu, N);
35
36 printf("\n通过考试的名单写入文件: 已完成!\n");
37 write(stu_pass, cnt);
38
39 pass_rate = 1.0 * cnt / N;
40 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
41
42 system("pause");
43 return 0;
44 }
45
46 // 把所有考生完整信息输出到屏幕上
47 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
48 void output(STU st[], int n) {
49 int i;
50
51 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
52 for (i = 0; i < n; i++)
53 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);
54 }
55
56 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
57 void read(STU st[], int n) {
58 int i;
59 FILE *fin;
60
61 fin = fopen("examinee.txt", "r");
62 if (!fin) {
63 printf("fail to open file\n");
64 return;
65 }
66
67 for (i = 0; i < n; i++)
68 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
69
70 fclose(fin);
71 }
72
73 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
74 int process(STU st[], int n, STU st_pass[]) {
75 int i, j;
76 j = 0;
77 for (i = 0; i < n; i++) {
78 st[i].sum = st[i].objective + st[i].subjective;
79 if (st[i].sum >= 60) {
80 strcpy(st[i].result, "通过");
81 st_pass[j++] = st[i];
82 } else {
83 strcpy(st[i].result, "不通过");
84 }
85 }
86 return j;
87 }
88
89 // 把通过考试的考生完整信息写入文件list_pass.txt
90 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
91 void write(STU st[], int n) {
92 int i;
93 FILE *fout;
94
95 fout = fopen("list_pass.txt", "w");
96 if (!fout) {
97 printf("无法创建文件 list_pass.txt\n");
98 return;
99 }
100 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
101 for (i = 0; i < n; i++) {
102 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
103 st[i].id, st[i].name,
104 st[i].objective, st[i].subjective,
105 st[i].sum, st[i].result);
106 }
107 fclose(fout);
108 }
运行结果
![image]()
![image]()
实验任务6
代码
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5
6 #define TOTAL 80
7 #define LUCKY 5
8
9 typedef struct {
10 char id[20];
11 char name[20];
12 char class[50];
13 } STUDENT;
14
15 int main()
16 {
17 STUDENT stu[TOTAL], lucky[LUCKY];
18 int i, j, index;
19 int chosen[TOTAL];
20 FILE *fp;
21 char filename[100];
22
23 fp = fopen("list.txt", "r");
24 if (fp == NULL) {
25 printf("无法打开文件 list.txt!\n");
26 return 1;
27 }
28 for (i = 0; i < TOTAL; i++) {
29 fscanf(fp, "%s %s %s", stu[i].id, stu[i].name, stu[i].class);
30 }
31 fclose(fp);
32
33 srand((unsigned int)time(NULL));
34 for (i = 0; i < TOTAL; i++) {
35 chosen[i] = 0;
36 }
37
38 for (i = 0; i < LUCKY; i++) {
39 do {
40 index = rand() % TOTAL;
41 } while (chosen[index] == 1);
42 lucky[i] = stu[index];
43 chosen[index] = 1;
44 }
45
46 printf("\n中奖名单\n");
47 printf("学号\t\t姓名\t班级\n");
48 for (i = 0; i < LUCKY; i++) {
49 printf("%s\t%s\t%s\n", lucky[i].id, lucky[i].name, lucky[i].class);
50 }
51
52 printf("\n输入文件名:");
53 scanf("%s", filename);
54
55 fp = fopen(filename, "w");
56 if (fp == NULL) {
57 printf("无法创建文件 %s!\n", filename);
58 return 1;
59 }
60 fprintf(fp, "中奖名单\n");
61 fprintf(fp, "学号\t\t姓名\t班级\n");
62 for (i = 0; i < LUCKY; i++) {
63 fprintf(fp, "%s\t%s\t%s\n", lucky[i].id, lucky[i].name, lucky[i].class);
64 }
65 fclose(fp);
66
67 printf("保存成功!\n");
68
69 system("pause");
70 return 0;
71 }
运行结果
![image]()
![image]()