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

发布时间 2023-12-14 19:55:24作者: zxy溢

4.task_4

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];
 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 void output(Book x[],int n)
38 {
39     int i;
40     printf("ISBN号                   书名                          作者                售价      销售册数\n");
41     for (i=0;i<n;i++)
42     {
43         printf("%-25s",x[i].isbn);
44         printf("%-30s",x[i].name);
45         printf("%-20s",x[i].author);
46         printf("%-10.1f",x[i].sales_price);
47         printf("%d\n",x[i].sales_count);
48     }
49 }
50 
51 void sort(Book x[],int n)
52 {
53     Book y[N];
54     int i,j;
55     for (i=0;i<n;i++)
56     {
57         for (j=i+1;j<n;j++)
58         {
59             if (x[i].sales_count<x[j].sales_count)
60             {
61                 y[0]=x[i];
62                 x[i]=x[j];
63                 x[j]=y[0];
64             }
65         }
66     }
67 }
68 
69 double sales_amount(Book x[],int n)
70 {
71     int i;
72     double amount=0;
73     for (i=0;i<n;i++)
74     {
75         amount+=x[i].sales_price*x[i].sales_count;
76     }
77     return amount;
78 }

 

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

 

6.task_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     Account x[] = {{"A1001", "123456", student},
16                     {"A1002", "123abcdef", student},
17                     {"A1009", "xyz12121", student}, 
18                     {"X1009", "9213071x", admin},
19                     {"C11553", "129dfg32k", teacher},
20                     {"X3005", "921kfmg917", student}};
21     int n;
22     n = sizeof(x)/sizeof(Account);
23     output(x, n);
24 
25     return 0;
26 }
27 
28 void output(Account x[], int n) {
29     int i,j;
30     for (i=0;i<n;i++)
31     {
32         printf("%-20s",x[i].username);
33         for (j=0;j<strlen(x[i].password);j++)
34             printf("*");
35         for (;j<20;j++)
36             printf(" ");
37         switch(x[i].type)
38         {
39             case 0:printf("admin");break;
40             case 1:printf("student");break;
41             case 2:printf("teacher");break;
42         }
43         printf("\n");
44     }
45 }