實驗6 C語言結構體、列舉應用程式設計

青栀%發表於2024-06-10

4. 實驗任務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);
13 void sort(Book x[],int n);
14 double sales_amount(Book x[],int);
15 
16 int main()
17 {
18     Book x[N]={{"978-7-229-14156-1","源泉","安·蘭德",84,59},
19                 {"978-7-5133-5261-1", "李白來到舊金山", "譚夏陽",48,16},
20                 {"978-7-5617-4347-8", "陌生人日記", "周怡芳", 72.6,27},
21                 {"978-7-5722-5475-8", "晶片簡史", "汪波", 74.9, 49},
22                 {"978-7-5046-9568-0", "資料化決策", "道格拉斯·W·哈伯德", 49, 42},
23                 {"978-7-5133-4388-6", "美好時代的背後", "凱瑟琳.布", 34.5, 39},
24                 {"978-7-1155-0509-5", "無窮的開始:世界進步的本源", "戴維·多伊奇", 37.5,55},
25                 {"978-7-5321-5691-7", "何為良好生活", "陳嘉映", 29.5 , 31},
26                 {"978-7-5133-5109-6", "你好外星人", "英國未來出版集團", 118,42},
27                 {"978-7-2011-4617-1", "世界盡頭的咖啡館", "約翰·史崔勒基", 22.5,44}};
28 
29     printf("圖書館銷量排名:\n");
30     sort(x,N);
31     output(x,N);
32 
33     printf("\n圖書館銷售總額:%.2lf\n",sales_amount(x,N));
34 
35     return 0;
36 }
37 void output(Book x[],int n)
38 {
39     int i;
40     
41         printf("ISBN號\t\t\t書名\t\t\t\t作者\t\t\t售價\t\t銷售冊數\n"); 
42     
43     for(i=0;i<n;++i)
44         printf("%-24s%-32s%-20s\t%-13.2lf\t%4d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
45 }
46 void sort(Book x[],int n)
47 {
48     int i,j,k;
49     Book min;
50 
51     for(i=0;i<n-1;++i)
52         {
53             for(j=1,min=x[0],k=0;j<n-i;++j)
54                 if(x[j].sales_count<min.sales_count)
55                     {
56                         min=x[j];
57                         k=j;
58                     }
59                 x[k]=x[j-1];
60                 x[j-1]=min;
61         }
62 }
63 double sales_amount(Book x[],int n)
64 {
65     int i;
66     double s=0;
67 
68     for(i=0;i<n;++i)
69         s+=x[i].sales_count*x[i].sales_price;
70 
71     return s;
72 }

5. 實驗任務5

  1 #include<stdio.h>
  2 typedef struct
  3 {
  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 void test2();
 15 
 16 int main()
 17 {
 18     printf("測試1: 輸入日期, 列印輸出這是一年中第多少天\n");
 19     test1();
 20 
 21     printf("\n測試2: 兩個人年齡大小關係\n");
 22     test2();
 23 
 24     return 0;
 25 }
 26 void test1()
 27 {
 28     Date d;
 29     int i;
 30 
 31     printf("輸入日期:(以形如2024-06-01這樣的形式輸入)\n");
 32     for(i = 0; i < 3; ++i) {
 33         input(&d);
 34         printf("%04d-%02d-%02d是這一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
 35     }
 36 
 37 }
 38 void test2()
 39 {
 40     Date Alice_birth, Bob_birth;
 41     int i;
 42     int ans;
 43 
 44     printf("輸入Alice和Bob出生日期:(以形如2005-08-11這樣的形式輸入)\n");
 45     for(i = 0; i < 3; ++i)
 46     {
 47 
 48         input(&Alice_birth);
 49         input(&Bob_birth);
 50         ans=compare_dates(Alice_birth,Bob_birth);
 51 
 52         if(ans==0)
 53             printf("Alice和Bob一樣大\n\n");
 54         else if(ans == -1)
 55             printf("Alice比Bob大\n\n");
 56         else
 57             printf("Alice比Bob小\n\n");
 58     }
 59 
 60 }
 61 void input(Date *pd)
 62 {
 63     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 64 }
 65 int day_of_year(Date d)
 66 {
 67     int i,s=0;
 68 
 69     for(i=1;i<d.month;++i)
 70         if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
 71             s+=31;
 72         else if(i==2)
 73             if(d.year%4==0&&d.year%100!=0 ||d.year%400==0)
 74                 s+=29;
 75             else 
 76                 s+=28;
 77         else
 78             s+=30;
 79 
 80     s+=d.day;
 81 
 82     return s;
 83 }
 84 int compare_dates(Date d1,Date d2)
 85 {
 86     if(d1.year>d2.year)
 87         return 1;
 88     else if(d1.year<d2.year)
 89         return -1;
 90     else if(d1.month>d2.month)
 91         return 1;
 92     else if(d1.month<d2.month)
 93         return -1;
 94     else if(d1.day>d2.day)
 95         return 1;
 96     else if(d1.day<d2.day)
 97         return -1;
 98     else
 99         return 0;
100 }

6. 實驗任務6

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

7. 實驗任務7

  1 #include<stdio.h>
  2 #include<string.h>
  3 typedef struct
  4 {
  5     char name[20];
  6     char phone[12];
  7     int vip;
  8 }Contact;
  9 
 10 void set_vip_contact(Contact x[],int n,char name[]);
 11 void output(Contact x[],int n);
 12 void display(Contact x[],int n);
 13 
 14 #define N 10
 15 
 16 int main()
 17 {
 18     Contact list[N]={{"劉一", "15510846604", 0},
 19                      {"陳二", "18038747351", 0},
 20                      {"張三", "18853253914", 0},
 21                      {"李四", "13230584477", 0},
 22                      {"王五", "15547571923", 0},
 23                      {"趙六", "18856659351", 0},
 24                      {"周七", "17705843215", 0},
 25                      {"孫八", "15552933732", 0},
 26                      {"吳九", "18077702405", 0},
 27                      {"鄭十", "18820725036", 0}};
 28     int vip_cnt,i;
 29     char name[20];
 30 
 31     printf("顯示原始通訊錄資訊:\n");
 32     output(list,N);
 33 
 34     printf("\n輸入要設定的緊急聯絡人個數:");
 35     scanf("%d",&vip_cnt);
 36     printf("輸入%d個緊急聯絡人姓名:\n",vip_cnt);
 37     for(i=0;i<vip_cnt;++i)
 38         {
 39             scanf("%s",name);
 40             set_vip_contact(list,N,name);
 41     }
 42 
 43     printf("\n顯示通訊錄列表:(按姓名字典升序排列,緊急聯絡人最先顯示)\n");
 44     display(list,N);
 45     output(list,N);
 46 
 47     return 0;
 48 }
 49 void set_vip_contact(Contact x[],int n,char name[])
 50 {
 51     int i;
 52 
 53     for(i=0;i<n;++i)
 54         if(strcmp(name,x[i].name)==0)
 55             {
 56                 x[i].vip=1;
 57                 break;
 58             }
 59 }
 60 void display(Contact x[],int n)
 61 {
 62     int i,j,k=0;
 63     Contact t;
 64 
 65     for(i=0;i<n;++i)
 66         if(x[i].vip)
 67             {
 68                 t=x[i];
 69                 x[i]=x[k];
 70                 x[k++]=t;
 71         }
 72 
 73         for(i=0;i<k-1;++i)
 74             for(j=0;j<k-i-1;++j)
 75                 if(strcmp(x[j].name,x[j+1].name)>0)
 76                 {
 77                     t=x[j];
 78                     x[j]=x[j+1];
 79                     x[j+1]=t;
 80                 }
 81     for(i=k;i<n;++i)
 82         for(j=k;j<n-1;++j)
 83             if(strcmp(x[j].name,x[j+1].name)>0)
 84             {
 85                 t=x[j];
 86                 x[j]=x[j+1];
 87                 x[j+1]=t;
 88             }
 89 }
 90 void output(Contact x[],int n)
 91 {
 92     int i;
 93 
 94     for(i=0;i<n;++i)
 95     {
 96         printf("%-10s%-15s",x[i].name,x[i].phone);
 97         if(x[i].vip)
 98             printf("%5s","*");
 99         printf("\n");
100     }
101 }

相關文章