帮我看下为什么不排序

来源:百度知道 编辑:UC知道 时间:2024/05/05 22:09:45
#include <stdio.h>
#include "string.h"
void output(struct book b[]);
void sort(struct book b[]);
struct book
{
char name[10];
int price;
int num;
int amount;
}b[10];
int count=0;
void main()
{
int i=0;
int t;
while(1)
{
printf("输入书的名字");
scanf("%s",&b[i].name);
printf("输入书的价格");
scanf("%d",&b[i].price);
printf("输入书的数量");
scanf("%d",&b[i].num);
b[i].amount=b[i].num*b[i].price;
i++;
count++;
printf("1)继续,2)退出");
scanf("%d",&t);
if(t==2)
break;
}
output(b);
sort(b);
output(b);
}
void output(struct book b[])
{
int all=0;
int i;
printf("名称\t\t价格\t数量\t总价\n");
for(i=0;i<count;i++)
{
all+=b[i].amount;
pr

scanf("%s",&b[i].name); //这句有问题,不需要取地址符,因为数组名本身就是地址
所以你的name字段都没有读进去内容,当然无法排序。

因为你的程序太长,没时间细看,先改了这个错误重新编译看吧

你尝试把
strcmp(b[j].name,b[j-1].name)==-1
改成
strcmp(b[j].name,b[j-1].name)>0

strcmp(b[j].name,b[j-1].name)<0