任务四
源代码
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <stdlib.h>
4 int main()
5 {
6 int line = 0, ch, char_cnt = 0;
7 int flag_newline = 1;
8 FILE *fp = fopen("data4.txt", "r");
9 if (fp == NULL)
10 {
11 perror("open failed");
12 return 1;
13 }
14
15 while ((ch = fgetc(fp)) != EOF)
16 {
17
18 if (isspace(ch))
19 {
20 if (ch == '\n' && flag_newline == 0)
21 {
22 line++;
23 flag_newline = 1;
24 }
25 continue;
26 }
27
28 char_cnt++;
29 flag_newline = 0;
30 }
31
32 if (flag_newline == 0) line++;
33
34 fclose(fp);
35 printf("data4.txt统计结果:\n");
36 printf("行数:%d\n", line);
37 printf("字符数(不计空白符):%d\n", char_cnt);
38 system("pause");
39 return 0;
40 }
结果
![屏幕截图 2026-06-20 121718]()
任务五
源代码
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
35 printf("\n通过考试的名单写入文件:已完成!\n");
36 write(stu_pass, cnt);
37
38 pass_rate = 1.0 * cnt / N;
39 printf("\n本次等级考试通过率:%.2f%%\n", pass_rate*100);
40
41 return 0;
42 }
43
44
45 void output(STU st[], int n) {
46 int i;
47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
48 for (i = 0; i < n; i++)
49 printf("%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", st[i].id, st[i].name,
50 st[i].objective, st[i].subjective, st[i].sum, st[i].result);
51 }
52
53
54 void read(STU st[], int n) {
55 int i;
56 FILE *fin;
57 fin = fopen("examinee.txt", "r");
58 if (!fin) {
59 printf("fail to open file\n");
60 return;
61 }
62 for (i = 0; i < n; i++)
63 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
64 fclose(fin);
65 }
66
67
68 int process(STU st[], int n, STU st_pass[]) {
69 int pass_cnt = 0;
70 for(int i = 0; i < n; i++){
71
72 st[i].sum = st[i].objective + st[i].subjective;
73
74 if(st[i].sum >= 60){
75 strcpy(st[i].result, "通过");
76
77 st_pass[pass_cnt] = st[i];
78 pass_cnt++;
79 }else{
80 strcpy(st[i].result, "未通过");
81 }
82 }
83 return pass_cnt;
84 }
85
86
87 void write(STU st[], int n) {
88 FILE *fout = fopen("list_pass.txt", "w");
89 if(!fout){
90 printf("fail to open output file\n");
91 return;
92 }
93
94 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
95 for(int i = 0; i < n; i++){
96 fprintf(fout, "%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n",
97 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
98 }
99 fclose(fout);
100 }
结果
![屏幕截图 2026-06-17 140919]()
![屏幕截图 2026-06-20 121916]()
任务六
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <string.h>
5
6 #define STU_NUM 80
7 #define WIN_NUM 5
8 #define LINE_LEN 128
9
10
11 typedef struct {
12 char id[16];
13 char name[16];
14 char cls[32];
15 } Student;
16 void sortWinner(Student win[], int n)
17 {
18 int i, j;
19 Student temp;
20 for(i = 0; i < n - 1; i++)
21 {
22 for(j = 0; j < n - i - 1; j++)
23 {
24 if(strcmp(win[j].id, win[j+1].id) > 0)
25 {
26 temp = win[j];
27 win[j] = win[j+1];
28 win[j+1] = temp;
29 }
30 }
31 }
32 }
33
34 int main()
35 {
36 Student allStu[STU_NUM];
37 FILE *fp = fopen("list.txt", "r");
38 if(fp == NULL)
39 {
40 printf("无法打开list.txt文件!\n");
41 return 1;
42 }
43 int total = 0;
44 char buf[LINE_LEN];
45 while(fgets(buf, LINE_LEN, fp) != NULL && total < STU_NUM)
46 {
47 sscanf(buf, "%s %s %s", allStu[total].id, allStu[total].name, allStu[total].cls);
48 total++;
49 }
50 fclose(fp);
51 srand((unsigned)time(NULL));
52 int flag[STU_NUM] = {0};
53 Student winner[WIN_NUM];
54 int winCnt = 0;
55 while(winCnt < WIN_NUM)
56 {
57 int idx = rand() % total;
58 if(flag[idx] == 0)
59 {
60 flag[idx] = 1;
61 winner[winCnt++] = allStu[idx];
62 }
63 }
64
65
66 sortWinner(winner, WIN_NUM);
67
68
69 printf("----------------中奖名单----------------\n");
70 for(int i = 0; i < WIN_NUM; i++)
71 {
72 printf("%s\t%s\t%s\n", winner[i].id, winner[i].name, winner[i].cls);
73 }
74
75
76 char fileName[32];
77 time_t now = time(NULL);
78 struct tm *t = localtime(&now);
79 strftime(fileName, sizeof(fileName), "%Y%m%d.txt", t);
80 printf("自动生成文件名:%s\n", fileName);
81
82 FILE *fout = fopen(fileName, "w");
83 if(fout == NULL)
84 {
85 printf("文件创建失败!\n");
86 return 1;
87 }
88 for(int i = 0; i < WIN_NUM; i++)
89 {
90 fprintf(fout, "%s\t%s\t%s\n", winner[i].id, winner[i].name, winner[i].cls);
91 }
92 fclose(fout);
93 printf("保存成功!\n");
94
95 return 0;
96 }
结果
![image]()
![image]()