实验7

实验任务4:

程序源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int i;
 6     char ch;
 7     int sum=0,line=0;
 8     FILE *fp;
 9     fp=fopen("F:\\data4.txt","r");
10     if(!fp)
11     {
12     printf("fail to open");
13         return 1;
14     }
15     while((ch=fgetc(fp))!=EOF)
16     {
17         if(ch=='\n')
18             line++;
19         else if(ch!=' '&&ch!='\t')
20         sum++;
21     }
22     line++;
23     printf("data4.txt统计结果:\n");
24     printf("行数:\t\t%d\n",line);
25     printf("字符数:\t\t%d\n",sum);
26 
27     fclose(fp);
28     system("pause");
29     return 0;
30 }
View Code

 

运行结果截图:

实验任务5:

程序源代码:

  1 #include <stdio.h>
  2 #include<stdlib.h>
  3 #include <string.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long id;            // 准考证号
  9     char name[20];      // 姓名
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char result[10];    // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void read(STU st[], int n);
 18 void write(STU st[], int n);
 19 void output(STU st[], int n);
 20 int process(STU st[], int n, STU st_pass[]);
 21 
 22 int main() {
 23     STU stu[N], stu_pass[N];
 24     int cnt;
 25     double pass_rate;
 26 
 27     printf("从文件读入%d个考生信息...\n", N);
 28     read(stu, N);
 29 
 30     printf("\n对考生成绩进行统计...\n");
 31     cnt = process(stu, N, stu_pass);
 32 
 33     printf("\n通过考试的名单:\n");
 34     output(stu, N);   // 输出所有考生完整信息到屏幕
 35     write(stu, N);    // 输出考试通过的考生信息到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 39 
 40     system("pause");
 41     return 0;
 42 }
 43 
 44 // 把所有考生完整信息输出到屏幕上
 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 46 void output(STU st[], int n) {
 47     int i;
 48     
 49     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 50     for (i = 0; i < n; i++)
 51         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);
 52 }
 53 
 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 55 void read(STU st[], int n) {
 56     int i;
 57     FILE *fin;
 58 
 59     fin = fopen("F:\\examinee.txt", "r");
 60     if (!fin) {
 61         printf("fail to open file\n");
 62         return;
 63     }
 64 
 65     while (!feof(fin)) {
 66         for (i = 0; i < n; i++)
 67             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 68     }
 69 
 70     fclose(fin);
 71 }
 72 
 73 // 把通过考试的考生完整信息写入文件list_pass.txt
 74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 75 void write(STU s[], int n) {
 76  int i,j=0;
 77  FILE *f;
 78  STU a[N];
 79  for(i=0;i<N;i++)
 80  {
 81  if(s[i].sum>=60)
 82  {
 83  a[j].id=s[i].id;
 84  strcpy(a[j].name,s[i].name);
 85  a[j].objective=s[i].objective;
 86  a[j].subjective=s[i].subjective;
 87  a[j].sum=s[i].sum;
 88  strcpy(a[j].result,s[i].result);
 89  j++;
 90  }
 91  }
 92  f=fopen("list_pass.txt","w");
 93  if(!f)
 94  {
 95  printf("fail to open");
 96  return ;
 97  }
 98  fprintf(f,"准考证号     姓名     客观题得分            操作题得分        总分         结果\n");
 99  for(i=0;i<j;i++)
100  {
101      
102  fprintf(f,"%d        %s             %.2f              %.2f            %.2f          %s\n",
103      a[i].id,a[i].name,a[i].objective,a[i].subjective,a[i].sum,a[i].result);
104  }
105 
106 
107     
108 
109      fclose(f);
110 }
111 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
112 int process(STU st[], int n, STU st_pass[]) {
113     int i,num=0;
114     for(i=0;i<N;i++)
115     {
116     st[i].sum=st[i].objective+st[i].subjective;
117     }
118     for(i=0;i<N;i++)
119     {
120     if(st[i].sum>=60)
121     {
122     strcpy(st[i].result,"通过");
123     num++;
124     }
125     
126     if(st[i].sum<60){
127         strcpy(st[i].result,"不通过");}
128     }
129     return num;
130     }
View Code

 

运行结果截图:

 

 

 

实验任务6:

程序源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 100
 5 
 6 typedef struct{
 7 char info[N][N];
 8 int flag;
 9 }award;
10 
11 
12 int main()
13 {
14     FILE *fp,*f;
15     int ch,i;
16     char name[N];
17     fp=fopen("F:\\list.txt","r");
18         if(!fp){
19         printf("fail to open\n");
20         return 1;
21         }
22     award x[N];
23     for(i=0;i<N;i++)
24         {
25             fgets(x[i].info[i],80,fp);
26             x[i].flag=0;
27     }
28     printf("-------------中奖名单-------------\n");
29     srand((unsigned)time(NULL));
30     for(i=0;i<5;)
31     {
32         ch=rand()%80;
33         if(x[i].flag==0)
34         {
35             printf("%s",x[ch].info[ch]);
36             x[i].flag=1;
37             i++;
38         }
39     }
40     printf("-------------保存到文件-------------\n");
41     printf("输入文件名:");
42     scanf("%s",&name);
43     f=fopen(name,"w");
44     if(!f){
45         printf("fail to open\n");
46         return 1;
47         }
48     else{
49     printf("保存成功!\n");
50     }
51     for(i=0;i<N;i++)
52     {
53     if(x[i].flag==1)
54         {
55             fputs(x[i].info[i],f);
56         }
57     }
58     
59     fclose(fp);
60     fclose(f);
61     system("pause");
62 return 0;
63 }
View Code

运行结果截图:

 

posted @ 2025-06-03 20:28  起司配咖啡  阅读(26)  评论(0)    收藏  举报