实验七

4.
 1 #include <stdio.h>
 2 int main(){
 3 char ch;
 4 int line=0,num=0;
 5 FILE *fp;
 6 fp=fopen("data4.txt","r");
 7 if(!fp){
 8     printf("1");
 9     return 0;
10 }
11  while(!feof(fp)){
12      ch=fgetc(fp);
13      if(ch=='\n'){
14          line++;
15      }
16      else if(ch==' '||ch=='\t'){
17          continue;
18      }
19      else{
20          num++;
21      }
22  }
23 fclose(fp);
24 printf("%d\n%d",line+1,num-1);
25 }

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 // 函数声明
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     while (!feof(fin)) {
64         for (i = 0; i < n; i++)
65             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
66     }
67 
68     fclose(fin);
69 }
70 
71 // 把通过考试的考生完整信息写入文件list_pass.txt
72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
73 void write(STU s[], int n) {
74     int i;
75     FILE *fp;
76     fp=fopen("list_pass.txt","w");
77     for(i=0;i<n;i++){
78         fprintf(fp,"%ld %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].result);
79     }
80     fclose(fp);}
81 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
82 int process(STU st[], int n, STU st_pass[]) {
83     int i,j,num;
84     for(i=0,j=0;i<n;i++){
85         st[i].sum=st[i].objective+st[i].subjective;
86         if(st[i].sum>60){
87             st_pass[j]=st[i];
88             strcpy(st[i].result,"通过");
89             j++;
90             num++;
91         }
92         else{
93             strcpy(st[i].result,"不通过"); 
94         }
95     }
96     return num;
97 }

6.

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct{
 4     int num;
 5     char name[20];
 6     char ban[100];
 7 }stu;
 8 int main(){
 9     stu x[80],y[5];
10     char name1[20];
11     int i,n,j,m=5,z=0,num[5]={-1,-1,-1,-1,-1};
12     FILE *fp;
13     fp=fopen("list.txt","r");
14     while(!feof(fp)){
15         for(i=0;i<80;i++){
16             fscanf(fp,"%d %s %s",&x[i].num,x[i].name,x[i].ban);
17         }
18     }
19     for(i=0;i<m;i++){
20         srand((unsigned int)time(NULL));
21         n=rand()%204942001;
22         for(j=0;j<n;j++){
23             if(n==a[j]){
24                 m++;
25                 continue;
26             }
27         }
28         printf("%d %s %s\n",x[n].num,x[n].name,x[n].ban)
29         y[z++]=x[n];
30         a[i]=n;
31     }
32     FILE *fin;
33     scanf("%s",name1);
34     fin=fopen(name1,"w")
35     for(i=0;i<5;i++){
36         fprintf(fin,"%d %s %s\n",y[i].num,y[i].name,y[i].ban)
37     }
38     fclose(fin);
39 }

 

posted @ 2024-12-30 09:39  马子浩  阅读(13)  评论(0)    收藏  举报