实验七

实验4:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #define N 500
 4 int main() {
 5     FILE* fp;
 6     char ch;
 7     int s=0, m=0;
 8     fp = fopen("C:\\Users\\冯韬\\Desktop\\实验7数据文件及部分代码_gbk\\data4.txt","r");
 9     if (!fp)
10         perror("data4.txt");
11     while ((ch = fgetc(fp)) != EOF) {
12         if (ch == '\n')
13             s++;
14         if (ch != ' ' && ch != '\n' && ch != '\t')
15             m++;
16     }
17     s++;
18     printf("data4.txt统计结果:\n");
19     printf("行数:%-20d", s);
20     printf("字符数(不计空白符):%-20d", m);
21     fclose(fp);
22     return 0;
23 }
View Code

4

实验6:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<time.h>
 5 #define N 90
 6 #define M 5
 7 int main() {
 8     FILE* fp;
 9     FILE* fout;
10     char filename[80];
11     int i, n;
12     int lucky_i;
13     int has_hit[N] = { 0 };
14     char s[N][N];
15     fp = fopen("C:\\Users\\冯韬\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt", "r");
16     if (!fp) {
17         perror("list.txt");
18         return 1;
19     }
20     i = 0;
21     while (fgets(s[i], N, fp) != NULL)
22         i++;
23     n = i;
24     srand(time(0));
25     for (i = 0; i < M; ) {
26         lucky_i = rand() % n;
27         if (has_hit[lucky_i])
28             continue;
29         has_hit[lucky_i] = 1;
30         printf("%s", s[lucky_i]);
31         ++i;
32         
33     }
34     printf("Enter filename:");
35     gets(filename);
36     fout = fopen(filename, "w");
37     if (!fout) {
38         perror(filename);
39         return 1;
40     }
41     else
42         printf("保存成功!");
43     fclose(fp);
44     fclose(fout);
45     return 0;
46 }
View Code

5

实验5:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #define N 10
 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 void read(STU st[], int n);
14 void write(STU st[], int n);
15 void output(STU st[], int n);
16 int process(STU st[], int n, STU st_pass[]);
17 int main() {
18     STU stu[N], stu_pass[N];
19     int cnt;
20     double pass_rate;
21     printf("从文件读入%d个考生信息...\n",N);
22     read(stu, N);
23     printf("\n对考生成绩进行统计...\n");
24     cnt = process(stu, N, stu_pass);
25     printf("\n通过考试名单:\n");
26     output(stu, N);
27     write(stu, N);
28     pass_rate = 1.0 * cnt / N;
29     printf("\n本次考试通过率:%.2f%%\n", pass_rate * 100);
30     return 0;
31 }
32 void output(STU st[], int n) {
33     int i;
34 
35     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
36     for (i = 0; i < n; i++)
37         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);
38 }
39 void read(STU st[], int n) {
40     int i;
41     FILE* fin;
42 
43     fin = fopen("C:\\Users\\冯韬\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\examinee.txt", "r");
44     if (!fin) {
45         printf("fail to open file\n");
46         return;
47     }
48 
49     for (i = 0; i < n; i++)
50         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
51 
52     fclose(fin);
53 }
54 int process(STU st[], int n, STU st_pass[]) {
55     int i;
56     int k=0;
57     for (i = 0; i < n; i++) {
58         st[i].sum = st[i].subjective + st[i].objective;
59         if (st[i].sum >= 60) {
60             strcpy(st[i].result, "通过");
61             st_pass[k++] = st[i];
62         }
63         else
64             strcpy(st[i].result, "不通过");
65     }
66     return k;
67 }
68 void write(STU st[], int n) {
69     FILE* fp;
70     int i;
71     fp = fopen("C:\\Users\\冯韬\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\list_pass.txt", "w");
72     if (!fp) {
73         perror("list_pass.txt");
74         return 1;
75     }
76     fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
77     for (i = 0; i < n; i++) {
78         if (st[i].sum >= 60)
79             fprintf(fp, "%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);
80     }
81     fclose(fp);
82 }
View Code

7

8

 

posted @ 2025-12-28 12:37  deepsigh  阅读(10)  评论(0)    收藏  举报