实验6 C语言结构体、枚举应用编程

发布时间 2023-12-17 22:58:22作者: 李雨诗

1、实验1

运行结果

2、实验2

源代码

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 #define M 80
 5 
 6 typedef struct {
 7     char name[M];       // 书名
 8     char author[M];     // 作者
 9 } Book;
10 
11 int main() {
12     Book x[N] = { {"《一九八四》", "乔治.奥威尔"},
13                   {"《美丽新世界》", "赫胥黎"},
14                   {"《昨日的世界》", "斯蒂芬.茨威格"}, 
15                   {"《万历十五年》", "黄仁宇"},
16                   {"《一只特立独行的猪》", "王小波"},
17                   {"《百年孤独》", "马尔克斯"},
18                   {"《查令十字街84号》", "海莲.汉芙"},
19                   {"《只是孩子》", "帕蒂.史密斯"}, 
20                   {"《刀锋》", "毛姆"},
21                   {"《沉默的大多数》", "王小波"} };
22    Book *ptr;
23    int i;
24    char author[M];
25    
26    printf("所有图书信息:\n");
27    for(ptr = x;ptr < x + N;++ptr)
28        printf("%-30s%-30s\n",ptr->name,ptr->author);
29    
30    printf("\n输入作者名:");
31    gets(author);
32    for(ptr = x;ptr < x + N;++ptr)
33    if(strcmp(ptr -> author, author)==0){
34        printf("%-30s%-30s\n",ptr->name,ptr->author);
35    } 
36    return 0;
37 }

 

运行结果

 

 

3、实验3.1

源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 4 
 5 typedef struct Filminfo{
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct Filminfo *next;
11 }Film;
12 
13 void output(Film *head);
14 Film *insert(Film *head,int n);
15 
16 int main()
17 {
18     int n;
19     Film *head;
20     
21     head = NULL;
22     printf("输入影片数目:");
23     scanf("%d",&n);
24     head = insert(head,n);
25     
26     printf("\n所有影片信息如下:\n");
27     output(head);
28     
29     return 0;
30 }
31 
32 Film *insert(Film *head,int n)
33 {
34     int i;
35     Film *p;
36     for(i = 1;i<= n;++i){
37         p = (Film*)malloc(sizeof(Film));
38         printf("请输入第%d部影片信息:",i);
39         scanf("%s %s %s %d",p->name,p->director,p->region,&p->year);
40         
41         p->next = head;
42         head = p;
43     }
44     
45     return head;
46         
47 }
48 void output(Film *head)
49 {
50     Film *p;
51     
52     p = head;
53     while(p != NULL){
54         printf("%-20s %-20s %-20s %d\n",p->name,p->director,p->region,p->year);
55         p = p->next;
56     }
57 }

 

运行结果

 

 

实验3.2

源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 4 
 5 typedef struct Filminfo{
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct Filminfo *next;
11 }Film;
12 
13 void output(Film *head);
14 Film *insert(Film *head,int n);
15 
16 int main()
17 {
18     int n;
19     Film *head;
20     Film *p;
21     
22     p = (Film*)malloc(sizeof(Film));
23     p->next = NULL;
24     head = p;
25     printf("输入影片数目:");
26     scanf("%d",&n);
27     
28     head = insert(head,n);
29     
30     printf("\n所有影片信息如下:\n");
31     output(head);
32     
33     return 0;
34 }
35 
36 Film *insert(Film *head,int n)
37 {
38     int i;
39     Film *p;
40     for(i = 1;i<= n;++i){
41         p = (Film*)malloc(sizeof(Film));
42         printf("请输入第%d部影片信息:",i);
43         scanf("%s %s %s %d",p->name,p->director,p->region,&p->year);
44         
45         p->next = head->next;
46         head->next = p;
47     }
48     
49     return head;
50         
51 }
52 void output(Film *head)
53 {
54     Film *p;
55     
56     p = head;
57     while(p != NULL){
58         printf("%-20s %-20s %-20s %d\n",p->name,p->director,p->region,p->year);
59         p = p->next;
60     }
61 }

 

运行结果

 

 

4、实验4

源代码

 

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];          // isbn号
 6     char name[80];          // 书名
 7     char author[80];        // 作者
 8     double sales_price;     // 售价
 9     int  sales_count;       // 销售册数
10 } Book;
11 
12 void output(Book x[], int n);
13 void sort(Book x[], int n);
14 double sales_amount(Book x[], int n);
15 
16 int main() {
17     Book x[N] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59},
18                  {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16},
19                  {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27},
20                  {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
21                  {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42},
22                  {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39},
23                  {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
24                  {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31},
25                  {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
26                  {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}};
27     
28     printf("图书销量排名: \n");
29     sort(x, N);
30     output(x, N);
31 
32     printf("\n图书销售总额: %.2f\n", sales_amount(x, N));
33     
34     return 0;
35 }
36 
37 // 待补足:函数output()实现
38 // ×××
39 void output(Book x[],int n)
40 {
41     double s;
42     int i;
43     printf("ISBN号\t\t\t书名\t\t\t    作者\t\t\t售价\t\t\t销售册数\n");
44     for(i = 0;i < n;i++){
45         printf("%-20s    %-30s%-20s%    %-10g        %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
46     }
47     
48 }
49 
50 // 待补足:函数sort()实现
51 // ×××
52 void sort(Book x[],int n)
53 {
54     int i,j;
55     Book t;
56     for(i = 0;i<n-1;i++){
57         for(j = i;j<=n-2;j++){
58             if(x[j+1].sales_count>x[j].sales_count){
59                 t = x[j];
60                 x[j] = x[j+1];
61                 x[j+1] = t;
62             }
63         }
64     }
65     
66 }
67 
68 // 待补足:函数sales_count()实现
69 // ×××
70 double sales_amount(Book x[],int n)
71 {
72     int i;
73     double s=0;
74     for(i = 0;i < n;i++){
75         s+= x[i].sales_price*x[i].sales_count*1.0;
76     }
77     return s;
78 }

 

运行结果

 

 

5、实验5

源代码

 

 1 #include<stdio.h>
 2 
 3 typedef struct{
 4     int year;
 5     int month;
 6     int day;
 7 }Date;
 8 
 9 void input(Date *pd);
10 int day_of_year(Date d);
11 int compare_dates(Date d1,Date d2);
12 
13 void test1()
14 {
15     Date d;
16     int i;
17     
18     printf("输入日期:(以形如2023-12-11这样的形式输入\n");
19     for(i = 0;i<3;++i){
20         input(&d);
21         printf("%d-%02d-%02d是这一年中的第%d天\n\n",d.year,d.month,d.day,day_of_year(d));
22     }
23 }
24 
25 void test2()
26 {
27     Date Alice_birth,Bob_birth;
28     int i;
29     int ans;
30     
31     printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入\n");
32     for(i = 0;i<3;++i)
33     {
34         input(&Alice_birth);
35         input(&Bob_birth);
36         ans = compare_dates(Alice_birth,Bob_birth);
37         
38         if(ans == 0)
39             printf("Alice和Bob一样大\n\n");
40         else if(ans == -1)
41             printf("Alice比Bob大\n\n");
42         else
43             printf("Alice比Bob小\n\n");
44             
45      } 
46 }
47 int main()
48 {
49     printf("测试1:输入日期,打印输出这是一年中第多少天\n");
50     test1();
51     
52     printf("\n测试2:两个人年龄大小关系\n");
53     test2();
54 }
55 void input(Date *pd)
56 {
57     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
58 }
59 int day_of_year(Date d)
60 {
61     int s[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
62     if(d.year%4 == 0&&d.year%400 == 0||d.year%4 == 0)
63         s[2] = 29;
64         int sum=0, i;
65         for(i = 1;i<=d.month-1;i++)
66         {
67             sum += s[i];
68         }
69         sum+=d.day;
70         return sum;
71 }
72 int compare_dates(Date d1,Date d2)
73 {
74     int ans;
75     if(d1.year<d2.year){
76         ans = -1;
77     }
78     else if(d1.year>d2.year){
79         ans = 2;
80     }
81     else{
82         if(d1.month<d2.month)
83             ans=-1;
84         else if(d1.month>d2.month)
85             ans=2;
86         else {
87             if(d1.day<d2.day)
88                 ans = -1;
89             else if(d1.day>d2.day)
90                 ans = 2;
91             else 
92                 ans = 0;
93         }
94     }
95     return ans;
96 }

 

运行结果

 

 

6、实验6

源代码

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 enum Role{admin,student,teacher};
 5 
 6 typedef struct{
 7     char username[20];
 8     char password[20];
 9     enum Role type;
10 }Account;
11 
12 void output(Account x[],int n);
13 
14 int main()
15 {
16     Account x[]={{"A1001","123456",student},
17                  {"A1002","123abcdef",student},
18                  {"A1009","xyz12121",student},
19                  {"X1009","9213071x",admin},
20                  {"C11553","129dfg32k",teacher},
21                  {"x3005","9221kfmg917",student}};
22     int n;
23     n = sizeof(x)/sizeof(Account);
24     output(x,n);
25     
26     return 0;
27 }
28 void output(Account x[],int n)
29 {
30     int i,j,k;
31     for(i = 0;i<n;i++){
32         printf("%s\t",x[i].username);
33         j = strlen(x[i].password);
34         
35         for(k = 0;k<j;k++)
36             printf("*");
37             if(j<7){
38                 printf("\t\t");
39             }
40             else{
41                 printf("\t");
42             }
43             switch(x[i].type){
44                 case admin:printf("admin");break;
45                 case student:printf("student");break;
46                 case teacher:printf("teacher");break;
47             }
48         printf("\n");
49     }
50 }

 

运行结果