实验7

实验7

实验4源代码、截图

 

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

7.4

实验5源代码、截图

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  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     system("pause");
 41     return 0;
 42 }
 43 
 44 // 把所有考生完整信息输出到屏幕上
 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 46 void output(STU st[], int n) {
 47     int i;
 48     
 49     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 50     for (i = 0; i < n; i++)
 51         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);
 52 }
 53 
 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 55 void read(STU st[], int n) {
 56     int i;
 57     FILE *fin;
 58 
 59     fin = fopen("examinee.txt", "r");
 60     if (!fin) {
 61         printf("fail to open file\n");
 62         return;
 63     }
 64 
 65     for (i = 0; i < n; i++)
 66         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67 
 68     fclose(fin);
 69 }
 70 
 71 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
 72 int process(STU st[], int n, STU st_pass[]) {
 73     int i, k = 0;
 74     for(i = 0; i < n; i++)
 75     {
 76         st[i].sum = st[i].objective + st[i].subjective;
 77         if(st[i].sum >= 60)
 78         {
 79             strcpy(st[i].result, "通过");
 80             st_pass[k] = st[i];
 81             k++;
 82         }
 83         else
 84         {
 85             strcpy(st[i].result, "不通过");
 86         }
 87     }
 88 
 89     return k;
 90 }
 91 
 92 // 把通过考试的考生完整信息写入文件list_pass.txt
 93 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 94 void write(STU st[], int n) {
 95    FILE *fp;
 96     int i;
 97     fp = fopen("list_pass.txt", "w");
 98     if(!fp)
 99     {
100         printf("fail to open output file\n");
101         return;
102     }
103     fprintf(fp,"准考证\t姓名\t客观题得分\t操作得分\t总分\t结果\n");
104     for(i = 0; i < n; i++)
105     {
106         fprintf(fp,"%ld %s %.2f %.2f %.2f %s\n",
107             st[i].id, st[i].name, st[i].objective,
108             st[i].subjective, st[i].sum, st[i].result);
109     }
110     fclose(fp);
111 }

7.5

实验6源代码、截图

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 #define N 80    
 7 #define LEN 100 
 8 
 9 int main(void)
10 {
11     FILE *fp;
12     char stu[N][LEN];  
13     int flag[N] = {0}; 
14     int i, count = 0, rnd;
15     char filename[30]; 
16 
17     fp = fopen("list.txt", "r");
18     if (fp == NULL)
19     {
20         printf("打开list.txt失败!\n");
21         system("pause");
22         return 1;
23     }
24     for (i = 0; i < N; i++)
25     {
26         fgets(stu[i], LEN, fp);
27         stu[i][strcspn(stu[i], "\n")] = '\0';
28     }
29     fclose(fp);
30 
31     srand((unsigned)time(NULL));
32 
33     printf("-----------中奖名单-----------\n");
34     while (count < 5)
35     {
36         rnd = rand() % N; 
37         if (flag[rnd] == 0) 
38         {
39             flag[rnd] = 1;
40             printf("%s\n", stu[rnd]);
41             count++;
42         }
43     }
44     printf("\n");
45     printf("----------保存到文件----------\n");
46     printf("输入文件名:");
47     scanf("%s", filename);
48 
49     fp = fopen(filename, "w");
50     if (fp == NULL)
51     {
52         printf("文件创建失败!\n");
53         system("pause");
54         return 1;
55     }
56     for (i = 0; i < N; i++)
57     {
58         if (flag[i] == 1)
59         {
60             fprintf(fp, "%s\n", stu[i]);
61         }
62     }
63     fclose(fp);
64     printf("保存成功!\n");
65 
66     system("pause");
67     return 0;
68 }

7.6

7.6.2

 

posted @ 2026-06-23 22:13  宇r  阅读(5)  评论(0)    收藏  举报