实验七

 

 

 

 

 

实验任务4444444444444

 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

 

image

 

实验任务55555555555

 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("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

image

 

实验66666666666

 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

不知道为什么乱码了

image

 

 

老师上次作业实在是不会才抄的他的,sorry啊

posted @ 2025-12-30 20:33  顾添乐  阅读(6)  评论(0)    收藏  举报