实验7

task4

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

image

 

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, 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 void output(STU st[], int n) {
46     int i;
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",
50                st[i].id, st[i].name, st[i].objective, st[i].subjective,
51                st[i].sum, st[i].result);
52 }
53 
54 // 从文本文件examinee.txt读入考生信息
55 void read(STU st[], int n) {
56     int i;
57     FILE *fin = fopen("examinee.txt", "r");
58     if (!fin) {
59         printf("fail to open file\n");
60         return;
61     }
62     for (i = 0; i < n; i++)
63         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name,
64                &st[i].objective, &st[i].subjective);
65     fclose(fin);
66 }
67 
68 // 对考生信息进行处理:计算总分、结果,返回通过人数
69 int process(STU st[], int n, STU st_pass[]) {
70     int cnt = 0;
71     int i;
72     for (i = 0; i < n; i++) {
73         st[i].sum = st[i].objective + st[i].subjective;
74         if (st[i].sum >= 60) {
75             strcpy(st[i].result, "通过");
76             st_pass[cnt++] = st[i];
77         } else {
78             strcpy(st[i].result, "未通过");
79         }
80     }
81     return cnt;
82 }
83 
84 // 将通过考试的考生信息写入文件list_pass.txt
85 void write(STU st[], int n) {
86     FILE *fout = fopen("list_pass.txt", "w");
87     if (!fout) {
88         printf("无法创建文件 list_pass.txt\n");
89         return;
90     }
91     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
92     int i;
93     for (i = 0; i < n; i++) {
94         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
95                 st[i].id, st[i].name, st[i].objective, st[i].subjective,
96                 st[i].sum, st[i].result);
97     }
98     fclose(fout);
99 }

image

image

 

task6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <time.h>
 5 
 6 #define MAX_STUDENTS 100
 7 
 8 typedef struct {
 9     char id[20];
10     char name[20];
11     char class_name[50];
12 } Student;
13 
14 int main() {
15     FILE *fp;
16     Student students[MAX_STUDENTS];
17     int count = 0;
18     char filename[100];
19     int i, j, chosen[5], rand_idx;
20 
21     // 1. 打开并读取 list.txt
22     fp = fopen("list.txt", "r");
23     if (fp == NULL) {
24         printf("无法打开文件 list.txt\n");
25         return 1;
26     }
27     while (fscanf(fp, "%s %s %s", students[count].id, students[count].name, students[count].class_name) == 3) {
28         count++;
29         if (count >= MAX_STUDENTS) break;
30     }
31     fclose(fp);
32 
33     if (count < 5) {
34         printf("学生人数不足5人,无法抽奖\n");
35         return 1;
36     }
37 
38     // 2. 随机抽取5个不重复的索引
39     srand((unsigned)time(NULL));
40     for (i = 0; i < 5; i++) {
41         do {
42             rand_idx = rand() % count;
43             // 检查是否已经选过
44             int duplicate = 0;
45             for (j = 0; j < i; j++) {
46                 if (chosen[j] == rand_idx) {
47                     duplicate = 1;
48                     break;
49                 }
50             }
51             if (!duplicate) {
52                 chosen[i] = rand_idx;
53                 break;
54             }
55         } while (1);
56     }
57 
58     // 3. 输出到屏幕
59     printf("---中奖名单---\n");
60     for (i = 0; i < 5; i++) {
61         printf("%s\t%s\t%s\n", students[chosen[i]].id, students[chosen[i]].name, students[chosen[i]].class_name);
62     }
63 
64     // 4. 输入文件名并保存到文件
65     printf("\n---保存到文件---\n");
66     printf("输入文件名:");
67     scanf("%s", filename);
68 
69     fp = fopen(filename, "w");
70     if (fp == NULL) {
71         printf("无法创建文件 %s\n", filename);
72         return 1;
73     }
74     fprintf(fp, "---中奖名单---\n");
75     for (i = 0; i < 5; i++) {
76         fprintf(fp, "%s\t%s\t%s\n", students[chosen[i]].id, students[chosen[i]].name, students[chosen[i]].class_name);
77     }
78     fclose(fp);
79     printf("保存成功!\n");
80 
81     return 0;
82 }

image

image

 

posted @ 2026-06-22 21:27  花椰菜不吃西兰花  阅读(10)  评论(0)    收藏  举报