实验7

task1

自行测试

task2

自行测试

task3

自行测试

task4

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main() {
 4     FILE *fp;
 5     char filename[] = "data4.txt";
 6     int lines = 0, characters = 0;
 7     int ch;
 8     fp = fopen(filename, "r");
 9     while ((ch = fgetc(fp))!= EOF) {
10         if (ch!= '\n' && ch!= '\t' && ch!= ' ') {
11             characters++;
12         }
13         if (ch == '\n') {
14             lines++;
15         }
16     }
17     if (characters > 0) {
18         lines++;
19     }
20     fclose(fp);
21     printf("%s统计结果:\n", filename);
22     printf("行数:\t%d\n", lines);
23     printf("字符数(不计空白符):\t%d\n", characters);
24     return 0;
25 }

task5

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

task6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define SN 80
 5 #define RN 5
 6 
 7 typedef struct {
 8     char id[10];
 9     char name[20];
10     char cls[30];
11 } Stu;
12 
13 void rdStu(Stu s[]) {
14     FILE *fp;
15     int i = 0;
16     fp = fopen("list.txt", "r");
17     if (fp == NULL) {
18         printf("无法打开文件list.txt\n");
19         exit(1);
20     }
21     while (i < SN && fscanf(fp, "%s %s %s", s[i].id, s[i].name, s[i].cls) == 3) {
22         i++;
23     }
24     fclose(fp);
25 }
26 
27 void genRand(int r[], int n, int m) {
28     int i, j, tmp;
29     int used[m];
30     srand((unsigned int)time(NULL));
31     for (i = 0; i < m; i++) {
32         used[i] = 0;
33     }
34     for (i = 0; i < n; i++) {
35         tmp = rand() % m;
36         while (used[tmp]) {
37             tmp = rand() % m;
38         }
39         used[tmp] = 1;
40         r[i] = tmp;
41     }
42 }
43 
44 void dispSelStu(Stu s[], int r[]) {
45     int i;
46     printf("------------随机抽点名单------------\n");
47     for (i = 0; i < RN; i++) {
48         printf("%s\t\t%s\t%s\n", s[r[i]].id, s[r[i]].name, s[r[i]].cls);
49     }
50 }
51 
52 void wrtSelStu(Stu s[], int r[], char *fn) {
53     FILE *fp;
54     int i;
55     fp = fopen(fn, "w");
56     if (fp == NULL) {
57         printf("无法创建文件%s\n", fn);
58         exit(1);
59     }
60     fprintf(fp, "------------随机抽点名单------------\n");
61     for (i = 0; i < RN; i++) {
62         fprintf(fp, "%s\t\t%s\t%s\n", s[r[i]].id, s[r[i]].name, s[r[i]].cls);
63     }
64     fclose(fp);
65     printf("保存成功!\n");
66 }
67 
68 int main() {
69     Stu stu[SN];
70     int randInd[RN];
71     char fileName[50];
72     rdStu(stu);
73     genRand(randInd, RN, SN);
74     dispSelStu(stu, randInd);
75     printf("\n输入文件名: ");
76     scanf("%s", fileName);
77     wrtSelStu(stu, randInd, fileName);
78     return 0;
79 }

 

posted @ 2024-12-30 08:02  易香兑  阅读(4)  评论(0)    收藏  举报