帮忙改下错 10分悬赏~~~

来源:百度知道 编辑:UC知道 时间:2024/05/08 03:20:18
#include <stdio.h>
#include <string.h>
#define N 5
struct book
{
char num[100];
char name[100];
double price;
};

void sort(struct book *a[N],int m)
{struct book *temp;
int i,j;
for(i=1;i<m;i++)
for(j=0;j<m-i;j++)
if(strcmp(a[j]->name,a[j+1]->name)>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

void print(struct book *a[N],int m)
{int i;
printf(" 书号 书名 价格\n");
for(i=0;i<m;i++)
printf("%-30s%-30s%7lf\n",a[i]->num,a[i]->name,a[i]->price);
}

void main()
{struct book *a[]={{"ISBN 7-80679-007-1","Iron Box Of an Island",30.00},{"ISBN 7-80657-250-3","The Lord of the Rings",22.60},{"ISBN 978-0-7475-9105-4",
"Harry Potter and the death

按照您的要求用到指针交换,我修改了下。把sort函数的参数改写成指针,应该是满足要求的。

错在main函数中定义的a数组即可,不需要加个*返回地址的呀。
下面的程序在vc6.0中测试通过。

#include <stdio.h>
#include <string.h>
#define N 5
struct book
{
char num[100];
char name[100];
double price;
};

void sort(struct book *a,int m)
{struct book temp;
int i,j;
for(i=1;i<m;i++)
for(j=0;j<m-i;j++)
if(strcmp((a+j)->name,(a+j+1)->name)>0)
{
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;
}
}

void print(struct book *a,int m)
{int i;
printf(" 书号 书名 价格\n");
for(i=0;i<m;i++)
printf("%-30s%-30s%7lf\n",(a+i)->num,(a+i)->name,(a+i)->price);
}

void main()
{struct book a[]={{"ISBN 7-80679-007-1","Iron Box Of an Island",30.00},{"ISBN 7-80657-250-3","T