实验七

task4

 1 #include<stdio.h>
 2 #include<ctype.h>
 3 
 4 int main(){
 5     int line=0,chars=0,ch;
 6     FILE *fp;
 7     
 8     fp=fopen("C:\\Users\\ZhuanZ(无密码)\\Desktop\\实验7数据文件及部分代码_gbk\\data4.txt","r");
 9     printf("data4.txt统计结果:\n");
10     if(!fp){
11         perror("data4.txt");
12         return 1;
13     }
14     while((ch=fgetc(fp))!=EOF){
15         if(!isspace(ch)){
16             chars++;
17         }
18     if(ch=='\n'){
19         line++;
20     }
21 }
22     ch=fgetc(fp);
23     if(ch!='\n')
24     line++;
25     
26     fclose(fp);
27     printf("字符数:%d\n",chars);
28     printf("行数:%d",line);
29     return 0;
30 }

image

 ```

task5

 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 void read(STU st[], int n) {
50     int i;
51     FILE *fin;
52 
53     fin = fopen("C:\\Users\\ZhuanZ(无密码)\\Desktop\\实验7数据文件及部分代码_gbk\\examinee.txt", "r");
54     if (!fin) {
55         printf("fail to open file\n");
56         return;
57     }
58 
59     for (i = 0; i < n; i++)
60         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
61 
62     fclose(fin);
63 }
64 
65 void write(STU st[], int n) {
66     int i,j;
67     FILE *fp;
68     
69    fp=fopen("C:\\Users\\ZhuanZ(无密码)\\Desktop\\实验7数据文件及部分代码_gbk\\list_pass.txt","w");
70     
71     if(fp==NULL){
72         printf("fail to open file to write\n");
73         return;
74     }
75     for(i=0;i<n;++i)
76     if(st[i].result=="通过"){
77         st[j]=st[i];
78         j++; 
79 }
80     for(i=0;i<n;++i)
81     fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[j].id, st[j].name, st[j].objective, st[j].subjective,st[j].sum, st[j].result);
82      fclose(fp);
83 }
84 
85 int process(STU st[], int n, STU st_pass[]) {
86     int i,count=0;
87     for(i=0;i<n;i++){
88         st[i].sum=st[i].objective+st[i].subjective;
89         if(st[i].sum>=60){
90             st_pass[count]=st[i];
91             count++;
92             strcpy(st[i].result,"通过");
93         }
94         else
95         strcpy(st[i].result,"未通过");
96     }
97     return count;
98 }
image

image

```

task6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 100
 5 
 6 int main()
 7 {
 8     char s[N][N];
 9     FILE *fp,*fp_out;
10     int i,n;
11     
12     fp=fopen("C:\\Users\\ZhuanZ(无密码)\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt","r");
13     if(!fp){
14         perror("list.txt");
15         return 1;
16     }
17     i=0;
18     while(fgets(s[i],N,fp)!=NULL)
19     ++i;
20     n=i;
21     
22     char filename[N];
23     printf("输入文件名:");
24     scanf("%s",&filename);
25     fp_out=fopen(filename,"w");
26     if(!fp_out){
27         perror(filename);
28         return 1;
29     }
30     
31     int t;
32     int flag[N]={0};
33     srand(time(NULL));
34     for(i=0;i<5;++i){
35     do{
36     t=rand()%81;
37 }
38     while(flag[t]==1);
39         flag[t]=1;
40     printf("%s",s[t]);
41     fprintf(fp_out,"%s",s[t]);
42 
43 }
44     fclose(fp_out);
45     return 0;
46     
47 }

image

image

 

posted @ 2025-12-25 21:29  李佳颖  阅读(1)  评论(0)    收藏  举报