实验7

任务四

 1 #include<stdio.h>
 2 #define N 100
 3 
 4 int main(){
 5     FILE *fp;
 6     char a[N];
 7     int i=0,n,m=0,k=0;
 8     
 9     fp=fopen("data4.txt","r");
10     if(fp==NULL){
11         printf("无法打开文件");
12         return 1;
13     }
14     
15     while((a[i]=fgetc(fp))!=EOF){
16         
17         i++;
18     } 
19     n=i;
20     
21     
22     for(i=0;i<n;++i){
23         if(a[i]=='\n')
24             m++;
25         if(a[i]==' ')
26             k++;
27     }
28     
29     n=n-m-k;
30     printf("data4.txt统计结果:\n");
31     printf("行数: %d\n",m+1);
32     printf("字符数(不计空白符):%d\n",n);
33     
34     fclose(fp);
35     return 0; 
36     
37 } 
View Code

屏幕截图 2025-12-27 155457

 

任务五

  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 st[], int n) {
 72     FILE *fp;
 73     int i;
 74     fp=fopen("list_pass.txt","w");
 75     
 76     fprintf(fp,"准考证号\t姓名  \t客观题得分\t主观题得分\t操作题得分\t总分结果\n");
 77     for(i=0;i<n;i++){
 78         fprintf(fp,"%ld\t%s   \t%.2lf          \t%.2lf       \t%.2lf       \t%s\n",st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result);
 79     
 80     }
 81     
 82      
 83 }
 84 
 85 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 86 int process(STU st[], int n, STU st_pass[]) {
 87     int i,cnt=0,j=0;
 88     
 89     for(i=0;i<n;i++){
 90         st[i].sum=st[i].objective+st[i].subjective;
 91     
 92         if(st[i].sum>=60){
 93         
 94             strcpy(st[i].result,"及格") ;
 95             st_pass[j++]=st[i];
 96             cnt++; 
 97         }
 98         else
 99             strcpy(st[i].result,"不及格");
100         
101     } 
102     
103     return cnt;
104    
105 }
View Code

屏幕截图 2025-12-27 165350

屏幕截图 2025-12-27 170645

 

任务六

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

屏幕截图 2025-12-27 140634

屏幕截图 2025-12-27 140655

 

posted @ 2025-12-27 17:10  仇昕泽  阅读(4)  评论(0)    收藏  举报