实验7

实验4

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #define N 1000
 4 
 5 int main() {
 6     FILE* fp;
 7     char s[N];
 8     int i = 0, n, lines = 1, feizifu = 0;
 9 
10     fp = fopen("data4.txt", "r");
11     if (fp == NULL) {
12         printf("fail to open file to write\n");
13         return 1;
14     }
15 
16     while ((s[i] = fgetc(fp)) != EOF)
17         ++i;
18 
19     n = i;
20 
21     for (i = 0; i < n; i++) {
22         if (s[i] == '\n') lines++;
23         if (s[i] == ' ' || s[i] == '\t' || s[i] == '\n') feizifu++;
24     }
25 
26     printf("data4.txt统计结果:\n");
27     printf("行数:\t\t%d\n", lines);
28     printf("字符数(不含空白符):\t%d", n - feizifu);
29     fclose(fp);
30     return 0;
31 }
View Code

微信图片_2025-12-29_134203_438

实验5

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 typedef struct {
 5     long id; 
 6     char name[20];
 7     float objective; 
 8     float subjective;
 9     float sum;
10     char result[10]; 
11 } STU;
12 // 函数声明
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 // 把所有考生完整信息输出到屏幕上
33 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
34 void output(STU st[], int n) {
35     int i;
36 
37     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
38     for (i = 0; i < n; i++)
39         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name,
40             st[i].objective, st[i].subjective, st[i].sum, st[i].result);
41 }
42 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
43 void read(STU st[], int n) {
44     int i;
45     FILE* fin;
46     fin = fopen("examinee.txt", "r");
47     if (!fin) {
48         printf("fail to open file\n");
49         return;
50     }
51     for (i = 0; i < n; i++)
52         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective,&st[i].subjective);
53     fclose(fin);
54 }
55 // 把通过考试的考生完整信息写入文件list_pass.txt
56 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
57 void write(STU s[], int n) {
58     int i;
59     FILE* fout;
60     fout = fopen("list_pass.txt", "w");
61     if (!fout) {
62         printf("fail to open file\n");
63         return;
64     }
65     for (i = 0; i < n; i++)
66         if(s[i].sum>=60)
67             fprintf(fout, "%ld\t %s\t %f\t %f\t", s[i].id, s[i].name, s[i].objective,s[i].subjective);
68     fclose(fout);
69 
70 }
71 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
72 int process(STU st[], int n, STU st_pass[]) {
73     int i, j, cnt = 0;
74     float zongfen;
75     for (i = 0; i < n; i++) {
76         zongfen = st[i].objective + st[i].subjective;
77         st[i].sum = zongfen;
78         if (st[i].sum >= 60.00) {
79             strcpy(st[i].result, "通过");
80             st_pass[cnt++] = st[i];
81         }
82         else {
83             strcpy(st[i].result, "不通过");
84         }
85     }
86     return cnt;
87 }
View Code

微信图片_2025-12-29_145031_874

 

实验6

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #include <string.h>
 6 #define N 100
 7 int main() {
 8     srand((unsigned int)time(NULL));
 9     char s[N][N];
10     char hit[N][N];
11     FILE* fin;
12     FILE* fout;
13     int has_hit[N] = { 0 };
14     int i=0, n,lucky_i;
15     char name[80];
16     fin = fopen("list.txt","r");
17     if (!fin) {
18         perror("list.txt");
19         return 1;
20     }
21     while (fgets(s[i], N, fin) != NULL)
22         ++i;
23     n = i;
24     for (int j = 0; j < 5; )
25     {
26         lucky_i = rand() % n;
27         if (has_hit[lucky_i])
28             continue;
29         has_hit[lucky_i] = 1;
30         strcpy(hit[j], s[lucky_i]);
31         ++j;
32 
33     }
34     for (i = 0; i < 5; ++i) {
35         printf(hit[i]);
36         
37     }
38 
39     printf("Enter filename:");
40     gets(name);
41     fout = fopen(name, "w");
42     if (!fout)
43     {
44         perror(name);
45         return 1;
46     }
47     for (i = 0; i < 5; ++i)
48     {
49         fprintf(fout, name, hit[i]);
50     }
51     fclose(fout);
52     fclose(fin);
53     return 0;
54 
55 
56 }
View Code

Screenshot_20251229125043

Screenshot_20251229125031

 

posted @ 2025-12-29 14:51  哈气的大猫  阅读(2)  评论(0)    收藏  举报