实验七

task 4

源代码:

 1 #include<stdio.h>
 2 
 3 int main(){
 4     FILE *fp;
 5     fp = fopen("C:\\Users\\jr787\\Desktop\\实验7数据文件及部分代码_gbk\\data4.txt","r");
 6     if(fp == NULL){
 7         printf("fail to open file to write\n");
 8         return;
 9     }
10     
11     int row = 0,total = 0;
12     char ch;
13     while((ch = fgetc(fp)) != EOF){
14         if(ch != ' ' && ch != '\n' && ch != '\t')
15             total++;
16         if(ch == '\n')
17             row++;
18     }
19     row++;
20     fclose(fp);
21     printf("data4.txt统计结果:\n");
22     printf("行数:%d\n",row);
23     printf("字符数(不计空白符):%d\n",total);
24     return 0;
25 }

运行结果:
屏幕截图 2025-12-30 215028

task 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 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() {
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     write(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 void output(STU st[], int n) {
42     int i;
43     
44     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
45     for (i = 0; i < n; i++)
46         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);
47 }
48 
49 
50 void read(STU st[], int n) {
51     int i;
52     FILE *fin;
53 
54     fin = fopen("examinee.txt", "r");
55     if (!fin) {
56         printf("fail to open file\n");
57         return;
58     }
59 
60     for (i = 0; i < n; i++)
61         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
62 
63     fclose(fin);
64 }
65 
66 void write(STU st[],int n){
67     FILE *fp;
68     fp = fopen("list_past.txt","w");
69     if(fp == NULL){
70        perror("fail to open to write\n");
71        return;
72    }
73    
74     int i;
75     for(i = 0;i < n;++i){
76         fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",st[i].id,st[i].name,
77         st[i].objective,st[i].subjective,st[i].sum,st[i].result);
78     }
79    
80    fclose(fp);
81 }
82 
83 int process(STU st[],int n,STU stpass[]){
84     int cnt=0,i;
85     for(i = 0;i < n;++i){
86         st[i].sum = st[i].objective+st[i].subjective;
87        if(st[i].sum >= 60){
88            strcpy(st[i].result,"通过");
89             stpass[cnt++]=st[i];
90        }
91        else
92        strcpy(st[i].result,"未通过");
93    }
94    return cnt;
95 }

运行结果:

屏幕截图 2025-12-30 221153

屏幕截图 2025-12-30 225605

task 6

源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<string.h>
 5 #define N 90
 6 #define M 5
 7 
 8 int main(){
 9     char s[N][N];
10     char filename[80];
11     char hit[N][N];
12     FILE *fp,*fout;
13     int has_hit[N] = {0};
14     int i,n;
15     int lucky_i;
16     
17     fp = fopen("C:\\Users\\jr787\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt","r");
18     if(!fp){
19         perror("list.txt");
20         return 1;
21     }
22     
23     i = 0;
24     while(i < N && (fgets(s[i],N,fp)!= NULL)){
25         printf("%s",s[i]);
26         ++i; 
27     }
28     n = i;
29     
30     printf("\n------------中奖名单------------\n");
31     srand(time(0));
32     
33     for(i = 0;i < M;i++){
34         lucky_i = rand () % N ;
35         
36         if(has_hit[lucky_i])
37             continue;
38         
39         has_hit[lucky_i] = 1;
40         strcpy(hit[i],s[lucky_i]);
41         printf("%s",s[lucky_i]);
42     }
43     
44     printf("\n------------保存到文件------------\n");
45     printf("输入文件名:") ;
46     gets(filename);
47     fout = fopen(filename,"w");
48     if(!fout){
49         perror(filename);
50         return 1;
51     }
52     
53     for(i =0;i < M;++i)
54         fprintf(fout,"%s\n",hit[i]);
55     
56     printf("保存成功!");
57     
58     fclose(fout);
59     fclose(fp);
60     return 0; 
61 
62 }

运行结果:

屏幕截图 2025-12-30 224811

屏幕截图 2025-12-30 224857

 



posted @ 2025-12-30 22:58  追随天光  阅读(2)  评论(0)    收藏  举报