实验7

实验任务4:

源代码:

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

运行结果:

 

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

运行结果:

 

实验任务6:

源代码:

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

运行结果:

选做:

源代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h> 
 4 #include <time.h>
 5 typedef struct student{
 6     long id;
 7     char name[20];
 8     char classes[20];
 9 }STU;
10 void read(STU st[])
11 {
12     FILE *fp;
13     fp=fopen("D:\\list.txt","r");
14     if (!fp) {
15         printf("fail to open file\n");
16         return;
17     }
18     int i,number;
19     while (!feof(fp)) {
20         for (i = 0; i < 80; i++)
21         {
22             number=fscanf(fp, "%ld %s %s ", &st[i].id, st[i].name,st[i].classes);
23             if(number!=3)
24             break;
25         }
26     }
27     fclose(fp);
28 }
29 void select(STU st[])
30 {
31     srand(time(NULL));
32     int i,temp,count=0,j;
33     int selected[5];
34     while(count<5)
35     {
36         int sign=1;
37         temp=rand()%80;
38         for(i=0;i<count;i++)
39         {
40             if(count==selected[i])
41             {
42                 sign=0;
43                 break;
44             }
45         }
46         if(sign!=0)
47         {
48             selected[i]=temp;
49             count++;
50         }
51     }
52     for (i=0;i<4;i++)
53     {
54         for(j=0;j<4-i;j++)
55         {
56             if(st[selected[j]].id>st[selected[j+1]].id)
57             {
58                 STU s=st[selected[j]];
59                 st[selected[j]]=st[selected[j+1]];
60                 st[selected[j+1]]=s;
61             }
62         }
63     }
64     time_t t;
65     time(&t);
66     struct tm* p;
67     p = localtime(&t);    
68     char ct[50];
69     strftime(ct, sizeof(ct), "%Y%m%d", p);
70     printf("---------%s随机抽点名单---------\n",ct);
71     for (i = 0; i < 5; i++) {
72         printf("%ld %-6s %s\n", st[selected[i]].id, st[selected[i]].name, st[selected[i]].classes);
73     }
74     char name[1000]="";
75     strcat(name,"D:\\");
76     strcat(name,ct);
77     strcat(name,".txt");
78     FILE *fout = fopen(name, "w");
79     if (!fout) {
80         printf("fail to open file \n");
81         return;
82     }
83     for (i = 0; i < 5; i++) {
84         fprintf(fout, "%ld %-6s %s\n", st[selected[i]].id, st[selected[i]].name, st[selected[i]].classes);
85     }
86     fclose(fout);
87     printf("文件保存成功!\n");
88 }
89 int main(){
90     STU st[80];
91     read(st);
92     select(st);
93     return 0;    
94 }

运行结果:

 

 

附页:

实验任务1:
源代码:

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

运行结果:

 

实验任务2:

源代码:

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

运行结果:

 

实验任务3:

源代码:

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

运行结果:

 

posted @ 2024-12-26 17:32  noeleven  阅读(7)  评论(0编辑  收藏  举报