实验七

任务三

 问题回答:

1.\是转义单引号,编译器不会保存单引号本身;

2.作用是限制最多读取N行,防止字符组溢出。

 

任务四

源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     FILE *fp;
 6     int line_num=0;
 7     int char_num=0;
 8     char x;
 9 
10     fp=fopen("C:\\Users\\34691\\Desktop\\C语言\\博客园\\实验七\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\data4.txt","r");
11     if(fp==NULL)
12     {
13         printf("fail to open\n");
14 
15     }
16     while((x=fgetc(fp))!=EOF)
17     {
18         if(x=='\n')
19         {
20             line_num++;
21         }
22 
23         else if(x==' '||x=='\t')
24         {
25             continue;
26         }
27 
28         else
29         {
30             char_num++;
31         }
32     }
33         fclose(fp);
34         if(char_num!=0)
35         {
36             line_num++;
37         }
38 
39 
40         printf("行数: %d\n",line_num);
41         printf("字符数: %d\n",char_num);
42 
43         system("pause");
44         return 0;
45     
46 }

 

文件地址为我电脑上的。

 

测试结果截图

屏幕截图 2026-06-23 192339

 

 

任务五

源代码

 

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

 

测试结果截图

屏幕截图 2026-06-23 184943

 

 

任务六

源代码

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 80
 6 #define M 5
 7 
 8 
 9 struct Student
10 {
11     long id;
12     char name[20];
13     char banji[30];
14 };
15 
16 int main()
17 {
18   
19     struct Student stu[N];
20     struct Student jiang[M];
21     int biao[N];  
22     int suiji;
23     int count;
24     int i;
25     int n;
26     FILE *fp;
27     char wenjian[30];
28 
29     
30     fp = fopen("C:\\Users\\34691\\Desktop\\C语言\\博客园\\实验七\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\list.txt", "r");
31     if (fp == NULL)
32     {
33         printf("打不开list.txt!\n");
34         return 0;
35     }
36     n = 0;
37     while (fscanf(fp, "%ld %s %s", &stu[n].id, stu[n].name, stu[n].banji) == 3)
38     {
39         n = n + 1;
40     }
41     fclose(fp);
42 
43     
44     for (i = 0; i < N; i = i + 1)
45     {
46         biao[i] = 0;
47     }
48     srand((unsigned)time(NULL));
49     count = 0;
50     while (count < M)
51     {
52         suiji = rand() % n;
53         if (biao[suiji] == 0)
54         {
55             biao[suiji] = 1;
56             jiang[count] = stu[suiji];
57             count = count + 1;
58         }
59     }
60 
61  
62     printf("输入文件名:");
63     scanf("%s", wenjian);
64 
65     
66     printf("----------------中奖名单----------------\n");
67     for (i = 0; i < M; i = i + 1)
68     {
69         printf("%ld %s %s\n", jiang[i].id, jiang[i].name, jiang[i].banji);
70     }
71     printf("-----------------------------------------\n");
72 
73     
74     fp = fopen(wenjian, "w");
75     if (fp == NULL)
76     {
77         printf("fail to couduct\n");
78         return 0;
79     }
80     fprintf(fp, "----------------中奖名单----------------\n");
81     for (i = 0; i < M; i = i + 1)
82     {
83         fprintf(fp, "%ld %s %s\n", jiang[i].id, jiang[i].name, jiang[i].banji);
84     }
85     fclose(fp);
86 
87     printf("保存成功!%s\n", wenjian);
88 
89     return 0;
90 }

 

屏幕截图 2026-06-23 205611

 

 屏幕截图 2026-06-23 213428

 

测试结果截图如上

posted @ 2026-06-23 21:32  3469172728  阅读(4)  评论(0)    收藏  举报