task4
源代码
1 #include <stdio.h> 2 #include<string.h> 3 #define N 10 4 5 typedef struct { 6 char isbn[20]; // isbn号 7 char name[80]; // 书名 8 char author[80]; // 作者 9 double sales_price; // 售价 10 int sales_count; // 销售册数 11 } Book; 12 13 void output(Book x[], int n); 14 void sort(Book x[], int n); 15 double sales_amount(Book x[], int n); 16 17 int main() { 18 Book x[N] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}, 19 {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16}, 20 {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27}, 21 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 22 {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42}, 23 {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39}, 24 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, 25 {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31}, 26 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, 27 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}}; 28 29 printf("图书销量排名: \n"); 30 sort(x, N); 31 output(x, N); 32 33 printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); 34 35 return 0; 36 } 37 38 // 待补足:函数output()实现 39 // ××× 40 void output(Book x[],int n){ 41 printf("ISBN号 书名 作者 售价 销售册数\n"); 42 int i; 43 for(i=0;i<n;i++){ 44 printf("%-25s%-30s%-25s%-15.1lf%-15d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count); 45 } 46 } 47 48 // 待补足:函数sort()实现 49 // ××× 50 void sort(Book x[],int n){ 51 52 int i,j,t; 53 for(i=0;i<n-1;++i){ 54 for(j=0;j<n-i-1;++j){ 55 if(x[j].sales_count<x[j+1].sales_count){ 56 t=x[j].sales_count; 57 x[j].sales_count=x[j+1].sales_count; 58 x[j+1].sales_count=t; 59 } 60 61 } 62 } 63 64 65 66 } 67 68 // 待补足:函数sales_count()实现 69 // ××× 70 double sales_amount(Book x[],int n){ 71 int i; 72 double s=0; 73 for(i=0;i<n;i++){ 74 75 s=s+x[i].sales_count*x[i].sales_price; 76 77 } 78 return s; 79 }
结果
task5
源代码
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, 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出生日期:(以形如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 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 // 补足函数input实现 57 // 功能: 输入日期给pd指向的Date变量 58 void input(Date *pd) { 59 60 scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day); 61 } 62 63 // 补足函数day_of_year实现 64 // 功能:返回日期d是这一年的第多少天 65 int day_of_year(Date d) { 66 int s=0; 67 int i; 68 for(i=1;i<d.month;i++){ 69 switch(i){ 70 case 1: 71 case 3: 72 case 5: 73 case 7: 74 case 8: 75 case 10: 76 case 12:s+=31;continue; 77 case 2: 78 if((d.year%4==0&&d.year%100!=0)||d.year%400==0){ 79 s+=29; 80 }else{ 81 s+=28; 82 }continue; 83 84 default :s+=30;continue; 85 } 86 } 87 s+=d.day; 88 89 return s; 90 } 91 92 // 补足函数compare_dates实现 93 // 功能:比较两个日期: 94 // 如果d1在d2之前,返回-1; 95 // 如果d1在d2之后,返回1 96 // 如果d1和d2相同,返回0 97 int compare_dates(Date d1, Date d2) { 98 if(d1.year<d2.year){ 99 return -1; 100 }else if(d1.year==d2.year&&d1.month<d2.month){ 101 return -1; 102 }else if(d1.year==d2.year&&d1.month==d2.month&&d1.day<d2.day){ 103 return -1; 104 }else if(d1.year==d2.year&&d1.month==d2.month&&d1.day==d2.day){ 105 return 0; 106 }else { 107 return 1; 108 } 109 }
结果
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 13 // 函数声明 14 void output(Account x[], int n); // 输出账户数组x中n个账户信息,其中,密码用*替代显示 15 16 int main() { 17 Account x[] = {{"A1001", "123456", student}, 18 {"A1002", "123abcdef", student}, 19 {"A1009", "xyz12121", student}, 20 {"X1009", "9213071x", admin}, 21 {"C11553", "129dfg32k", teacher}, 22 {"X3005", "921kfmg917", student}}; 23 int n; 24 n = sizeof(x)/sizeof(Account); 25 output(x, n); 26 27 return 0; 28 } 29 30 // 待补足的函数output()实现 31 // 功能:遍历输出账户数组x中n个账户信息 32 // 显示时,密码字段以与原密码相同字段长度的*替代显示 33 void output(Account x[], int n) { 34 int i,j,k; 35 int s; 36 for(i=0;i<n;i++){ 37 38 printf("%-15s",x[i].username); 39 s=strlen(x[i].password); 40 for(j=0;j<s;j++){ 41 printf("*"); 42 } 43 for(k=0;k<10;k++){ 44 printf(" "); 45 } 46 switch(x[i].type){ 47 case admin:printf("admin");break; 48 case student:printf("student");break; 49 case teacher:printf("teacher");break; 50 51 } 52 printf("\n"); 53 54 55 56 } 57 58 }
结果
标签:int,void,编程,978,枚举,实验,printf,Date,output From: https://www.cnblogs.com/meng36/p/17894679.html