1.30 作业

在堆区申请5个连续的存储空间,实现车辆信息的输入 (品牌,颜色,价格)

1>调用函数在堆区申请空间

2>调用函数实现输入

3>调用函数对价格排序

4>调用函数输出

5>释放堆区空间

#include"head.h"
// vsp xxx
int main(int argc, const char *argv[])
{
int n=5;    
    struct cars *p;
    p=app(p,n);

    input(p,n);
    
    arr(p,n);
    output(p,n);//gai

free(p);
    return 0;
}
#include"head.h"

struct cars *app(struct cars *p,int n)
{
    p=(struct cars *)malloc(n*sizeof(struct cars));

    return p;
}

void input(struct cars *p,int n)
{
    int i=0;
    for(i;i<n;i++)
    {
    scanf("%s %c",(p+i)->n,&(p+i)->c);
    getchar();//吸收 空格?
    scanf("%d",&(p+i)->p);
    }
}

void output(struct cars car[],int n)
{
    int i=0;
    for(i;i<n;i++)
    {
    printf("%s\t%c\t",car[i].n,car[i].c);
    printf("%d\n",car[i].p);
    }
}


void arr(struct cars car[],int n)
{
    int i,j;
    struct cars temp;
    for(i=1;i<=n-1;i++)
    {
        for(j=0;j<=n-i-1;j++)
        {
            if(car[j].p>car[j+1].p)
            {
                temp=car[j];
                car[j]=car[j+1];
                car[j+1]=temp;
            }
        }
    }
}
#ifndef __head_h__
#define __head_h__
#include<stdio.h>
struct cars
{
    char n[20];
    char c;
    int p;
};



struct cars *app(struct cars *p,int n);
void input(struct cars *p,int n);
void output(struct cars *p,int n);
void arr(struct cars *p,int n);



#endif








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值