实验7

任务4

 1 #include <stdio.h>
 2 
 3 void my_print()
 4 {
 5     FILE *fp;
 6     fp = fopen("data4.txt", "r");
 7     char a;
 8     
 9     if (fp == NULL)
10     {
11         printf("fail to open file to read\n");
12         return;
13     }
14     
15     int count_line = 1;
16     int count_only_char = 0;
17     
18     while ((a = fgetc(fp))! = EOF)    
19     {
20         if (a == '\n')
21         count_line += 1;
22         if (a != ' ' && a != '\n')
23         count_only_char += 1;
24     }
25     printf("行数: %d\n字符数(不计空白符): %d\n", count_line, count_only_char);
26     
27     fclose(fp);
28 }
29 
30 int main()
31 {
32     printf("data4.txt统计结果\n");
33     my_print();
34     return 0;
35 }

 

 

任务5

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

 

 

任务6

 

 1 #include <stdio.h>
 2 #include <time.h>
 3 #define a 10
 4 #define b 100
 5 #define c 100
 6 
 7 typedef struct
 8 {
 9     int number;
10     char name[b];
11     char class[c];
12     int flag;
13 } STU;
14 
15 void read(STU s[])
16 {
17     int i;
18     FILE *fp;
19 
20     fp = fopen("list.txt", "r");
21     if (!fp) 
22     {
23         printf("fail to open file to read\n");
24         return;
25     }
26     
27     while (!feof(fp)) 
28     {
29         for (i = 0; i < 80; i++)
30             fscanf(fp, "%s%s%s", &s[i].number, &s[i].name, &s[i].class);
31     }
32 
33     fclose(fp);
34 }
35 
36 void select(STU s[])
37 {
38     int i, random;
39     srand (time (NULL));
40     
41     for (i = 0; i < 5; i++)
42     {
43         do
44         {
45             random = rand() % 80;
46         }while (s[random].flag == 1);
47         
48         s[random].flag = 1;
49         s[i] = s[random];
50     }
51 }
52 void store(STU s[], char *student)
53 {
54     FILE *fp;
55     int i;
56     fp = fopen("student.txt", "w");
57     if (! fp)
58     {
59         printf("fail to open file to write\n");
60         return;
61     }
62     for (i = 0; i < 5; i++)
63     {
64         fprintf(fp,"%-16d%-8s%s\n",s[i].number,s[i].name,s[i].class);
65     }
66     
67     fclose(fp);
68 }
69 
70 int main()
71 {
72     STU s[80];
73     char student[100];
74     int i;
75     for (i = 0; i < 80; i++)
76     s[i].flag = 0;
77     
78     printf("------------20241222抽点名单------------\n");
79     read(s);
80     select(s);
81     
82     for (i = 0; i < 5; i++)
83     printf("%-16d%-8s%s\n",s[i].number,s[i].name,s[i].class);
84     
85     printf("------------保存到文件-----------\n");
86     printf("输入文件名:");
87     gets(student);
88     printf("\n");
89      
90     store(s,student);
91     printf("保存成功!");
92      
93     return 0;
94 }

 

 

posted @ 2024-12-30 00:07  64rytr76d65  阅读(7)  评论(0)    收藏  举报