实验6

实验任务4

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

屏幕截图 2025-12-25 210111

实验任务5

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

屏幕截图 2025-11-16 185915

实验任务6

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 enum Role { admin, student, teacher };
 7 typedef struct {
 8     char username[20];  // 用户名
 9     char password[20];  // 密码
10     enum Role type;     // 账户类型
11 } Account;
12 // 函数声明
13 void output(Account x[], int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示
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     return 0;
25 }
26 // 待补足的函数output()实现
27 // 功能:遍历输出账户数组x中n个账户信息
28 //      显示时,密码字段以与原密码相同字段长度的*替代显示
29 void output(Account x[], int n) {
30     int i, j;
31     for (i = 0; i < n; i++) {
32         printf("%-10s", x[i].username);
33         for (j = 0; j < strlen(x[i].password); j++) {
34             putchar('*');
35         }
36         printf("\t");
37         switch (x[i].type) {
38         case admin:
39             printf("admin");
40             break;
41         case student:
42             printf("student");
43             break;
44         case teacher:
45             printf("teacher");
46             break;
47         }
48         printf("\n");
49     }
50 }
View Code

屏幕截图 2025-12-26 002402

实验任务7

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 typedef struct {
 7     char name[20];
 8     char phone[12];
 9     int  vip;
10 } Contact;
11 // 姓名
12 // 手机号
13 // 是否为紧急联系人,是取1;否则取0
14 // 函数声明
15 void set_vip_contact(Contact x[], int n, char name[]);  // 设置紧急联系人
16 void output(Contact x[], int n);
17 // 输出x中联系人信息
18 void display(Contact x[], int n);   // 按联系人姓名字典序升序显示信息,紧急联系人最先显示
19 #define N 10
20 int main() {
21     Contact list[N] = { {"刘一", "15510846604", 0},
22                            {"陈二", "18038747351", 0},
23                            {"张三", "18853253914", 0},
24                            {"李四", "13230584477", 0},
25                            {"王五", "15547571923", 0},
26                            {"赵六", "18856659351", 0},
27                            {"周七", "17705843215", 0},
28                            {"孙八", "15552933732", 0},
29                            {"吴九", "18077702405", 0},
30                            {"郑十", "18820725036", 0} };
31     int vip_cnt, i;
32     char name[20];
33     printf("显示原始通讯录信息: \n");
34     output(list, N);
35     printf("\n输入要设置的紧急联系人个数: ");
36     scanf("%d", &vip_cnt);
37     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
38     for (i = 0; i < vip_cnt; ++i) {
39         scanf("%s", name);
40         set_vip_contact(list, N, name);
41     }
42     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
43     display(list, N);
44     return 0;
45 }
46 // 补足函数set_vip_contact实现
47 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1)
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 // 补足函数display实现
56 // 功能: 显示联系人数组x中的联系人信息
57 //      按姓名字典序升序显示, 紧急联系人显示在最前面
58 void display(Contact x[], int n) {
59     int i, j;
60     Contact t;
61     for (i = 0; i < n - 1; ++i) {
62         for (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 (i = 0; i < n - 1; ++i) {
71         for (j = 0; j < n - 1 - i; ++j) {
72             if (x[j].vip < x[j + 1].vip) {
73                 t = x[j];
74                 x[j] = x[j + 1];
75                 x[j + 1] = t;
76             }
77         }
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 }
86 void output(Contact x[], int n) {
87     int i;
88     for (i = 0; i < n; ++i) {
89         printf("%-10s%-15s", x[i].name, x[i].phone);
90         if (x[i].vip)
91             printf("%5s", "*");
92         printf("\n");
93     }
94 }
View Code

屏幕截图 2025-12-26 005929

 

 

posted @ 2025-12-26 01:00  刘奕晨  阅读(5)  评论(0)    收藏  举报