实验6

任务4

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

任务5

  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 int calculate(int x[],Date d);
 17 
 18 void test1() {
 19     Date d;
 20     int i;
 21 
 22     printf("输入日期:(以形如2023-12-11这样的形式输入)\n");
 23     for(i = 0; i < 3; ++i) {
 24         input(&d);
 25         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
 26     }
 27 }
 28 
 29 void test2() {
 30     Date Alice_birth, Bob_birth;
 31     int i;
 32     int ans;
 33 
 34     printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n");
 35     for(i = 0; i < 3; ++i) {
 36         input(&Alice_birth);
 37         input(&Bob_birth);
 38         ans = compare_dates(Alice_birth, Bob_birth);
 39         
 40         if(ans == 0)
 41             printf("Alice和Bob一样大\n\n");
 42         else if(ans == -1)
 43             printf("Alice比Bob大\n\n");
 44         else
 45             printf("Alice比Bob小\n\n");
 46     }
 47 }
 48 
 49 int main() {
 50     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
 51     test1();
 52 
 53     printf("\n测试2: 两个人年龄大小关系\n");
 54     test2();
 55 }
 56 
 57 // 补足函数input实现
 58 // 功能: 输入日期给pd指向的Date变量
 59 void input(Date *pd) {
 60     // 待补足
 61     // ×××
 62     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 63 }
 64 
 65 // 补足函数day_of_year实现
 66 // 功能:返回日期d是这一年的第多少天
 67 int day_of_year(Date d) {
 68     // 待补足
 69     // ×××
 70     int pin[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 71     int run[12]={31,29,31,30,31,30,31,31,30,31,30,31};
 72     int day;
 73     if(d.year%4==0){
 74         day=calculate(run,d);
 75     }
 76     else{
 77         day=calculate(pin,d);
 78     }
 79     return day;
 80 }
 81 int calculate(int x[],Date d){
 82     int day,i;
 83     day=0;
 84     for(i=0;i<d.month-1;i++){
 85         day+=x[i];
 86     }
 87     day+=d.day;
 88     return day;
 89 }
 90 
 91 // 补足函数compare_dates实现
 92 // 功能:比较两个日期: 
 93 // 如果d1在d2之前,返回-1;
 94 // 如果d1在d2之后,返回1
 95 // 如果d1和d2相同,返回0
 96 int compare_dates(Date d1, Date d2) {
 97     // 待补足
 98     // ×××
 99     int Dd1,Dd2;
100     Dd1 = day_of_year(d1);
101     Dd2 = day_of_year(d2);
102     if(Dd1<Dd2){
103         return -1;
104     }
105     else if(Dd1>Dd2){
106         return 1;
107     }
108     else{
109         return 0;
110     }
111 }

任务6

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.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 
14 // 函数声明
15 void output(Account x[], int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示
16 
17 int main() {
18     Account x[] = {{"A1001", "123456", student},
19                     {"A1002", "123abcdef", student},
20                     {"A1009", "xyz12121", student}, 
21                     {"X1009", "9213071x", admin},
22                     {"C11553", "129dfg32k", teacher},
23                     {"X3005", "921kfmg917", student}};
24     int n;
25     n = sizeof(x)/sizeof(Account);
26     output(x, n);
27     system("pause");
28 
29     return 0;
30 }
31 
32 // 待补足的函数output()实现
33 // 功能:遍历输出账户数组x中n个账户信息
34 //      显示时,密码字段以与原密码相同字段长度的*替代显示
35 void output(Account x[], int n) {
36     // 待补足
37     // ×××
38     int i,strLength,pw;
39     char pwOut[20];
40     char status[20];
41 
42     for(i=0;i<n;i++){
43         switch(x[i].type){
44             case 0:strcpy(status,"admin");break;
45             case 1:strcpy(status,"student");break;
46             case 2:strcpy(status,"teacher");break;
47             
48         }    
49         strLength=strlen(x[i].password);
50         for(pw=0;pw<strLength;pw++){
51             pwOut[pw]='*';
52         }
53         pwOut[pw]='\0';
54         printf("%-9s%-9s%-9s\n",x[i].username,pwOut,status);
55     }
56 }

 

posted @ 2023-12-11 19:28  Sn002  阅读(22)  评论(0)    收藏  举报