实验7

任务三

1.“\'’”是转义字符的一种,表示“ ’ ”

2.防止当i>N时,超出数组的范围

任务四

源代码:

 1 #include <stdio.h>
 2 
 3 int main() {
 4    int i;
 5     FILE* fp;
 6     char ch;
 7     int a = 1;
 8     int b = 1;
 9 
10     fp = fopen("D:/data4.txt", "r");
11     if (fp == NULL) {
12         printf("fail to open file to read\n");
13         return;
14     }
15     while ((ch = fgetc(fp)) != EOF) {
16         if (ch == '\n')
17             a++;
18         if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') 
19             b++;
20     }
21     fclose(fp);
22     printf("data4.txt统计结果:\n");
23     printf("行数:%d\n", a);
24     printf("字符数(不计空白符):%6d\n", b);
25 
26     return 0;
27 }
View Code

运行截图:

屏幕截图 2026-06-23 135954

任务五

源代码:

  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("D:/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 i, pass_cnt = 0;
 74     for (i = 0; i < n; i++) {
 75         st[i].sum = st[i].objective + st[i].subjective;
 76         if (st[i].sum >= 60) {
 77             strcpy(st[i].result, "通过");
 78             st_pass[pass_cnt] = st[i];
 79             pass_cnt++;
 80         }
 81         else 
 82             strcpy(st[i].result, "未通过");
 83        
 84     }
 85     return pass_cnt;
 86 }
 87 
 88 // 把通过考试的考生完整信息写入文件list_pass.txt
 89 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 90 void write(STU st[], int n) {
 91     int i;
 92     FILE* fp;
 93     fp = fopen("list_pass.txt", "w");
 94     if (fp==NULL) {
 95         printf("fail to open write file\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\t%.2f\t\t%.2f\t%s\n",
101             st[i].id, st[i].name, st[i].objective,
102             st[i].subjective, st[i].sum, st[i].result);
103     }
104     fclose(fp);
105 }
View Code

运行截图

屏幕截图 2026-06-23 142309

屏幕截图 2026-06-23 142507

 

 

任务六

源代码:

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

运行截图

屏幕截图 2026-06-23 143642

 

posted @ 2026-06-23 14:38  dfuty  阅读(8)  评论(0)    收藏  举报