实验7

任务4

源代码

 1 #include<stdio.h>
 2 
 3 int main(){
 4     FILE *fp;
 5     char ch;
 6     int line=0,count=0;
 7     
 8     fp=fopen("data4.txt","r");
 9     if(fp==NULL){
10         printf("文件打开失败!\n");
11         return 1;
12     }
13     
14     while ((ch=fgetc(fp))!=EOF){
15         if(ch=='\n')
16             line++;
17         if(ch!=' '&&ch!='\t'&&ch!='\n')
18             count++;            
19     }
20     line++; 
21     fclose(fp);
22     
23     printf("data4.txt统计结果:\n");
24     printf("行数:%d\n",line);
25     printf("字符数(不计空白符):%d\n",count);
26     
27     return 0;
28 }
View Code

运行结果截图

屏幕截图 2026-06-18 200635

任务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 
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", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
49 }
50 
51 void read(STU st[], int n) {
52     int i;
53     FILE *fin;
54 
55     fin = fopen("examinee.txt", "r");
56     if (!fin) {
57         printf("fail to open file\n");
58         return;
59     }
60 
61     for (i = 0; i < n; i++)
62         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
63 
64     fclose(fin);
65 }
66 
67 int process(STU st[], int n, STU st_pass[]) {
68     int i,num=0;
69     for(i=0;i<n;i++){
70         st[i].sum=st[i].objective+st[i].subjective;
71         if(st[i].sum>60){
72             strcpy(st[i].result,"通过");
73             st_pass[num]=st[i];
74             num++;
75         }else
76                 strcpy(st[i].result,"不通过");
77     }
78     return num;
79 }
80 
81 void write(STU st[], int n) {
82     int i;
83     FILE *fout;
84     fout=fopen("list_pass.txt","w");
85     fprintf(fout,"准考证号\t姓名\t客观题\t操作题\t总分\t考试结果\n");
86     for(i=0;i<n;i++)
87         fprintf(fout,"%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result);
88     fclose(fout);    
89 }
View Code

运行结果截图

屏幕截图 2026-06-19 100747

屏幕截图 2026-06-19 100853

任务6

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 typedef struct{
 7     char id[15];
 8     char name[15];
 9     char cls[25];
10 }Student;
11 
12 int main(void)
13 {
14     FILE *fp;
15     Student stu[85];
16     Student win[5];
17     int flag[85]={0};
18     int n=0,i,j,idx,temp;
19     char filename[30];
20 
21     fp=fopen("list.txt","r");
22     if(fp==NULL){
23         printf("无法打开list.txt!\n");
24         system("pause");
25         return 1;
26     }
27     while(fscanf(fp,"%s %s %s",stu[n].id,stu[n].name,stu[n].cls)!=EOF){
28         n++;
29     }
30     fclose(fp);
31     srand((unsigned)time(NULL));
32     for(i=0;i<5;i++){
33         while(1){
34             idx=rand()%n;
35             if(flag[idx]==0){
36                 flag[idx]=1;
37                 win[i]=stu[idx];
38                 break;
39             }
40         }
41     }
42 
43     for(i=0;i<4;i++){
44         for(j=0;j<4-i;j++){
45             if(strcmp(win[j].id,win[j+1].id)>0){
46                 Student t=win[j];
47                 win[j]=win[j+1];
48                 win[j+1]=t;
49             }
50         }
51     }
52 
53     time_t now=time(NULL);
54     struct tm *t=localtime(&now);
55     strftime(filename,sizeof(filename),"%Y%m%d.txt",t);
56     printf("---------%s中奖名单---------\n",filename);
57     for(i=0;i<5;i++){
58         printf("%s\t%s\t%s\n",win[i].id,win[i].name,win[i].cls);
59     }
60 
61     fp=fopen(filename,"w");
62     for(i=0;i<5;i++){
63         fprintf(fp,"%s\t%s\t%s\n",win[i].id,win[i].name,win[i].cls);
64     }
65     fclose(fp);
66     printf("----------------------------\n");
67     printf("保存到文件:%s\n保存成功!\n",filename);
68 
69     system("pause");
70     return 0;
71 }
View Code

运行结果截图

屏幕截图 2026-06-19 104729

屏幕截图 2026-06-19 104843

 

posted @ 2026-06-19 10:50  thinkbout  阅读(5)  评论(0)    收藏  举报