实验6
实验任务4
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 10 4 5 typedef struct{ 6 char isbn[20]; 7 char name[80]; 8 char author[80]; 9 double sales_prince; 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-5327-6082-4","门将之死","罗纳德.伦",42,51}, 19 {"978-7-308-17047-5","自由与爱之地:入以色列记","云也退",49,30}, 20 {"978-7-5404-9344-8","伦敦人","克莱格泰勒",68,27}, 21 {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 22 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 23 {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42}, 24 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}, 25 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, 26 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5,55}, 27 {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}}; 28 29 printf("图书销量排名(按销售册数:\n)"); 30 sort(x,N); 31 32 output(x,N); 33 34 printf("\n图书销售总额:%.2f\n",sales_amount(x,N)); 35 36 system("pause"); 37 return 0; 38 } 39 40 void output(Book x[],int n){ 41 int i; 42 printf("ISBN号\t\t书名\t\t作者\t售价\t销售册\n"); 43 for(i = 0;i < n;i++){ 44 printf("%s\t\t%s\t\t%s\t%g\t%d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_prince, x[i].sales_count); 45 46 } 47 } 48 49 void sort(Book x[],int n){ 50 int i,j; 51 Book t; 52 for(i = 0;i < n;i++){ 53 for(j=0;j<n;j++){ 54 if(x[j].sales_count<x[j+1].sales_count){ 55 t = x[j]; 56 x[j] = x[j+1]; 57 x[j+1] = t; 58 } 59 } 60 } 61 62 } 63 64 double sales_amount(Book x[],int n){ 65 double sum=0.0; 66 int i; 67 for(i=0;i<n;i++){ 68 sum += x[i].sales_count * x[i].sales_prince; 69 } 70 return sum; 71 }
运行结果截图

实验任务5
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct{ 5 int year; 6 int month; 7 int day; 8 }Date; 9 10 void input(Date *pd);//输入日期 11 int day_of_year(Date d);//返回日期d是这一年的第多少天 12 int compare_dates(Date d1,Date d2);//比较两个日期,d1在d2前,返回-1;在之后,返回1;相同,返回0。 13 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,day_of_year(d)); 21 } 22 } 23 24 void test2(){ 25 Date Alice_birth,Bob_birth; 26 int i; 27 int ans; 28 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 45 int main(){ 46 printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); 47 test1(); 48 49 printf("\n测试2: 两个人年龄大小关系\n"); 50 test2(); 51 52 system("pause"); 53 return 0; 54 } 55 56 void input(Date *pd){ 57 scanf("%d-%02d-%02d",&pd->year,&pd->month,&pd->day); 58 } 59 60 61 int day_of_year(Date d){ 62 int i; 63 int sum = 0; 64 if(d.month >= 12) 65 sum +=334; 66 else if(d.month >= 11) 67 sum +=304; 68 else if(d.month >= 10) 69 sum +=273; 70 else if(d.month >= 9) 71 sum +=243; 72 else if(d.month >= 8) 73 sum +=212; 74 else if(d.month >= 7) 75 sum +=181; 76 else if(d.month >= 6) 77 sum +=151; 78 else if(d.month >= 5) 79 sum +=120; 80 else if(d.month >= 4) 81 sum +=90; 82 else if(d.month >= 3) 83 sum +=59; 84 else if(d.month >= 2) 85 sum +=31; 86 else 87 sum +=0; 88 89 if(d.month >= 3 && ((d.year % 4 == 0 && d.year % 100 != 0)||d.year % 400 == 0)) 90 sum +=1; 91 92 sum +=d.day; 93 return sum; 94 } 95 96 int compare_dates(Date d1,Date d2){ 97 if(d1.year == d2.year){ 98 if(d1.month == d2.month){ 99 if(d1.day == d2.day) 100 return 0; 101 else if(d1.day < d2.day) 102 return -1; 103 else 104 return 1; 105 } 106 else if(d1.month < d2.month) 107 return -1; 108 else 109 return 1; 110 } 111 else if(d1.year < d2.year) 112 return -1; 113 else 114 return 1; 115 116 117 }
运行结果截图

实验任务6
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 enum Role{admin,student,teacher}; 6 7 typedef struct{ 8 char username[20]; 9 char password[20]; 10 enum Role type; 11 } Account; 12 13 void output(Account x[],int n); 14 const char* role_to_str(enum Role m){ 15 switch(m){ 16 case 0:return "admin"; 17 case 1:return "student"; 18 case 2:return "teacher"; 19 default:return "unknown"; 20 } 21 } 22 23 int main(){ 24 Account x[] = {{"A1001", "123456", student}, 25 {"A1002", "123abcdef", student}, 26 {"A1009", "xyz12121", student}, 27 {"X1009", "9213071x", admin}, 28 {"C11553", "129dfg32k", teacher}, 29 {"X3005", "921kfmg917", student}}; 30 int n; 31 n = sizeof(x)/sizeof(Account); 32 output(x,n); 33 34 system("pause"); 35 return 0; 36 } 37 38 void output(Account x[],int n){ 39 int i,j; 40 int length; 41 for(i=0;i<n;i++){ 42 printf("%s\t",x[i].username); 43 length = strlen(x[i].password); 44 for(j=0;j<length;j++){ 45 printf("*"); 46 } 47 48 printf("\t%s\n",role_to_str(x[i].type)); 49 } 50 51 }
运行结果截图

实验任务7
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 10 5 6 typedef struct{ 7 char name[20]; 8 char phone[12]; 9 int vip; 10 }Contact; 11 12 void set_vip_contact(Contact x[],int n,char name[]); 13 void output(Contact x[],int n); 14 void display(Contact x[],int n); 15 16 17 18 int main(){ 19 Contact list[N] = {{"刘一", "15510846604", 0}, 20 {"陈二", "18038747351", 0}, 21 {"张三", "18853253914", 0}, 22 {"李四", "13230584477", 0}, 23 {"王五", "15547571923", 0}, 24 {"赵六", "18856659351", 0}, 25 {"周七", "17705843215", 0}, 26 {"孙八", "15552933732", 0}, 27 {"吴九", "18077702405", 0}, 28 {"郑十", "18820725036", 0}}; 29 int vip_cnt,i; 30 char name[20]; 31 32 printf("显示原始通讯录信息:\n"); 33 output(list,N); 34 35 printf("\n输入要设置的紧急联系人个数:"); 36 scanf("%d",&vip_cnt); 37 printf("输入%d个紧急联系人姓名:\n",vip_cnt); 38 for(i=0;i<vip_cnt;++i){ 39 scanf("%s",name); 40 set_vip_contact(list,N,name); 41 } 42 43 printf("\n显示通讯录列表:(按姓名字典升序排列,紧急联系人最先显示)\n"); 44 display(list,N); 45 46 system("pause"); 47 return 0; 48 } 49 50 void output(Contact x[],int n){ 51 int i; 52 53 for(i=0;i<n;++i){ 54 printf("%-10s%-15s",x[i].name,x[i].phone); 55 if(x[i].vip) 56 printf("%5s","*"); 57 printf("\n"); 58 } 59 } 60 61 void set_vip_contact(Contact x[],int n,char name[]){ 62 int i; 63 for(i=0;i<n;i++){ 64 if(strcmp(x[i].name,name) == 0) 65 x[i].vip = 1; 66 67 } 68 } 69 70 void display(Contact x[],int n){ 71 int i,j; 72 Contact t; 73 for(i=0;i<n-1;i++){ 74 for(j=0;j<n-1-i;j++){ 75 if(x[j].vip < x[j+1].vip || (x[j].vip == x[j+1].vip && strcmp(x[j].name,x[j+1].name)>0)){ 76 t = x[j]; 77 x[j] = x[j+1]; 78 x[j+1] = t; 79 } 80 } 81 } 82 for(i = 0;i<n;i++){ 83 printf("%s\t%s\t%s\n",x[i].name,x[i].phone,x[i].vip?"*":" "); 84 } 85 }
运行结果截图

浙公网安备 33010602011771号