标题:请看看这个冒泡排序
只看楼主
HSU
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2010-1-3
结帖率:83.33%
已结贴  问题点数:10 回复次数:5 
请看看这个冒泡排序
# include <stdio.h>
# include <string.h>
/*结构体定义*/
struct books
{
    char name[30], publisher[15];
    int num;
    float price;
}book[50];
void rank(books book[], int n);
int main()
{
    int i,n;
    printf("请输入图书数量:");
    scanf("%d",&n);
    printf("请分别输入书名、出版社、数量、单价(以空格分开):\n");
    for (i=0 ; i<n ; i++)
        {
            scanf("%s%s%d%f",&book[i].name,&book[i].publisher,&book[i].num,&book[i].price);
        }
    printf("\n");
    rank(book,n);
    return 0;
}

/*排序,输出函数rank*/
void rank(books book[], int n)
{
    int i,j,tnum;
    char tname[30],tpublisher[15];
    float tprice;
    for (i=0 ; i<n-1 ; i++)
    {
        for (j=0 ; j<n-i-1 ; j++)
            if(book[i].price>book[i+1].price)
            {
                strcpy(tname,book[i].name), strcpy(book[i].name,book[i+1].name), strcpy(book[i+1].name,tname);//交换书名
                strcpy(tpublisher,book[i].publisher), strcpy(book[i].publisher,book[i+1].publisher), strcpy(book[i+1].publisher,tpublisher);//交换出版商
                tnum=book[i].num, book[i].num=book[i+1].num, book[i+1].num=tnum;//交换数目
                tprice=book[i].price, book[i].price=book[i+1].price, book[i+1].price=tprice;//交换单价
            }
    }
    for (i=0 ; i<n ; i++)
        {
            printf("%s %s %d %.2f\n",book[i].name,book[i].publisher,book[i].num,book[i].price);
        }
}


按说输入以下内容:
3
a aa 50 25.5
b bb 30 31.55
c cc 60 18.3
时应该出现的结果是:
c cc 60 18.3
a aa 50 25.5
b bb 30 31.55
可为何却出现:
a aa 50 25.5
c cc 60 18.3
b bb 30 31.55
搜索更多相关主题的帖子: 冒泡 
2010-03-20 22:28
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
得分:7 
void rank(books book[], int n)
{
    int i,j,tnum;
    char tname[30],tpublisher[15];
    float tprice;
    struct books tmp;
    for (i=0 ; i<n ; i++)
    {
        for (j=i+1 ; j<n ; j++)
            if(book[i].price>book[j].price)
            {
                tmp = book[i];
                book[i] = book[j];
                book[j] = tmp;
            }
    }
    for (i=0 ; i<n ; i++)
    {
        printf("%s %s %d %.2f\n",book[i].name,book[i].publisher,book[i].num,book[i].price);
    }
}
2010-03-20 23:30
HSU
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2010-1-3
得分:0 
我试过这样的确可以,但是能否解释一下为什么我那个就会出现我所说的问题么
2010-03-21 00:09
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
得分:3 
if(book[i].price>book[i+1].price)
to
 if(book[j].price>book[j+1].price)
下面花括号里的i也相应地改为j

2010-03-21 00:21
云杨
Rank: 1
等 级:新手上路
帖 子:20
专家分:4
注 册:2011-10-25
得分:0 
只需要把
 if(book[i].price>book[i+1].price)
            {
                strcpy(tname,book[i].name), strcpy(book[i].name,book[i+1].name), strcpy(book[i+1].name,tname);//交换书名
                strcpy(tpublisher,book[i].publisher), strcpy(book[i].publisher,book[i+1].publisher), strcpy(book[i+1].publisher,tpublisher);//交换出版商
                tnum=book[i].num, book[i].num=book[i+1].num, book[i+1].num=tnum;//交换数目
                tprice=book[i].price, book[i].price=book[i+1].price, book[i+1].price=tprice;//交换单价
            }
中的i全部换成j
2011-10-30 13:25
云杨
Rank: 1
等 级:新手上路
帖 子:20
专家分:4
注 册:2011-10-25
得分:0 
我刚调过 没错的 你把比较的对象搞混淆了
2011-10-30 13:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-300039-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.307132 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved