实验7

task1

 1 #include <stdio.h>
 2 
 3 #define N 80
 4 #define M 100
 5 
 6 typedef struct {
 7     char name[N];       // 书名
 8     char author[N];     // 作者
 9 } Book;
10 
11 void write();
12 void read();
13 
14 int main() {
15     printf("测试1: 把图书信息写入文本文件\n");
16     write();
17 
18     printf("\n测试2: 从文本文件读取图书信息, 打印输出到屏幕\n");
19     read();
20 
21     return 0;
22 }
23 
24 void write() {
25     Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
26                   {"《灯塔》", "克里斯多夫.夏布特"},
27                   {"《人的局限性》", "塞缪尔.约翰生"}, 
28                   {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
29                   {"《大地之上》", "罗欣顿·米斯特里"}, 
30                   {"《上学记》", "何兆武"}, 
31                   {"《命运》", "蔡崇达"} };
32     int n, i;
33     FILE *fp;
34 
35     // 计算数组x中元素个数
36     n = sizeof(x) / sizeof(x[0]);       
37     
38     // 以写的方式打开文本文件data1.txt 
39     fp = fopen("data1.txt", "w");
40     
41     // 如果打开文件失败,输出提示信息并返回 
42     if(fp == NULL) {
43         printf("fail to open file to write\n");
44         return;
45     }
46     
47     // 将结构体数组x中的图书信息格式化写到fp指向的文件data1.txt
48     for(i = 0; i < n; ++i)
49         fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author);
50     
51     fclose(fp);
52 }
53 
54 void read() {
55     Book x[M]; 
56     int i, n;
57     
58     FILE *fp;
59     
60     // 以读的方式打开文本文件data1.txt 
61     fp = fopen("data1.txt", "r");
62     
63     // 如果打开文件失败,输出提示信息并返回 
64     if(fp == NULL) {
65         printf("fail to open file to read\n");
66         return;
67     }
68     
69     // 从文件中读取图书信息,保存到结构体数组x中
70     i = 0;
71     while(fscanf(fp, "%s%s", x[i].name, x[i].author) != EOF) 
72         ++i;
73     
74     // 将图书信息打印输出到屏幕上
75     n = i;
76     for(i = 0; i < n; ++i)
77         printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author);
78     
79     fclose(fp);
80 }
View Code

1

1.2

task.2

 1 #include <stdio.h>
 2 
 3 #define N 80
 4 #define M 100
 5 
 6 typedef struct {
 7     char name[N];       // 书名
 8     char author[N];     // 作者
 9 } Book;
10 
11 void write();
12 void read();
13 
14 int main() {
15     printf("测试1: 把图书信息以数据块方式写入二进制文件\n");
16     write();
17 
18     printf("\n测试2: 从二进制文件读取图书信息, 打印输出到屏幕\n");
19     read();
20 
21     return 0;
22 }
23 
24 void write() {
25     Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
26                   {"《灯塔》", "克里斯多夫.夏布特"},
27                   {"《人的局限性》", "塞缪尔.约翰生"}, 
28                   {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
29                   {"《大地之上》", "罗欣顿·米斯特里"}, 
30                   {"《上学记》", "何兆武"}, 
31                   {"《命运》", "蔡崇达"} };
32     int n, i;
33     FILE *fp;
34 
35     // 计算数组x中元素个数
36     n = sizeof(x) / sizeof(x[0]);     
37     
38     // 以写的方式打开二进制文件data2.dat
39     fp = fopen("data2.dat", "wb");
40     
41     // 如果打开文件失败,输出提示信息并返回 
42     if(fp == NULL) {
43         printf("fail to open file to write\n");
44         return;
45     }
46     
47     // 将结构体数组x中的图书信息以数据块方式写入二进制文件data2.dat
48     fwrite(x, sizeof(Book), n, fp);
49     
50     fclose(fp);
51 }
52 
53 void read() {
54     Book x[M]; 
55     int i, n;
56     
57     FILE *fp;
58     
59     // 以读的方式打开二进制文件data2.dat
60     fp = fopen("data2.dat", "rb");
61     
62     // 如果打开文件失败,输出提示信息并返回 
63     if(fp == NULL) {
64         printf("fail to open file to read\n");
65         return;
66     }
67     
68     // 从二进制文件data2.dat以数据块方式读取图书信息存储到结构体数组x
69     i = 0;
70     while(fread(&x[i], sizeof(Book), 1, fp) == 1) 
71         ++i;
72 
73     // 在屏幕上打印输出
74     n = i;
75     for(i = 0; i < n; ++i)
76         printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author);
77     
78     fclose(fp);
79 }
View Code

2.2

2

task.3

 1 #include <stdio.h>
 2 #define N 100
 3 #define M 80
 4 
 5 void write();
 6 void read_str();
 7 void read_char();
 8 
 9 int main() {
10     printf("测试1: 把一组字符信息以字符串方式写入文本文件\n");
11     write();
12 
13     printf("\n测试2: 从文件以字符串方式读取, 输出到屏幕\n");
14     read_str();
15 
16     printf("\n测试3: 从文件以单个字符方式读取, 输出到屏幕\n");
17     read_char();
18 
19     return 0;
20 }
21 
22 void write() {
23     char *ptr[] = { "Working\'s Blues",
24                      "Everything Will Flow",
25                      "Streets of London",
26                      "Perfect Day",
27                      "Philadelphia"};
28     int i, n;
29     FILE *fp;
30 
31     fp = fopen("data3.txt", "w");
32     if(fp == NULL) {
33         printf("fail to open file to write\n");
34         return;
35     }
36 
37     n = sizeof(ptr)/sizeof(ptr[0]);
38 
39     for(i = 0; i < n; ++i) {
40         fputs(ptr[i], fp);
41         fputs("\n", fp);
42     }
43     
44     fclose(fp);
45 }
46 
47 void read_str() {
48     char songs[N][M];
49     int i, n;
50     FILE *fp;
51 
52     fp = fopen("data3.txt", "r");
53     if(fp == NULL) {
54         printf("fail to open file to read\n");
55         return;
56     }
57 
58     i = 0;
59     while(i < N && (fgets(songs[i], M, fp) != NULL))
60         ++i;
61 
62     n = i;
63     for(i = 0; i < n; ++i)
64         printf("%d. %s", i+1, songs[i]);
65     
66     fclose(fp);
67 }
68 
69 void read_char() {
70     int ch;
71     FILE *fp;
72 
73     fp = fopen("data3.txt", "r");
74     if(fp == NULL) {
75         printf("fail to open file to read\n");
76         return;
77     }
78 
79     while((ch = fgetc(fp)) != EOF)
80         putchar(ch);
81 
82     fclose(fp);
83 }
View Code

3,2

3

思考:

(1)“\”是转义字符\'表示的是‘本身

(2)限制循环,确保数组不会越界

task 4

 1 #include<stdio.h>
 2 int main(){
 3     FILE *fp;
 4     char ch;
 5     int lines=0,chars=0;
 6 
 7     fp=fopen("data4.txt","r");
 8     if(fp==NULL){
 9         printf("failed to open!\n");
10         return 1;
11     }
12     while((ch=fgetc(fp))!=EOF){
13         if(ch=='\n')
14         {lines++;}
15         else if(ch!=' '&&ch!='\r'&&ch!='\t'&&ch!='\n')
16         {chars++;}
17     }
18     fclose(fp);
19     printf("data4.txt统计结果如下:\n");
20     printf("行数:%d\n",lines);
21     printf("字符数(不计空白符):%d\n",chars);
22     return 0;
23 }

4。1

task.5

  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 
 35     printf("\n通过考试的名单写入文件: 已完成!\n");
 36     write(stu_pass, cnt);
 37 
 38     pass_rate = 1.0 * cnt / N;
 39     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 40 
 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("examinee.txt", "r");
 60     if (!fin) {
 61         printf("fail to open file\n");
 62         return;
 63     }
 64 
 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     fclose(fin);
 69 }
 70 
 71 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
 72 int process(STU st[], int n, STU st_pass[]) {
 73     int i,cnt=0;
 74     for(i=0;i<n;i++){
 75        st[i].sum=st[i].objective+st[i].subjective;
 76        if(st[i].sum>=60){
 77            strcpy(st[i].result,"通过");
 78            st_pass[cnt]=st[i];
 79            cnt++;
 80        }
 81        else{
 82            strcpy(st[i].result,"未通过");}
 83     }
 84     return cnt;
 85 }
 86 
 87 // 把通过考试的考生完整信息写入文件list_pass.txt
 88 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 89 void write(STU st[], int n) {
 90     FILE *fp;
 91     if(n==0){
 92         printf("不好!没有人通过!");
 93         return ;
 94     }
 95     fp=fopen("list_pass.txt","wb");
 96     if(fp==NULL){
 97         printf("fail to open!\n");
 98         return ;
 99     }
100     fwrite(st,sizeof(STU),n,fp);
101     fclose(fp);
102 }

5

task6

 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 id[20];
 9     char name[20];
10     char clas[50];
11     int flag;
12 }STU;
13 
14 int main(){
15     STU st[N],winner[M];
16     int t,index=0,count=0,i;
17     char filename[100];
18     //读取
19     FILE *fin;
20     FILE *fp;
21     fin=fopen("list.txt","r");
22     if(fin==NULL){
23         printf("fail to open!\n");
24         return 1;
25     }
26     for(t=0;t<N;t++){
27         fscanf(fin,"%s%s%s",st[t].id,st[t].name,st[t].clas);
28         st[t].flag=0;
29     }
30     fclose(fin);
31 printf("成功读取信息\n");
32 ///抽奖
33 srand((unsigned int)time (NULL));
34 while(count<M){
35     index=rand()%N;
36     if(st[index].flag==0){
37         st[index].flag=1;
38         winner[count]=st[index];
39         count++;
40     }
41 }
42 
43 //输出
44 printf("\n---------  中奖名单 --------\n");
45 for(i=0;i<M;i++){
46     printf("%-15s %-8s %-20s\n",winner[i].id,
47         winner[i].name,winner[i].clas);}
48 printf("\n");
49 printf("\n----------保存到文件----------\n");
50 //文件名
51 printf("输入文件名:\n");
52 scanf("%s",filename);
53 //写入文件
54 
55 fp=fopen(filename,"w");
56 if(!fp){
57     printf("fail to open file!\n");
58     return 1;
59 }
60 for(i=0;i<M;i++){
61     fprintf(fp,"%-20s%-8s%-8s\n",winner[i].id,winner[i].name,winner[i].clas);
62 }
63 fclose(fp);
64 printf("保存成功!\n");
65 return 0;
66 }

5.1

 

posted @ 2026-06-23 17:27  asteria11  阅读(8)  评论(0)    收藏  举报