实验6

实验任务4

源代码:

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

运行结果:

image

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

运行结果:

image

 实验任务6

源代码:

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

运行结果:

image

 实验任务7

源代码:

  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 
 17 #define N 10
 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     
 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 
 47     return 0;
 48 }
 49 
 50 // 补足函数set_vip_contact实现
 51 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1)
 52 void set_vip_contact(Contact x[], int n, char name[]) {
 53     int i;
 54     for (i = 0; i < n; i++) {
 55             if (strcmp(x[i].name, name) == 0) {
 56                 x[i].vip=1;
 57             }
 58     }
 59 }
 60 
 61 // 补足函数display实现
 62 // 功能: 显示联系人数组x中的联系人信息
 63 //      按姓名字典序升序显示, 紧急联系人显示在最前面
 64 void display(Contact x[], int n) {
 65     int i,a[N],j=0,b[N],c=0,d,e;
 66     for (i = 0; i < n; i++) {
 67         if (x[i].vip == 1) {
 68             a[j]=i;
 69             j++;
 70         }
 71         else {
 72             b[c]=i;
 73             c++;
 74         }
 75     }
 76     for (i = 0; i < j; i++) {
 77         for (d=0;d<j-1-i;d++) {
 78             if (strcmp(x[a[d]].name,x[a[d+1]].name) > 0) {
 79                 e=a[d];
 80                 a[d]=a[d+1];
 81                 a[d+1]=e;
 82             }
 83         }
 84     }
 85     for (i = 0; i < c; i++) {
 86         for (d=0;d<c-1-i;d++) {
 87             if (strcmp(x[b[d]].name,x[b[d+1]].name) > 0) {
 88                 e=b[d];
 89                 b[d]=b[d+1];
 90                 b[d+1]=e;
 91             }
 92         }
 93     }
 94     for (i = 0; i < j; i++) {
 95         printf("%s\t%s\t*\n",x[a[i]].name,x[a[i]].phone);
 96     }
 97     for (i = 0; i < c; i++) {
 98         printf("%s\t%s\t\n",x[b[i]].name,x[b[i]].phone);
 99     }
100 }
101 
102 void output(Contact x[], int n) {
103     int i;
104 
105     for(i = 0; i < n; ++i) {
106         printf("%-10s%-15s", x[i].name, x[i].phone);
107         if(x[i].vip)
108             printf("%5s", "*");
109         printf("\n");
110     }
111 }
View Code

运行结果:

image

 

posted @ 2025-12-30 17:59  尚逸辰  阅读(4)  评论(0)    收藏  举报