实验七

task4

源代码

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

运行结果截图

屏幕截图 2026-06-23 183020

task5

源代码

  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 int process(STU st[], int n, STU st_pass[]){
 72     int pass_cnt = 0;
 73      int i;
 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[pass_cnt] = st[i];
 81             pass_cnt++;
 82          }
 83          else
 84          {
 85              strcpy(st[i].result,"未通过");
 86          }
 87      }
 88      return pass_cnt;
 89 }
 90  void write(STU st[], int n)
 91  {
 92      int i;
 93      FILE *fp = fopen("list_pass.txt","w");
 94     fprintf(fp,"%-10s%-10s%-12s%-12s%-10s%s\n",
 95      "准考证号","姓名","客观题得分","操作题得分","总分","结果");
 96      for(i=0;i<n;i++)
 97      {
 98          fprintf(fp,"%-10ld%-10s%-12.2f%-12.2f%-10.2f%s\n",
 99          st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result);
100      }
101      fclose(fp);
102     getchar();
103      getchar();
104  }
View Code

运行结果截图

屏幕截图 2026-06-20 131807

屏幕截图 2026-06-23 183540

task6

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 typedef struct
 6 {
 7     char id[15];
 8     char name[12];
 9     char cls[30];
10 }Stu;
11 
12 int main()
13 {
14     Stu all[80], win[5];
15     int flag[80] = {0};
16     int total = 0, i, r;
17     char filename[30];
18     FILE *fp, *fw;
19 
20     fp = fopen("list.txt", "r");
21     while(fscanf(fp,"%s%s%s",all[total].id,all[total].name,all[total].cls)!=EOF)
22         total++;
23     fclose(fp);
24 
25     srand((unsigned)time(NULL));
26     for(i=0;i<5;i++)
27     {
28         do{
29             r = rand()%total;
30         }while(flag[r]==1);
31         flag[r] = 1;
32         win[i] = all[r];
33     }
34     printf("--------中奖名单--------\n");
35     for(i=0;i<5;i++)
36     {
37         printf("%-12s%-10s%s\n",win[i].id,win[i].name,win[i].cls);
38     }
39     printf("--------保存到文件--------\n");
40     printf("输入文件名:");
41     scanf("%s",filename);
42     fw = fopen(filename,"w");
43     for(i=0;i<5;i++)
44     {
45         fprintf(fw,"%-12s%-10s%s\n",win[i].id,win[i].name,win[i].cls);
46     }
47     fclose(fw);
48     printf("保存成功!\n");
49     getchar();
50     getchar();
51     return 0;
52 }
View Code

运行结果截图

屏幕截图 2026-06-20 114937

屏幕截图 2026-06-20 130258

 

posted @ 2026-06-20 13:21  yq8  阅读(4)  评论(0)    收藏  举报