实验7

task4

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

     

  • 截图

    4屏幕截图 2026-06-21 213955

 

task5

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

     

  • 截图

    5屏幕截图 2026-06-21 221917

    5屏幕截图 2026-06-21 222616

 

task6

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

     

     

  • 截图

    6屏幕截图 2026-06-22 224329

 

    6屏幕截图 2026-06-22 224415

 

   

 

posted @ 2026-06-22 18:33  Miracle_rui  阅读(9)  评论(1)    收藏  举报