实验7

任务四

源代码:

 1 #include <stdio.h>
 2 
 3 int main() {
 4     FILE* fp;
 5     int ch;
 6     int line_count = 0;
 7     int char_count = 0;
 8     int has_content = 0;
 9 
10     fp = fopen("data4.txt", "r");
11     if (fp == NULL) {
12         printf("无法打开文件 data4.txt\n");
13         return 1;
14     }
15 
16     while ((ch = fgetc(fp)) != EOF) {
17         has_content = 1;
18 
19         if (ch == '\n') {
20             line_count++;
21         }
22 
23         if (ch != ' ' && ch != '\n' && ch != '\t') {
24             char_count++;
25         }
26     }
27 
28     if (has_content) {
29         line_count++;
30     }
31 
32     fclose(fp);
33 
34     printf("data4.txt统计结果:\n");
35     printf("行数:\t\t%d\n", line_count);
36     printf("字符数(不计空白符):\t%d\n", char_count);
37 
38     return 0;
39 }
View Code

 

运行结果截图:

8b973c228ed64dd2c7f1e5d595c7aa1e

 

任务五

源代码:

  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, N);
 34 
 35     printf("\n通过考试的名单写入文件: 已完成!\n");
 36     write(stu_pass, cnt);
 37 
 38     pass_rate = 1.0 * cnt / N;
 39     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
 40 
 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].result);
 52 }
 53 
 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 55 void read(STU st[], int n) {
 56     int i;
 57     FILE* fin;
 58 
 59     fin = fopen("examinee.txt", "r");
 60     if (!fin) {
 61         printf("fail to open file\n");
 62         return;
 63     }
 64 
 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     fclose(fin);
 69 }
 70 
 71 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
 72 int process(STU st[], int n, STU st_pass[]) {
 73     int count = 0;
 74     int i;
 75     for (i = 0; i < n; i++) {
 76         st[i].sum = st[i].objective + st[i].subjective;
 77         if (st[i].sum >= 60) {
 78             strcpy(st[i].result, "通过");
 79             st_pass[count] = st[i];
 80             count++;
 81         }
 82         else {
 83             strcpy(st[i].result, "未通过");
 84         }
 85     }
 86     return count;
 87 }
 88 
 89 // 把通过考试的考生完整信息写入文件list_pass.txt
 90 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 91 void write(STU st[], int n) {
 92     FILE* fp = fopen("fileList_pass.txt", "w");
 93     int i;
 94     if (fp == NULL) {
 95         printf("无法创建或写入文件 fileList_pass.txt\n");
 96         return;
 97     }
 98     fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
 99     for (i = 0; i < n; i++) {
100         fprintf(fp, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",
101             st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
102     }
103     fclose(fp);
104 }
View Code

 

运行结果截图:

bb0529e9281f45defe681867d981a596

 

任务六

源代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 typedef struct {
 7     char id[20];
 8     char name[20];
 9     char cls[50];
10 } STU;
11 
12 int main() {
13     FILE *fp;
14     STU stu[80];
15     int count = 0, i, r;
16     char filename[50];
17     STU temp;
18 
19     srand((unsigned)time(NULL));
20 
21     fp = fopen("list.txt", "r");
22     if (fp == NULL) {
23         printf("无法打开文件 list.txt\n");
24         return 1;
25     }
26 
27     while (fscanf(fp, "%s %s %s", stu[count].id, stu[count].name, stu[count].cls) == 3) {
28         count++;
29     }
30     fclose(fp);
31 
32     for (i = 0; i < 5; i++) {
33         r = i + rand() % (count - i);
34         temp = stu[i];
35         stu[i] = stu[r];
36         stu[r] = temp;
37     }
38 
39     printf("----------------------------------------\n");
40     printf("             中奖名单\n");
41     printf("----------------------------------------\n");
42     for (i = 0; i < 5; i++) {
43         printf("%s\t%s\t%s\n", stu[i].id, stu[i].name, stu[i].cls);
44     }
45     printf("----------------------------------------\n");
46     printf("保存到文件\n");
47     printf("输入文件名:");
48     scanf("%s", filename);
49 
50     fp = fopen(filename, "w");
51     if (fp == NULL) {
52         printf("文件打开失败!\n");
53         return 1;
54     }
55     for (i = 0; i < 5; i++) {
56         fprintf(fp, "%s\t%s\t%s\n", stu[i].id, stu[i].name, stu[i].cls);
57     }
58     fclose(fp);
59 
60     printf("保存成功!\n");
61 
62     return 0;
63 }
View Code

 

运行结果截图:

4efee08992c8a793767151dab9ecdce4

 

2fa3d780bec3894d6f8b25671e72f9cd

 

posted @ 2026-06-23 14:51  月色群青  阅读(2)  评论(0)    收藏  举报