实验5

2022/6/6 23:19:49

本文主要是介绍实验5,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

 

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

 

task2_1
#include<stdio.h>
 2 #include<stdlib.h>
 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    {"美丽新世界","赫胥黎"},
16    {"昨日的世界","斯蒂芬.茨威格"},
17    {"万历十五年","黄仁宇"},
18    {"一只特立独行的猪","王小波"}}; 
19    
20    int i;
21    
22    FILE *fp;
23    
24    fp=fopen("data2.dat","wb");
25    
26    if(fp==NULL)
27    {
28        printf("fail to open file\n");
29        return 1;
30    }
31    
32    fwrite(x,sizeof(Book),N,fp);
33    
34    fclose(fp);
35    
36    system("PAUSE");
37    return 0;
task2_2
#include <stdio.h>
 2 #include<stdlib.h>
 3 
 4 #define N 5
 5 #define M 80
 6 typedef struct
 7 {
 8 char name[M]; // 书名
 9 char author[M]; // 作者
10 }Book;
11 int main()
12 {
13 Book x[N];
14 
15 int i;
16 
17 FILE *fp;
18 // 以读的方式打开二进制文件data2.dat
19 fp = fopen("data2.dat", "rb");
20 // 如果打开文件失败,输出提示信息并返回
21 if(fp == NULL)
22 {
23 printf("fail to open file\n");
24 return 1;
25 }
26 // 从fp指向的文件中读取数据块到x对应的地址单元
27 // 数据块大小为sizeof(Book)×N
28 
29 fread(x, sizeof(Book), N, fp);
30 // 在屏幕上输出结构体数组x中保存的数据
31 
32 for(i=0; i<N; ++i)
33 printf("%-20s%-20s\n", x[i].name, x[i].author);
34 fclose(fp);
35 
36 system("PAUSE");
37 return 0;
38 }

 

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

 

task6
#include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define M 80
 5 #define N 5
 6 typedef struct
 7 {
 8     long int id;
 9     char name[20];
10     char classes[80];
11     int target;
12 } STU;
13 void input(STU s[], int n);
14 void random(STU s[],STU lucky[],int m,int n);
15 void output(STU lucky[], int n);
16 int main()
17 {
18     STU stu[M];
19     STU lucky[N];
20     input(stu, M);
21     random(stu,lucky,M,N);
22     output(lucky,N);
23     return 0;
24 }
25 void input(STU s[], int n)
26 {
27     int i;
28     FILE *fin;
29     fin = fopen("list.txt", "r");
30     if (fin == NULL)
31     {
32         printf("fail to open file\n");
33         exit(0);
34     }
35     while (!feof(fin))
36     {
37         for (i = 0; i < n; i++)
38             fscanf(fin, "%ld %s %s", &s[i].id, s[i].name,
39                    s[i].classes);
40     }
41     fclose(fin);
42 }
43 void random(STU s[],STU lucky[],int m,int n)
44 {
45     int k = 0;
46     srand(time(NULL));
47     while (k < n)
48     {
49         int i = rand()%m + 1;
50         lucky[k] = s[i];
51         k++;
52     }
53 }
54 void output(STU lucky[], int n)
55 {
56     for (int i = 0; i < 5; i++)
57         printf("%ld\t%-10s %-10s\n", lucky[i].id, lucky[i].name, lucky[i].classes);
58     FILE *fout;
59     fout = fopen("lucky.txt", "w");
60     for (int i = 0; i < n; i++)
61         fprintf(fout, "%ld\t%-10s %-10s\n", lucky[i].id, lucky[i].name, lucky[i].classes);
62     fclose(fout);
63 }

 

 



这篇关于实验5的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程