实验6

实验任务4

源代码

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

运行截图

屏幕截图 2025-12-19 091517

实验任务5

源代码

 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("输入日期:(以形如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     system("pause");
55 }
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 
65 // 补足函数day_of_year实现
66 // 功能:返回日期d是这一年的第多少天
67 int day_of_year(Date d) {
68     int s=0;
69     int i;
70     int x[12]={31,28,31,30,31,30,31,31,30,31,30,31};
71     if((d.year%4==0&&d.year%100!=0)||d.year%400==0)
72         x[1]=29;
73     for(i=0;i<d.month-1;i++)
74         s+=x[i];
75     s+=d.day;
76     return s;
77 
78     
79 }
80 
81 // 补足函数compare_dates实现
82 // 功能:比较两个日期: 
83 // 如果d1在d2之前,返回-1;
84 // 如果d1在d2之后,返回1
85 // 如果d1和d2相同,返回0
86 int compare_dates(Date d1, Date d2) {
87     if(d1.year<d2.year)return -1;
88     else if(d1.year>d2.year)return 1;
89     else{
90     if( day_of_year(d1)<day_of_year(d2))return -1;
91     else if( day_of_year(d1)>day_of_year(d2))return 1;
92     else return 0;}}
93    
View Code

运行截图

屏幕截图 2025-12-19 165510

实验任务6

源代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h>
 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     system("pause");
27     return 0;
28 }
29 
30 // 待补足的函数output()实现
31 // 功能:遍历输出账户数组x中n个账户信息
32 //      显示时,密码字段以与原密码相同字段长度的*替代显示
33 void output(Account x[], int n) {
34     int i=0;
35     int j=0;
36     for(i=0;i<n;i++)
37         {printf("%-10s",x[i].username);
38     for(j=0;j<strlen(x[i].password);j++)
39         printf("*");
40     printf("\t");
41     switch(x[i].type){
42     case admin:
43     printf("admin\n");
44     break;
45     case student:
46     printf("student\n");
47     break;
48     case teacher:
49     printf("teacher\n");
50     break;
51     default :
52     printf("unknown\n");
53     break;}}
54 }
View Code

运行结果截图

屏幕截图 2025-12-21 194032

实验任务7

源代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h>
 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     system("pause");
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     int j;
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,j;
65     Contact temp;
66     for(i=0;i<n-1;i++)
67         for(j=0;j<n-i-1;j++)
68         {if((x[j].vip<x[j+1].vip)||(x[j].vip==x[j+1].vip&&strcmp(x[j].name,x[j+1].name)>0))
69         {temp=x[j];
70     x[j]=x[j+1];
71     x[j+1]=temp;}}
72         output(x,n);
73 }
74 
75 void output(Contact x[], int n) {
76     int i;
77 
78     for(i = 0; i < n; ++i) {
79         printf("%-10s%-15s", x[i].name, x[i].phone);
80         if(x[i].vip)
81             printf("%5s", "*");
82         printf("\n");
83     }
84 }
View Code

运行结果截图

屏幕截图 2025-12-21 201012

 

posted @ 2025-12-21 20:11  每户下山流量  阅读(0)  评论(0)    收藏  举报