实验6

实验任务4

源代码

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];         
 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 void output(Book x[], int n){
37      int i;
38      printf("ISBN号              书名                          作者                售价          销售册数\n");
39      for(i=0;i<n;i++){
40          printf("%-20s%-30s%-20s%-6g\t%6d\n",x[i].isbn,x[i].name,x[i].author,
41             x[i].sales_price,x[i].sales_count);
42      }
43 }
44 void sort(Book x[], int n){
45     Book t={0};
46     int i,j;
47     for(i=0;i<n;i++){
48         for(j=0;j<n-i;j++){
49             if(x[j].sales_count<x[j+1].sales_count){
50             t=x[j];
51             x[j]=x[j+1];
52             x[j+1]=t;
53             }
54         }
55     }
56 }
57 double sales_amount(Book x[], int n){
58     int i;
59     double sum=0,s[N]={0};
60     for(i=0;i<n;i++){
61         s[i]=x[i].sales_count*x[i].sales_price;
62         sum+=s[i];
63     }
64     return sum;
65 }

运行结果截图

4

实验任务5

源代码

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 typedef struct {
 4     int year;
 5     int month;
 6     int day;
 7 } Date;
 8 void input(Date *pd);                   
 9 int day_of_year(Date d);               
10 int compare_dates(Date d1, Date d2);  
11 void test1() {
12     Date d;
13     int i;
14     printf("输入日期:(以形如2025-12-19这样的形式输入)\n");
15     for(i = 0; i < 3; ++i) {
16         input(&d);
17         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
18     }
19 }
20 void test2() {
21     Date Alice_birth, Bob_birth;
22     int i;
23     int ans;
24     printf("输入Alice和Bob出生日期:(以形如2025-12-19这样的形式输入)\n");
25     for(i = 0; i < 3; ++i) {
26         input(&Alice_birth);
27         input(&Bob_birth);
28         ans = compare_dates(Alice_birth, Bob_birth);
29         if(ans == 0)
30             printf("Alice和Bob一样大\n\n");
31         else if(ans == -1)
32             printf("Alice比Bob大\n\n");
33         else
34             printf("Alice比Bob小\n\n");
35     }
36 }
37 int main() {
38     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
39     test1();
40     printf("\n测试2: 两个人年龄大小关系\n");
41     test2();
42     system("pause");
43 }
44 void input(Date *pd) {
45     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
46 }
47 int day_of_year(Date d) {
48     int a=0;
49     switch(d.month){
50     case 12:a+=30;
51     case 11:a+=31;
52     case 10:a+=30;
53     case 9:a+=31;
54     case 8:a+=31;
55     case 7:a+=30;
56     case 6:a+=31;
57     case 5:a+=30;
58     case 4:a+=31;
59     case 3:if((d.year%4==0&&d.year%100!=0)||(d.year%400==0)){
60               a+=29;
61            }else
62                a+=28;
63     case 2:a+=31;
64     case 1:a+=0;break;}
65     a+=d.day;
66     return a;
67 }
68 int compare_dates(Date d1, Date d2) {
69     if (d1.year != d2.year) {
70         return d1.year < d2.year ? -1 : 1; 
71     }
72     if (d1.month != d2.month) {
73         return d1.month < d2.month ? -1 : 1; 
74     }
75     if (d1.day != d2.day) {
76         return d1.day < d2.day ? -1 : 1; 
77     }
78     return 0;
79 }

运行结果截图

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 void output(Account x[], int n);    
12 
13 int main() {
14     Account x[] = {{"A1001", "123456", student},
15                     {"A1002", "123abcdef", student},
16                     {"A1009", "xyz12121", student}, 
17                     {"X1009", "9213071x", admin},
18                     {"C11553", "129dfg32k", teacher},
19                     {"X3005", "921kfmg917", student}};
20     int n;
21     n = sizeof(x)/sizeof(Account);
22     output(x, n);
23     system("pause");
24     return 0;
25 }
26 void output(Account x[], int n) {
27     int len,i,j;
28     for(i=0;i<n;i++){
29         len=strlen(x[i].password);
30         for(j=0;j<len;j++){
31             x[i].password[j]='*';
32         }
33     }
34     for(i=0;i<n;i++){
35         printf("%s\t\t%-20s",x[i].username,x[i].password);
36         switch(x[i].type){
37         case(student):printf("student\n");break;
38         case(admin):printf("admin\n");break;
39         case(teacher):printf("teacher\n");break;
40         
41         }
42     }
43 }

运行结果截图

6

实验任务7

源代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 typedef struct {
 4     char name[20];     
 5     char phone[12];     
 6     int  vip;           
 7 } Contact; 
 8 void set_vip_contact(Contact x[], int n, char name[]);
 9 void output(Contact x[], int n);    
10 void display(Contact x[], int n);   
11 #define N 10
12 int main() {
13     Contact list[N] = {{"刘一", "15510846604", 0},
14                        {"陈二", "18038747351", 0},
15                        {"张三", "18853253914", 0},
16                        {"李四", "13230584477", 0},
17                        {"王五", "15547571923", 0},
18                        {"赵六", "18856659351", 0},
19                        {"周七", "17705843215", 0},
20                        {"孙八", "15552933732", 0},
21                        {"吴九", "18077702405", 0},
22                        {"郑十", "18820725036", 0}};
23     int vip_cnt, i;
24     char name[20];
25 
26     printf("显示原始通讯录信息: \n"); 
27     output(list, N);
28 
29     printf("\n输入要设置的紧急联系人个数: ");
30     scanf("%d", &vip_cnt);
31     
32     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
33     for(i = 0; i < vip_cnt; ++i) {
34         scanf("%s", name);
35         set_vip_contact(list, N, name);
36     }
37 
38     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
39     display(list, N);
40     system("pause");
41     return 0;
42 }
43 void set_vip_contact(Contact x[], int n, char name[]) {
44     int i;
45     for(i=0;i<n;i++){
46         if(strcmp(x[i].name,name)==0)
47             x[i].vip=1;
48     }
49 }
50 void display(Contact x[], int n) {
51      int i,j;
52      Contact t,temp[N+1]={0};
53      for(i=0;i<n-1;i++){
54          for(j=0;j<n-i-1;j++){
55              if(strcmp(x[j].name,x[j+1].name)>0){
56              t=x[j+1];
57              x[j+1]=x[j];
58              x[j]=t;
59              }
60          }
61      }
62      j=0;
63      for(i=0;i<n;i++){
64          if(x[i].vip==1){
65            temp[j++]=x[i];
66          }
67      }
68      for(i=0;i<n;i++){
69          if(x[i].vip==0){
70            temp[j++]=x[i];
71          }
72      }
73      for(i=0;i<n;i++){
74            x[i]=temp[i];
75      }
76       for(i = 0; i < n; ++i) {
77         printf("%-10s%-15s", x[i].name, x[i].phone);
78         if(x[i].vip)
79             printf("%5s", "*");
80         printf("\n");
81     }
82 }
83 void output(Contact x[], int n) {
84     int i;
85 
86     for(i = 0; i < n; ++i) {
87         printf("%-10s%-15s", x[i].name, x[i].phone);
88         if(x[i].vip)
89             printf("%5s", "*");
90         printf("\n");
91     }
92 }

运行结果截图

7

 

posted @ 2025-12-20 14:07  wsh12345  阅读(7)  评论(0)    收藏  举报