实验7

任务4

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

 

任务5

  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     write(stu, N);  
 35 
 36     pass_rate = 1.0 * cnt / N;
 37     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 38 
 39     return 0;
 40 }
 41 
 42 
 43 void output(STU st[], int n) {
 44     int i;
 45     
 46     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 47     for (i=0;i<n;i++)
 48         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);
 49 }
 50 
 51 
 52 void read(STU st[], int n) {
 53     int i;
 54     FILE *fin;
 55 
 56     fin = fopen("examinee.txt", "r");
 57     if (!fin) {
 58         printf("fail to open file\n");
 59         return;
 60     }
 61 
 62     while (!feof(fin)) {
 63         for (i=0;i<n;i++)
 64             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 65     }
 66 
 67     fclose(fin);
 68 }
 69 
 70 
 71 void write(STU st[], int n) 
 72 {
 73     FILE *fout;
 74     int i;
 75 
 76     fout=fopen("list_pass.txt", "w");
 77     if (!fout) {
 78         printf("fail to open file\n");
 79         return;
 80     }
 81 
 82     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 83     for (i=0;i<n;i++) {
 84         fprintf(fout, "%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);
 85     }
 86 
 87     fclose(fout);
 88 }
 89 
 90     
 91 
 92 
 93 
 94 
 95 int process(STU st[], int n, STU st_pass[]) 
 96 {
 97     int i, cnt=0;
 98 
 99     for (i=0;i<n;i++) 
100     {
101         st[i].sum =st[i].objective+st[i].subjective;
102         if (st[i].sum>=60) 
103         {
104             strcpy(st[i].result,"通过");
105             st_pass[cnt++] = st[i];
106 
107         } else 
108             strcpy(st[i].result,"不通过");
109         
110     }
111 
112     return cnt;
113     
114 }

 

任务6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 #define N 80
 7 #define M 5
 8 
 9 // 学生结构体
10 typedef struct 
11 {
12     char id[N];
13     char name[N];
14     char class[N];
15 } Stu;
16 
17 // 从文件读取学生数据
18 void read(Stu s[], const char *fn) 
19 {
20     FILE *fp =fopen(fn,"r");
21     int i;
22     if (fp==NULL) 
23     {
24         printf("无法打开文件");
25         return 1;
26     }
27     for (i=0;i<N;i++) {
28         fscanf(fp, "%s %s %s",s[i].id,s[i].name,s[i].class);
29     }
30     fclose(fp);
31 }
32 
33 // 随机抽取学生
34 void pick(Stu a[],Stu s[],int m) 
35 {
36     srand(time(0));
37     int used[N]={0};
38     int i,idx;
39     for (i=0;i<m;i++) 
40     {
41        
42         do {
43             idx = rand() % N;
44         } while (used[idx]);
45         used[idx] = 1;
46         strcpy(s[i].id, a[idx].id);
47         strcpy(s[i].name, a[idx].name);
48         strcpy(s[i].class, a[idx].class);
49     }
50 }
51 
52 // 在屏幕显示抽取的学生
53 void show(Stu s[], int m) 
54 {
55     int i;
56     printf("抽取的学生信息如下:\n");
57     for (i=0;i<m;i++) 
58         printf("学号:%s,姓名:%s,班级:%s\n", s[i].id, s[i].name, s[i].class);
59     
60 }
61 
62 // 将抽取的学生写入文件
63 void write(Stu s[], int m, const char *fn) 
64 {
65     int i;
66     FILE *fp = fopen(fn, "w");
67     if (fp==NULL) {
68         printf("无法打开文件");
69         return 1;
70     }
71     for (i=0;i<m;i++) {
72         fprintf(fp, "学号:%s,姓名:%s,班级:%s\n", s[i].id, s[i].name, s[i].class);
73     }
74     fclose(fp);
75 }
76 
77 int main() 
78 {
79     Stu all[N];
80     Stu select[M];
81     char fileName[100];
82 
83     read(all, "list.txt");
84     pick(all, select, M);
85     show(select, M);
86 
87     printf("请输入要写入的文件名: ");
88     scanf("%s", fileName);
89     write(select, M, fileName);
90     printf("\n文件保存成功!");
91 
92     return 0;
93 }

 

posted @ 2024-12-29 17:45  宇望舒  阅读(11)  评论(0)    收藏  举报