实验7

 任务4

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

任务5

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  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     system("pause");
 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,j=0;
 75  STU pass[N];
 76  FILE *fp;
 77  for(i=0;i<n;++i)
 78       s[i].sum=s[i].objective +s[i].subjective ;
 79      if(s[i].sum >=60){
 80         strcpy(s[i].result , "通过");
 81          pass[j]=s[i];
 82         
 83          j++;
 84      }
 85 fp=fopen("list_pass.txt","w");
 86 if(!fp){
 87     printf("fail to open");
 88     system("pause");
 89     return ;
 90 }
 91 for(i=0;i<j;i++)
 92     fprintf(fp,"%ld\t%s\t%.2lf\t%.2lf\t%.2lf\t %s\n",pass[i].id ,pass[i].name ,pass[i].objective ,pass[i].subjective ,pass[i].sum ,pass[i].result );
 93 fclose(fp);
 94 
 95 
 96 
 97 
 98 }
 99 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
100 int process(STU st[], int n, STU st_pass[]) {
101     int i,count=0,j=0;
102     FILE *fp;
103     for(i=0;i<n;++i){
104       st[i].sum=st[i].objective +st[i].subjective ;
105      if(st[i].sum >=60){
106         strcpy(st[i].result , "通过");
107          st_pass[j]=st[i];
108          j++;
109          count++;
110 
111      }
112      else 
113          strcpy(st[i].result , "不通过");
114     
115     }
116      return count;
117 
118 
119 }

 

 

 

 

 

任务6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<time.h>
 4 #define N 80
 5 #define M 5
 6 
 7 typedef struct{
 8     char number[N];
 9     char name[N];
10     char clas[N];
11 }table;
12 
13 int main(){
14     table info[N];
15     table winner[N];
16      FILE *fp;
17     char file[N];
18     int lucky,n,i;
19     srand(time(NULL));
20   
21     fp=fopen("list","r");
22     if(!fp){
23         printf("Fail to open the file");
24         return 0;
25     }
26     for(i=0;i<80;++i)
27     fscanf(fp,"%s %s %s",info[i].number,info[i].name,info[i].clas);
28     
29     fclose(fp);
30     
31     printf("------中奖名单-------\n");
32     for(i=0;i<M;++i){
33         
34         lucky=rand()%N;
35         winner[i]=info[lucky];
36         printf("%s %s %s\n",winner[i].number ,winner[i].name ,winner[i].clas );
37 
38     }
39 
40     
41     printf("保存至文件\n");
42     printf("输入文件名");
43     scanf("%s", file);
44     fp=fopen(file,"w");
45     if(!fp){
46         printf("Fail to open the file");
47         return 0;
48     }
49     for(i=0;i<M;++i)
50     fprintf(fp,"%s %s %s\n",winner[i].number,winner[i].name,winner[i].clas);
51 
52     fclose(fp);
53     printf("---保存成功---\n");
54     system ("pause");
55     return 0;
56 }

 

不知道为什么打不开文件

posted @ 2025-06-08 18:00  雾曦  阅读(30)  评论(0)    收藏  举报