数据结构之结构体

思维导图

练习

使用多文件编辑,

定义商品信息:商品名称,商品单价,商品购买个数,商品描述,循环输入购买的商品,按单价排序,输出商品信息,计算最贵的商品以及一共花了多少钱?

头文件

  1 #ifndef __HEAD_1_22_H__
  2 #define __HEAD_1_22_H__
  3 
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <stdlib.h>
  7 typedef struct all_goods
  8 {
  9     char name[40];
 10     float price;
 11     float quantity;
 12     char describe[40];
 13 }goods;
 14 
 15 goods* creat();
 16 void Input(goods *p);
 17 void bubble(goods *p);
 18 float Max(goods *p);
 19 float Money(goods *p);
 20 void Output(goods *p);
 21 goods *free_space(goods *p);
 22 #endif      

自定义函数

  1 #include "head_1_22.h"
  2 
  3 
  4 goods* creat()
  5 {
  6     goods *p =(goods*)malloc(sizeof( goods)*5);
  7     if(NULL==p)
  8         return NULL;
  9     return p;
 10 }
 11 
 12 void Input(goods *p)
 13 {
 14     for(int i=0;i<5;i++)
 15     {
 16         scanf("%s",(p+i)->name);
 17         getchar();
 18         scanf("%f",&(p+i)->price);
 19         getchar();
 20         scanf("%f",&(p+i)->quantity);
 21         getchar();
 22         scanf("%s",(p+i)->describe);
 23         getchar();
 24     }
 25 }
 26 
 27 void bubble(goods *p)
 28 {
 29     for(int i=0;i<5;i++)
 30     {
 31         for(int j=0;j<5-i-1;j++)
 32         {
 33             if((*(p+j)).price<(*(p+j+1)).price)
 34             {
 35                 goods temp=*(p+j);
 36                 *(p+j)=*(p+j+1);
 37                 *(p+j+1)=temp;
 38             }
 39         }                                                                                                             
 40     }
 41 }
 42                                                                                                                       
 43 float Max(goods *p)
 44 {
 45     int max=0;
 46     for(int i=0;i<5;i++)
 47     {
 48         if(max<(*(p+i)).price)
 49             max=(*(p+i)).price;
 50     }
 51     return max;
 52 }
 53 
 54 float Money(goods *p)
 55 {
 56     float count=0;
 57     for(int i=0;i<5;i++)
 58     {
 59         count+=(*(p+i)).price*(*(p+i)).quantity;
 60     }
 61     return count;
 62 }
 63 
 64 void Output(goods *p)
 65 {
 66     printf("名称\t单价\t个数\t描述\n");
 67     for(int i=0;i<5;i++)
 68     {
 69         printf("%s\t%.2f\t%.2f\t%s\n",(p+i)->name,(p+i)->price,(p+i)->quantity,(p+i)->describe);
 70     }
 71 }
 72 
 73 goods *free_space(goods *p)
 74 {
 75     if(p==NULL)
 76         return NULL;
 77 
 78     free(p);
 79     p=NULL;
 80     return p;
 81 }
在create函数,请实现在堆区申请内存5个连续的内存
goods* creat()
{
    goods *p =(goods*)malloc(sizeof( goods)*5);
    if(NULL==p)
        return NULL;
    return p;
}    
在input函数,请实现循环输入购买的商品
void Input(goods *p) 
{
    for(int i=0;i<5;i++)
    {   
        scanf("%s",(p+i)->name);
        getchar();
        scanf("%f",&(p+i)->price);
        getchar();                                           
        scanf("%f",&(p+i)->quantity);
        getchar();
        scanf("%s",(p+i)->describe);
        getchar();
    }   
}
在bubble函数,请实现按单价排序
void bubble(goods *p) 
{
    for(int i=0;i<5;i++)
    {   
        for(int j=0;j<5-i-1;j++)
        {
            if((*(p+j)).price<(*(p+j+1)).price)
            {
                goods temp=*(p+j);
                *(p+j)=*(p+j+1);
                *(p+j+1)=temp;
            }
        }
    }
}
在Max函数,计算最贵的商品名称
float Max(goods *p)
{
    int max=0;
    for(int i=0;i<5;i++)
    {
        if(max<(*(p+i)).price)
            max=(*(p+i)).price;
    }
    return max;
}
在Money函数,计算共花了多少钱
float Money(goods *p)
{
    float count=0;
    for(int i=0;i<5;i++)
    {
        count+=(*(p+i)).price*(*(p+i)).quantity;
    }
    return count;
} 
在output函数,请实现输出
void Output(goods *p)
{
    printf("名称\t单价\t个数\t描述\n");
    for(int i=0;i<5;i++)
    {
        printf("%s\t%.2f\t%.2f\t%s\n",(p+i)->name,(p+i)->pric
    }
}
在free_space函数。实现释放堆区内存
goods *free_space(goods *p)
{
    if(p==NULL)
        return NULL;

    free(p);
    p=NULL;
    return p;
}

主函数

  1 #include "head_1_22.h"
  2 int main(int argc, const char *argv[])
  3 {
  4     goods* p=creat();
  5     Input(p);
  6     bubble(p);                                                 
  7     float max=Max(p);
  8     printf("max=%.2f\n",max);
  9     float money=Money(p);
 10     printf("money=%.2f\n",money);
 11     Output(p);
 12     p=free_space(p);
 13     return 0;
 14 }
 15 

效果图

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值