佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1069|回复: 8

C++ Bubble sort问题

[复制链接]
发表于 12-11-2008 08:53 PM | 显示全部楼层
既然是OOP,为何不考虑弄一个Food class/struct呢?
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 12-11-2008 10:08 PM | 显示全部楼层

回复 2# 苦瓜汤 的帖子

这位大大。。请问是如何呢?
我还没有学到structure那些。。
不过如果大大可以解释给我听我也愿意学习的。。谢谢
回复

使用道具 举报

 楼主| 发表于 12-11-2008 06:34 PM | 显示全部楼层 |阅读模式
最近学校给的一个问题,已经两个星期了。。我还是不会做,希望各位大大可以助我一臂之力。

问题:
用C++的oop method写出一个program,可以接收5种食物的名字,之后再接收该5种食物的quantity.最后会display出那些食物(根据quantity最多的开始到最少的)

例子:
pls key in  food 1 = aaa
pls key in  food 2 = bbb
pls key in food 3 = ccc
pls key in food 4 = ddd
pls key in food 5 =eee
enter quantity of food 1 = 1
enter quantity of food 2 = 10
enter quantity of food 3 = 4
enter quantity of food 4 = 15
enter quantity of food 5 = 20

eee----20
ddd----15
bbb----10
ccc-----4
aaa-----1




我自己已经做了出来。得到的答案书数目是正确的sort出来了。
可是食物名字就乱了。。
我使用的方法是swap
我也上网查过了,要swap char是不容易的。。所以在这里希望有人可以帮忙我,
告诉我应该要用什么方法去解答?

下面是我的program

  1. #include <iostream>
  2. using namespace std;
  3. class Sort
  4. {
  5. public:
  6. char a[5][10];
  7. int b[5];
  8. int i,j;
  9. void receive();
  10. void execute();
  11. void display();
  12. };
  13. void Sort::receive()
  14. {
  15. for(i=0;i<5;i++)
  16. {
  17.   cout<<"\nPlease enter "<<i+1<<" food : ";
  18.   gets(a[i]);
  19. }
  20. for(i=0;i<5;i++)
  21. {
  22.   cout<<"\nPlease enter the quantity for food "<<i+1<<" : ";
  23.   cin>>b[i];
  24. }
  25. };
  26. void Sort::execute()
  27. {
  28. for(i=1;i<5;i++)
  29. {
  30.   for(j=0;j<5-i;j++)
  31.   {
  32.    if(b[j]<b[j+1])
  33.    {
  34.     swap(a[j][10],a[j+1][10]);
  35.     swap(b[j],b[j+1]);
  36.    }
  37.   }
  38. }
  39. };
  40. void Sort::display()
  41. {
  42. cout<<"Food Name -------Quantity\n";
  43. for(i=0;i<5;i++)
  44. {
  45.   cout<<a[i][20]<<"------"<<b[i]<<endl;
  46. }
  47. };
  48. void main()
  49. {
  50. Sort aaa;
  51. aaa.receive();
  52. aaa.execute();
  53. aaa.display();
  54. }
复制代码
回复

使用道具 举报

发表于 13-11-2008 10:29 AM | 显示全部楼层
原帖由 冷血趙雲 于 12-11-2008 10:08 PM 发表
这位大大。。请问是如何呢?
我还没有学到structure那些。。
不过如果大大可以解释给我听我也愿意学习的。。谢谢


还没学到structure?你不是在用着class吗?

ok... 你可以用struct来取代multi-dim array

struct Food
{
           char * name;
           int qty;
};

然后直接用一个array加载这个struct

Food myFoods[5];

这样的话操作起来肯定比multi-dim array简单多。
回复

使用道具 举报

发表于 13-11-2008 10:50 AM | 显示全部楼层

回复 3# 冷血趙雲 的帖子

直接以一个 food class来作就行了
  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. class Food
  5. {
  6.     public:
  7.         Food();
  8.         void set_quantity(int i);
  9.         void set_name(int i);
  10.         int get_quantity();
  11.         void print();

  12.     private:
  13.         int quantity;
  14.         string name;
  15. };

  16. Food::Food()
  17. {
  18.     quantity = 0;
  19.     name = "";
  20. }

  21. void Food::set_quantity(int i)
  22. {


  23.     cout << "enter quantity of food " << i << " : ";
  24.     cin >> quantity;


  25. }

  26. void Food::set_name(int i)
  27. {

  28.     cout << "pls key in food " << i << " : ";
  29.     cin >> name;

  30. }

  31. int Food::get_quantity()
  32. {
  33.     return quantity;
  34. }

  35. void Food::print()
  36. {
  37.     cout << name << " ----- " << quantity << endl;
  38. }



  39. int main()
  40. {
  41.     Food food[5];
  42.     string input;
  43.     int n, i, j;

  44.     for( i = 0; i < 5; i++)
  45.     {
  46.         food.set_name(i + 1);
  47.     }

  48.     for( i = 0; i < 5; i++)
  49.     {
  50.         food.set_quantity(i + 1);
  51.     }

  52.     for( i = 1; i < 5; i++)
  53.         for( j = 0; j < 5 - i; j++)
  54.             if( food[j].get_quantity() < food[j + 1].get_quantity())
  55.                  swap( food[j], food[j + 1]);

  56.     for( i = 0; i < 5; i++)
  57.         food.print();

  58.     return 0;
  59. }
复制代码

[ 本帖最后由 onlylonly 于 13-11-2008 01:55 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 13-11-2008 08:31 PM | 显示全部楼层

回复 5# onlylonly 的帖子

谢谢楼上两位的帮忙。

我试用onlyonly的program来compile看。可是发现error
D:\Program Files\Microsoft Visual Studio\MyProjects\3\3.cpp(125) : error C2228: left of '.set_name' must have class/struct/union type
D:\Program Files\Microsoft Visual Studio\MyProjects\3\3.cpp(130) : error C2228: left of '.set_quantity' must have class/struct/union type..
D:\Program Files\Microsoft Visual Studio\MyProjects\3\3.cpp(139) : error C2228: left of '.print' must have class/struct/union type

可是我看已经有了Food food[5]了。。是什么问题呢?
回复

使用道具 举报

Follow Us
 楼主| 发表于 13-11-2008 08:33 PM | 显示全部楼层
原帖由 苦瓜汤 于 13-11-2008 10:29 AM 发表


还没学到structure?你不是在用着class吗?

ok... 你可以用struct来取代multi-dim array

struct Food
{
           char * name;
           int qty;
};

然后直接用一个array加载这个struct
...


你说的我真的没有学过。。
可能因为我本身是电子科系的。。所以没有学那么多。。。。
不过真的谢谢你噢~
回复

使用道具 举报

发表于 14-11-2008 09:38 AM | 显示全部楼层
原帖由 冷血趙雲 于 13-11-2008 08:31 PM 发表
谢谢楼上两位的帮忙。

我试用onlyonly的program来compile看。可是发现error
D:\Program Files\Microsoft Visual Studio\MyProjects\3\3.cpp(125) : error C2228: left of '.set_name' must have class/struct/un ...


因为佳里自动吧我的 [i]当成是italic。。

用下面的code
  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. class Food
  5. {
  6.     public:
  7.         Food();
  8.         void set_quantity(int i);
  9.         void set_name(int i);
  10.         int get_quantity();
  11.         void print();

  12.     private:
  13.         int quantity;
  14.         string name;
  15. };

  16. Food::Food()
  17. {
  18.     quantity = 0;
  19.     name = "";
  20. }

  21. void Food::set_quantity(int i)
  22. {

  23.     cout << "enter quantity of food " << i << " : ";
  24.     cin >> quantity;
  25. }

  26. void Food::set_name(int i)
  27. {
  28.     cout << "pls key in food " << i << " : ";
  29.     cin >> name;

  30. }

  31. int Food::get_quantity()
  32. {
  33.     return quantity;
  34. }

  35. void Food::print()
  36. {
  37.     cout << name << " ----- " << quantity << endl;
  38. }



  39. int main()
  40. {
  41.     Food food[5];
  42.     int i, j;

  43.     for( i = 0; i < 5; i++)
  44.     {
  45.         food[i].set_name(i + 1);
  46.     }

  47.     for( i = 0; i < 5; i++)
  48.     {
  49.         food[i].set_quantity(i + 1);
  50.     }

  51.     for( i = 1; i < 5; i++)
  52.         for( j = 0; j < 5 - i; j++)
  53.             if( food[j].get_quantity() < food[j + 1].get_quantity())
  54.                  swap( food[j], food[j + 1]);

  55.     for( i = 0; i < 5; i++)
  56.         food[i].print();

  57.     return 0;
  58. }
复制代码

[ 本帖最后由 onlylonly 于 14-11-2008 09:40 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 14-11-2008 09:29 PM | 显示全部楼层
原帖由 onlylonly 于 14-11-2008 09:38 AM 发表


因为佳里自动吧我的 [i]当成是italic。。

用下面的code#include
#include

using namespace std;

class Food
{
    public:
        Food();
        void set_quantity(int i);
        voi ...

原来是这个原因噢!
可以了。我现在就去慢慢理解你给的例子。
谢谢啦!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 11:14 AM , Processed in 0.106059 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表