实验七

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

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

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

 

posted @ 2024-12-30 00:26  糯檽檽米  阅读(12)  评论(0)    收藏  举报