实验6
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 37 void output(Book x[], int n) { 38 printf("%-25s %-40s %-20s %-10s %-10s\n", 39 "ISBN", "书名", "作者", "售价", "销售册数"); 40 for (int i = 0; i < n; i++) { 41 printf("%-25s %-40s %-20s %-10.3g %-10d\n", 42 x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count); 43 } 44 } 45 46 void sort(Book x[], int n) { 47 for (int i = 0; i < n - 1; i++) { 48 for (int j = 0; j < n - i - 1; j++) { 49 if (x[j].sales_count < x[j + 1].sales_count) { 50 Book temp = x[j]; 51 x[j] = x[j + 1]; 52 x[j + 1] = temp; 53 } 54 } 55 } 56 } 57 58 double sales_amount(Book x[], int n) { 59 double total = 0.0; 60 for (int i = 0; i < n; i++) { 61 total += x[i].sales_price * x[i].sales_count; 62 } 63 return total; 64 }
截图

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

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 void output(Account x[], int n); 13 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 25 return 0; 26 } 27 28 void output(Account x[], int n) { 29 for (int i = 0; i < n; i++) { 30 int password_len = strlen(x[i].password); 31 char password_mask[20] = ""; 32 33 for (int j = 0; j < password_len; j++) { 34 password_mask[j] = '*'; 35 } 36 password_mask[password_len] = '\0'; 37 38 char type_str[20]; 39 switch (x[i].type) { 40 case admin: 41 strcpy(type_str, "admin"); 42 break; 43 case student: 44 strcpy(type_str, "student"); 45 break; 46 case teacher: 47 strcpy(type_str, "teacher"); 48 break; 49 default: 50 break; 51 } 52 53 printf("%-15s %-20s %-10s\n", x[i].username, password_mask, type_str); 54 } 55 }
截图

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

浙公网安备 33010602011771号