DAY16 结构体

 #include <stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 struct product
  5 {
  6     char name[20];
  7     int price;
  8     int number;
  9     char describe[20];
 10 
 11 
 12 };
 13 
 14 struct product *create();
 15 
 16 void input(struct product *p);
 17 void  bubble(struct product *p);
 18 void max(struct product *p);
 19 void money(struct product *p);
 20 void output(struct product *p );
 21 struct product *free_pro(struct product*p);
 22 int main(int argc, const char *argv[])
 23 {
 24     struct product *p =create();
 25     input(p);
 26     bubble(p);
 27     max(p);
 28     money(p);
 29     output(p);
 30     p=free_pro(p);
 31     return 0;
 32 }
 33 
 34 struct product *create()
 35 {
 36     struct product *p=(struct product *)malloc(sizeof(struct product)*5);            
 37     if(p==NULL)
 38         return NULL;
 39 
 40     return p;
 41 }
 42 
 43 void input(struct product *p)
 44 {
 45     int i;                                                                           
 46     printf("输入要购买的商品");
 47     for(i=0;i<5;i++)
 48     {
 49         scanf("%s %d",(p+i)->name,&(p+i)->price);
 50 
 51     }
 52 
 53 }
 54 
 55 void  bubble(struct product *p)
 56 {
 57     int i,j;
 58     for(i=0;i<5;i++)
 59     {
 60         for(j=0;j<4-i;j++)
 61         {
 62             if((p+j+1)->price>(p+j)->price)
 63             {
 64             struct product b =*(p+j);
 65             *(p+j+1)=*(p+j);
 66             *(p+j)=b;
 67             }
 68         }
 69 
 70 
 71         printf("第%d个商品的价格是%d\n",i+1,&(p+i)->price);
 72     }
 73 
 74 
 75 
 76 }
 77 
 78
 79 void max(struct product *p)
 80 {
 81     int i;                                                                           
 82     int max;
 83     for(i=0;i<4;i++)
 84     {
 85         if((p+i)->price>(p+i+1)->price)
 86         {
 87             max=(p+i)->price;
 88         }
 89     }
 90 
 91     printf("最贵的商品价格是%d\n",&max);
 92 }
 93 
 94 
 95 void money(struct product *p)
 96 {
 97     int i;
 98     int  sum;
 99     for(i=0;i<5;i++)
100     {
101         sum+=(p+i)->price;
102     }
103     printf("一共花了%d\n",&sum);
104 }
105 
106 
107 void output(struct product *p )
108 {
109     int i;
110     for(i=0;i<5;i++)
111     {
112     printf("商品名称:%s\n",(p+i)->name);
113     printf("商品单价:%d\n",&(p+i)->price);
114     printf("商品购买个数:%d\n",&(p+i)->number);
115     printf("商品描述:%s\n",(p+i)->describe);
    }
117 }
118 
119 struct product *free_pro(struct product*p)
120 {
121     free(p);
122     p=NULL;
123     return p;
124 }         

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值