实验七作业

任务四

源代码

 1 #include <ctype.h>
 2 #include <stdio.h>
 3 
 4 int main(void) {
 5     FILE *fp;
 6     int ch;
 7     int lines = 0;
 8     int chars = 0;
 9     int has_char_in_line = 0;
10 
11     fp = fopen("data4.txt", "r");
12     if (fp == NULL) {
13         printf("fail to open file\n");
14         return 1;
15     }
16 
17     while ((ch = fgetc(fp)) != EOF) {
18         if (ch == '\n') {
19             ++lines;
20             has_char_in_line = 0;
21         } else {
22             has_char_in_line = 1;
23         }
24 
25         if (!isspace((unsigned char)ch))
26             ++chars;
27     }
28 
29     if (has_char_in_line)
30         ++lines;
31 
32     fclose(fp);
33 
34     printf("文件data4.txt中共有%d行\n", lines);
35     printf("文件data4.txt中共有%d个非空白字符\n", chars);
36 
37     return 0;
38 }
View Code

截图

9f504092-051f-4366-9535-0b914d9ce3a5

任务五

源代码

  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 void read(STU st[], int n);
 16 void write(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(void) {
 21     STU stu[N], stu_pass[N];
 22     int cnt;
 23     double pass_rate;
 24 
 25     printf("从文件读入%d个考生信息: 已完成\n", N);
 26     read(stu, N);
 27 
 28     printf("\n对考生成绩进行统计: 已完成\n");
 29     cnt = process(stu, N, stu_pass);
 30 
 31     printf("\n所有考生完整信息:\n");
 32     output(stu, N);
 33 
 34     printf("\n通过考试的名单写入文件: 已完成!\n");
 35     write(stu_pass, cnt);
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
 39 
 40     return 0;
 41 }
 42 
 43 void output(STU st[], int n) {
 44     int i;
 45 
 46     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 47     for (i = 0; i < n; i++)
 48         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
 49                st[i].id, st[i].name, st[i].objective, st[i].subjective,
 50                st[i].sum, st[i].result);
 51 }
 52 
 53 void read(STU st[], int n) {
 54     int i;
 55     FILE *fin;
 56 
 57     fin = fopen("examinee.txt", "r");
 58     if (!fin) {
 59         printf("fail to open file\n");
 60         return;
 61     }
 62 
 63     for (i = 0; i < n; i++)
 64         fscanf(fin, "%ld %19s %f %f", &st[i].id, st[i].name,
 65                &st[i].objective, &st[i].subjective);
 66 
 67     fclose(fin);
 68 }
 69 
 70 int process(STU st[], int n, STU st_pass[]) {
 71     int i;
 72     int cnt = 0;
 73 
 74     for (i = 0; i < n; i++) {
 75         st[i].sum = st[i].objective + st[i].subjective;
 76 
 77         if (st[i].sum >= 60) {
 78             strcpy(st[i].result, "通过");
 79             st_pass[cnt] = st[i];
 80             ++cnt;
 81         } else {
 82             strcpy(st[i].result, "未通过");
 83         }
 84     }
 85 
 86     return cnt;
 87 }
 88 
 89 void write(STU st[], int n) {
 90     int i;
 91     FILE *fout;
 92 
 93     fout = fopen("list_pass.txt", "w");
 94     if (!fout) {
 95         printf("fail to open file\n");
 96         return;
 97     }
 98 
 99     fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
100     for (i = 0; i < n; i++)
101         fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",
102                 st[i].id, st[i].name, st[i].objective, st[i].subjective,
103                 st[i].sum, st[i].result);
104 
105     fclose(fout);
106 }
View Code

截图

5b8b0f89-da5a-4213-b24b-0edfc9fa8ac5

033368a0-2643-46d0-a86c-75e25e13685c

任务六

源代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <time.h>
  5 
  6 #define N 80
  7 #define PICK 5
  8 #define LEN 128
  9 
 10 typedef struct {
 11     long id;
 12     char name[32];
 13     char class_name[64];
 14 } Student;
 15 
 16 int read_students(Student st[], int maxn);
 17 void make_date_filename(char filename[], int size);
 18 void pick_students(int selected[], int n, int count);
 19 void sort_by_id(Student st[], int n);
 20 void output_students(FILE *fp, Student st[], int n);
 21 
 22 int main(int argc, char *argv[]) {
 23     Student students[N], winners[PICK];
 24     int selected[PICK];
 25     int n, i;
 26     char filename[LEN];
 27     FILE *fout;
 28 
 29     n = read_students(students, N);
 30     if (n < PICK) {
 31         printf("学生人数不足,无法抽取%d位中奖名单\n", PICK);
 32         return 1;
 33     }
 34 
 35     if (argc > 1) {
 36         strncpy(filename, argv[1], LEN - 1);
 37         filename[LEN - 1] = '\0';
 38     } else {
 39         printf("请输入保存中奖名单的文件名(直接回车则使用系统日期): ");
 40         if (fgets(filename, LEN, stdin) == NULL)
 41             filename[0] = '\0';
 42 
 43         filename[strcspn(filename, "\r\n")] = '\0';
 44         if (filename[0] == '\0')
 45             make_date_filename(filename, LEN);
 46     }
 47 
 48     srand((unsigned int)time(NULL) ^ (unsigned int)clock());
 49     pick_students(selected, n, PICK);
 50 
 51     for (i = 0; i < PICK; i++)
 52         winners[i] = students[selected[i]];
 53 
 54     sort_by_id(winners, PICK);
 55 
 56     fout = fopen(filename, "w");
 57     if (fout == NULL) {
 58         printf("fail to open file\n");
 59         return 1;
 60     }
 61 
 62     printf("中奖名单如下:\n");
 63     output_students(stdout, winners, PICK);
 64 
 65     output_students(fout, winners, PICK);
 66     fclose(fout);
 67 
 68     printf("中奖名单已写入文件: %s\n", filename);
 69 
 70     return 0;
 71 }
 72 
 73 int read_students(Student st[], int maxn) {
 74     int n = 0;
 75     FILE *fin;
 76 
 77     fin = fopen("list.txt", "r");
 78     if (fin == NULL) {
 79         printf("fail to open file\n");
 80         return 0;
 81     }
 82 
 83     while (n < maxn &&
 84            fscanf(fin, "%ld %31s %63s", &st[n].id, st[n].name,
 85                   st[n].class_name) == 3) {
 86         ++n;
 87     }
 88 
 89     fclose(fin);
 90     return n;
 91 }
 92 
 93 void make_date_filename(char filename[], int size) {
 94     time_t now;
 95     struct tm *local;
 96 
 97     now = time(NULL);
 98     local = localtime(&now);
 99     if (local == NULL || strftime(filename, (size_t)size, "%Y%m%d.txt", local) == 0)
100         strncpy(filename, "winners.txt", (size_t)size);
101 
102     filename[size - 1] = '\0';
103 }
104 
105 void pick_students(int selected[], int n, int count) {
106     int flag[N] = {0};
107     int i = 0;
108     int index;
109 
110     while (i < count) {
111         index = rand() % n;
112         if (!flag[index]) {
113             flag[index] = 1;
114             selected[i] = index;
115             ++i;
116         }
117     }
118 }
119 
120 void sort_by_id(Student st[], int n) {
121     int i, j;
122     Student temp;
123 
124     for (i = 0; i < n - 1; i++) {
125         for (j = 0; j < n - 1 - i; j++) {
126             if (st[j].id > st[j + 1].id) {
127                 temp = st[j];
128                 st[j] = st[j + 1];
129                 st[j + 1] = temp;
130             }
131         }
132     }
133 }
134 
135 void output_students(FILE *fp, Student st[], int n) {
136     int i;
137 
138     fprintf(fp, "学号\t\t姓名\t班级\n");
139     for (i = 0; i < n; i++)
140         fprintf(fp, "%ld\t%s\t%s\n", st[i].id, st[i].name, st[i].class_name);
141 }
View Code

截图

b6317f6c-758a-4e29-95e9-83e6ceb806a1

 

posted @ 2026-06-23 21:58  遇见离开  阅读(4)  评论(0)    收藏  举报