实验7 文件应用编程

task4

 

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

 

 

task5

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.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     return 0;
 41 }
 42 
 43 // 把所有考生完整信息输出到屏幕上
 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 45 void output(STU st[], int n) {
 46     int i;
 47 
 48     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 49     for (i = 0; i < n; i++)
 50         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);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void read(STU st[], int n) {
 55     int i;
 56     FILE* fin;
 57 
 58     fin = fopen("C:\\task7\\task7\\examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 62     }
 63 
 64     while (!feof(fin)) {
 65         for (i = 0; i < n; i++)
 66             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67     }
 68 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void write(STU s[], int n) {
 75     int i;
 76     FILE* fp;
 77 
 78     fp = fopen("C:\\task7\\task7\\list_pass.txt","w");
 79     if (!fp) {
 80         printf("fail to open file\n");
 81         return;
 82     }
 83     fprintf(fp, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", "准考证号\t", "姓名\t", "客观题得分\t", "操作题得分\t", "总分\t\t", "结果\n");
 84     for (i = 0;i < n;++i) {
 85         if (s[i].sum >= 60)
 86             fprintf(fp,"%ld %s %f %f %.2f %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 87     }
 88     fclose(fp);
 89 }
 90 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 91 int process(STU st[], int n, STU st_pass[]) {
 92     int i;
 93     int k = 0;
 94     for (i = 0;i < n;++i) {
 95         st[i].sum = st[i].objective + st[i].subjective;
 96     }
 97     for (i = 0;i < n;++i) {
 98         if (st[i].sum >= 60)
 99             strcpy(st[i].result, "通过");
100         else
101             strcpy(st[i].result, "不通过");
102     }
103     for (i = 0;i < n;++i) {
104         if (st[i].sum >= 60) {
105             st_pass[k] = st[i];
106             k++;
107         }
108         
109     }
110     return k;
111 }

 

task6

 

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

 

posted @ 2024-12-28 22:55  sunishope  阅读(31)  评论(0)    收藏  举报