实验7

四、实验结论

 

4.实验任务4

task4.c源码及运行结果截图:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include <stdio.h>
 4 #include <ctype.h>
 5 
 6 void count_file_stats(const char* filename) {
 7     FILE* file = fopen(filename, "r");
 8     if (file == NULL) {
 9         printf("无法打开文件 %s\n", filename);
10         return;
11     }
12     int line_count = 0;
13     int char_count = 0;
14     int c;
15     while ((c = fgetc(file)) != EOF) {
16         if (c == '\n')
17             line_count++;
18         if (!isspace(c))
19             char_count++;
20     }
21     if (char_count > 0)
22         line_count++;
23     fclose(file);
24     printf("%s统计结果:\n", filename);
25     printf("行数:    %d\n", line_count);
26     printf("字符数(不计空白符):    %d\n", char_count);
27 }
28 int main() {
29     count_file_stats("data4.txt");
30     return 0;
31 }
task4

 

 

 

 

 

5.实验任务5

task5.c源码及运行结果截图:

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.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     write(stu, N);    // 输出考试通过的考生信息到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 39 
 40     return 0;
 41 }
 42 
 43 // 把所有考生完整信息输出到屏幕上
 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 45 void output(STU st[], int n) {
 46     int i;
 47     
 48     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 49     for (i = 0; i < n; i++)
 50         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);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void read(STU st[], int n) {
 55     int i;
 56     FILE *fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 62     }
 63 
 64     while (!feof(fin)) {
 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 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void write(STU s[], int n) {
 75     // 待补足
 76     // xxx
 77     FILE* fp;
 78     fp = fopen("list_pass.txt", "w");
 79     if (!fp) {
 80         printf("fail to open file\n");
 81         return;
 82     }
 83     for (int i = 0; i < n; ++i) {
 84         if (strcmp(s[i].result, "pass") == 0) {
 85             fprintf(fp, "%101d%-20s%-10f%-10f%-10f%-10s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum
 86                 , s[i].result);
 87         }
 88     }
 89     fclose(fp);
 90 }
 91 
 92 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 93 int process(STU st[], int n, STU st_pass[]) {
 94     // 待补足
 95     // xxx
 96     for (int i = 0; i < n; ++i) {
 97         st[i].sum = st[i].objective + st[i].subjective;
 98         if (st[i].sum > 60) {
 99             strcpy(st[i].result, "pass");
100         }
101         else {
102             strcpy(st[i].result, "no pass");
103         }
104     }
105     int j = 0;
106     for (int i = 0; i < n; i++) {
107 
108         if (strcmp(st[i].result, "pass") == 0) {
109             st_pass[j] = st[i];
110             j++;
111         }
112 
113     }
114     return j;
115 }
task5

 

 

 

 

 

6.实验任务6

task6源码及运行结果截图:

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<time.h>
 6 
 7 #define N 5//抽选人数
 8 #define M 80//班级总人数
 9 #define MAX 100
10 
11 typedef struct
12 {
13     long id;//学号
14     char name[20];//姓名
15     char classname[40]; //班级
16 } STU;
17 void random(STU temp[], STU a[], int n, int m);//从总共m个人的temp[]中随机选取n个人并存入a[]中
18 void read(FILE* fp, STU temp[], int n);//从fp所指的文件里读取n名学生信息并保存到temp[]中
19 void output(STU a[], int n);//向屏幕上输出中奖名单
20 void write(STU a[], int n, char filename[]);//将名单保存至文件中
21 
22 int main() {
23     STU a[N], temp[M];
24     FILE* fp;
25 
26     fp = fopen("list.txt", "r");
27     if (!fp) {
28         printf("Fail to open\n");
29         return;
30     }
31     read(fp, temp, M);
32     fclose(fp);
33     random(temp, a, N, M);
34     output(a, N);
35     printf("------------保存到文件------------\n");
36     char filename[20];
37     printf("输入文件名:");
38     scanf("%s", filename);
39     write(a, N, filename);
40 
41     fp = fopen(filename, "r");
42     if (!fp)
43         printf("保存失败!\n");
44     else
45         printf("保存成功!\n");
46     fclose(fp);
47 
48     return 0;
49 }
50 void random(STU temp[], STU a[], int n, int m) {
51     int i, num;
52     int flag[MAX] = { 0 };
53     srand((unsigned)time(NULL));
54     for (i = 0; i < n;) {
55         num = rand() % m;
56         if (!flag[num]) {
57             a[i] = temp[num];
58             flag[num] = 1;
59             i++;
60         }
61 
62     }
63 }
64 void read(FILE* fp, STU temp[], int n) {
65     int i;
66     while (!feof(fp)) {
67         for (i = 0; i < n; ++i)
68             fscanf(fp, "%ld %s %s", &temp[i].id, temp[i].name, temp[i].classname);
69     }
70 }
71 void output(STU a[], int n) {
72     int i;
73     printf("------------中奖名单------------\n");
74     for (i = 0; i < n; ++i) {
75         printf("%-20d%-10s%s\n", a[i].id, a[i].name, a[i].classname);
76     }
77 }
78 void write(STU a[], int n, char filename[]) {
79     FILE* fp;
80     fp = fopen(filename, "w");
81     int i;
82     for (i = 0; i < n; ++i) {
83         fprintf(fp, "%-20d%-15s%s\n", a[i].id, a[i].name, a[i].classname);
84     }
85     fclose(fp);
86 }
task6

 

posted @ 2025-06-09 10:26  DelPaulo  阅读(8)  评论(0)    收藏  举报