如果值相同,则尝试不与冒泡排序交换

时间:2013-09-18 21:10:47

标签: c++ algorithm bubble-sort

我的程序运行正常,除了一个我似乎无法弄清楚的小问题。开始时,我希望用户输入一个排序方法,然后在指针数组中输入值,直到用户用^ D结束输入。用户结束输入后,我的程序根据所需的排序方法从最高到最低打印用户输入。同样,我已经完成所有工作并且排序确实发生了,但我想要做的是基于用户输入的顺序打印,如果排序方法的两个值相同。这是我的代码,如果需要进一步说明,请问。

的main.cpp

5 #include<iostream>
6 using namespace std;
7
8 #include "video.h"
9
10 #include<string>
11
12 int main()
13 {
14  const int MAX = 100; // maximum number of things stored in the array
15  Video *vids[MAX]; // Video pointer for Video objects
16  int num_vids = 0; // counter for number of videos
17
18  string title, url, comment; // for title, url, and comment respectively
19  float length; // for length
20  int rating; // for rating
21
22  string sort; // for sorting method
23
24  cout << "How would you like to sort these videos, by rating, length, or title? " <<     endl;
25  cin >> sort;
26  cin.ignore();
27
28  if( (sort != "length")  && (sort != "rating") && (sort != "title") )
29  {
30   cerr << sort << " is not a legal sorting method, giving up." << endl;
31   return 1;
32  }
33
34  cout << "Enter a Title: " << endl;
35  while(getline(cin,title))
36  {
37   cout << "Enter the URL: " << endl;
38   getline(cin,url);
39
40   cout <<  "Enter a comment: " << endl;
41   getline(cin,comment);
42
43   cout << "Enter the length: " << endl;
44   cin >> length;
45
46   cout << "Enter a rating of 1-5: " << endl;
47   cin >> rating;
48   cin.ignore();
49
50   vids[num_vids] = new Video(title,url,comment,length,rating);
51   num_vids++;
52
53   cout << "Enter a Title: " << endl;
54  }
55   cout << endl;
56
57  if( (num_vids > MAX) )
58  {
59   cerr << "Too many video entries, giving up." << endl;
60   return 1;
61  }
62
63  if( (sort == "length") )
64  {
65   for(int last = num_vids-1; last > 0; last--)
66    for(int cur = 0; cur < last; cur++)
67     if( (vids[cur]->longer(vids[cur+1]) == false) )
68      swap(vids[cur], vids[cur+1]);
69  }
70
71  else if( (sort == "rating") )
72  {
73   for(int last = num_vids-1; last > 0; last--)
74    for(int cur = 0; cur < last; cur++)
75     if( (vids[cur]->largerRating(vids[cur+1]) == false) )
76    // if( (vids[cur] != vids[cur+1]) )
77       swap(vids[cur], vids[cur+1]);
78  }
79  else if( (sort == "title") )
80  {
81   for(int last = num_vids-1; last > 0; last--)
82    for(int cur = 0; cur < last; cur++)
83     if( (vids[cur]->alphabetical(vids[cur+1]) == false) )
84      swap(vids[cur], vids[cur+1]);
85  }
86
87
88  for(int i = 0; i < num_vids; i++)
89  {
90   vids[i]->print();
91  }
92  return 0;
93  }

video.cpp

 5 #include<iostream>
 6 using namespace std;
 7
 8 #include<string>
 9
 10 #include "video.h"
 11
 12 using namespace std;
 13
 14  Video::Video(string title, string url, string comment, float length, int rating)
 15  {
 16   m_title = title;
 17   m_url = url;
 18   m_comment = comment;
 19   m_rating = rating;
 20   m_length = length;
 21  }
 22
 23  Video::~Video()
 24  {
 25   cout << "object is desructing" << endl;
 26  }
 27
 28  bool Video::longer(Video *other)
 29  {
 30   return m_length > other->m_length;
 31   if( (other->m_length > m_length) )
 32   {
 33    return true;
 34   }else
 35   {
 36    return false;
 37   };
 38  }
 39
 40  bool Video::largerRating(Video *other)
 41  {
 42   return m_rating > other->m_rating;
 43   if( (other->m_rating > m_length) )
 44   {
 45    return true;
 46   }
 47   else
 48   {
 49   return false;
 50   };
 51  }
 52
 53  bool Video::alphabetical(Video *other)
 54  {
 55   return other->m_title > m_title;
 56   if( (other->m_title > m_title) )
 57   {
 58    return true;
 59   }else
 60   {
 61    return false;
 62   };
 63  }
 64
 65  void Video::print()
 66  {
 67   cout << m_title << ", " << m_url << ", " << m_comment << ", " << m_length << ", ";
 68   for(int i = 0; i < m_rating; i++)
 69  {
 70   cout << "*";
 71  }
 72  cout << endl;
 73  }
 74

video.h

5 #ifndef VIDEO_H
6 #define VIDEO_H
7
8 #include<iostream>
9 using namespace std;
10
11 #include<string>
12
13 class Video
14 {
15  public:
16   Video(string title, string url, string comment,float length, int rating);
17   ~Video();
18   bool longer(Video *other);
19   bool largerRating(Video *other);
20   bool alphabetical(Video *other);
21   void print();
22
23  private:
24   string m_title;
25   string m_url;
26   string m_comment;
27   float m_length;
28   int m_rating;
29 };
30
31 #endif

同样,它的大部分都在工作,但是如果所需排序方法的两个值相同,我不希望我的程序交换冒泡排序,我尝试过这样的事情:

71  else if( (sort == "rating") )
72  {
73   for(int last = num_vids-1; last > 0; last--)
74    for(int cur = 0; cur < last; cur++)
75     if( (vids[cur]->largerRating(vids[cur+1]) == false) )
76      if( (vids[cur] != vids[cur+1]) )
77       swap(vids[cur], vids[cur+1]);
78  }

但是,仍然没有运气。任何输入都会有很大的意义。

2 个答案:

答案 0 :(得分:0)

此问题可能有两种原因:

1-您正在比较可能导致精度问题的浮点数或双精度数,因此,浮点类型之间的比较不应该像这样(例如):

double a,b;
//some code that defines a and b
if(a == b) //this is very bad
    //do something

这可能会导致精度问题,因为像float和double这样的浮动类型可能具有最不显着的位精度问题,因此比较应该是以下形式:

if(a - b <= std::numeric_limits<float>::epsilon)
    //do something

阅读有关比较浮点变量的更多信息

2-您的比较比较器导致问题,例如,而不是

if(a <= b)

使用

if(a < b)

反之亦然。试试两个!

答案 1 :(得分:-1)

只需更改您的交换条件:

for(int last = num_vids-1; last > 0; last--) {
    for(int cur = 0; cur < last; cur++) {
        if(vids[cur+1]->largerRating(vids[cur]) ) {
            swap(vids[cur], vids[cur+1]);
        }
    }
}

请注意,将布尔值与布尔值进行比较完全是多余的,所以不要写这样的条件:

if(condition == true) {
if(negated == false) {

你应该写

if(condition) {
if(!negated) {

毕竟这就是bool的用途。