task4 code
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-229-14156-1", "源泉", "安.兰德", 84, 59}, 18 {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16}, 19 {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27}, 20 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 21 {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49,42}, 22 {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39}, 23 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇",37.5, 55}, 24 {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31}, 25 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118,42}, 26 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5,44} }; 27 28 printf("图书销量排名: \n"); 29 sort(x, N); 30 output(x, N); 31 printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); 32 33 return 0; 34 } 35 36 void sort(Book x[], int n) { 37 int i, j; 38 Book temp; 39 Book* ptr = x; 40 41 for (i = 0; i < n - 1; i++) 42 for (j = 0; j < n - 1 - i; j++) { 43 if (x[j].sales_count < x[j + 1].sales_count) { 44 temp = x[j]; 45 x[j] = x[j+1]; 46 x[j + 1] = temp; 47 48 } 49 } 50 } 51 52 void output(Book x[], int n) { 53 Book* ptr; 54 int i; 55 56 printf("%-25s%-25s%-25s%-25s%-25s\n", "ISBN号", "书名", "作者", "售价", "销售册数"); 57 for (ptr = x; ptr < x + n; ptr++) 58 printf("%-25s%-25s%-25s%-25.1lf%-25d\n", ptr->isbn, ptr->name, ptr->author, ptr->sales_price, 59 ptr->sales_count); 60 } 61 62 double sales_amount(Book x[], int n) { 63 double total = 0; 64 int i; 65 66 for (i = 0; i < n; i++) 67 total += (x[i].sales_price * x[i].sales_count); 68 69 }View Code
task4 result
task5 code
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("输入日期:(以形如2023-12-11这样的形式输入)\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 33 printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\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 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 int t1, t2; 56 scanf("%d", &pd->year); 57 scanf("%d", &t1); 58 pd->month = -t1; 59 scanf("%d", &t2); 60 pd->day = -t2; 61 } 62 // 补足函数day_of_year实现 31 63 // 功能:返回日期d是这一年的第多少天 64 int day_of_year(Date d) { 65 int leap = 0; 66 int sum = 0; 67 68 if (d.year % 4 == 0 && d.year % 100 != 0 || d.year % 400 == 0) 69 leap = 1; 70 71 if (leap) { 72 switch (d.month) { 73 case 1:sum = 0; break; 74 case 2:sum = 31; break; 75 case 3:sum = 60; break; 76 case 4:sum = 91; break; 77 case 5:sum = 121; break; 78 case 6:sum = 152; break; 79 case 7:sum = 182; break; 80 case 8:sum = 213; break; 81 case 9:sum = 244; break; 82 case 10:sum = 274; break; 83 case 11:sum = 305; break; 84 case 12:sum = 334; break; 85 } 86 sum += d.day; 87 } 88 else { 89 switch (d.month) { 90 case 1:sum = 0; break; 91 case 2:sum = 31; break; 92 case 3:sum = 59; break; 93 case 4:sum = 90; break; 94 case 5:sum = 120; break; 95 case 6:sum = 151; break; 96 case 7:sum = 181; break; 97 case 8:sum = 212; break; 98 case 9:sum = 243; break; 99 case 10:sum = 273; break; 100 case 11:sum = 304; break; 101 case 12:sum = 334; break; 102 } 103 sum += d.day; 104 } 105 106 return sum; 107 } 108 // 补足函数compare_dates实现 109 // 功能:比较两个日期: 110 // 如果d1在d2之前,返回-1; 111 // 如果d1在d2之后,返回1 112 // 如果d1和d2相同,返回0 113 int compare_dates(Date d1, Date d2) { 114 if (d1.year < d2.year) 115 return -1; 116 else if (d1.year > d2.year) 117 return 1; 118 else { 119 if (day_of_year(d1) < day_of_year(d2)) 120 return -1; 121 else if (day_of_year(d1) > day_of_year(d2)) 122 return 1; 123 else 124 return 0; 125 } 126 127 }View Code
task5 result
task6 code
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); // 输出账户数组x中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 23 n = sizeof(x) / sizeof(Account); 24 output(x, n); 25 26 return 0; 27 } 28 // 功能:遍历输出账户数组x中n个账户信息 29 // 显示时,密码字段以与原密码相同字段长度的*替代显示 30 void output(Account x[], int n) { 31 int i, j, k; 32 char t[20] = {'\0'}; 33 34 for (i = 0; i < n; i++) { 35 k = strlen(x[i].password); 36 for (j = 0; j < k; j++) 37 t[j] = '*'; 38 t[k] = '\0'; 39 40 printf("%-9s %-9s ", x[i].username, t); 41 switch (x[i].type) { 42 case 0: printf(" admin"); break; 43 case 1: printf(" student"); break; 44 case 2: printf(" teacher"); break; 45 } 46 printf("\n"); 47 48 for (j = 0; j < 20; j++) 49 t[j] = '\0'; 50 } 51 }View Code
task6 result
标签:case,int,sum,printf,break,实验,year From: https://www.cnblogs.com/nuist0415/p/17894960.html