0

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

主文件

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  }

视频.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

视频.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  }

但是,仍然没有运气。任何输入将不胜感激。

4

2 回答 2

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)

反之亦然。两个都试试!

于 2013-09-18T21:28:18.640 回答
-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毕竟,这就是s 的用途。

于 2013-09-18T21:28:08.280 回答