• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuist202513350098
博客园    首页    新随笔    联系   管理    订阅  订阅

实验6

任务4

 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 void output(Book x[],int n){
38     int i;
39     printf("%-25s%-22s%-15s%8s%8s%\n","ISBN","书名","作者","售价","销量");
40     for(i=0;i<n;i++){
41     printf("%-25s%-22s%-15s%8.2lf%8d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
42     }
43 }// 待补足:函数output()实现
44 // ×××
45 
46 
47 void sort(Book x[],int n){
48     int i,j;
49     Book temp;
50     for(i=0;i<n-1;i++){
51         for(j=0;j<n-1-i;j++){
52             if(x[j].sales_count<x[j+1].sales_count){
53                 temp=x[j];
54                 x[j]=x[j+1];
55                 x[j+1]=temp;
56             }
57         }
58     }
59 }// 待补足:函数sort()实现
60 // ×××
61 
62 
63 double sales_amount(Book x[],int n){
64     double total=0;
65     int i;
66     for(i=0;i<n;i++){
67         total+=x[i].sales_price*x[i].sales_count;
68         }
69     return total;
70 }// 待补足:函数sales_count()实现
71 // ×××

4

任务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 
 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 }
 63 
 64 // 补足函数day_of_year实现
 65 // 功能:返回日期d是这一年的第多少天
 66 int day_of_year(Date d) {
 67     int month_days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 68     int total=0;
 69     int is_leap=0;
 70     if((d.year%4==0&&d.year%100!=0)||(d.year%400==0)){
 71         is_leap=1;
 72         }
 73     int i;
 74     for(i=0;i<d.month-1;i++){
 75         total+=month_days[i];
 76         }
 77     if(is_leap&&d.month>2){
 78         total+=1;
 79         }
 80     total+=d.day;
 81     return total;// 待补足
 82     // ×××
 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     if(d1.year>d2.year)return 1;
 92     if(d1.year<d2.year)return -1;
 93     if(d1.month>d2.month)return 1;
 94     if(d1.month<d2.month)return -1;
 95     if(d1.day>d2.day)return 1;
 96     if(d1.day<d2.day)return -1;
 97     return 0;
 98     // 待补足
 99     // ×××
100 }

5

任务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,j;
35     int pwd_len;
36     printf("%-10s%-15s%s\n","用户名","密码","账户类型");
37     for(i=0;i<n;i++){
38         pwd_len=strlen(x[i].password);
39         printf("%-10s",x[i].username);
40         for(j=0;j<pwd_len;j++){
41             printf("*");
42             }
43         for(j=pwd_len;j<16;j++){
44             printf(" ");
45         }
46         if(x[i].type==admin)
47             printf("admin\n");
48         else if(x[i].type==student)
49             printf("student\n");
50         else if(x[i].type==teacher)
51             printf("teacher\n");
52         }
53     // 待补足
54     // ×××
55 }

6

 任务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 
 62 // 补足函数display实现
 63 // 功能: 显示联系人数组x中的联系人信息
 64 //      按姓名字典序升序显示, 紧急联系人显示在最前面
 65 void display(Contact x[], int n) {
 66     Contact vipArr[N], norArr[N];
 67     int vipNum =0,norNum = 0;
 68     int i;
 69     for(i=0;i<n;i++){
 70         if (x[i].vip == 1) {
 71             vipArr[vipNum++] = x[i];
 72         }
 73         else {
 74             norArr[norNum++] = x[i];
 75         }
 76     }
 77     for(int a = 0; a < vipNum - 1; a++){
 78         for(int b = 0; b < vipNum - 1 - a; b++){
 79             if(strcmp(vipArr[b].name, vipArr[b+1].name) > 0){
 80                 Contact temp = vipArr[b];
 81                 vipArr[b] = vipArr[b+1];
 82                 vipArr[b+1] = temp;
 83             }
 84         }
 85     }
 86     for(int a = 0; a < norNum - 1; a++){
 87         for(int b = 0; b < norNum - 1 - a; b++){
 88             if(strcmp(norArr[b].name, norArr[b+1].name) > 0){
 89                 Contact temp = norArr[b];
 90                 norArr[b] = norArr[b+1];
 91                 norArr[b+1] = temp;
 92             }
 93         }
 94     }
 95     for(i = 0; i < vipNum; i++){
 96         printf("%-10s%-15s%5s\n", vipArr[i].name, vipArr[i].phone, "*");
 97     }
 98     for(i = 0; i < norNum; i++){
 99         printf("%-10s%-15s\n", norArr[i].name, norArr[i].phone);
100     }
101 
102         // 待补足
103     // ×××
104 }
105 
106 void output(Contact x[], int n) {
107     int i;
108 
109     for(i = 0; i < n; ++i) {
110         printf("%-10s%-15s", x[i].name, x[i].phone);
111         if(x[i].vip)
112             printf("%5s", "*");
113         printf("\n");
114     }
115 }

7

 

posted on 2026-06-14 18:19  宁宁咩  阅读(10)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3