4.任务4
1 #include <stdio.h> 2 #define N 10 3 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 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 32 printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); 33 34 return 0; 35 } 36 37 void output(Book x[], int n){ 38 int i; 39 printf("ISBN号 书名 作者 售价 销售册数\n"); 40 for(i=0;i<n-1;i++) 41 printf("%-17s %-25s %-20s %-6.0f %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count); 42 } 43 44 void sort(Book x[], int n){ 45 int i,j; 46 Book t; 47 for(i=0;i<n-1;i++) 48 for(j=0;j<n-1-i;j++) 49 if(x[j].sales_count<x[j+1].sales_count){ 50 t=x[j]; 51 x[j]=x[j+1]; 52 x[j+1]=t; 53 } 54 } 55 56 double sales_amount(Book x[], int n){ 57 int i; 58 double m=0.0; 59 for(i=0;i<n;i++){ 60 m=m+x[i].sales_price*x[i].sales_count; 61 } 62 return m; 63 }
5.实验5
1 #include <stdio.h> 2 3 typedef struct { 4 int year; 5 int month; 6 int day; 7 } Date; 8 9 void input(Date *pd); 10 int day_of_year(Date d); 11 int compare_dates(Date d1, Date d2); 12 13 14 void test1() { 15 Date d; 16 int i; 17 18 printf("输入日期:(以形如2023-12-11这样的形式输入)\n"); 19 for(i = 0; i < 3; ++i) { 20 input(&d); 21 printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d)); 22 } 23 } 24 25 void test2() { 26 Date Alice_birth, Bob_birth; 27 int i; 28 int ans; 29 30 printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n"); 31 for(i = 0; i < 3; ++i) { 32 input(&Alice_birth); 33 input(&Bob_birth); 34 ans = compare_dates(Alice_birth, Bob_birth); 35 36 if(ans == 0) 37 printf("Alice和Bob一样大\n\n"); 38 else if(ans == -1) 39 printf("Alice比Bob大\n\n"); 40 else 41 printf("Alice比Bob小\n\n"); 42 } 43 } 44 45 int main() { 46 printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); 47 test1(); 48 49 printf("\n测试2: 两个人年龄大小关系\n"); 50 test2(); 51 } 52 53 void input(Date *pd) { 54 scanf("%d-%d-%d",&(*pd).year,&(*pd).month,&(*pd).day); 55 56 } 57 58 int day_of_year(Date d) { 59 int i,a=0; 60 int x[13]={31,28,31,30,31,30,31,31,30,31,30,31}; 61 if((d.year%400==0)||d.year%4==0&&d.year%100!=0){ 62 x[1]=29; 63 } 64 for(i=0;i<d.month-1;i++){ 65 a=a+x[i]; 66 } 67 a=a+d.day; 68 return a; 69 70 71 } 72 73 int compare_dates(Date d1, Date d2) { 74 int x; 75 if(d1.year<d2.year ){ 76 x=-1; 77 return x; 78 } 79 else if(d1.year>d2.year){ 80 x=1; 81 return x; 82 } 83 else{ 84 if(d1.month<d2.month ){ 85 x=-1; 86 return x; 87 } 88 else if(d1.month>d2.month ){ 89 x=1; 90 return x; 91 } 92 else{ 93 if(d1.day<d2.day ){ 94 x=-1; 95 return x; 96 } 97 else if(d1.day>d2.day ){ 98 x=1; 99 return x; 100 } 101 else{x=0; 102 return x; 103 104 } 105 106 } 107 } 108 109 }
6.实验6
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 29 void output(Account x[], int n) { 30 int i,j; 31 for(i=0;i<n;i++){ 32 printf("%-10s",x[i].username); 33 for(j=0;j<strlen(x[i].password);j++){ 34 printf("*"); 35 } 36 printf("\t"); 37 switch(x[i].type){ 38 case 0:printf("admin\n");break; 39 case 1:printf("student\n");break; 40 case 2:printf("teacher\n"); 41 } 42 } 43 44 }
标签:int,31,C语言,枚举,实验,printf,Date,978,void From: https://www.cnblogs.com/TangZhiXuan/p/17894713.html