实验7

task4

 1 #include<stdio.h>
 2 
 3 void read();
 4 
 5 int main()
 6 {
 7     read();
 8     
 9     return 0;
10 }
11 
12 void read()
13 {
14     FILE *fp;
15     int len=0,num=0,a;
16     
17     fp=fopen("data4.txt","r");
18     if(!fp)
19     {
20         printf("fail\n");
21         return;
22     }
23     
24     while(!feof(fp))
25     {
26         a=fgetc(fp);
27         if(a==EOF)
28         {
29             break;
30         }
31         if(a!='\n'&&a!=' '&&a!='\t')
32         {
33             len++;
34         }
35         
36         if(a=='\n')
37         {
38             num++;
39         }
40     }
41     if(a!='\n'&&num>0)
42     {
43         num++;
44     }
45     
46     fclose(fp);
47     
48     printf("data4.txt统计结果:\n");
49     printf("行数:%20d\n",num);
50     printf("字符数:%19d\n",len); 
51 }

 

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

 

task6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 80
 5 typedef struct{
 6     char num[20];
 7     char name[20];
 8     char classnum[50];
 9     int tmp;
10 }STU;
11 void chou(STU st[N],STU st_chou[N],int n);
12 void output(STU st_chou[N]);
13 void write(STU t_chou[N]);
14 void read(STU st[N],int n);
15 int main(){
16     STU st[N],st_chou[N]; int i;
17     for(i=1;i<=N;i++)
18     st[i].tmp=i;
19     read(st,N);
20     chou( st,st_chou,N);
21         output(st_chou);
22     write( st_chou);
23 }
24 
25 void read(STU st[],int n){
26     FILE *fp; int i;
27     fp=fopen("list.txt","r");
28     if(!fp){
29         printf("error");
30         return;
31     }
32     while(!feof(fp)){
33     for(i=1;i<=n;i++)
34     fscanf(fp,"%s %s %s",st[i].num,st[i].name,st[i].classnum);}
35     fclose(fp);
36 }
37 
38 void chou(STU st[N],STU st_chou[],int n){
39     int i;
40     int x[N]={0}; int j=1,z=1,g=1; 
41     srand((unsigned)time(NULL));
42     for(i=1;i<=5;i++)
43     {
44     x[i]=rand()%80+1; 
45      for(j=1;j<i;j++)
46       { if(x[i]==x[j]) 
47              i--;
48       }
49     }
50     for(i=1;i<=n;i++)
51      for(z=1;z<=5;z++)
52     if(st[i].tmp==x[z])
53     st_chou[g++]=st[i];
54 }
55 
56 void output(STU st_chou[N]) {
57     int i;
58     printf("--------------随机抽点名单--------------\n");
59     for(i=1;i<=5;i++)
60     printf("%s      %s %s\n",st_chou[i].num,st_chou[i].name,st_chou[i].classnum);
61     printf("--------------保存到文件------------\n");}
62 
63 void write(STU st_chou[N]){
64     int i;  char x[N];
65     printf("输入文件名:");
66      gets(x);
67     FILE *fp;
68     fp=fopen(x,"w");
69     if(!fp)
70     {  
71         printf("error");
72         return;
73     }
74     for(i=1;i<=5;i++)
75     fprintf(fp,"%s   %s    %s\n",st_chou[i].num,st_chou[i].name,st_chou[i].classnum);
76     fclose(fp);    
77     printf("保存成功!"); 
78 }

 

posted @ 2024-12-26 11:33  yueTO  阅读(14)  评论(0)    收藏  举报