实验7

任务4

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     FILE *fp;
 7     char ch;
 8     int line = 0;
 9     int validChar = 0;
10     int isNewLine = 1;
11     
12     fp = fopen("C:\\Users\\daisy\\Desktop\\实验7\\data4.txt", "r");
13     
14     if (fp == NULL) {
15         printf("fail to open file to read\n");
16         system("pause");
17         return 1;
18     }
19 
20     while ((ch = fgetc(fp)) != EOF)
21     {
22         if (ch == '\n')
23         {
24             line++;
25             isNewLine = 1;
26             continue;
27         }
28         if (ch == ' ' || ch == '\t')
29         {
30             continue;
31         }
32         validChar++;
33         isNewLine = 0;
34     }
35     if(isNewLine == 0)
36     {
37         line++;
38     }
39 
40     fclose(fp);
41 
42     printf("data4.txt统计结果: \n");
43     printf("行数: %d\n", line);
44     printf("字符数(不计空白符): %d\n", validChar);
45 
46     system("pause");
47     return 0;
48 }

运行结果截图

屏幕截图 2026-06-23 163743

任务·5

源代码

  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("C:\\Users\\daisy\\Desktop\\实验7\\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, count = 0;
 74     for (i = 0; i < n; i++)
 75     {
 76         st[i].sum = st[i].objective + st[i].subjective;
 77 
 78         if (st[i].sum >= 60)
 79         {
 80             strcpy(st[i].result, "通过");
 81             st_pass[count] = st[i];
 82             count++;
 83         }else{
 84             strcpy(st[i].result, "不通过");
 85         }
 86     }
 87     return count;
 88 }
 89 
 90 // 把通过考试的考生完整信息写入文件list_pass.txt
 91 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 92 void write(STU st[], int n) {
 93     int i;
 94     FILE *fout;
 95     fout = fopen("list_pass.txt", "w");
 96     if(!fout)
 97     {
 98         printf("无法创建输出文件\n");
 99         return;
100     }
101     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
102     for(i = 0; i < n; i++)
103     {
104         fprintf(fout, " %ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 
105     }
106     fclose(fout);
107 }

运行结果

屏幕截图 2026-06-23 170543

屏幕截图 2026-06-23 170944

任务6

源代码

 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("C:\\Users\\daisy\\Desktop\\实验7\\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 }

运行结果

 

 

屏幕截图 2026-06-23 193040

 

posted @ 2026-06-23 19:32  oππ  阅读(10)  评论(0)    收藏  举报