实验六

task4.

 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-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51},
18                   {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30},
19                   {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27},
20                   {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 
21                   {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
22                   {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42},
23                   {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44},
24                   {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
25                   {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
26                   {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}};
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 void output(Book x[], int n)
37 {    
38     int i; 
39     printf("ISBN号             书名             作者             售价     销售册数\n");
40     for (i = 0; i < n; i++)
41     {
42         printf("%-15s    %-25s    %-20s    %-5g    %-5d\n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count);
43     }
44 }
45 void sort(Book x[], int n)
46 {    
47     int i,j;
48     for ( i = 0; i < n - 1; i++)
49     {
50         for (j = 0; j < n-1-i; j++)
51         {
52             if (x[j].sales_count < x[j+1].sales_count)
53             {
54                 Book temp = x[j];
55                 x[j] = x[j+1];
56                 x[j+1] = temp;
57             }
58         }
59     }
60 }
61 double sales_amount(Book x[], int n)
62 {
63     double sum = 0;
64     int i;
65     for ( i = 0; i < n; i++)
66     {
67         sum += x[i].sales_price * x[i].sales_count;
68     }
69     return sum;
70 }
 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-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51},
18                   {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30},
19                   {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27},
20                   {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 
21                   {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
22                   {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42},
23                   {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44},
24                   {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
25                   {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
26                   {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}};
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 void output(Book x[], int n)
37 {    
38     int i; 
39     printf("ISBN号             书名             作者             售价     销售册数\n");
40     for (i = 0; i < n; i++)
41     {
42         printf("%-15s    %-25s    %-20s    %-5g    %-5d\n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count);
43     }
44 }
45 void sort(Book x[], int n)
46 {    
47     int i,j;
48     for ( i = 0; i < n - 1; i++)
49     {
50         for (j = 0; j < n-1-i; j++)
51         {
52             if (x[j].sales_count < x[j+1].sales_count)
53             {
54                 Book temp = x[j];
55                 x[j] = x[j+1];
56                 x[j+1] = temp;
57             }
58         }
59     }
60 }
61 double sales_amount(Book x[], int n)
62 {
63     double sum = 0;
64     int i;
65     for ( i = 0; i < n; i++)
66     {
67         sum += x[i].sales_price * x[i].sales_count;
68     }
69     return sum;
70 }

 

task5.

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

task6.

 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 
13 // 函数声明
14 void output(Account x[], int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示
15 
16 int main() {
17     Account x[] = {{"A1001", "123456", student},
18                     {"A1002", "123abcdef", student},
19                     {"A1009", "xyz12121", student}, 
20                     {"X1009", "9213071x", admin},
21                     {"C11553", "129dfg32k", teacher},
22                     {"X3005", "921kfmg917", student}};
23     int n;
24     n = sizeof(x)/sizeof(Account);
25     output(x, n);
26 
27     return 0;
28 }
29 void output(Account x[], int n) 
30 {
31     int i,j,sum;
32     char word[20];
33     
34     for(i=0;i<n;++i)
35     {
36         printf("%s\t",x[i].username);
37         sum=strlen(x[i].password);
38         for(j=0;j<sum;++j)
39             word[j]='*';
40         word[j]='\0';
41         printf("%-20s",word);
42         switch(x[i].type)
43         {
44             case 0:printf("admin\n");break;
45             case 1:printf("student\n");break;
46             case 2:printf("teacher\n");break;
47             default:printf("somethint must be wrong");break;
48         }
49     }
50          
51 }

task7.

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 typedef struct {
 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 
15 #define N 10
16 int main() {
17     Contact list[N] = {{"刘一", "15510846604", 0},
18                        {"陈二", "18038747351", 0},
19                        {"张三", "18853253914", 0},
20                        {"李四", "13230584477", 0},
21                        {"王五", "15547571923", 0},
22                        {"赵六", "18856659351", 0},
23                        {"周七", "17705843215", 0},
24                        {"孙八", "15552933732", 0},
25                        {"吴九", "18077702405", 0},
26                        {"郑十", "18820725036", 0}};
27     int vip_cnt, i;
28     char name[20];
29 
30     printf("显示原始通讯录信息: \n"); 
31     output(list, N);
32 
33     printf("\n输入要设置的紧急联系人个数: ");
34     scanf("%d", &vip_cnt);
35     
36     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
37     for(i = 0; i < vip_cnt; ++i) {
38         scanf("%s", name);
39         set_vip_contact(list, N, name);
40     }
41 
42     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
43     display(list, N);
44 
45     return 0;
46 }
47 
48 void set_vip_contact(Contact x[], int n, char name[]) {
49     int i;
50    for(i=0;i<n;i++){
51        if(strcmp(x[i].name,name)==0){
52            x[i].vip=1;
53        }
54    }
55 }
56 
57 void display(Contact x[], int n) {
58     Contact temp;
59     int i,j;
60     for(i=0;i<n-1;i++){
61         for(j=0;j<n-i-1;j++){
62              if (x[j].vip < x[j + 1].vip) {
63                 temp = x[j];
64                 x[j] = x[j + 1];
65                 x[j + 1] = temp;
66             } else if (x[j].vip == x[j + 1].vip && strcmp(x[j].name, x[j + 1].name) > 0) {
67                 temp = x[j];
68                 x[j] = x[j + 1];
69                 x[j + 1] = temp;
70             }
71         }
72     }
73     output(x,n);
74 }
75 
76 void output(Contact x[], int n) {
77     int i;
78 
79     for(i = 0; i < n; ++i) {
80         printf("%-10s%-15s", x[i].name, x[i].phone);
81         if(x[i].vip)
82             printf("%5s", "*");
83         printf("\n");
84     }
85 }

 

posted @ 2024-12-18 15:18  Goldie0316  阅读(8)  评论(0编辑  收藏  举报