实验6

发布时间 2023-12-11 15:47:52作者: 热爱睡觉

四、实验结论

4. 实验任务4 此部分书写内容: 正确补足后完整的task4.c源码,及,运行结果截图

 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 
32     printf("\n图书销售总额: %.2f\n", sales_amount(x, N));
33     
34     return 0;
35 }
36 
37 // 待补足:函数output()实现
38 void output(Book x[], int n){
39     int i;
40     
41     printf("ISBN号"); 
42     for(i=0;i<20;++i) printf(" ");
43     printf("书名");
44     for(i=0;i<22;++i) printf(" ");
45     printf("作者");
46     for(i=0;i<22;++i) printf(" ");
47     printf("售价");
48     for(i=0;i<12;++i) printf(" ");
49     printf("销售册数\n");
50     
51     
52     for(i=0;i<n;++i){
53         printf("%-25s %-25s %-25s %-15.1f %-15d\n",
54                 x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);    
55     } 
56 }
57 
58 
59 // 待补足:函数sort()实现
60 void sort(Book x[], int n){
61     int i,j;
62     int t;
63     
64     for(i=0;i<n-1;++i)
65         for(j=0;j<n-1-i;++j)
66             if(x[j].sales_count<x[j+1].sales_count){
67                 t=x[j].sales_count;
68                 x[j].sales_count=x[j+1].sales_count;
69                 x[j+1].sales_count=t;
70             } 
71 }
72 
73 // 待补足:函数sales_count()实现
74 double sales_amount(Book x[], int n){
75     double sales_amount=0;
76     int i;
77     
78     for(i=0;i<n;++i)
79         sales_amount+=x[i].sales_price*x[i].sales_count;
80     
81     return sales_amount;
82 }
View Code

 

5. 实验任务5 此部分书写内容: 正确补足后完整的task5.c源码,及,运行结果截图

  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     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 60 }
 61 
 62 // 补足函数day_of_year实现
 63 // 功能:返回日期d是这一年的第多少天
 64 int day_of_year(Date d) {
 65     int ans=0;
 66     int i;
 67     for(i=1;i<d.month;++i){
 68         switch(i){
 69             case 1:
 70             case 3:
 71             case 5:
 72             case 7:
 73             case 8:
 74             case 10:
 75             case 12: ans+=31; continue;
 76                     
 77             case 2: 
 78                 if((d.year%4==0&&d.year%100!=0)||d.year%400==0)  ans+=29;    
 79                 else  ans+=28;    
 80                 continue;
 81                             
 82             default: ans+=30; continue;
 83         }        
 84     }
 85     ans+=d.day;
 86     
 87     return ans;
 88 }
 89 
 90 // 补足函数compare_dates实现
 91 // 功能:比较两个日期: 
 92 // 如果d1在d2之前,返回-1;
 93 // 如果d1在d2之后,返回1
 94 // 如果d1和d2相同,返回0
 95 int compare_dates(Date d1, Date d2) {
 96     if(d1.year<d2.year)
 97         return -1;
 98         
 99     else if(d1.year==d2.year&&d1.month<d2.month)
100         return -1;
101         
102     else if(d1.year==d2.year&&d1.month==d2.month&&
103             day_of_year(d1)<day_of_year(d2))
104         return -1;
105         
106     else if(d1.year==d2.year&&d1.month==d2.month&&
107             day_of_year(d1)==day_of_year(d2))
108         return 0;
109         
110     else return 1;
111 }
View Code

 

6. 实验任务6 此部分书写内容: 正确补足后完整的task6.c源码,及,运行结果截图

 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     
36     for(k=0;k<n;++k){
37         printf("%-20s",x[k].username);
38         
39         for(j=0;x[k].password[j]!='\0';++j);
40         for(i=0;i<j;++i) printf("*");
41         for(i=0;i<20-j;++i) printf(" ");
42             
43         switch(x[k].type){
44             case 0: printf("admin\n");break;
45             case 1: printf("student\n");break;
46             case 2: printf("teacher\n");break;
47         }  
48              
49     }
50 
51 }
View Code

 

7. 实验任务7(选做*) 如果选做,此部分书写内容: 正确补足后完整的task7.c源码,及,运行结果截图

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 typedef struct {
  5     char name[20];      // 姓名
  6     char phone[12];     // 手机号
  7     int  vip;           // 是否为紧急联系人,是取1;否则取0
  8 } Contact; 
  9 
 10 
 11 // 函数声明
 12 void set_vip_contact(Contact x[], int n, char name[]);  // 设置紧急联系人
 13 void output(Contact x[], int n);    // 输出x中联系人信息
 14 void display(Contact x[], int n);   // 按联系人姓名字典序升序显示信息,紧急联系人最先显示
 15 
 16 void sort(Contact x[],int start,int end);
 17 void exchange(Contact x[],int a,int b);
 18 
 19 #define N 10
 20 int main() {
 21     Contact list[N] = {{"刘一", "15510846604", 0},
 22                        {"陈二", "18038747351", 0},
 23                        {"张三", "18853253914", 0},
 24                        {"李四", "13230584477", 0},
 25                        {"王五", "15547571923", 0},
 26                        {"赵六", "18856659351", 0},
 27                        {"周七", "17705843215", 0},
 28                        {"孙八", "15552933732", 0},
 29                        {"吴九", "18077702405", 0},
 30                        {"郑十", "18820725036", 0}};
 31     int vip_cnt, i;
 32     char name[20];
 33 
 34     printf("显示原始通讯录信息: \n"); 
 35     output(list, N);
 36 
 37     printf("\n输入要设置的紧急联系人个数: ");
 38     scanf("%d", &vip_cnt);
 39     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
 40     for(i = 0; i < vip_cnt; ++i) {
 41         scanf("%s", name);
 42         set_vip_contact(list, N, name);
 43     }
 44     
 45     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
 46     display(list, N);
 47     
 48     return 0;
 49 }
 50 
 51 // 补足函数set_vip_contact实现
 52 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1)
 53 void set_vip_contact(Contact x[], int n, char name[]) {
 54     int i;
 55     for(i=0;i<n;++i)
 56         if(strcmp(x[i].name,name)==0)
 57             x[i].vip=1;
 58 }
 59 
 60 // 补足函数display实现
 61 // 功能: 显示联系人数组x中的联系人信息
 62 //      按姓名字典序升序显示, 紧急联系人显示在最前面
 63 void display(Contact x[], int n) {
 64     int i;
 65     int vip_cnt=0;
 66     
 67     sort(x,0,n);
 68     
 69     for(i=0;i<n;++i)
 70         if(x[i].vip==1){
 71             exchange(x,vip_cnt,i);
 72             vip_cnt++;
 73         }
 74         
 75     sort(x,vip_cnt,n);
 76     
 77     output(x, n);         
 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 }
 90 
 91 
 92 void sort(Contact x[],int start,int end){
 93     int i,j;
 94     
 95     for(i=start;i<end-1;++i)
 96         for(j=start;j<end-1-i;++j)
 97             if(strcmp(x[j].name,x[j+1].name)>0)
 98                 exchange(x,j,j+1);                
 99 }
100 
101 void exchange(Contact x[],int a,int b){
102     char t[20];
103     int temp;
104     
105     strcpy(t,x[a].name);
106     strcpy(x[a].name,x[b].name);
107     strcpy(x[b].name,t);
108                 
109     strcpy(t,x[a].phone);
110     strcpy(x[a].phone,x[b].phone);
111     strcpy(x[b].phone,t);
112                 
113     temp=x[a].vip;
114     x[a].vip=x[b].vip;
115     x[b].vip=temp;    
116 } 
View Code