0

私は自分のプログラムを正常に動作させていますが、理解できない小さな問題が 1 つあります。最初に、ユーザーに並べ替え方法を入力してもらい、ユーザーが ^D で入力を終了するまでポインターの配列に値を入力します。ユーザーが入力を終了した後、私のプログラムは、目的の並べ替え方法に基づいて、ユーザー入力を最高から最低に出力します。繰り返しますが、すべてが機能しており、並べ替えが行われますが、並べ替え方法の 2 つの値が同じ場合、ユーザー入力の順序に基づいて印刷したいと考えています。これが私のコードです。追加の説明が必要な場合は、お尋ねください。

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  }

ビデオ.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

繰り返しますが、大部分は機能していますが、目的のソート方法の2つの値が同じ場合、プログラムをバブルソートでスワップしたくありません。次のようなことを試しました:

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

-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結局のところ、それが目的です。

于 2013-09-18T21:28:08.280 に答える