实验七

任务4:

 1 #include <stdio.h>
 2 #define N 10
 3 #define M 100
 4 
 5 int main(){
 6     char t,x[N][M];
 7     int i,j,line=0,s=0;
 8     FILE *fp;
 9     
10     fp=fopen("data4.txt","r");
11     if(fp==NULL){
12         printf("fail to open file to read\n");
13         return;
14     }
15     
16     for(i=0;i<N;i++){
17         if(fgets(x[i],M,fp)!=NULL)
18            line++;
19     }
20     
21     for(i=0;i<line;i++){
22         j = 0;
23         while(x[i][j]!='\0'){
24             t=x[i][j];
25             
26             if(t!=' '&&t!='\n'&&t!='\r')
27                s++;
28                
29             j++;
30         }
31     }
32     
33     fclose(fp);
34     
35     printf("data4.txt统计结果:\n");
36     printf("行数:%20d\n",line);
37     printf("字符数(不计空白符):%5d",s);
38     
39     return 0;
40 } 

任务5:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #define N 10
  4 
  5 typedef struct {
  6     long id;
  7     char name[20];
  8     float objective;
  9     float subjective;
 10     float sum;
 11     char result[10]; 
 12 } STU;
 13 
 14 void read(STU st[],int n);
 15 void write(STU st[],int n);
 16 void output(STU st[],int n);
 17 int process(STU st[],int n,STU st_pass[]);
 18 
 19 int main(){
 20     STU stu[N],stu_pass[N];
 21     int cnt;
 22     double pass_rate;
 23     
 24     printf("从文件读入%d个考生信息...\n",N);
 25     read(stu,N);
 26     
 27     printf("\n对考生成绩进行统计...\n");
 28     cnt=process(stu,N,stu_pass);
 29     
 30     printf("\n通过考试的名单:\n");
 31     output(stu,N);
 32     write(stu,N);
 33     
 34     pass_rate=1.0*cnt/N;
 35     printf("\n本次等级考试通过率:%.2f%%\n",pass_rate*100);
 36     
 37     return 0;
 38 }
 39 
 40 void output(STU st[],int n){
 41     int i;
 42     
 43     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 44     for(i = 0;i < n;i++)
 45         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",st[i].id,st[i].name,
 46         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("examinee.txt","r");
 54     if(!fin){
 55         printf("fail to open file\n");
 56         return;
 57     }
 58     
 59     while(!feof(fin)){
 60         for(i = 0;i < n;i++)
 61             fscanf(fin,"%ld %s %f %f",&st[i].id,st[i].name,
 62             &st[i].objective,&st[i].subjective);
 63     }
 64     
 65     fclose(fin);
 66 }
 67 
 68 void write(STU st[],int n){
 69     int i;
 70     FILE *fp;
 71     
 72     fp = fopen("list_pass.txt","w");
 73     
 74     if(!fp){
 75         printf("fail to open file\n");
 76         return;
 77     }
 78     
 79     fprintf(fp,"准考证号        姓名         客观题得分       操作题得分      总分      结果\n"); 
 80     for(i=0;i<n;i++){
 81         if(st[i].sum>=60){
 82          fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",st[i].id,st[i].name,
 83         st[i].objective,st[i].subjective,st[i].sum,st[i].result);
 84         } 
 85     }
 86     
 87     fclose(fp);
 88 }
 89 
 90 int process(STU st[],int n,STU st_pass[]){
 91     int i,j;
 92     int count=0;
 93     
 94     for(i = 0;i < n;i++){
 95         st[i].sum=st[i].objective+st[i].subjective;
 96         
 97         if(st[i].sum>=60){
 98             strcpy(st[i].result,"通过");
 99             st_pass[count++]=st[i]; 
100         }
101         else
102             strcpy(st[i].result,"未通过"); 
103     }
104     
105     return count;
106 }

                        

任务6:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 80
 5 
 6 typedef struct {
 7     char id[10];
 8     char name[20];
 9     char class[20];
10 }STU;
11 
12 int main(){
13     STU st[N];
14     int i;
15     FILE *fp1;
16     
17     fp1 = fopen("list.txt","r");
18     if(!fp1){
19         printf("fail to open file\n");
20         return;                                       
21     }
22     
23     while(!feof(fp1)){
24         for(i = 0;i < N;i++)
25         fscanf(fp1,"%s %s %s",st[i].id,st[i].name,st[i].class);
26     }
27     
28     fclose(fp1);
29     
30     int x[5];   
31     int j,k=0;
32     
33     srand(time(NULL));
34     for(i = 0;i < 5;i++){
35         x[i] = rand()%80 + 1;
36         for(j = 0;j < i;j++){
37             if(x[j]==x[i]){
38                 i--;
39                 continue;
40             }
41         }
42     }
43     
44     char t[N];
45     
46     printf("---------------随机抽点名单---------------\n");
47     for(i = 0;i < 5;i++)
48     printf("%-20s%-10s%-10s\n",st[x[i]].id,st[x[i]].name,st[x[i]].class);
49     printf("----------保存到文件----------\n");
50     printf("输入文件名:");
51     scanf("%s",t);
52     FILE *fp2;
53     
54     fp2 = fopen(t,"w");
55     if(!fp2){
56         printf("fail to open file\n");
57         return;
58     }
59     
60     for(i = 0;i < 5;i++)
61         fprintf(fp2,"%-20s%-10s%-10s\n",st[x[i]].id,st[x[i]].name,st[x[i]].class);
62         
63     fclose(fp2);
64     
65     printf("保存成功!");
66     return 0;
67 }

                

 

posted @ 2024-12-30 08:11  hydrangea1123  阅读(11)  评论(0)    收藏  举报