task4.c
 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-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 
37 
38 void output(Book x[], int n) {
39     for (int i = 0; i < n; i++) {
40         printf("%-20s%-26s%-20s\t%0.1lf\t%d\n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count);
41     }
42 }
43 
44 
45 void sort(Book x[], int n) {
46     Book t;
47     for (int i = 0; i < n ; i++) {
48         for (int j = 0; j < n - 1 - i; j++) {
49             if (x[j].sales_count < x[j + 1].sales_count) {
50                 t = x[j];
51                 x[j] = x[j + 1];
52                 x[j + 1] = t;
53             }
54         }
55     }
56 }
57 
58 double sales_amount(Book x[], int n) {
59     double ans = 0;
60     for (int i = 0; i < n; i++) {
61         ans += x[i].sales_count * x[i].sales_price;
62     }
63     return ans;
64 }

task5.c

 

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

task6.c

 

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

 task7.c

 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 
11 void set_vip_contact(Contact x[], int n, char name[]);  
12 void output(Contact x[], int n);  
13 void display(Contact x[], int n);   
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 
50     for (int i = 0; i < n; i++) {
51         if (strcmp(x[i].name,name) == 0) {
52             x[i].vip = 1;
53         }
54     }
55 }
56 
57 
58 void display(Contact x[], int n) {
59     
60     Contact t;
61     for (int i = 0; i < n; i++) {
62         for (int j = 0; j < n - 1 - i; j++) {
63             if (strcmp(x[j].name, x[j + 1].name) > 0) {
64                 t = x[j];
65                 x[j] = x[j + 1];
66                 x[j + 1] = t;
67             }
68         }
69     }
70     for (int i = 0; i < n; i++) {
71         if (x[i].vip) {
72             printf("%-10s%-15s", x[i].name, x[i].phone);
73             if (x[i].vip)
74                 printf("%5s", "*");
75             printf("\n");
76         }
77     }
78     for (int i = 0; i < n; i++) {
79         if (!x[i].vip) {
80             printf("%-10s%-15s", x[i].name, x[i].phone);
81             if (x[i].vip)
82                 printf("%5s", "*");
83             printf("\n");
84         }
85     }
86 }
87 
88 void output(Contact x[], int n) {
89     int i;
90 
91     for (i = 0; i < n; ++i) {
92         printf("%-10s%-15s", x[i].name, x[i].phone);
93         if (x[i].vip)
94             printf("%5s", "*");
95         printf("\n");
96     }
97 }