实验七

任务4

task4.c

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 
 4 int main() {
 5     FILE* file;
 6     char filename[] = "data4.txt";
 7     char ch;
 8     int line_count = 0;
 9     int char_count = 0;
10     int is_new_line = 1;
11 
12     file = fopen(filename, "r");
13     if (file == NULL) {
14         printf("fail to open file to read\n");
15         return 1;
16     }
17 
18     while ((ch = fgetc(file)) != EOF) {
19         if (ch == '\n') {
20             line_count++;
21             is_new_line = 1;
22         }
23         else if (ch != ' ' && ch != '\t' && ch != '\n') {
24             char_count++;
25             is_new_line = 0;
26         }
27     }
28 
29     if (!is_new_line) {
30         line_count++;
31     }
32 
33     fclose(file);
34 
35     printf("%s统计结果:\n", filename);
36     printf("行数:%d\n", line_count);
37     printf("字符数(不计空白符):%d\n", char_count);
38 
39     return 0;
40 }

运行截图

 

任务5

task5.c

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <string.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     write(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].result);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void read(STU st[], int n) {
 55     int i;
 56     FILE* fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 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 
 73 // 把通过考试的考生完整信息写入文件list_pass.txt
 74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 75 void write(STU s[], int n) {
 76     FILE* file = fopen("list_pass.txt", "w");
 77     if (file == NULL) {
 78         printf("fail to open\n");
 79         return;
 80     }
 81 
 82     fprintf(file, "准考证号\t\t姓名\t\t客观题得分\t\t操作题得分\t\t总分\t\t结果\n");
 83     for (int i = 0; i < n; i++) {
 84         if (s[i].sum >= 60) 
 85             fprintf(file, "%ld\t\t%s\t\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);  
 86     }
 87     fclose(file);
 88 }
 89 
 90 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 91 int process(STU st[], int n, STU st_pass[]) {
 92     int pass_count = 0;
 93     for (int i = 0; i < n; i++) {
 94         st[i].sum = st[i].objective + st[i].subjective;
 95 
 96         if (st[i].sum >= 60) {
 97             strcpy(st[i].result, "通过");
 98             st_pass[pass_count++] = st[i];
 99         }
100         else {
101             strcpy(st[i].result, "未通过");
102         }
103     }
104     return pass_count;
105 }

运行截图

 

任务6

task6.c

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h> 
 3 #include <time.h>
 4 
 5 #define N 80
 6 #define M 5
 7 
 8 typedef struct {
 9     char id[10];
10     char name[20];
11     char class[20];
12 }STUDENT;
13 
14 int main() {
15     STUDENT st[N],lucky[M];
16     int lucky_index;
17     int i=0,number;
18     char filename[20];
19     int flag[N] = { 0 };
20 
21     FILE* fp1;
22 
23     fp1 = fopen("list.txt", "r");
24 
25     if (fp1 == NULL) {
26         printf("fail to open file to read\n");
27         return;
28     }
29 
30     while (!feof(fp1)){
31         number = fscanf(fp1, "%s%s%s", st[i].id, st[i].name, st[i].class);
32         if (number != 3)
33             break;
34         i++;
35     }
36     fclose(fp1);
37 
38     printf("-------中奖名单-------\n");
39 
40     srand(time(NULL));
41     for (i = 0; i < M; i++) {
42         lucky_index = rand() % N;
43 
44         if (flag[lucky_index] = 1) {
45             lucky[i] = st[lucky_index];
46             printf("%-10s%-20s%-20s\n", st[lucky_index].id, st[lucky_index].name, st[lucky_index].class);
47             flag[lucky_index] = 0;
48         }
49 
50         else
51             i--;
52     }
53 
54     printf("-------保存到文件-------\n");
55     printf("输入文件名:");
56     scanf("%s", filename);
57 
58     FILE* fp2;
59 
60     fp2 = fopen(filename, "wb");
61 
62     if (fp2 == NULL) {
63         printf("fail to open file to write\n");
64         return;
65     }
66 
67     for (i = 0; i < M; i++) 
68         fprintf(fp2,"%-10s%-20s%-20s\n",lucky[i].id,lucky[i].name,lucky[i].class);
69 
70     fclose(fp2);
71 
72     printf("保存成功\n");
73 
74     return 0;
75 }

 

运行截图

 

posted @ 2025-06-05 14:43  tingying  阅读(21)  评论(0)    收藏  举报