在堆区申请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