修改一下.还差一步了,只能显示最长的名字,其它的不能显示.
#include<stdio.h>#include<string.h>
struct book
{
char name[10];
float price;
}
pers[10];
void change(void);
void print_book(void);
void putinbook(void);
void main()
{
putinbook();
print_book();
change();
printf("That's ok!\n");
system("pause");
}
void print_book(void)
{
int i;
printf("the first turn:\n");
for(i=0;i<10;i++)
printf("%s\t%g\n",pers[i].name,pers[i].price);
}
void putinbook(void)
{
int i;
printf("Please input 10 books name,price:\n");
for(i=0;i<10;i++)
scanf("%s %f",pers[i].name,&pers[i].price);
}
void change(void)
{
int i,finish,j;
float min;
char minname[10];
char ove[100];
printf("the turn after sort:\n");
printf("book name\tbook price\n");
for(i=0;i<10;i++)
{
finish=1;
for(j=0;j<10;j++)
{
if(pers[j].price<pers[j+1].price)
{
min=pers[j].price;
pers[j].price=pers[j+1].price;
pers[j+1].price=min;
strcpy(minname,pers[j].name);
strcpy(pers[j].name,pers[j+1].name);
strcpy(pers[j+1].name,minname);
finish=0;
}
if(pers[j].price==pers[j+1].price)
{
if(strcmp(pers[j].name,pers[j+1].name)<0)
{
ove[100]=pers[j].price;
pers[j].price=pers[j+1].price;
pers[j+1].price=ove[100];
strcpy(pers[j].name,pers[j+1].name);
strcpy(pers[j+1].name,ove);
finish=0;
}
}
}
if(finish==1)
break;
}
for(i=0;i<10;i++)
printf("%s\t\t%g\n",pers[i].name,pers[i].price);
}