实验七

实验一:

已完成

实验二:

已完成

实验三:

已完成:

实验四:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 
 4 int main() {
 5     FILE* fp;
 6     int ch;
 7     int l = 0, c = 0;
 8     int a = 0;
 9 
10     fp = fopen("data4.txt", "r");
11     if (fp == NULL) {
12         printf("无法打开文件 data4.txt\n");
13         return 1;
14     }
15 
16     while ((ch = fgetc(fp)) != EOF) {
17         if (ch == '\n') {
18             l++;
19             a = 1;
20         }
21         else {
22             if (ch != ' ' && ch != '\t' && ch != '\n') {
23                 c++;
24             }
25             a = 0;
26         }
27     }
28 
29     
30     if (a==0) {
31         l++;
32     }
33 
34     printf("data4.txt统计结果:\n");
35     printf("行数:    %d\n", l);
36     printf("字符数(不计空白符):  %d\n", c);
37 
38     fclose(fp);
39     return 0;
40 }
View Code

image

 实验五:

 1 int process(STU st[], int n, STU st_pass[]) {
 2     int  i = 0, k = 0;;
 3     for (i = 0; i < n; i++)
 4     {
 5         st[i].sum = st[i].objective + st[i].subjective;
 6         if (st[i].sum >= 60) {
 7             strcpy(st[i].result, "通过");
 8             st_pass[k] = st[i];
 9             k++;
10         }
11         else
12             strcpy(st[i].result, "不通过");
13     }
14     return k;
15 }
16 
17 // 把通过考试的考生完整信息写入文件list_pass.txt
18 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
19 void write(STU st[], int n) {
20     FILE* fp;
21     int i;
22     fp = fopen("list_pass.txt", "w");
23     if (fp == NULL) {
24         printf("无法打开 list_pass.txt\n");
25         return 1;
26     }
27     fprintf(fp, "准考证号\t姓名\t\t客观题得分\t操作题得分\t总分\t结果\n");
28 
29     // 写入每位通过考生的信息
30     for (i = 0; i < n; i++) {
31         fprintf(fp, "%ld\t%s\t\t%.2f\t\t%.2f\t%.2f\t%s\n",
32             st[i].id, st[i].name,
33             st[i].objective, st[i].subjective,
34             st[i].sum, st[i].result);
35     }
36 
37     fclose(fp);
38 }
View Code

 

image

 

image

 实验六:

image

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <time.h>
 6 
 7 #define N 80
 8 #define M 5
 9 
10 typedef struct {
11     char id[20];
12     char name[50];
13     char class_name[50];
14 } Student;
15 
16 void output(Student arr[], int n);
17 void save(Student arr[], int n, const char* filename);
18 
19 int main() {
20     Student all[N];
21     Student lucky[M];
22     char filename[100];
23     time_t t;
24     struct tm* tm_info;
25     int i = 0,number;
26 
27     FILE* fp = fopen("list.txt", "r");
28     if (!fp) {
29         printf("打开文件失败");
30         return 1;
31     }
32      while (i<N){
33      fscanf(fp, "%s %s %s", all[i].id, all[i].name, all[i].class_name);
34         i++;
35     }
36     fclose(fp);
37 
38 
39     srand(time(0));
40     for (i = 0; i < M; i++) {
41         number = rand() % N ;
42         lucky[i] = all[number];
43     }
44 
45     output(lucky, M);
46     printf("\n---------保存到文件---------\n");
47     printf("请输入文件名");
48     scanf("%s", filename);
49 
50     save(lucky, M, filename);
51 
52     return 0;
53 }
54 
55 void output(Student arr[], int n) {
56     
57     printf("\n---------中奖名单---------\n");
58     
59     printf("学号\t姓名\t班级\n");
60 
61     for (int i = 0; i < n; i++) {
62         printf("%s\t%s\t%s\n", arr[i].id, arr[i].name, arr[i].class_name);
63     }
64 
65 }
66 
67 
68 void save(Student arr[], int n, const char* filename) {
69     FILE* fp = fopen(filename, "w");
70     if (!fp) {
71         printf("创建文件失败");
72         return 1;
73     }
74     fprintf(fp, "学号\t姓名\t班级\n");
75     
76 
77     for (int i = 0; i < n; i++) {
78         
79         fprintf(fp, "%s\t%s\t%s\n", arr[i].id, arr[i].name, arr[i].class_name);
80     }
81     fclose(fp);
82 
83     fp = fopen(filename, "r");
84     if (fp) {
85         printf("保存成功!");
86         return 1;
87     }
88 }
View Code

选做:

image

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 
 6 #define N 80
 7 #define M 5
 8 
 9 typedef struct {
10     char id[20];
11     char name[50];
12     char class_name[50];
13 } Student;
14 
15 void output(Student arr[], int n);
16 void save(Student arr[], int n, const char* filename);
17 
18 int main() {
19     Student all[N];
20     Student lucky[M];
21     char filename[100];
22     long long t;
23     struct tm* tm_info;
24     int i = 0,number;
25 
26     FILE* fp = fopen("list.txt", "r");
27     if (!fp) {
28         printf("打开文件失败");
29         return 1;
30     }
31      while (i<N){
32      fscanf(fp, "%s %s %s", all[i].id, all[i].name, all[i].class_name);
33         i++;
34     }
35     fclose(fp);
36 
37 
38     srand(time(0));
39     for (i = 0; i < M; i++) {
40         number = rand() % N ;
41         lucky[i] = all[number];
42     }
43 
44     output(lucky, M);
45 
46     t = time(NULL);
47     tm_info = localtime(&t);
48     sprintf(filename, "%04d%02d%02d.txt", tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday);
49 
50     save(lucky, M, filename);
51 
52     return 0;
53 }
54 
55 void output(Student arr[], int n) {
56     
57     printf("\n---------中奖名单---------\n");
58     
59     printf("学号\t姓名\t班级\n");
60 
61     for (int i = 0; i < n; i++) {
62         printf("%s\t%s\t%s\n", arr[i].id, arr[i].name, arr[i].class_name);
63     }
64 
65 }
66 
67 
68 void save(Student arr[], int n, const char* filename) {
69     FILE* fp = fopen(filename, "w");
70     if (!fp) {
71         printf("创建文件失败");
72         return 1;
73     }
74     fprintf(fp, "学号\t姓名\t班级\n");
75     
76 
77     for (int i = 0; i < n; i++) {
78         
79         fprintf(fp, "%s\t%s\t%s\n", arr[i].id, arr[i].name, arr[i].class_name);
80     }
81     fclose(fp);
82 
83     fp = fopen(filename, "r");
84     if (fp) {
85         printf("保存成功!");
86         return 1;
87     }
88 }
View Code

 

posted @ 2026-06-23 20:25  郑云翔  阅读(5)  评论(0)    收藏  举报