实验七

任务4

 1 #include <stdio.h> 
 2 
 3 int main() {
 4     FILE *fp;
 5     int line_count = 0;   
 6     int char_count = 0;   
 7     int ch;              
 8 
 9     fp = fopen("data4.txt", "r");
10     if (fp == NULL) {
11         perror("文件打开失败");
12         return 1;
13     }
14 
15 
16     while ((ch = fgetc(fp)) != EOF) {
17         if (ch == '\n') {
18             line_count++;
19         }
20 
21 
22         if (!(ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r' || ch == '\v' || ch == '\f')) {
23             char_count++; 
24         }
25     }
26 
27     if (line_count > 0 || char_count > 0) {
28         line_count++;
29     }
30 
31 
32     fclose(fp);
33 
34 
35     printf("data4.txt统计结果:\n");
36     printf("行数:%d\n", line_count);
37     printf("字符数(不计空白符):%d\n", char_count);
38 
39     return 0;
40 }

屏幕截图 2025-12-29 151036

 

任务5

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

屏幕截图 2025-12-29 210948

 

任务6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 100
 5 #define m 5
 6 int main(){
 7     char s[N][N];
 8     FILE *fp;
 9     int i,n,lucky_i;                                       
10     char filename[100];
11     int lucky_index[m];
12     int j, is_repeated; 
13 
14     fp=fopen("list.txt","r");
15     if(!fp){
16         perror("list.txt");
17         return 1;
18     }
19     i=0;
20     while(fgets(s[i],N,fp)!=NULL)
21     ++i;
22     n=i;
23    fclose(fp);
24 
25     srand(time(0));
26     for(i=0;i<m;++i){
27         do{
28     is_repeated= 0; 
29     lucky_i = rand()%n; 
30 
31     for(j=0; j<i; j++){
32         if(lucky_index[j] == lucky_i){
33             is_repeated = 1; 
34             break;
35         }
36     }
37 }while(is_repeated);
38 
39 lucky_index[i] = lucky_i; 
40 printf("%s",s[lucky_i]);
41     }
42 
43     scanf("%s",filename);
44     FILE *p=fopen(filename,"w");
45     for(i = 0; i < m; i++){
46         lucky_i = lucky_index[i];
47         fprintf(p, "%s", s[lucky_i]);
48     }
49     fclose(p);
50     
51     return 0;
52 }

屏幕截图 2025-12-26 164815

屏幕截图 2025-12-26 164852

 

posted @ 2025-12-29 21:10  郭语涵  阅读(0)  评论(0)    收藏  举报