实验7

试验任务4

代码:

 1 #include <stdio.h>
 2 #include <windows.h>
 3 
 4 int main()
 5 {
 6     FILE* fp;
 7     int line_cnt = 0, char_cnt = 0;
 8     char ch;
 9     int has_content = 0; 
10 
11     fp = fopen("data4.txt", "r");
12     if (fp == NULL)
13     {
14         printf("文件打开失败,请检查data4.txt位置\n");
15         system("pause");
16         return 1;
17     }
18 
19     while ((ch = fgetc(fp)) != EOF)
20     {
21         has_content = 1; 
22         if (ch == '\n')
23         {
24             line_cnt++;
25         }
26         if (ch == ' ' || ch == '\n' || ch == '\t')
27         {
28             continue;
29         }
30         char_cnt++;
31     }
32     if (has_content)
33     {
34         line_cnt++;
35     }
36 
37     fclose(fp);
38 
39     printf("data4.txt统计结果:\n");
40     printf("行数:        %d\n", line_cnt);
41     printf("字符数(不计空白符): %d\n", char_cnt);
42     system("pause");
43     return 0;
44 }

 

截图:

屏幕截图 2026-06-21 211931

 

试验任务5

代码:

  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 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     system("pause");
 42     return 0;
 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 void read(STU st[], int n) {
 54     int i;
 55     FILE *fin;
 56 
 57     fin = fopen("examinee.txt", "r");
 58     if (!fin) {
 59         printf("fail to open file\n");
 60         return;
 61     }
 62 
 63     for (i = 0; i < n; i++)
 64         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 65 
 66     fclose(fin);
 67 }
 68 
 69 int process(STU st[], int n, STU st_pass[]) {
 70     int i, pass_cnt = 0;
 71     for (i = 0; i < n; i++)
 72     {
 73         st[i].sum = st[i].objective + st[i].subjective;
 74         if (st[i].sum >= 60)
 75         {
 76             strcpy(st[i].result, "通过");
 77             st_pass[pass_cnt] = st[i];
 78             pass_cnt++;
 79         }
 80         else
 81         {
 82             strcpy(st[i].result, "不通过");
 83         }
 84     }
 85     return pass_cnt;
 86 }
 87 
 88 void write(STU st[], int n) {
 89     int i;
 90     FILE *fout;
 91     fout = fopen("list_pass.txt", "w");
 92     if (!fout)
 93     {
 94         printf("文件打开失败\n");
 95         return;
 96     }
 97     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
 98     for (i = 0; i < n; i++)
 99     {
100         fprintf(fout, "%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n",
101             st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
102     }
103     fclose(fout);
104 }

 

截图:

屏幕截图 2026-06-21 212939

 

试验任务6

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 #define STU_NUM 80  
 7 #define DRAW_NUM 5  
 8 
 9 typedef struct
10 {
11     long id;
12     char name[20];
13     char cls[30];
14 } Student;
15 
16 void readStu(Student stu[])
17 {
18     FILE* fp;
19     int i;
20     fp = fopen("list.txt", "r");
21     if (fp == NULL)
22     {
23         printf("list.txt 文件打开失败!\n");
24         system("pause");
25         exit(1);
26     }
27     i = 0;
28     while (fscanf(fp, "%ld %s %s", &stu[i].id, stu[i].name, stu[i].cls) != EOF && i < STU_NUM)
29     {
30         i++;
31     }
32     fclose(fp);
33 }
34 
35 int main()
36 {
37     Student allStu[STU_NUM];
38     Student winner[DRAW_NUM];
39     int flag[STU_NUM] = { 0 };
40     int i, count, randIdx;
41     char fileName[30];
42     FILE* fout;
43 
44     count = 0;
45     readStu(allStu);
46 
47     srand((unsigned)time(NULL));
48 
49     while (count < DRAW_NUM)
50     {
51         randIdx = rand() % STU_NUM;
52         if (flag[randIdx] == 0)
53         {
54             flag[randIdx] = 1;
55             winner[count] = allStu[randIdx];
56             count++;
57         }
58     }
59 
60     printf("----------中奖名单----------\n");
61     for (i = 0; i < DRAW_NUM; i++)
62     {
63         printf("%ld\t%s\t%s\n", winner[i].id, winner[i].name, winner[i].cls);
64     }
65     printf("----------保存到文件----------\n");
66     printf("输入文件名:");
67     scanf("%s", fileName);
68 
69     fout = fopen(fileName, "w");
70     if (fout == NULL)
71     {
72         printf("文件创建失败!\n");
73         system("pause");
74         return 1;
75     }
76     for (i = 0; i < DRAW_NUM; i++)
77     {
78         fprintf(fout, "%ld\t%s\t%s\n", winner[i].id, winner[i].name, winner[i].cls);
79     }
80     fclose(fout);
81 
82     printf("保存成功!\n");
83     system("pause");
84     return 0;
85 }

 

截图:

屏幕截图 2026-06-21 213844

 

posted @ 2026-06-21 21:40  Hhhm00  阅读(3)  评论(0)    收藏  举报