• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuist0415
博客园    首页    新随笔    联系   管理    订阅  订阅
实验七

task4 code

 1 #include<stdio.h>
 2 
 3 int main() {
 4     FILE* fp;
 5     long cnt = 0, c = 0;
 6     char ch;
 7 
 8     fp = fopen("data4.txt", "r");
 9     if (fp == NULL) {
10         printf("failed to open");
11         return 1;
12     }
13 
14     fseek(fp, 0L, SEEK_END);
15     cnt = ftell(fp);
16     fseek(fp, 0L, SEEK_SET);
17 
18     while (1) {
19         ch = fgetc(fp);
20         if (ch == '\n' || ch == ' ')
21             c++;
22         if (ch == EOF)
23             break;
24     }
25     cnt--;
26     printf("data4.txt中共包含字符数(除去空白):%ld", cnt - c);
27     fclose(fp);
28 
29     return 0;
30 }
View Code

 task5 code

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #define N 10
  5 
  6 typedef struct {
  7     long int id;
  8     char name[20];
  9     float objective; // 客观题得分
 10     float subjective; // 操作题得分
 11     float sum; // 总分
 12     char ans[10]; // 考试结果
 13 } STU;
 14 
 15 void finput(STU st[], int n);
 16 void foutput(STU st[], int n);
 17 void output(STU st[], int n);
 18 int process(STU st[], int n, STU st_pass[]);
 19 
 20 int main() {
 21     STU stu[N], stu_pass[N];
 22     int cnt;
 23     double pass_rate;
 24 
 25     printf("从文件读入%d个考生信息...\n", N);
 26     finput(stu, N);
 27 
 28     printf("\n对考生成绩进行统计...\n");
 29     cnt = process(stu, N, stu_pass);
 30 
 31     printf("\n通过考试的名单:\n");
 32     output(stu, N); // 输出到屏幕
 33     foutput(stu, N); // 输出到文件
 34 
 35     pass_rate = 1.0 * cnt / N;
 36     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
 37 
 38     return 0;
 39 }
 40 // 把通过考试的考生完整信息输出到屏幕上
 41 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 42 void output(STU st[], int n) {
 43     int i;
 44 
 45     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 46     for (i = 0; i < n; i++)
 47         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id,
 48             st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans);
 49 }
 50 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 51 void finput(STU st[], int n) {
 52     int i;
 53     FILE* fin;
 54 
 55     fin = fopen("examinee.txt", "r");
 56     if (fin == NULL) {
 57         printf("fail to open file\n");
 58         exit(0);
 59     }
 60 
 61     while (!feof(fin)) {
 62         for (i = 0; i < n; i++)
 63             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name,
 64                 &st[i].objective, &st[i].subjective);
 65     }
 66 
 67     fclose(fin);
 68 }
 69 // 把通过考试的考生完整信息写入文件list_pass.txt
 70 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 71 void foutput(STU s[], int n) {
 72     FILE* fout;
 73     int i;
 74 
 75     // 保存到文件
 76     fout = fopen("list_pass.txt", "w");
 77     if (!fout) {
 78         printf("fail to open or create list_pass.txt\n");
 79         exit(0);
 80     }
 81 
 82     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 83 
 84     for (i = 0; i < n; i++)
 85         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id,
 86             s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans);
 87 
 88     fclose(fout);
 89 }
 90 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 91 int process(STU st[], int n, STU st_pass[]) {
 92     int i, j = 0;
 93 
 94     for (i = 0; i < n; i++) {
 95         st[i].sum = st[i].objective + st[i].subjective;
 96         if (st[i].sum >= 60) {
 97             strcpy(st[i].ans, "pass");
 98             st_pass[j++] = st[i];
 99         }
100         else
101             strcpy(st[i].ans, "fail");
102     }
103 
104     return j;
105 }
View Code

task5 result

task6 code

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define M 0
 5 #define N 79
 6 
 7 typedef struct {
 8     long int id;
 9     char name[20];
10     char class[40];
11 }STU;
12 
13 void foutput(int ran[], STU stu[]);
14 
15 int main() {
16     FILE* fp;
17     STU stu[100];
18     int ran[5];
19     int i = 0, j = 0, k = 0;
20 
21     srand((unsigned int)time(NULL));
22     for (i = 0; i < 5; i++) {
23         ran[i] = rand() % (N - M + 1) + M;
24     }
25 
26     fp = fopen("list.txt", "r");
27     if (fp == NULL) {
28         printf("failed to open");
29         return 1;
30     }
31     i = 0;
32     while (!feof(fp)) {
33         fscanf(fp, "%ld %s %s", &stu[i].id, &stu[i].name, &stu[i].class);
34         i++;
35     }
36     i--;
37     for (j = 0; j < 5; j++, k++)
38         printf("%ld %s %s\n", stu[ran[k]].id, stu[ran[k]].name, stu[ran[k]].class);
39     foutput(ran, stu);
40     fclose(fp);
41 
42     return 0;
43 }
44 
45 void foutput(int ran[], STU stu[]) {
46     FILE* fout;
47     int i, k = 0;
48 
49     fout = fopen("lucky.txt", "w");
50     if (!fout) {
51         printf("fail to open or create list_pass.txt\n");
52         exit(0);
53     }
54 
55     for (i = 0; i < 5; i++,k++)
56         fprintf(fout, "%ld %s %s\n",stu[ran[k]].id, stu[ran[k]].name, stu[ran[k]].class);
57 
58     fclose(fout);
59 }
View Code

task6 result

 

posted on 2023-12-16 21:07  Haruto  阅读(15)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3