struct CAR
{
char name[20];
char color[20];
float price;
};
struct CAR *create(int n)
{
struct CAR *p = (struct CAR *) malloc (sizeof(struct CAR)*n);
if(p == NULL) return NULL;
return p;
}
void input(struct CAR *p,int n)
{
for(int i = 0; i < n; i++)
{
printf("please input the %d number\n",i+1);
printf("name\tcolor\tprice\n");
scanf("%s%s%f",(p+i)->name,(p+i)->color,&(p+i)->price);
}
}
void mp(struct CAR *p,int n)
{
for(int i = 1; i < n; i++)
{
int count = 0;
for(int j = 0; j < n-i; j++)
{
if((p+j)->price < (p+j+1)->price)
{
struct CAR tmp;
tmp = *(p+j);
*(p+j) = *(p+j+1);
*(p+j+1) = tmp;
count++;
}
if(!count) return;
}
}
}
void output(struct CAR *p,int n)
{
for(int i =0; i < n; i++)
{
printf("name:%s\tcolor:%s\tprice:%.2f\n",(p+i)->name,(p+i)->color,(p+i)->price);
}
}
int main(int argc, const char *argv[])
{
struct CAR *p = create(5);
input(p,5);
mp(p,5);
output(p,5);
free(p);
p = NULL;
return 0;
}
【数据结构】day1
于 2023-04-17 20:08:10 首次发布