实验七

源代码4:

 1 #include <stdio.h>
 2 int main() {
 3     FILE* fp;
 4     fp = fopen("F:\\data4.txt", "r");
 5     if (fp == NULL) {
 6         return -1;
 7     }
 8     char t;
 9     int num_line = 0, num_ch = 0;
10     while (feof(fp) == 0) {
11         t = fgetc(fp);
12         if (t == EOF) {
13             num_line++;
14             break;
15         }
16         else if (t!=' '&& t!= '\n' && t!= '\t') {
17             num_ch++;
18         }
19         else if (t == '\n') {
20             num_line++;
21         }
22     }
23     printf("data4.txt统计结果\n行数:%d\n字数:%d", num_line, num_ch);
24 }

 

截图4:

 

源代码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("F:\\examinee.txt", "r");
 58     if (!fin) {
 59         printf("fail to open file\n");
 60         return;
 61     }
 62 
 63     while (!feof(fin)) {
 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 
 68     fclose(fin);
 69 }
 70 
 71 // 把通过考试的考生完整信息写入文件list_pass.txt
 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 73 void write(STU s[], int n) {
 74     FILE* fp;
 75     fp = fopen("list_pass.txt", "w");
 76     if (fp == NULL) {
 77         printf("文件打开失败\n");
 78         return ;
 79     }
 80     int i;
 81     for (i = 0;i < n;i++) {
 82         if (s[i].sum >= 60) {
 83             fprintf(fp, "%ld %s %f %f %f %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 84         }
 85     }
 86     fclose(fp);
 87 }
 88     
 89 
 90 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 91 int process(STU st[], int n, STU st_pass[]) {
 92     int i;
 93     int count = 0;
 94     for (i = 0;i < n;i++) {
 95         st[i].sum = st[i].objective + st[i].subjective;
 96         if (st[i].sum >= 60) {
 97             strcpy(st[i].result, "通过");
 98             st_pass[count] = st[i];
 99             count++;
100         }
101         else {
102             strcpy(st[i].result, "未通过");
103         }
104     }
105     return count;
106 }
107 
108     

截图5:

 

源代码6:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 #define N 100
 7 
 8 typedef struct {
 9     int id;
10     char name[N];
11     char ban[N];
12 } stu;
13 
14 void read(stu s[], int *n);
15 void write(stu s[], int n);
16 void extract(stu s[], int n, stu infor[], int totalCount);
17 void output(stu s[], int n);
18 
19 int main() {
20     stu s[N], infor[N];
21     int totalCount = 0;
22     int selectedCount = 5;
23 
24     read(s, &totalCount);
25     if (totalCount > 0) {
26         extract(s, selectedCount, infor, totalCount);
27         printf("----------随机抽点名单----------\n");
28         output(infor, selectedCount);
29         printf("\n");
30         printf("----------保存到文件----------\n");
31         write(infor, selectedCount);
32     }
33     return 0;
34 }
35 
36 void read(stu s[], int *n) {
37     int i = 0;
38     FILE *fp = fopen("F:\\list.txt", "r");
39     if (fp == NULL) {
40         printf("无法打开文件\n");
41         return;
42     }
43     while (fscanf(fp, "%d %s %s", &s[i].id, s[i].name, s[i].ban) == 3) {
44         i++;
45         if (i >= *n) {
46             break;
47         }
48     }
49     *n = i;
50     fclose(fp);
51 }
52 
53 void output(stu s[], int n) {
54     int i;
55     for (i = 0; i < n; i++) {
56         printf("%-20d%-10s%20s\n", s[i].id, s[i].name, s[i].ban);
57     }
58 }
59 
60 void extract(stu s[], int n, stu infor[], int totalCount) {
61     int i, j;
62     int used[N] = {0};
63     srand((unsigned)time(NULL));
64 
65     for (i = 0; i < n; i++) {
66         do {
67             j = rand() % totalCount;
68         } while (used[j]);
69         infor[i] = s[j];
70         used[j] = 1;
71     }
72 }
73 
74 void write(stu infor[], int n) {
75     char x[256];
76     int i;
77     printf("输入文件名:");
78     if (scanf("%255s", x)!= 1) {
79         printf("输入错误\n");
80         return;
81     }
82 
83     FILE *fp = fopen(x, "w");
84     if (fp == NULL) {
85         perror("无法打开文件");
86         return;
87     }
88 
89     for (i = 0; i < n; i++) {
90         if (fprintf(fp, "%-20d%-10s%-20s\n", infor[i].id, infor[i].name, infor[i].ban) < 0) {
91             perror("写入文件失败");
92             fclose(fp);
93             return;
94         }
95     }
96     printf("保存成功!");
97     fclose(fp);
98 }

截图6:

 

posted @ 2024-12-30 08:02  杨启霖  阅读(4)  评论(0)    收藏  举报