实验七

实验七

task4

源代码

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

 

运行截屏

4

 

task5

源代码

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <string.h>
  4 
  5 
  6 #define N 10
  7 
  8 typedef struct {
  9     long id;            // 准考证号
 10     char name[20];      // 姓名
 11     float objective;    // 客观题得分
 12     float subjective;   // 操作题得分
 13     float sum;          // 总分
 14     char result[10];    // 考试结果
 15 } STU;
 16 
 17 // 函数声明
 18 void read(STU st[], int n);
 19 void write(STU st[], int n);
 20 void output(STU st[], int n);
 21 int process(STU st[], int n, STU st_pass[]);
 22 
 23 int main() {
 24     STU stu[N], stu_pass[N];
 25     int cnt;
 26     double pass_rate;
 27 
 28     printf("从文件读入%d个考生信息: 已完成\n", N);
 29     read(stu, N);
 30 
 31     printf("\n对考生成绩进行统计: 已完成\n");
 32     cnt = process(stu, N, stu_pass);
 33 
 34     printf("\n所有考生完整信息:\n");
 35     output(stu, N);
 36 
 37     printf("\n通过考试的名单写入文件: 已完成!\n");
 38     write(stu_pass, cnt);
 39 
 40     pass_rate = 1.0 * cnt / N;
 41     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
 42 
 43     return 0;
 44 }
 45 
 46 // 把所有考生完整信息输出到屏幕上
 47 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 48 void output(STU st[], int n) {
 49     int i;
 50 
 51     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 52     for (i = 0; i < n; i++)
 53         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);
 54 }
 55 
 56 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 57 void read(STU st[], int n) {
 58     int i;
 59     FILE* fin;
 60 
 61     fin = fopen("examinee.txt", "r");
 62     if (!fin) {
 63         printf("fail to open file\n");
 64         return;
 65     }
 66 
 67     for (i = 0; i < n; i++)
 68         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 69 
 70     fclose(fin);
 71 }
 72 
 73 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
 74 int process(STU st[], int n, STU st_pass[]) {
 75     // 待补足
 76     // xxx
 77     int i,j=0;
 78     for (i = 0; i < n; i++) {
 79         st[i].sum = st[i].objective + st[i].subjective;
 80         if (st[i].sum >= 60) {
 81             strcpy(st[i].result, "通过");
 82             st_pass[j] = st[i];
 83             j++;
 84         }
 85         else
 86             strcpy(st[i].result, "不通过");
 87     }
 88     return j;
 89 }
 90 
 91 // 把通过考试的考生完整信息写入文件list_pass.txt
 92 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 93 void write(STU st[], int n) {
 94     // 待补足
 95     // xxx
 96     int i;
 97     FILE* fin;
 98 
 99     fin = fopen("list_pass.txt", "w");
100     if (fin == NULL) {
101         printf("fail to open file to write\n");
102         return;
103     }
104 
105     fprintf(fin, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
106     for (i = 0; i < n; i++)
107         fprintf(fin, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
108             st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
109     fclose(fin);
110 }
View Code

 

运行截屏

5.1

5.2

 

task6

源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <time.h>
 5 #include<stdlib.h>
 6 #define N 80
 7 
 8 typedef struct {
 9     int mun;
10     char name[20];
11     char class[50];
12 }STU;
13 
14 void func1(STU st[]);
15 void func2(STU st[],STU st_chosen[]);
16 void func3(STU st[],char x[]);
17 
18 int main() {
19     STU st[N],st_chosen[N] ;
20     char name[N];
21     srand(time(NULL));
22     func1(st);//录入数据
23     func2(st,st_chosen);//抽奖和输出
24     printf("---------保存到文件---------\n");
25     printf("请输入文件名:");
26     scanf("%s", name);
27     func3(st_chosen,name);//储存名单
28     printf("保存成功!");
29     return 0;
30 }
31 
32 void func1(STU st[]) {
33     int i;
34     FILE* fp;
35     fp = fopen("list.txt", "r");
36 
37     if (!fp) {
38         printf("fail to open file\n");
39         return;
40     }
41 
42     for (i = 0; i < N; i++)
43         fscanf(fp, "%d %s %s", &st[i].mun, st[i].name, st[i].class);
44 
45     fclose(fp);
46 }
47 
48 
49 void func2(STU st[], STU st_chosen[]) {
50     int i,k,j=0,s,sign=1;
51     int a[5];
52     printf("---------中奖名单---------\n");
53 
54     for (i = 0; i < 5; i++) {
55         k = rand() % 80;
56         if (i == 0)
57             a[0] = k;
58         else {
59             for (s = 0; s < i; s++) {
60                 if (k == a[s]) {
61                     sign = 0;
62                     break;
63                 }
64                 else
65                     a[i] = k;
66             }
67         }
68         if (sign == 0)
69             continue;
70         st_chosen[i] = st[k];
71         printf("%d\t%s\t%s\n", st_chosen[i].mun, st_chosen[i].name, st_chosen[i].class);
72     }
73 }
74 
75 void func3(STU st[],  char x[]) {
76     int i;
77     FILE* fin;
78 
79     fin = fopen("x.txt", "w");
80     if (fin == NULL) {
81         printf("fail to open file to write\n");
82         return;
83     }
84 
85     fprintf(fin, "学号\t姓名\t班级\n");
86     for (i = 0; i < 5; i++)
87         fprintf(fin, "%d\t\t%s\t\t%s\n",
88             st[i].mun, st[i].name, st[i].class);
89     fclose(fin);
90 }
View Code

 

运行截屏

6.1

6.2

 

task6选做

源代码

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include<string.h>
  4 #include <time.h>
  5 #include<stdlib.h>
  6 #define N 80
  7 
  8 typedef struct {
  9     int mun;
 10     char name[20];
 11     char class[50];
 12 }STU;
 13 
 14 void func1(STU st[]);//录入数据
 15 void func2(STU st[],STU st_chosen[]);//抽奖
 16 void func3(STU st[],char x[]);//储存名单
 17 void func4(STU st[]);//排序
 18 void func5(STU st_chosen[]);//屏幕输出
 19 void func6(char* name);//时间命名
 20 
 21 
 22 int main() {
 23     STU st[N],st_chosen[N];
 24     char name[N];
 25     srand(time(NULL));
 26     func1(st);
 27     func2(st,st_chosen);
 28     func4(st_chosen);
 29     func5(st_chosen);
 30     printf("---------保存到文件---------\n");
 31     func6(name);
 32     puts(name);
 33     func3(st_chosen,name);
 34     printf("保存成功!");
 35     return 0;
 36 }
 37 
 38 
 39 void func1(STU st[]) {
 40     int i;
 41     FILE* fp;
 42     fp = fopen("list.txt", "r");
 43 
 44     if (!fp) {
 45         printf("fail to open file\n");
 46         return;
 47     }
 48     for (i = 0; i < N; i++)
 49         fscanf(fp, "%d %s %s", &st[i].mun, st[i].name, st[i].class);
 50     fclose(fp);
 51 }
 52 
 53 
 54 void func2(STU st[], STU st_chosen[]) {
 55     int i,k,j=0,s,sign=1;
 56     int a[5];
 57     printf("---------中奖名单---------\n");
 58     for (i = 0; i < 5; i++) {
 59         k = rand() % 80;
 60         if (i == 0)
 61             a[0] = k;
 62         else {
 63             for (s = 0; s < i; s++) {
 64                 if (k == a[s]) {
 65                     sign = 0;
 66                     break;
 67                 }
 68                 else
 69                     a[i] = k;
 70             }
 71         }
 72         if (sign == 0)
 73             continue;
 74         st_chosen[i] = st[k];
 75         
 76     }
 77 }
 78 
 79 void func3(STU st[],  char x[]) {
 80     int i;
 81     FILE* fin;
 82     fin = fopen(x, "w");
 83     if (fin == NULL) {
 84         printf("fail to open file to write\n");
 85         return;
 86     }
 87     fprintf(fin, "学号\t姓名\t班级\n");
 88     for (i = 0; i < 5; i++)
 89         fprintf(fin, "%d\t\t%s\t\t%s\n",
 90             st[i].mun, st[i].name, st[i].class);
 91     fclose(fin);
 92 }
 93 
 94 void func4(STU st[]) {
 95     int i, j;
 96     STU t;
 97     for (i = 0; i < 4; i++) {
 98         for (j = 0; j < 4 - i; j++) {
 99             if (st[j].mun > st[j + 1].mun) {
100                 t = st[j];
101                 st[j] = st[j+1];
102                 st[j + 1] = t;
103             }
104         }
105     }
106 }
107 
108 void func5(STU st_chosen[]) {
109     int i;
110     for(i=0;i<5;i++)
111     printf("%d\t%s\t%s\n", st_chosen[i].mun, st_chosen[i].name, st_chosen[i].class);
112 }
113 
114 void func6(char* name) {
115     time_t now = time(NULL);
116     struct tm* local = localtime(&now);
117     snprintf(name, 20, "%04d%02d%02d.txt",local->tm_year + 1900,local->tm_mon + 1,local->tm_mday);        
118 }
View Code

 

运行截屏

6.11

6.22

 

posted @ 2026-06-20 01:28  B1uerose  阅读(13)  评论(0)    收藏  举报