实验报告7

task4

代码:

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main() {
 6     FILE *fp = fopen("data4.txt", "r");
 7     if (fp == NULL) {
 8         printf("打开文件失败!\n");
 9         system("pause");
10         return 1;
11     }
12     int ch;
13     int line = 0;
14     int charCnt = 0;
15     int lastIsEnter = 1; //标记上一个是换行,用来计数行
16 
17     while ((ch = fgetc(fp)) != EOF) {
18         // 判断是否空白符:空格、\t、\n、\r
19         if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') {
20             //换行时行数+1
21             if (ch == '\n' && lastIsEnter == 0) {
22                 line++;
23                 lastIsEnter = 1;
24             }
25             continue;
26         }
27         //有效字符
28         charCnt++;
29         lastIsEnter = 0;
30     }
31     fclose(fp);
32 
33     printf("data4.txt统计结果:\n");
34     printf("行数: %d\n", line);
35     printf("字符数(不计空白符): %d\n", charCnt);
36 
37     system("pause");
38     return 0;
39 }

截图:

image

 task5

代码:

  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 
 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 
 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 passNum = 0,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             strcpy(st[i].result, "通过");
 79             st_pass[passNum] = st[i];
 80             passNum++;
 81         } else {
 82             strcpy(st[i].result, "不通过");
 83         }
 84     }
 85     return passNum;
 86 }
 87 
 88 // 把通过考试的考生完整信息写入文件list_pass.txt
 89 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 90 void write(STU st[], int n) {
 91    FILE *fp = fopen("list_pass.txt", "w");
 92    int i;
 93     if (fp == NULL) {
 94         printf("打开写入文件失败\n");
 95         return;
 96     }
 97     fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
 98     for (i = 0; i < n; i++) {
 99         fprintf(fp, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",
100                 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
101     }
102     fclose(fp);
103 }

截图:

image

 task6

代码:

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #define MAX_STU 80
 6 
 7 typedef struct {
 8     char id[20];
 9     char name[20];
10     char cls[40];
11 } Student;
12 
13 int main() {
14     int i;
15     FILE *fp = fopen("task6.txt", "r");
16     if (!fp) {
17         printf("无法打开task6.txt\n");
18         system("pause");
19         return 1;
20     }
21     Student stu[MAX_STU];
22     int total = 0;
23     while (fscanf(fp, "%s %s %s", stu[total].id, stu[total].name, stu[total].cls) == 3) {
24         total++;
25     }
26     fclose(fp);
27 
28     int flag[MAX_STU] = {0};
29     Student luck[5];
30     srand((unsigned)time(NULL)); 
31     int count = 0;
32     while (count < 5) {
33         int r = rand() % total;
34         if (flag[r] == 0) {
35             flag[r] = 1;
36             luck[count++] = stu[r];
37         }
38     }
39     printf("=====中奖名单=====\n");
40     for (i = 0; i < 5; i++) {
41         printf("%s %s %s\n", luck[i].id, luck[i].name, luck[i].cls);
42     }
43     char filename[50];
44     printf("\n输入保存文件名:");
45     scanf("%s", filename);
46     FILE *fw = fopen(filename, "w");
47     if (!fw) {
48         printf("文件创建失败\n");
49         system("pause");
50         return 1;
51     }
52     for (i = 0; i < 5; i++) {
53         fprintf(fw, "%s %s %s\n", luck[i].id, luck[i].name, luck[i].cls);
54     }
55     fclose(fw);
56     printf("保存成功!\n");
57     system("pause");
58     return 0;
59 }

 

截图:

image

 

posted @ 2026-06-23 17:31  franxx2022  阅读(3)  评论(0)    收藏  举报