实验7

++实验任务4

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>  // system()所需头文件
 3 
 4 int main() {
 5     FILE *fp;
 6     char ch;
 7     int line_count = 0;
 8     int char_count = 0;
 9 
10     if ((fp = fopen("data4.txt", "r")) == NULL) {
11         printf("文件打开失败!请检查data4.txt是否和程序放在同一文件夹\n");
12         system("pause");
13         return 1;
14     }
15 
16     while ((ch = fgetc(fp)) != EOF) {
17         if (ch == '\n') {
18             line_count++;
19         }
20         else if (ch != ' ' && ch != '\t') {
21             char_count++;
22         }
23     }
24 
25     fclose(fp);
26 
27     printf("data4.txt统计结果:\n");
28     printf("行数:%18d\n", line_count);
29     printf("字符数(不计空白符):%8d\n", char_count);
30 
31     system("pause");
32     return 0;
33 }

运行截图

QQ20260622-231649

++实验任务5

源代码

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

QQ20260622-232604

QQ20260622-232759

++实验任务6

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 #define MAX_STU 80   // 最多80名学生
 7 #define WIN_NUM 5    // 抽取5位中奖
 8 
 9 // 学生结构体
10 typedef struct {
11     char id[15];
12     char name[20];
13     char cls[30];
14 } Student;
15 
16 // 读取全部学生文件
17 int readList(Student st[]) {
18     FILE *fp = fopen("list.txt", "r");
19     if (fp == NULL) {
20         printf("无法打开list.txt!请把文件放在项目目录\n");
21         system("pause");
22         exit(1);
23     }
24     int count = 0;
25     while (fscanf(fp, "%s %s %s", st[count].id, st[count].name, st[count].cls) == 3) {
26         count++;
27     }
28     fclose(fp);
29     return count;
30 }
31 
32 int main() {
33     Student allStu[MAX_STU];
34     Student winStu[WIN_NUM];
35     int total = readList(allStu);
36     int selected[MAX_STU] = {0}; // 标记是否已抽取,0未选1已选
37     int i, idx, winCnt = 0;
38     char saveName[50];
39     FILE *outFp;
40 
41     // 设置随机数种子
42     srand((unsigned)time(NULL));
43 
44     // 随机抽取5个不重复学生
45     while (winCnt < WIN_NUM) {
46         idx = rand() % total;
47         if (selected[idx] == 0) {
48             selected[idx] = 1;
49             winStu[winCnt] = allStu[idx];
50             winCnt++;
51         }
52     }
53 
54     // 控制台输出中奖名单
55     printf("----------------中奖名单----------------\n");
56     for (i = 0; i < WIN_NUM; i++) {
57         printf("%s\t%s\t%s\n", winStu[i].id, winStu[i].name, winStu[i].cls);
58     }
59     printf("----------------保存到文件----------------\n");
60 
61     // 输入保存文件名
62     printf("输入文件名:");
63     scanf("%s", saveName);
64 
65     // 写入文件
66     outFp = fopen(saveName, "w");
67     if (outFp == NULL) {
68         printf("文件创建失败!\n");
69         system("pause");
70         return 1;
71     }
72     for (i = 0; i < WIN_NUM; i++) {
73         fprintf(outFp, "%s\t%s\t%s\n", winStu[i].id, winStu[i].name, winStu[i].cls);
74     }
75     fclose(outFp);
76     printf("保存成功!\n");
77 
78     system("pause");
79     return 0;
80 }

运行截图

QQ20260622-233058

QQ20260622-233241

 

posted @ 2026-06-22 23:33  _Vulcan  阅读(11)  评论(0)    收藏  举报