ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验五

2022-06-06 22:00:35  阅读:136  来源: 互联网

标签:fp 20 int char 实验 include define


task1-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     fp = fopen("data1.txt", "w"); 
20     if(fp == NULL) 
21     { 
22         printf("fail to open file\n"); 
23         return 1; 
24     }
25     for(i=0; i<N; ++i) 
26     { 
27         fprintf(fp, "%-20s %-20s\n", x[i].name, x[i].author); 
28         printf("%-20s %-20s\n", x[i].name, x[i].author); 
29     }
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     fp = fopen("data1.txt", "r"); 
15     if(fp == NULL) 
16     { 
17         printf("fail to open file\n"); 
18         return 1; 
19     }
20     for(i=0; i<N; ++i) 
21     { 
22         fscanf(fp, "%s %s\n", x[i].name, x[i].author); 
23         printf("%-20s %-20s\n", x[i].name, x[i].author); 
24     }
25     fclose(fp); 
26     return 0; 
27 }

x[i].name和x[i].author本身作为一个地址。

 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     fp = fopen("data2.dat", "wb"); 
20     if(fp == NULL) 
21     { 
22         printf("fail to open file\n"); 
23         return 1; 
24     }
25     fwrite(x, sizeof(Book), N, fp); 
26     fclose(fp);
27     return 0;
28 }

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

1.Yes

2.No

task3.c

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

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

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 81
 5 #define M 5
 6 typedef struct
 7 {
 8    long int id; 
 9    char name[20];
10    char banji[60];
11 }STU;
12 int main()
13 {
14     int i,j; 
15     STU x[N],ran[M];
16     FILE *fin,*fout;
17     fin = fopen("list.txt","r");
18     if(fin == NULL) 
19     { 
20        printf("fail to open list.txt\n"); 
21        return 1;
22     }  
23     for(i=1;i<=N;i++)
24        fscanf(fin, "%ld %s %s", &x[i].id, x[i].name, x[i].banji); 
25     fclose(fin);
26     srand(time(0));   
27     j = rand()%(80)+1;   
28     for(i=0;i<M;i++)
29     {
30         ran[i] = x[j];
31         j = rand()%(80)+1;
32     }
33     fout = fopen("lucky.txt", "w"); 
34     if(fout == NULL) 
35     { 
36        printf("fail to open file\n"); 
37        return 1; 
38     }
39     for(i=0;i<M;i++) 
40     {
41        fprintf(fout, "%ld %s %s\n", ran[i].id, ran[i].name, ran[i].banji); 
42        printf("%ld %s %s\n", ran[i].id, ran[i].name, ran[i].banji);
43     }
44     fclose(fout); 
45     return 0;
46 }

 

标签:fp,20,int,char,实验,include,define
来源: https://www.cnblogs.com/Canton/p/16349883.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有