实验6
task4
1 #include <stdio.h> 2 #include <string.h> 3 #define N 10 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 void output(Book x[], int n); 12 void sort(Book x[], int n); 13 double sales_amount(Book x[], int n); 14 int main() { 15 Book x[N] = { {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}, 16 {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16}, 17 {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27}, 18 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 19 {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 20 42}, 21 {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39}, 22 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 23 37.5, 55}, 24 {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31}, 25 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 26 42}, 27 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 28 44} }; 29 printf("图书销量排名: \n"); 30 sort(x, N); 31 output(x, N); 32 printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); 33 return 0; 34 } 35 36 void output(Book x[], int n) { 37 printf("ISBN号\t\t\t书名\t\t\t\t作者\t\t\t售价\t销售册数\n"); 38 for (int i = 0; i < n; i++) { 39 printf("%-20s\t%-30s\t%-20s\t%g\t%d\n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count); 40 } 41 } 42 43 void sort(Book x[], int n) { 44 for (int i = 0; i < n; i++) { 45 for (int j = i; j < n; j++) { 46 if (x[i].sales_count <= x[j].sales_count) { 47 int t = x[i].sales_count; 48 x[i].sales_count = x[j].sales_count; 49 x[j].sales_count = t; 50 t = x[i].sales_price; 51 x[i].sales_price = x[j].sales_price; 52 x[j].sales_price = t; 53 char tt[100]; 54 strcpy(tt, x[i].isbn); 55 strcpy(x[i].isbn, x[j].isbn); 56 strcpy(x[j].isbn, tt); 57 strcpy(tt, x[i].name); 58 strcpy(x[i].name, x[j].name); 59 strcpy(x[j].name, tt); 60 strcpy(tt, x[i].author); 61 strcpy(x[i].author, x[j].author); 62 strcpy(x[j].author, tt); 63 } 64 } 65 } 66 } 67 68 double sales_amount(Book x[], int n) { 69 double sum = 0.0; 70 for (int i = 0; i < n; i++) { 71 sum += x[i].sales_price * x[i].sales_count * 1.0; 72 } 73 return sum; 74 }
task5
1 #include <stdio.h> 2 typedef struct { 3 int year; 4 int month; 5 int day; 6 } Date; 7 8 void input(Date* pd); 9 int day_of_year(Date d); 10 11 void test1() { 12 Date d; 13 int i; 14 printf("输入日期:(以形如2023-12-11这样的形式输入)\n"); 15 for (i = 0; i < 3; ++i) { 16 input(&d); 17 printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, 18 day_of_year(d)); 19 } 20 } 21 22 void test2() { 23 Date Alice_birth, Bob_birth; 24 int i; 25 int ans; 26 printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n"); 27 for (i = 0; i < 3; ++i) { 28 input(&Alice_birth); 29 input(&Bob_birth); 30 ans = compare_dates(Alice_birth, Bob_birth); 31 if (ans == 0) 32 printf("Alice和Bob一样大\n\n"); 33 else if (ans == -1) 34 printf("Alice比Bob大\n\n"); 35 else 36 printf("Alice比Bob小\n\n"); 37 } 38 } 39 40 int main() { 41 printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); 42 test1(); 43 printf("\n测试2: 两个人年龄大小关系\n"); 44 test2(); 45 } 46 47 void input(Date* pd) { 48 scanf("%d-%d-%d", &(*pd).year, &(*pd).month, &(*pd).day); 49 } 50 51 int day_of_year(Date d) { 52 int m[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 }; 53 if ((d.year % 4 == 0 && d.year % 400 == 0) || d.year % 4 == 0) { 54 m[2] = 29; 55 } 56 int sum = 0; 57 for (int i = 1; i <= d.month - 1; i++) { 58 sum += m[i]; 59 } 60 sum += d.day; 61 return sum; 62 } 63 64 int compare_dates(Date d1, Date d2) { 65 int ans; 66 if (d1.year < d2.year) { 67 ans = -1; 68 return ans; 69 } 70 else if (d1.year > d2.year) { 71 ans = 1; 72 return ans; 73 } 74 else { 75 if (d1.month < d2.month) { 76 ans = -1; 77 return ans; 78 } 79 else if (d1.month > d2.month) { 80 ans = 1; 81 return ans; 82 } 83 else { 84 if (d1.day < d2.day) { 85 ans = -1; 86 return ans; 87 } 88 else if (d1.day > d2.day) { 89 ans = 1; 90 return ans; 91 } 92 else { 93 ans = 0; 94 return ans; 95 } 96 } 97 } 98 }
task6
1 #include <stdio.h> 2 #include <string.h> 3 enum Role { admin, student, teacher }; 4 typedef struct { 5 char username[20]; 6 char password[20]; 7 enum Role type; 8 } Account; 9 10 void output(Account x[], int n); 11 int main() { 12 Account x[] = { {"A1001", "123456", student}, 13 {"A1002", "123abcdef", student}, 14 {"A1009", "xyz12121", student}, 15 {"X1009", "9213071x", admin}, 16 {"C11553", "129dfg32k", teacher}, 17 {"X3005", "921kfmg917", student} }; 18 int n; 19 n = sizeof(x) / sizeof(Account); 20 output(x, n); 21 return 0; 22 } 23 24 void output(Account x[], int n) { 25 for (int i = 0; i < n; i++) { 26 printf("%s\t", x[i].username); 27 int s = strlen(x[i].password); 28 for (int i = 0; i < s; i++) { 29 printf("*"); 30 } 31 if (s < 7) { 32 printf("\t\t"); 33 } 34 else { 35 printf("\t"); 36 } 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 } 47 printf("\n"); 48 } 49 }
task7
1 #include <stdio.h> 2 #include <string.h> 3 typedef struct { 4 char name[20]; 5 char phone[12]; 6 int vip; 7 } Contact; 8 9 void set_vip_contact(Contact x[], int n, char name[]); 10 void output(Contact x[], int n); 11 void display(Contact x[], int n); 12 13 #define N 10 14 int main() { 15 Contact list[N] = { {"刘一", "15510846604", 0}, 16 {"陈二", "18038747351", 0}, 17 {"张三", "18853253914", 0}, 18 {"李四", "13230584477", 0}, 19 {"王五", "15547571923", 0}, 20 {"赵六", "18856659351", 0}, 21 {"周七", "17705843215", 0}, 22 {"孙八", "15552933732", 0}, 23 {"吴九", "18077702405", 0}, 24 {"郑十", "18820725036", 0} }; 25 int vip_cnt, i; 26 char name[20]; 27 printf("显示原始通讯录信息: \n"); 28 output(list, N); 29 printf("\n输入要设置的紧急联系人个数: "); 30 scanf("%d", &vip_cnt); 31 printf("输入%d个紧急联系人姓名:\n", vip_cnt); 32 for (i = 0; i < vip_cnt; ++i) { 33 scanf("%s", name); 34 set_vip_contact(list, N, name); 35 } 36 printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n"); 37 display(list, N); 38 return 0; 39 } 40 41 void set_vip_contact(Contact x[], int n, char name[]) { 42 for (int i = 0; i < n; i++) { 43 if (strcmp(x[i].name, name) == 0) { 44 x[i].vip = 1; 45 } 46 } 47 } 48 49 void display(Contact x[], int n) { 50 51 for (int i = n - 1; i >= 0; i--) { 52 for (int j = i; j >= 0; j--) { 53 if (x[i].vip == 1 && x[j].vip == 0) { 54 char nn[100]; 55 strcpy(nn, x[i].name); 56 strcpy(x[i].name, x[j].name); 57 strcpy(x[j].name, nn); 58 strcpy(nn, x[i].phone); 59 strcpy(x[i].phone, x[j].phone); 60 strcpy(x[j].phone, nn); 61 int ss = x[i].vip; 62 x[i].vip = x[j].vip; 63 x[j].vip = ss; 64 } 65 } 66 } 67 68 for (int i = 0; i < n; i++) { 69 for (int j = i; j < n; j++) { 70 if (strcmp(x[i].name, x[j].name) > 0 && x[i].vip != 1 && x[j].vip != 1) { 71 char nn[100]; 72 strcpy(nn, x[j].name); 73 strcpy(x[j].name, x[i].name); 74 strcpy(x[i].name, nn); 75 strcpy(nn, x[j].phone); 76 strcpy(x[j].phone, x[i].phone); 77 strcpy(x[i].phone, nn); 78 } 79 } 80 } 81 82 for (int i = 0; i < n; i++) { 83 for (int j = i; j < n; j++) { 84 if (x[i].vip == 1 && x[j].vip == 1) { 85 if (strcmp(x[i].name, x[j].name) > 0){ 86 char nn[100]; 87 strcpy(nn, x[j].name); 88 strcpy(x[j].name, x[i].name); 89 strcpy(x[i].name, nn); 90 strcpy(nn, x[j].phone); 91 strcpy(x[j].phone, x[i].phone); 92 strcpy(x[i].phone, nn); 93 } 94 } 95 } 96 } 97 98 output(x, n); 99 } 100 void output(Contact x[], int n) { 101 int i; 102 for (i = 0; i < n; ++i) { 103 printf("%-10s%-15s", x[i].name, x[i].phone); 104 if (x[i].vip) 105 printf("%5s", "*"); 106 printf("\n"); 107 } 108 }