实验6
任务4
源代码
1 #include<stdio.h> 2 #define N 10 3 typedef struct{ 4 char isbn[20]; 5 char name[80]; 6 char author[80]; 7 double sales_price; 8 int sales_count; 9 }Book; 10 11 void output(Book x[],int n); 12 void sort(Book x[],int n); 13 double sales_amount(Book x[],int n); 14 15 int main(){ 16 Book x[N]={ {"978-7-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51}, 17 {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49,30 }, 18 {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27}, 19 {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 20 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 21 {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42}, 22 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}, 23 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, 24 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, 25 {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}}; 26 printf("图书销量排名(按销售册数):\n"); 27 printf("ISBN号 书名 作者 售价 销售册数\n"); 28 sort(x,N); 29 output(x,N); 30 printf("\n图书销售总额:%.2f\n",sales_amount(x,N)); 31 return 0; 32 33 } 34 35 void sort(Book x[],int n){ 36 int i,j; 37 Book t; 38 for(i=0;i<n-1;i++) 39 for(j=0;j<n-1-i;j++){ 40 if(x[j].sales_count<x[j+1].sales_count){ 41 t=x[j]; 42 x[j]=x[j+1]; 43 x[j+1]=t; 44 } 45 } 46 } 47 void output(Book x[],int n){ 48 int i; 49 for(i=0;i<n;i++){ 50 printf("%s %-25s %-20s %-9g %d\n",x[i].isbn,x[i].name,x[i].author, 51 x[i].sales_price,x[i].sales_count); 52 } 53 } 54 double sales_amount(Book x[],int n){ 55 int i; 56 double total=0; 57 for(i=0;i<n;i++){ 58 total+=x[i].sales_price*x[i].sales_count; 59 } 60 61 return total; 62 63 } 64
结果
任务5
源代码
1 #include<stdio.h> 2 typedef struct { 3 int year; 4 int month; 5 int day; 6 }date; 7 8 void input(date *pd); 9 int day_of_year(date d); 10 int compare_dates(date d1,date d2); 11 12 void test1(){ 13 date d; 14 int i; 15 printf("输入日期:(以形如2024-12-16这样的形式输入)\n"); 16 for(i=0;i<3;i++){ 17 input(&d); 18 printf("%d-%02d-%02d是这一年中的第%d天\n\n",d.year,d.month, 19 d.day,day_of_year(d)); 20 } 21 } 22 void test2(){ 23 date alice_birth,bob_birth; 24 int i; 25 int ans; 26 printf("输入Alice和bob出生日期:(形如2024-12-16这样的)\n"); 27 for(i=0;i<3;i++){ 28 input(&alice_birth); 29 input(&bob_birth); 30 ans=compare_dates(alice_birth,bob_birth); 31 if(ans==0) 32 printf("alice和bob一样大\n\n"); 33 else if(ans==-1) 34 printf("alice比bob大\n\n"); 35 else 36 printf("alice比bob小\n\n"); 37 38 } 39 } 40 int main(){ 41 printf("测试1:输入日期,打印输出这是一年中第多少天\n"); 42 test1(); 43 printf("\n测试2:两个人年龄大小关系\n"); 44 test2(); 45 } 46 void input(date *pd){ 47 scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day); 48 49 } 50 51 int day_of_year(date d){ 52 int x; 53 if(d.month==1) 54 x=d.day; 55 else if(d.month==2) 56 x=31+d.day; 57 else if(d.month==3) 58 x=59+d.day; 59 else if(d.month==4) 60 x=90+d.day; 61 else if(d.month==5) 62 x=120+d.day; 63 else if(d.month==6) 64 x=151+d.day; 65 else if(d.month==7) 66 x=181+d.day; 67 else if(d.month==8) 68 x=212+d.day; 69 else if(d.month==9) 70 x=243+d.day; 71 else if(d.month==10) 72 x=273+d.day; 73 else if(d.month==11) 74 x=304+d.day; 75 else 76 x=334+d.day; 77 if(d.year%400==0||(d.year%4==0&&d.year%100!=0)&&d.month>=3){ 78 x+=1; 79 } 80 return x; 81 } 82 int compare_dates(date d1,date d2){ 83 if(d1.year<d2.year) 84 return -1; 85 else if(d1.year>d2.year) 86 return 1; 87 else 88 if(d1.month<d2.month) 89 return -1; 90 else if(d1.month>d2.month) 91 return 1; 92 else 93 if(d1.day<d2.day) 94 return -1; 95 else if(d1.day>d2.day) 96 return 1; 97 else 98 return 0; 99 }
结果
任务6
源代码
1 #include<stdio.h> 2 #include<string.h> 3 enum Role{ 4 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 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("%-15s",x[i].username); 33 for(j=0;j<strlen(x[i].password)+10;j++){ 34 if(j>strlen(x[i].password)&&j<16) 35 printf(" "); 36 else if(j<=strlen(x[i].password)) 37 printf("*"); 38 } 39 switch(x[i].type){ 40 case admin: 41 printf("%5s\n","admin"); 42 break; 43 case student: 44 printf("%5s\n","student"); 45 break; 46 case teacher: 47 printf("%5s\n","teacher"); 48 break; 49 } 50 } 51 }
结果
实验7
源代码
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 int cmp_name(const void *a,const void *b); 14 15 #define N 10 16 int main(){ 17 contact list[N]= {{"刘一", "15510846604", 0}, 18 {"陈二", "18038747351", 0}, 19 {"张三", "18853253914", 0}, 20 {"李四", "13230584477", 0}, 21 {"王五", "15547571923", 0}, 22 {"赵六", "18856659351", 0}, 23 {"周七", "17705843215", 0}, 24 {"孙八", "15552933732", 0}, 25 {"吴九", "18077702405", 0}, 26 {"郑十", "18820725036", 0}}; 27 int vip_cnt,i; 28 char name[20]; 29 30 printf("显示原始通讯录信息:\n"); 31 output(list,N); 32 33 printf("\n输入要设置的紧急联系人个数:"); 34 scanf("%d",&vip_cnt); 35 printf("输入%d个紧急联系人姓名:\n",vip_cnt); 36 for(i=0;i<vip_cnt;i++){ 37 scanf("%s",name); 38 set_vip_contact(list,N,name); 39 } 40 41 printf("\n显示通讯录列表:(按姓名字典升序排列,紧急联系人最先显示)\n"); 42 display(list,N); 43 44 return 0; 45 } 46 47 void set_vip_contact(contact x[],int n,char name[]){ 48 int i; 49 for(i=0;i<n;i++){ 50 if(strcmp(x[i].name,name)==0){ 51 x[i].vip=1; 52 break; 53 } 54 } 55 } 56 void display(contact x[],int n){ 57 int i,j; 58 contact t; 59 for(i=0;i<n;i++) 60 for(j=0;j<n-1-i;j++){ 61 if(strcmp(x[j].name,x[j+1].name)>0){ 62 t=x[j]; 63 x[j]=x[j+1]; 64 x[j+1]=t; 65 } 66 } 67 for(i=0;i<n;i++){ 68 if(x[i].vip==1){ 69 printf("%-10s%-15s%-15c\n",x[i].name,x[i].phone,'*'); 70 } 71 } 72 for(i=0;i<n;i++){ 73 if(x[i].vip==0){ 74 printf("%-10s%-15s\n",x[i].name,x[i].phone); 75 } 76 } 77 } 78 79 80 void output(contact x[],int n){ 81 int i; 82 83 for(i=0;i<n;++i){ 84 printf("%-10s%-15s",x[i].name,x[i].phone); 85 if(x[i].vip) 86 printf("%5s","*"); 87 printf("\n"); 88 } 89 }
结果