#include <stdio.h>
#define N 10
typedef struct {
char isbn[20]; // isbn号
char name[80]; // 书名
char author[80]; // 作者
double sales_price; // 售价
int sales_count; // 销售册数
} Book;
void output(Book x[], int n);
void sort(Book x[], int n);
double sales_amount(Book x[], int n);
int main() {
Book x[N] = { {"978-7-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51},
{"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30},
{"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27},
{"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90},
{"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
{"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42},
{"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44},
{"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
{"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59} };
printf("图书销量排名(按销售册数): \n");
sort(x, N);
output(x, N);
printf("\n图书销售总额: %.2f\n", sales_amount(x, N));
return 0;
}
void output(Book x[], int n) {
int i;
printf("ISBN号 书名 作者 售价 销售册数\n");
for (i = 0; i < n; i++) {
printf("%-20s%-34s%-28s%-16.2f%-14d\n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count);
}
}
void sort(Book x[], int n) {
int i, j;
Book t;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++) {
if (x[j].sales_count < x[j + 1].sales_count) {
t = x[j];
x[j] = x[j + 1];
x[j + 1] = t;
}
}
}
}
double sales_amount(Book x[], int n) {
double sum = 0;
int i;
for (i = 0; i < n; i++) {
sum += x[i].sales_price * x[i].sales_count;
}
return sum;
}
![]()
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 scanf("%d-%d-%d", &(pd->year), &(pd->month), &(pd->day));
58 }
59
60 int day_of_year(Date d) {
61 int day = 0, i;
62 int days_in_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
63 if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0)) {
64 days_in_month[1] = 29;
65 }
66
67 for (i = 0; i < d.month - 1; i++) {
68 day += days_in_month[i];
69 }
70 day += d.day;
71
72 return day;
73 }
74
75 int compare_dates(Date d1, Date d2) {
76 int i, j;
77 i = day_of_year(d1);
78 j = day_of_year(d2);
79
80 if (i < j)
81 return -1;
82 else if (i > j)
83 return 1;
84 else
85 return 0;
86 }
![]()
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 void output(Account x[], int n);
14
15 int main() {
16 Account x[] = { {"A1001", "123456", student},
17 {"A1002", "123abcdef", student},
18 {"A1009", "xyz12121", student},
19 {"X1009", "9213071x", admin},
20 {"C11553", "129dfg32k", teacher},
21 {"X3005", "921kfmg917", student} };
22 int n;
23 n = sizeof(x) / sizeof(Account);
24 output(x, n);
25
26 return 0;
27 }
28 void output(Account x[], int n) {
29 int i, k;
30 for (i = 0; i < n; i++) {
31 for (k = 0; x[i].password[k] != '\0'; k++) {
32 x[i].password[k] = '*';
33 }
34 }
35 for (i = 0; i < n; i++) {
36
37 const char* role_str = (x[i].type == admin) ? "admin" :
38 (x[i].type == student) ? "student" :
39 "teacher";
40 printf("%-20s%-20s%-10s\n", x[i].username, x[i].password, role_str);
41 }
42 }
![]()
1 #include <stdio.h>
2 #include <string.h>
3
4 typedef struct {
5 char name[20]; // 姓名
6 char phone[12]; // 手机号
7 int vip; // 是否为紧急联系人,是取1;否则取0
8 } Contact;
9
10
11 // 函数声明
12 void set_vip_contact(Contact x[], int n, char name[]); // 设置紧急联系人
13 void output(Contact x[], int n); // 输出x中联系人信息
14 void display(Contact x[], int n); // 按联系人姓名字典序升序显示信息,紧急联系人最先显示
15
16
17 #define N 10
18 int main() {
19 Contact list[N] = { {"刘一", "15510846604", 0},
20 {"陈二", "18038747351", 0},
21 {"张三", "18853253914", 0},
22 {"李四", "13230584477", 0},
23 {"王五", "15547571923", 0},
24 {"赵六", "18856659351", 0},
25 {"周七", "17705843215", 0},
26 {"孙八", "15552933732", 0},
27 {"吴九", "18077702405", 0},
28 {"郑十", "18820725036", 0} };
29 int vip_cnt, i;
30 char name[20];
31
32 printf("显示原始通讯录信息: \n");
33 output(list, N);
34
35 printf("\n输入要设置的紧急联系人个数: ");
36 scanf("%d", &vip_cnt);
37
38 printf("输入%d个紧急联系人姓名:\n", vip_cnt);
39 for (i = 0; i < vip_cnt; ++i) {
40 scanf("%s", name);
41 set_vip_contact(list, N, name);
42 }
43
44 printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
45 display(list, N);
46
47 return 0;
48 }
49 void set_vip_contact(Contact x[], int n, char name[]) {
50 int i;
51 for (i = 0; i < n; i++) {
52 if (strcmp(x[i].name, name) == 0) {
53 x[i].vip = 1;
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 }
67 else if (x[j].vip == x[j + 1].vip && strcmp(x[j].name, x[j + 1].name) > 0) {
68 temp = x[j];
69 x[j] = x[j + 1];
70 x[j + 1] = temp;
71 }
72 }
73 }
74 output(x, n);
75 }
76
77 void output(Contact x[], int n) {
78 int i;
79
80 for (i = 0; i < n; ++i) {
81 printf("%-10s%-15s", x[i].name, x[i].phone);
82 if (x[i].vip)
83 printf("%5s", "*");
84 printf("\n");
85 }
86 }
![]()