实验七

实  验  4
 1 #define  _CRT_SECURE_NO_WARNINGS
 2  #include <stdio.h>
 3 int main()
 4 {
 5     FILE* fp;
 6     fp = fopen("data4.txt", "r");
 7     if (!fp)
 8     {
 9         printf("fail to open\n");
10         return 0;
11 
12     }
13     int line = 1;
14     int count = 0;
15     int a;
16     while ((a = fgetc(fp)) != EOF)
17     {
18         if (a == '\n')
19             line++;
20         else if (a != ' ' && a != '\t')
21             count++;
22 
23     }
24     fclose(fp);
25     printf("data4.txt统计结果:\n");
26     printf("行数:%d\n", line);
27     printf("字符数:%d\n", count);
28     return 0;
29 }

image

 实  验  5

  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     write(stu, N);    // 输出考试通过的考生信息到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 39 
 40     return 0;
 41 }
 42 
 43 // 把所有考生完整信息输出到屏幕上
 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 45 void output(STU st[], int n) {
 46     int i;
 47     
 48     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 49     for (i = 0; i < n; i++)
 50         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);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void read(STU st[], int n) {
 55     int i;
 56     FILE *fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 62     }
 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 
 67     fclose(fin);
 68 }
 69 
 70 // 把通过考试的考生完整信息写入文件list_pass.txt
 71 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 72 void write(STU s[], int n)
 73 {
 74     FILE* fin;
 75     fin = fopen("C:\\Users\\szh07\\Desktop\\list_pass.txt", "w");
 76     if (fin == NULL)
 77         return -1;
 78     int i = 0;
 79     fprintf(fin, "准考证号\t姓名\t客观题分\t操作题得分\t总分\t结果\n");
 80     for (i = 0; i < n; i++)
 81     {
 82 
 83         fprintf(fin, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 84     }
 85     fclose(fin);
 86 }
 87 
 88 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 89 int process(STU st[], int n, STU st_pass[])
 90 {
 91     int i = 0, count = 0;
 92     for (i = 0; i < n; i++)
 93     {
 94         st[i].sum = st[i].objective + st[i].subjective;
 95         if (st[i].sum >= 60.0)
 96         {
 97             strcpy(st[i].result, "通过");
 98             st_pass[count++] = st[i];
 99         }
100         else
101         {
102             strcpy(st[i].result, "不通过");
103         }
104     }
105     return count;
106 }

image

 实  验  6

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <time.h>
  6 #include <windows.h>
  7 
  8 #define N 80
  9 #define M 5
 10 #define L 100
 11 
 12 typedef struct {
 13     long id;
 14     char name[20];
 15     char c[30];
 16 } S;
 17 
 18 int cmp(const void* a, const void* b) {
 19     S* sA = (S*)a;
 20     S* sB = (S*)b;
 21     return (sA->id - sB->id);
 22 }
 23 
 24 int read(S a[]) {
 25     FILE* f = fopen("C:\\Users\\szh07\\Desktop\\list.txt", "r");
 26     if (!f) {
 27         printf("Cannot open list.txt!\n");
 28         return -1;
 29     }
 30 
 31     int i = 0;
 32     char l[L];
 33     while (fgets(l, L, f) && i < N) {
 34         sscanf(l, "%ld %s %[^\n]", &a[i].id, a[i].name, a[i].c);
 35         i++;
 36     }
 37     fclose(f);
 38     return i;
 39 }
 40 
 41 void rand_sel(S a[], int b, S c[]) {
 42     int f[N] = { 0 };
 43     int i = 0;
 44     srand((unsigned int)time(NULL));
 45 
 46     while (i < M) {
 47         int d = rand() % b;
 48         if (!f[d]) {
 49             c[i++] = a[d];
 50             f[d] = 1;
 51         }
 52     }
 53 }
 54 
 55 void get_fn(char a[]) {
 56     time_t t = time(NULL);
 57     struct tm* tm = localtime(&t);
 58     strftime(a, 20, "%Y%m%d.txt", tm);
 59 }
 60 
 61 void out(S a[]) {
 62     qsort(a, M, sizeof(S), cmp);
 63 
 64     char fn[20];
 65     get_fn(fn);
 66 
 67     printf("----------%sWinners----------\n", fn);
 68     for (int i = 0; i < M; i++) {
 69         printf("%ld\t%s\t%s\n", a[i].id, a[i].name, a[i].c);
 70     }
 71 
 72     FILE* f = fopen(fn, "w");
 73     if (!f) {
 74         printf("File write failed!\n");
 75         return;
 76     }
 77     for (int i = 0; i < M; i++) {
 78         fprintf(f, "%ld\t%s\t%s\n", a[i].id, a[i].name, a[i].c);
 79     }
 80     fclose(f);
 81     printf("File saved successfully!\n");
 82 }
 83 
 84 int main() {
 85     SetConsoleOutputCP(CP_UTF8);
 86     SetConsoleCP(CP_UTF8);
 87 
 88     S s[N];
 89     S w[M];
 90     int t = read(s);
 91     if (t == -1) return 1;
 92     if (t < M) {
 93         printf("Not enough students!\n");
 94         return 1;
 95     }
 96 
 97     rand_sel(s, t, w);
 98     out(w);
 99 
100     return 0;
101 }

image

 

posted @ 2025-12-31 14:24  Groundc  阅读(6)  评论(0)    收藏  举报