实验七

实验四

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

屏幕截图 2026-06-22 225403

实验五

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

屏幕截图 2026-06-22 225502

实验六

 1 #define _CRT_SECURE_NO_WARNINGS 
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #include <string.h>
 6 
 7 #define MAX_STU 80
 8 #define WIN_NUM 5
 9 
10 // 学生结构体
11 typedef struct {
12     long id;
13     char name[20];
14     char cls[30];
15 } Student;
16 
17 int main()
18 {
19     Student stu[MAX_STU];
20     int total = 0;
21     int flag[MAX_STU] = { 0 }; // 标记是否已抽取
22     Student win[WIN_NUM];
23     char filename[50];
24 
25     // 1.读取list.txt全部学生
26     FILE* fp = fopen("list.txt", "r");
27     if (fp == NULL)
28     {
29         printf("无法打开list.txt\n");
30         return 1;
31     }
32     while (fscanf(fp, "%ld %s %[^\n]", &stu[total].id, stu[total].name, stu[total].cls) == 3)
33     {
34         total++;
35     }
36     fclose(fp);
37 
38     // 2.随机抽取不重复5人
39     srand((unsigned)time(NULL));
40     int cnt = 0;
41     while (cnt < WIN_NUM)
42     {
43         int r = rand() % total;
44         if (flag[r] == 0)
45         {
46             flag[r] = 1;
47             win[cnt++] = stu[r];
48         }
49     }
50 
51     // 3.屏幕输出中奖名单
52     printf("=====中奖名单=====\n");
53     for (int i = 0; i < WIN_NUM; i++)
54     {
55         printf("%ld %s %s\n", win[i].id, win[i].name, win[i].cls);
56     }
57 
58     // 4.输入文件名,写入文件
59     printf("输入保存文件名:");
60     scanf("%s", filename);
61     FILE* fw = fopen(filename, "w");
62     if (fw == NULL)
63     {
64         printf("文件创建失败\n");
65         return 1;
66     }
67     for (int i = 0; i < WIN_NUM; i++)
68     {
69         fprintf(fw, "%ld %s %s\n", win[i].id, win[i].name, win[i].cls);
70     }
71     fclose(fw);
72     printf("保存成功!\n");
73 
74     system("pause");
75     return 0;
76 }
View Code

屏幕截图 2026-06-22 225552

 

posted @ 2026-06-22 22:57  阿狸波澜  阅读(14)  评论(0)    收藏  举报