陈凯迪的实验5

一、实验结论

  1.1

    代码:

    
 1 #include <stdio.h>
 2 #define N 5
 3 #define M 80
 4 typedef struct {
 5   char name[M];
 6   char author[M];
 7 } Book;
 8 
 9 int main() {
10   Book x[N] = {{"一九八四", "乔治.奥威尔"},
11                {"美丽新世界", "赫胥黎"},
12                {"昨日的世界", "斯蒂芬.茨威格"},
13                {"万历十五年", "黄仁宇"},
14                {"一只特立独行的猪", "王小波"}};
15   int i;
16   FILE *fp;
17   fp = fopen("data1.txt", "w");
18   if (fp == NULL) {
19     printf("fail to open file\n");
20     return 1;
21   }
22 
23   for (i = 0; i < N; i++) {
24     fprintf(fp, "%-20s %-20s\n", x[i].name, x[i].author);
25     printf("%-20s %-20s\n", x[i].name, x[i].author);
26   }
27   fclose(fp);
28   return 0;
29 }                                                                 
View Code

    图片:

    
Picture

  1.2

    代码:

    
 1 #include <stdio.h>
 2 
 3 #define N 5
 4 #define M 80
 5 typedef struct {
 6   char name[M];
 7   char author[M];
 8 } Book;
 9 int main() {
10   Book x[N];
11   int i;
12   FILE *fp;
13   fp = fopen("data1.txt", "r");
14   if (fp == NULL) {
15     printf("fail to open file\n");
16     return 1;
17   }
18   for (i = 0; i < N; i++) {
19     fscanf(fp, "%s %s\n", x[i].name, x[i].author);
20     printf("%-20s %-20s\n", x[i].name, x[i].author);
21   }
22 
23   fclose(fp);
24   return 0;
25 }
View Code

    图片:

    
Picture

  1.3

    答:数组名本身就是地址。

 

  2.1

    代码:

    
 1 #include <stdio.h>
 2 #define N 5
 3 #define M 80
 4 typedef struct {
 5   char name[M];
 6   char author[M];
 7 } Book;
 8 
 9 int main() {
10   Book x[N] = {{"一九八四", "乔治.奥威尔"},
11                {"美丽新世界", "赫胥黎"},
12                {"昨日的世界", "斯蒂芬.茨威格"},
13                {"万历十五年", "黄仁宇"},
14                {"一只特立独行的猪", "王小波"}};
15   int i;
16   FILE *fp;
17   fp = fopen("data2.dat", "wb");
18   if (fp == NULL) {
19     printf("fail to open file\n");
20     return 1;
21   }
22   fwrite(x, sizeof(Book), N, fp);
23   fclose(fp);
24   return 0;
25 }
View Code

    图片:

    
Picture

  2.2

    代码:

    
 1 #include <stdio.h>
 2 #define N 5
 3 #define M 80
 4 typedef struct {
 5   char name[M];
 6   char author[M];
 7 } Book;
 8 int main() {
 9   Book x[N];
10   int i;
11   FILE *fp;
12   fp = fopen("data2.dat", "rb");
13   if (fp == NULL) {
14     printf("fail to open file\n");
15     return 1;
16   }
17   fread(x, sizeof(Book), N, fp);
18   for (i = 0; i < N; i++)
19     printf("%-20s%-20s\n", x[i].name, x[i].author);
20   fclose(fp);
21   return 0;
22 }
View Code

    图片:

    
Picture

  

  3

    代码:

    
 1 #include <stdio.h>
 2 int main() {
 3   FILE *fin, *fout;
 4   char ch;
 5   int i = 0;
 6 
 7   fin = fopen("data3_1.txt", "r");
 8   if (fin == NULL) {
 9     printf("fail to open file data3_1.txt\n");
10     return 1;
11   }
12 
13   fout = fopen("data3_2.txt", "w");
14   if (fout == NULL) {
15     printf("fail to open file data3_2.txt\n");
16     return 1;
17   }
18 
19   while (!feof(fin)) {
20     ch = fgetc(fin);
21     if (ch >= 'a' && ch <= 'z')
22       ch -= 32;
23 
24     if (ch >= 33 && ch <= 126)
25       i += 1;
26     fputc(ch, fout);
27   }
28   fclose(fin);
29   fclose(fout);
30   printf("共有字符数%d", i);
31   return 0;
32 }
View Code

    图片:

    
Picture

  

  4

    代码:

    
 1 #include <stdio.h>
 2 #define N 5
 3 #define M 20
 4 typedef struct {
 5   char number[M];
 6   char name[M];
 7   int score;
 8 } Student;
 9 int findMinList(Student s[], int n, Student m[]);
10 int main() {
11   Student stu[N], minlist[N];
12   int count, i;
13   FILE *fin, *fout;
14   fin = fopen("data4_1.txt", "r");
15   if (fin == NULL) {
16     printf("fail to open file data4_1.txt\n");
17     return 1;
18   }
19   for (i = 0; i < N; i++) {
20     fscanf(fin, "%s %s %d\n", stu[i].number, stu[i].name, &stu[i].score);
21     printf("%s %s %d\n", stu[i].number, stu[i].name, stu[i].score);
22   }
23   fclose(fin);
24 
25   count = findMinList(stu, N, minlist);
26   printf("\n共有%d个最低分,信息如下\n", count);
27 
28   for (i = 0; i < count; i++)
29     printf("%s %s %d\n", minlist[i].number, minlist[i].name, minlist[i].score);
30 
31   fout = fopen("data4_2.txt", "w");
32   if (fout == NULL) {
33     printf("fail to open file data4_2.txt\n");
34     return 1;
35   }
36   for (i = 0; i < count; i++)
37     fprintf(fout, "%s %s %d\n", minlist[i].number, minlist[i].name,
38             minlist[i].score);
39 
40   fclose(fout);
41 
42   return 0;
43 }
44 int findMinList(Student s[], int n, Student m[]) {
45   int min, k = 0, i;
46   min = s[0].score;
47   for (i = 1; i < n; i++)
48     if (min > s[i].score)
49       min = s[i].score;
50   for (i = 0; i < n; i++)
51     if (s[i].score == min) {
52       m[k] = s[i];
53       k++;
54     }
55   return k;
56 }
View Code

    图片:

    
Picture

  

  5

    代码:

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

    图片:

    
Picture

 

  6 必做&选做

    代码:

    
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <time.h>
 5 
 6 #define N 80
 7 #define M 5
 8 typedef struct {
 9   char data[N];
10   int flag;
11 } Stu;
12 int main() {
13   struct tm *p;
14   time_t lt;
15   lt = time(NULL);
16   p = localtime(&lt);
17   //获取时间
18   char now1[30];
19   strftime(now1, 30, "%Y%m%d.txt", p);
20 
21   int i, j, k;
22   Stu s[N], l[M];
23   FILE *fin, *fout;
24   fin = fopen("list.txt", "r");
25   if (fin == NULL) {
26     printf("fail to open file\n");
27     return 0;
28   }
29   for (i = 0; i < N; i++) {
30     fgets(s[i].data, N, fin);
31     printf("%s\n", s[i].data);
32     s[i].flag = 0;
33   }
34   fclose(fin);
35 
36   for (j = 0; j < M; j++) {
37     i = 0;
38     srand((unsigned)time(NULL));
39     k = rand() % (N - j);
40     while (k) {
41       if (s[i].flag == 0)
42         k--;
43       i++;
44     }
45     l[j] = s[i];
46     s[i].flag = 1;
47   }
48 
49   fout = fopen(now1, "w");
50   if (fout == NULL) {
51     printf("fail to open file\n");
52     return 0;
53   }
54   for (j = 0; j < M; j++) {
55     fprintf(fout, "%s\n", l[j].data);
56   }
57   fclose(fout);
58   return 0;
59 }
View Code

    图片:

    
Picture

 

 

 

 

 

 二、实验总结

  额外学习了时间类的函数及用法,认识了数字转字符串的一种方法

 

posted on 2022-06-05 21:19  CKDDOUBI  阅读(12)  评论(2编辑  收藏  举报

导航