实验六

task4

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.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     system("pause");
36     return 0;
37 }
38 // 待补足:函数output()实现
39 void output(Book x[], int n)
40 {
41     printf("isbn号\t\t        书名\t\t\t     作者\t\t     售价     销售册数\n");
42     for(int i=0;i<n;i++)
43     {
44         printf("%s     %-30s %-25s %-10.0lf %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
45     }
46 }
47 // 待补足:函数sort()实现
48 void sort(Book x[], int n)
49 {
50     Book t;
51     for(int i=0;i<n-1;i++)
52     {
53         for(int j=0;j<n-i-1;j++)
54         {
55             if(x[j].sales_count<x[j+1].sales_count)
56             {
57                 t=x[j];
58                 x[j]=x[j+1];
59                 x[j+1]=t;
60             }
61         }
62     }
63 }
64 // 待补足:函数sales_count()实现
65 double sales_amount(Book x[], int n)
66 {
67     double avg;
68     int s=0;
69     for(int i=0;i<n;i++)
70     {
71         s+=x[i].sales_price;
72     }
73     avg=s/n;
74     return avg;
75 }

 

2.运行结果

 

task.5

1.源代码

  1 #include<stdio.h>
  2 #include<stdlib.h>
  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     system("pause");
 55     return 0;
 56 }
 57 // 补足函数input实现
 58 // 功能: 输入日期给pd指向的Date变量
 59 void input(Date *pd)
 60 {
 61     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 62 }
 63 
 64 // 补足函数day_of_year实现
 65 // 功能:返回日期d是这一年的第多少天
 66 int day_of_year(Date d)
 67 {
 68     int k,s=0,m=d.month-1;
 69     for(;m>0;m--)
 70     {
 71     if(m==2)
 72         k=28;
 73     else if(m==4||m==6||m==9||m==11)
 74         k=30;
 75     else
 76         k=31;
 77     s+=k;
 78     }
 79     if((d.year%4==0&&d.year%100!=0)||d.year%400==0)
 80         s++;
 81     s+=d.day;
 82     return s;
 83 }
 84 
 85 // 补足函数compare_dates实现
 86 // 功能:比较两个日期: 
 87 // 如果d1在d2之前,返回-1;
 88 // 如果d1在d2之后,返回1
 89 // 如果d1和d2相同,返回0
 90 int compare_dates(Date d1, Date d2)
 91 {
 92     if(d1.year<d2.year)
 93         return -1;
 94     else if(d1.year>d2.year)
 95         return 1;
 96     else
 97     {
 98         if(day_of_year(d1)<day_of_year(d2))
 99             return -1;
100         else if(day_of_year(d1)==day_of_year(d2))
101             return 0;
102         else
103             return 1;
104     }
105 }

 

2.运行结果

 

task.6

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include <string.h>
 4 
 5 enum Role {admin, student, teacher};
 6 
 7 typedef struct
 8 {
 9     char username[20];  // 用户名
10     char password[20];  // 密码
11     enum Role type;     // 账户类型
12 } Account;
13 
14 
15 // 函数声明
16 void output(Account x[],int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示
17 
18 int main()
19 {
20     Account x[]={{"A1001", "123456", student},
21                  {"A1002", "123abcdef", student},
22                  {"A1009", "xyz12121", student}, 
23                  {"X1009", "9213071x", admin},
24                  {"C11553", "129dfg32k", teacher},
25                  {"X3005", "921kfmg917", student}};
26     int n;
27     n = sizeof(x)/sizeof(Account);
28     output(x,n);
29     system("pause");
30     return 0;
31 }
32 // 待补足的函数output()实现
33 // 功能:遍历输出账户数组x中n个账户信息
34 //      显示时,密码字段以与原密码相同字段长度的*替代显示
35 void output(Account x[],int n)
36 {
37     char k[10][10]={"admin","student","teacher"};
38     for(int i=0;i<n;i++)
39     {
40         int j=0;
41         while(x[i].password[j]!='\0')
42         {
43             x[i].password[j]='*';
44             j++;
45         }
46         printf("%s\t\t%-9s\t\t%s\n",x[i].username,x[i].password,k[x[i].type]);    
47     }
48 }

 

2.运行结果

 

task.7

1.源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include <string.h>
 4 
 5 typedef struct {
 6     char name[20];      // 姓名
 7     char phone[12];     // 手机号
 8     int  vip;           // 是否为紧急联系人,是取1;否则取0
 9 } Contact; 
10 
11 
12 // 函数声明
13 void set_vip_contact(Contact x[], int n, char name[]);  // 设置紧急联系人
14 void output(Contact x[], int n);    // 输出x中联系人信息
15 void display(Contact x[], int n);   // 按联系人姓名字典序升序显示信息,紧急联系人最先显示
16 
17 
18 #define N 10
19 int main() {
20     Contact list[N] = {{"刘一", "15510846604", 0},
21                        {"陈二", "18038747351", 0},
22                        {"张三", "18853253914", 0},
23                        {"李四", "13230584477", 0},
24                        {"王五", "15547571923", 0},
25                        {"赵六", "18856659351", 0},
26                        {"周七", "17705843215", 0},
27                        {"孙八", "15552933732", 0},
28                        {"吴九", "18077702405", 0},
29                        {"郑十", "18820725036", 0}};
30     int vip_cnt, i;
31     char name[20];
32 
33     printf("显示原始通讯录信息: \n"); 
34     output(list, N);
35 
36     printf("\n输入要设置的紧急联系人个数: ");
37     scanf("%d", &vip_cnt);
38     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
39     for(i = 0; i < vip_cnt; ++i) {
40         scanf("%s", name);
41         set_vip_contact(list, N, name);
42     }
43 
44     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
45     display(list, N);
46     output(list, N);
47     system("pause");
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 {
55     for(int i=0;i<n;i++)
56     {
57         if(strcmp(x[i].name,name)==0)
58             x[i].vip=1;
59     }
60 }
61 
62 // 补足函数display实现
63 // 功能: 显示联系人数组x中的联系人信息
64 //      按姓名字典序升序显示, 紧急联系人显示在最前面
65 void display(Contact x[], int n)
66 {
67     Contact t;
68     for(int i=0;i<n-1;i++)
69     {
70         for(int j=0;j<n-i-1;j++)
71         {
72             if((strcmp(x[j].name,x[j+1].name)>0||x[j+1].vip==1))
73             {
74                 t=x[j];
75                 x[j]=x[j+1];
76                 x[j+1]=t;
77             }
78         }
79     }
80 }
81 
82 void output(Contact x[], int n) {
83     int i;
84 
85     for(i = 0; i < n; ++i) {
86         printf("%-10s%-15s", x[i].name, x[i].phone);
87         if(x[i].vip)
88             printf("%5s", "*");
89         printf("\n");
90     }
91 }

 

2.运行结果

 

posted @ 2023-12-11 15:13  JUVBuffon  阅读(53)  评论(0)    收藏  举报