看看这个标题排序
#include<stdio.h>#define MATITL 40
#define MAAUTL 40
#define MABK 3
struct book{
char title[MATITL];
char author[MAAUTL];
float value;
};
void print1(struct book guan[],int n);
void print2(struct book *ptr[],int n);
main()
{
int count,i;
struct book guan[MABK],*ptr2[MABK];
char ch;
printf("please enter the book\n");
count=0;
while(count<3&&gets(guan[count].title)!=NULL&&guan[count].title[0]!='\0')
{
printf("please enter the author\n");
gets(guan[count].author);
printf("please enter the value\n");
scanf("%f",&guan[count].value);
count++;
ch=getchar();
printf("please enter the book\n");
}
for(i=0;i<3;i++)
{
ptr2[i]=&guan[i];
}
print1(guan,3);
printf("another input:\n");
print2(ptr2,3);
}
void print1(struct book guan[],int n)
{
int count;
count=0;
for(count=0;count<n;count++)
{
printf(" %s by %s:$%.2f\n",guan[count].title,guan[count].author,guan[count].value);
}
}
void print2(struct book *ptr[],int n)//以标题先后输出
{
int i,j;
struct book *temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((strcmp(ptr[i]->title,ptr[j]->title))>0)
{
temp=ptr[j];
ptr[j]=ptr[i];
ptr[i]=temp;
}
}
for(i=0;i<n;i++)
{
printf(" %s by %s:$%.2f\n",ptr[i]->title,ptr[i]->author,ptr[i]->value);
}
}
}