实验五

task1-1

 1 typedef struct
 2 {
 3 char name[M]; // 书名
 4 char author[M]; // 作者
 5 }Book;
 6 int main()
 7 {
 8 Book x[N] = { {"一九八四", "乔治.奥威尔"},
 9               {"美丽新世界", "赫胥黎"},
10               {"昨日的世界", "斯蒂芬.茨威格"},
11               {"万历十五年", "黄仁宇"},
12               {"一只特立独行的猪", "王小波"}
13             };
14 int i;
15 FILE *fp;
16 // 以写的方式打开文本文件data1.txt
17 fp = fopen("data1.txt", "w");
18 // 如果打开文件失败,输出提示信息并返回
19 if(fp == NULL)
20 {
21 printf("fail to open file\n");
22 return 1;
23 }
24 // 将结构体数组x中的图书信息写到fp指向的文件data1.txt
25 // 同时也输出到屏幕上
26 for(i=0; i<N; ++i)
27 {
28 fprintf(fp, "%-20s %-20s\n", x[i].name, x[i].author);
29 printf("%-20s %-20s\n", x[i].name, x[i].author);
30 }
31 fclose(fp);
32 
33 return 0;
34 }

 1 #include <stdio.h>
 2 #define N 5
 3 #define M 80
 4 typedef struct
 5 {
 6 char name[M]; // 书名
 7 char author[M]; // 作者
 8 }Book;
 9 int main()
10 {
11 Book x[N];
12 int i;
13 FILE *fp;
14 // 以读的方式打开文本文件data1.txt
15 fp = fopen("data1.txt", "r");
16 // 如果打开文件失败,输出提示信息并返回
17 if(fp == NULL)
18 {
19 printf("fail to open file\n");
20 return 1;
21 }
22 // 从fp指向的文件data1.txt中读取信息到结构体数组x
23 // 同时,把x的内容输出到屏幕上
24 for(i=0; i<N; ++i)
25 {
26 fscanf(fp, "%s %s\n", x[i].name, x[i].author);
27 printf("%-20s %-20s\n", x[i].name, x[i].author);
28 }
29 fclose(fp);
30 return 0;
31 }

问题:因为在字符串的数组中,数组名就代表数组的地址

 

task2-1

 1 #include <stdio.h>
 2 #define N 5
 3 #define M 80
 4 typedef struct
 5 {
 6 char name[M]; // 书名
 7 char author[M]; // 作者
 8 }Book;
 9 int main()
10 {
11 Book x[N] = { {"一九八四", "乔治.奥威尔"},
12             {"美丽新世界", "赫胥黎"},
13             {"昨日的世界", "斯蒂芬.茨威格"},
14             {"万历十五年", "黄仁宇"},
15             {"一只特立独行的猪", "王小波"}
16             };
17 int i;
18 FILE *fp;
19 // 以写的方式打开二进制文件data2.dat
20 fp = fopen("data2.dat", "wb");
21 // 如果打开文件失败,输出提示信息并返回
22 if(fp == NULL)
23 {
24 printf("fail to open file\n");
25 return 1;
26 }
27 // 将结构体数组x中的图书信息写以数据块方式写入文件
28 // 把从地址x处开始sizeof(Book)×N个字节大小的数据块写入fp指向的文件
29 fwrite(x, sizeof(Book), N, fp);
30 fclose(fp);
31 return 0;
32 }

 

 1 #include <stdio.h>
 2 #define N 5
 3 #define M 80
 4 typedef struct
 5 {
 6 char name[M]; // 书名
 7 char author[M]; // 作者
 8 }Book;
 9 int main()
10 {
11 Book x[N];
12 int i;
13 FILE *fp;
14 // 以读的方式打开二进制文件data2.dat
15 fp = fopen("data2.dat", "rb");
16 // 如果打开文件失败,输出提示信息并返回
17 if(fp == NULL)
18 {
19 printf("fail to open file\n");
20 return 1;
21 }
22 // 从fp指向的文件中读取数据块到x对应的地址单元
23 // 数据块大小为sizeof(Book)×N
24 fread(x, sizeof(Book), N, fp);
25 // 在屏幕上输出结构体数组x中保存的数据
26 for(i=0; i<N; ++i)
27 printf("%-20s%-20s\n", x[i].name, x[i].author);
28 fclose(fp);
29 return 0;
30 }

task3-2

 1 #include <stdio.h>
 2 int main()
 3 {
 4 FILE *fin, *fout;
 5 char ch;
 6 // 以只读、文本方式打开文件data3_1.txt
 7 fin = fopen("data3_1.txt", "r");
 8 // 如果打开失败,输出提示信息并返回
 9 if(fin == NULL)
10 {
11 printf("fail to open data3_1.txt\n");
12 return 1;
13 }
14 
15 if(fout == NULL)
16 {
17 printf("fail to open data3_2.txt\n");
18 return 1;
19 }
20 int a=0;
21 // 当fin指向的文件data1_txt没有结束时
22 while( !feof(fin) )
23 {
24 // 从fin指向的文件data1_txt读取单个字符
25 ch = fgetc(fin);
26 // 如果ch是小写字母,转换成大写
27 if((ch >= 'a' && ch <= 'z')||(ch>=48&&ch<=57)||ch==45)
28 a++;
29 }
30 fclose(fin);
31 printf("%d",a);
32 return 0;
33 }

 task5

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<stdlib.h>
  5 
  6 #define N 10
  7 
  8 typedef struct
  9 {
 10     long int id;
 11     char name[20];
 12     float objective;
 13     float subjective;
 14     float sum;
 15     char level[10];
 16 }STU;
 17 
 18 void input(STU s[], int n);
 19 void output(STU s[], int n);
 20 void process(STU s[], int n);
 21 
 22 int main()
 23 {
 24     STU stu[N];
 25 
 26     printf("从文件读入%d个考生信息:准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
 27     input(stu, N);
 28 
 29     printf("\n对考生信息进行处理:计算总分,确定等级\n");
 30     process(stu, N);
 31 
 32     printf("\n打印考生完整信息,并保存到文件中");
 33     output(stu, N);
 34 
 35     return 0;
 36 }
 37 
 38 void input(STU s[], int n)
 39 {
 40     int i;
 41     FILE* fin;
 42 
 43     fin = fopen("examinee.txt", "r");
 44     if (fin == NULL)
 45     {
 46         printf("fail to open file\n");
 47         exit(0);
 48     }
 49 
 50     while (!feof(fin))
 51     {
 52         for (i = 0; i < n; i++)
 53             fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
 54     }
 55 
 56     fclose(fin);
 57 }
 58 
 59 void output(STU s[], int n)
 60 {
 61     FILE* fout;
 62     int i;
 63 
 64     printf("\n");
 65     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 66     for (i = 0; i < n; i++)
 67         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);
 68 
 69     fout = fopen("result.txt", "w");
 70     if (!fout)
 71     {
 72         printf("fail to open or create result.txt\n");
 73         exit(0);
 74     }
 75     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 76     for (i = 0; i < n; i++)
 77         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id,
 78             s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);
 79 
 80 
 81     fclose(fout);
 82 }
 83 
 84 void process(STU s[], int n)
 85 {
 86     int i, j;
 87     for (i = 0; i < n; i++)
 88         s[i].sum = s[i].objective  + s[i].subjective ;
 89 
 90     STU temp;
 91     for (i = 0; i < n; i++)
 92         for (j = 0; j < n - 1 - i; j++)
 93             if (s[j].sum < s[j + 1].sum)
 94             {
 95                 temp = s[j];
 96                 s[j] = s[j + 1];
 97                 s[j + 1] = temp;
 98             }
 99     for (i = 0; i < n; i++)
100     {
101         if ((i + 1) <= n * 0.1)
102             strcpy(s[i].level, "优秀");
103         else if ((i + 1) > n * 0.1 && (i + 1) <= n * 0.5)
104             strcpy(s[i].level, "合格");
105         else
106             strcpy(s[i].level, "不合格");
107     }
108 }

task6

int main()
{
    int i,j;
    STU x[N], lucky[M];
    FILE* fin,* fout;
    fin = fopen("list.txt", "r");
    if (fin == NULL)
    {
        printf("fail to open file\n");
        exit(0);
    }
    for (i = 0; i < N; i++)
    {
        fscanf(fin, "%ld %s %s", &x[i].no, x[i].name, x[i].bj);
    }
    fclose(fin);
    srand(time(0));
    for (i = 0; i < M; i++)
    {
        j = rand() % 80;
        lucky[i] = x[j];
    }
    fout = fopen("lucky.txt", "w");
    if (fout == NULL)
    {
        printf("fail to open file\n");
        return 1;
    }
    for (i = 0; i < M; i++)
    {
        fprintf(fout, "%ld %s %s\n", lucky[i].no, lucky[i].name, lucky[i].bj);
        printf("%ld %s %s\n", lucky[i].no, lucky[i].name, lucky[i].bj);
    }
    fclose(fout);
    return 0;
}

 

posted @ 2022-06-06 23:04  19度的小常  阅读(19)  评论(3)    收藏  举报