au0812

导航

 

task4

 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-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51},
18                   {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30},
19                   {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27},
20                   {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 
21                   {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
22                   {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42},
23                   {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44},
24                   {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
25                   {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
26                   {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}};
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 // ×××
39 void output(Book x[], int n){
40     int i;
41     for(i=0;i<n;i++){
42         printf("%-30s%-30s%-30s%-20.2lf %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
43     }
44 }
45 
46 // 待补足:函数sort()实现
47 // ×××
48 void sort(Book x[], int n){
49           int i,j;
50     Book t;
51     for(i=0;i<n;i++){
52         for(j=0;j<n-1-i;j++){
53             if(x[j].sales_count<x[j+1].sales_count){
54                 t=x[j];
55                 x[j]=x[j+1];
56                 x[j+1]=t;
57             }
58         }
59     }
60 }
61 
62 // 待补足:函数sales_count()实现
63 // ×××
64 double sales_amount(Book x[], int n){
65     double ans=0;
66     for(int i=0;i<n;i++){
67         ans+=x[i].sales_count*x[i].sales_price;
68     }
69     return ans;
70 }
View Code

截图

image

 task5

 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("输入日期:(以形如2025-12-19这样的形式输入)\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出生日期:(以形如2025-12-19这样的形式输入)\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 monthday[]={31,28,31,30,31,30,31,31,30,31,30,31};
66     int t=0,i;
67     for(i=0;i<d.month-1;i++){
68         t+=monthday[i];
69     }
70     t+=d.day;
71     if(d.year%4==0&&d.year%100!=0||d.year%400==0){
72         return t+1;
73     }
74     return t;
75 }
76 
77 // 补足函数compare_dates实现
78 // 功能:比较两个日期: 
79 // 如果d1在d2之前,返回-1;
80 // 如果d1在d2之后,返回1
81 // 如果d1和d2相同,返回0
82 int compare_dates(Date d1, Date d2) {
83     if(d1.year<d2.year)
84     return -1;
85     if(d1.year>d2.year)
86     return 1;
87     if(d1.month<d2.month)
88     return -1;
89     if(d1.month>d2.month)
90     return 1;
91     if(d1.day>d2.day)
92     return 1;
93      if(d1.day<d2.day)
94     return -1;
95     return 0;
96 }
View Code

 

 截图

image

 task6

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

 

截图

image

 task7

  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,j,count=0;
 66     Contact t;
 67     for(i=0;i<n;i++){
 68         if(x[i].vip==1){
 69             t=x[i];
 70             x[i]=x[count];
 71             x[count]=t;
 72             count++;
 73         }
 74     } 
 75     for(i=0;i<count-1;i++){
 76         for(j=0;j<count-1-i;j++){
 77             if(strcmp(x[j].name,x[j+1].name)>0){
 78                 t=x[j];
 79                 x[j]=x[j+1];
 80                 x[j+1]=t;
 81             }
 82         }
 83     for(i=count;i<n-1;i++){
 84         for(j=count;j<n-1-i+count;j++){
 85             if(strcmp(x[j].name,x[j+1].name)>0){
 86                 t=x[j];
 87                 x[j]=x[j+1];
 88                 x[j+1]=t;
 89             }
 90         }
 91     }
 92     
 93     }
 94     output(x,n);
 95 }
 96 
 97 void output(Contact x[], int n) {
 98     int i;
 99 
100     for(i = 0; i < n; ++i) {
101         printf("%-10s%-15s", x[i].name, x[i].phone);
102         if(x[i].vip)
103             printf("%5s", "*");
104         printf("\n");
105     }
106 }
View Code

image

 

posted on 2025-12-21 20:30  南信大Au  阅读(0)  评论(0)    收藏  举报