实验七

任务4

 1 #include <stdio.h>
 2 int main() {
 3     FILE *fp;
 4     char ch;
 5     int lines = 0, chars = 0;
 6     fp = fopen("C:\\Users\\admin\\Desktop\\data4.txt", "r");
 7     if (fp == NULL) {
 8         printf("fail to open file to read\n");
 9         return 1;
10     }
11     while ((ch = fgetc(fp)) != EOF) {
12         if (ch == '\n') {
13             lines++; 
14         } else if (ch != ' ' && ch != '\t' && ch != '\n') {
15             chars++;
16         }
17     }
18     lines++;
19     fclose(fp);
20     printf("data4.txt统计结果:\n");
21     printf("行数:%d\n", lines);
22     printf("字符数(不统计空白符):%d\n", chars);
23     return 0;
24 }
View Code

4

任务5

  1 #include <stdio.h>
  2 #include <string.h>
  3 #define N 10
  4 
  5 typedef struct {
  6     long id;            // 准考证号
  7     char name[20];      // 姓名
  8     float objective;    // 客观题得分
  9     float subjective;   // 操作题得分
 10     float sum;          // 总分
 11     char result[10];    // 考试结果
 12 } STU;
 13 
 14 // 函数声明
 15 void read(STU st[], int n);
 16 void write(STU st[], int n);
 17 void output(STU st[], int n);
 18 int process(STU st[], int n, STU st_pass[]);
 19 
 20 int main() {
 21     STU stu[N], stu_pass[N];
 22     int cnt;
 23     double pass_rate;
 24 
 25     printf("从文件读入%d个考生信息: 已完成\n", N);
 26     read(stu, N);
 27 
 28     printf("\n对考生成绩进行统计: 已完成\n");
 29     cnt = process(stu, N, stu_pass);
 30 
 31     printf("\n所有考生完整信息:\n");
 32     output(stu, N);   
 33 
 34     printf("\n通过考试的名单写入文件: 已完成!\n");
 35     write(stu_pass, cnt);
 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("C:\\Users\\admin\\Desktop\\examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 62     }
 63 
 64     for (i = 0; i < n; i++)
 65         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 66 
 67     fclose(fin);
 68 }
 69 
 70 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
 71 int process(STU st[], int n, STU st_pass[]) {
 72     // 待补足
 73     // xxx
 74     int i, count = 0;
 75     
 76     for (i = 0; i < n; i++) {
 77         st[i].sum = st[i].objective + st[i].subjective;
 78         if (st[i].sum >= 60) {
 79             strcpy(st[i].result, "通过");
 80             st_pass[count] = st[i];
 81             count++;
 82         } else {
 83             strcpy(st[i].result, "不通过");
 84         }
 85     } 
 86     return count;
 87 }
 88 
 89 // 把通过考试的考生完整信息写入文件list_pass.txt
 90 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 91 void write(STU st[], int n) {
 92     // 待补足
 93     // xxx
 94     int i;
 95     FILE *fout;
 96     
 97     fout = fopen("list_pass.txt", "w");
 98     if (!fout) {
 99         printf("fail to open file to writing\n");
100         return;
101     }
102     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
103     for (i = 0; i < n; i++) {
104         fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", 
105                 st[i].id, st[i].name, st[i].objective, 
106                 st[i].subjective, st[i].sum, st[i].result);
107     }
108     
109     fclose(fout);
110 }
View Code

5

任务6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <time.h>
 5 
 6 #define MAX_STUDENTS 80
 7 #define WINNERS 5
 8 #define MAX_LINE 256
 9 
10 typedef struct {
11     char id[20];
12     char name[50];
13     char classs[50];
14 } Student;
15 
16 int main() {
17     FILE *fp;
18     Student students[MAX_STUDENTS];
19     int count = 0;
20     int i, j;
21     int winner_indices[WINNERS];
22     int duplicate;
23     char filename[100];
24     fp = fopen("C:\\Users\\admin\\Desktop\\list.txt", "r");
25     if (fp == NULL) {
26         printf("错误:无法打开文件 list.txt\n");
27         return 1;
28     }
29     while (fscanf(fp, "%s %s %[^\n]", 
30                   students[count].id, 
31                   students[count].name, 
32                   students[count].classs) == 3) {
33         count++;
34         if (count >= MAX_STUDENTS) break;
35     }
36     fclose(fp);
37 
38     if (count < WINNERS) {
39         printf("错误:学生人数不足 %d 人\n", WINNERS);
40         return 1;
41     }
42     srand((unsigned)time(NULL));
43 
44     for (i = 0; i < WINNERS; i++) {
45         do {
46             winner_indices[i] = rand() % count;
47             duplicate = 0;
48             for (j = 0; j < i; j++) {
49                 if (winner_indices[j] == winner_indices[i]) {
50                     duplicate = 1;
51                     break;
52                 }
53             }
54         } while (duplicate);
55     }
56     printf("\n---中奖名单---\n");
57     for (i = 0; i < WINNERS; i++) {
58         int idx = winner_indices[i];
59         printf("%-12s%-12s%s\n", 
60                students[idx].id, 
61                students[idx].name, 
62                students[idx].classs);
63     }
64     printf("\n---保存到文件---\n");
65     printf("输入文件名:");
66     scanf("%s", filename);
67 
68     fp = fopen(filename, "w");
69     if (fp == NULL) {
70         printf("错误:无法创建文件 %s\n", filename);
71         return 1;
72     }
73 
74     fprintf(fp, "---中奖名单---\n");
75     for (i = 0; i < WINNERS; i++) {
76         int idx = winner_indices[i];
77         fprintf(fp, "%-12s%-12s%s\n", 
78                 students[idx].id, 
79                 students[idx].name, 
80                 students[idx].classs);
81     }
82     fclose(fp);
83 
84     printf("保存成功!\n");
85     system("pause");
86     return 0;
87 }
View Code

6

 

posted @ 2026-06-23 20:29  awd01  阅读(5)  评论(0)    收藏  举报