实验七

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

屏幕截图 2025-12-27 211224

 

实验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     write(stu, N);    // 输出考试通过的考生信息到文件
 35 
 36     pass_rate = 1.0 * cnt / N;
 37     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 38 
 39     return 0;
 40 }
 41 
 42 // 把所有考生完整信息输出到屏幕上
 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 44 void output(STU st[], int n) {
 45     int i;
 46     
 47     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 48     for (i = 0; i < n; i++)
 49         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);
 50 }
 51 
 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 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 // 把通过考试的考生完整信息写入文件list_pass.txt
 70 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 71 void write(STU st[], int n) {
 72     int i;
 73     FILE *fout;

75 fout = fopen("list_pass.txt", "w");
 76     if (!fout) {
 77         printf("fail to open write file\n");
 78         return;
 79     }
 81     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
 82
 83     for (i = 0; i < n; i++) {
 84         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n",
 85                 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
 86     }
 87     fclose(fout); 
 88 }
 89 
 90 int process(STU st[], int n, STU st_pass[]) {
 91     int i, pass_num = 0; 
 92     for (i = 0; i < n; i++) {
 93         
 94         st[i].sum = st[i].objective + st[i].subjective;
 95         
 96         if (st[i].sum >= 60) {
 97             strcpy(st[i].result, "通过"); 
 98             st_pass[pass_num] = st[i];    
 99             pass_num++;                   
100         } else {
101             strcpy(st[i].result, "未通过"); 
102         }
103     }
104     return pass_num; 
105 }

屏幕截图 2025-12-27 212535

屏幕截图 2025-12-27 212734

 

实验6

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

 

posted @ 2025-12-27 21:34  cchheenn  阅读(6)  评论(0)    收藏  举报