2016-10-11 6 views
0

私は映画レンタル事業(理論的には)のバイナリ検索ツリーを作っています。ムービーレンタル事業にないタイトルが入力された場合は、「ムービーが見つかりません」と出力する必要があります。毎回セグメンテーションフォルトを取得せずにこれを行う方法がわかりません。私はこのコードが理にかなっていると私に何を試すべきか分からない。私は初心者です。私は簡単に行ってください。どんな助けでも大歓迎です! (あなたは私のコードの終わりに向かって3つのスラッシュ(///)の間で「ムービーが見つかりません」を出力しようとした私の場合は機能を見つけることができます。バイナリ検索ツリーのセグメント化エラー

void MovieTree::rentMovie(std::string title) 
{ 
    MovieNode *foundMovie = root; 
    //MovieNode *parent = NULL; 
if (root == NULL) 
{ 
    cout << "Movie not found." << endl; 
    return; 
} 
else 
{ 
    foundMovie = root; 
    while (foundMovie != NULL) 
    { 
     if (foundMovie == NULL) //tree is empty 
     { 
      cout << "Movie not found." << endl; 
     } 

     else 
     { 
      if (foundMovie->title.compare(title) > 0) //uses ASCII to determine where titles are 
      { 
       foundMovie->parent = foundMovie; 
       foundMovie = foundMovie ->leftChild; 
       //cout << "printed left" << endl; //debugging 
       if (foundMovie->title.compare(title) == 0 && foundMovie->quantity > 0) 
       { 
        foundMovie->quantity--; 
        cout << "Movie has been rented." << endl; 
        //Title entered matches title found 
        cout << "Movie Info:" << endl; 
        cout << "===========" << endl; 
        cout << "Ranking:" << foundMovie->ranking << endl; 
        cout << "Title:" << foundMovie->title << endl; 
        cout << "Year:" << foundMovie->year << endl; 
        cout << "Quantity:" << foundMovie->quantity << endl; 
        break; 
       } 

       else if (foundMovie->quantity ==0) 
       { 
        //If movie is out of stock 
        cout << "Movie out of stock." << endl; 
        break; 
       } 
      } 
      else //check rightChild 
      { 
       foundMovie->parent = foundMovie; 
       foundMovie = foundMovie->rightChild; 
       //cout << "printed right" << endl; //debugging 
       if (foundMovie->title.compare(title) == 0 && foundMovie->quantity > 0) //title entered matches title found 
       { 
        foundMovie->quantity--; 
        cout << "Movie has been rented." << endl; 
        cout << "Movie Info:" << endl; 
        cout << "===========" << endl; 
        cout << "Ranking:" << foundMovie->ranking << endl; 
        cout << "Title:" << foundMovie->title << endl; 
        cout << "Year:" << foundMovie->year << endl; 
        cout << "Quantity:" << foundMovie->quantity << endl; 
        break; 
       } 
       else if (foundMovie->quantity ==0) 
       { 
        //movie is found but out of stock 
        cout << "Movie out of stock." << endl; 
        break; 
       } 
      } 
     } 
    } 
    /// 
    if (foundMovie->title == title) 
    { 
     cout << "found the movie" << endl; 
    } 
    else 
    { 
     cout << "Movie not found." << endl; 
    } 
    /// 
} 

}

+0

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim

+0

'foundMovie->親= foundMovie; 'は良い考えではありません。循環依存を作成します。 – Franck

+0

もう1つの推奨事項:ツリーロジックからムービーロジックを切り離します。これにより、ツリーとムービーを別々にテストし、障害のあるものを簡単に判別することができます。両方のテストを同時に行うと、実際にはバグを修正し、他のバグのためにそれを認識できない厄介な場所に置かれます。 – user4581301

答えて

1

の考察/バグであなたのコード

  • あなたは何より有用な条件を削除することはできません:それはあなたのコードの可読性を低減

    while (foundMovie != NULL) 
    { 
        if (foundMovie == NULL) // false by construction!!! 
        ... 
    } 
    
  • foundMovie->parent = foundMovie;が消えるはずです。循環依存を作成します。

  • 最後のステートメントif (foundMovie->title == title)は、 foundMovieがその時点でNULLになる可能性があるため、セグメンテーションフォルトを作成する可能性があります。

ここで可能な書き換えです:

void MovieTree::rentMovie(std::string title) 
{ 
    MovieNode *foundMovie = root; 
    if (root == NULL) 
    { 
     cout << "Movie not found." << endl; 
     return; 
    } 

    foundMovie = root; 
    while (foundMovie != NULL) 
    { 
     int compareTitle = foundMovie->title.compare(title); //uses ASCII to determine where titles are 
     if (compareTitle > 0) 
     { 
      foundMovie = foundMovie ->leftChild; 
     } 
     else if (compareTitle < 0) //check rightChild 
     { 
      foundMovie = foundMovie->rightChild; 
     } 
     else { // compareTitle == 0 
      if (foundMovie->quantity > 0) 
      { 
       foundMovie->quantity--; 
       cout << "Movie has been rented." << endl; 
       //Title entered matches title found 
       cout << "Movie Info:" << endl; 
       cout << "===========" << endl; 
       cout << "Ranking:" << foundMovie->ranking << endl; 
       cout << "Title:" << foundMovie->title << endl; 
       cout << "Year:" << foundMovie->year << endl; 
       cout << "Quantity:" << foundMovie->quantity << endl; 
       break; 
      } 
      else if (foundMovie->quantity ==0) 
      { 
       //If movie is out of stock 
       cout << "Movie out of stock." << endl; 
       break; 
      } 
     } 
    } 

    // be sure that foundMovie->title == title is equivalent to foundMovie->title.compare(title) == 0 
    if (foundMovie && foundMovie->title == title) 
    { 
     cout << "found the movie" << endl; 
    } 
    else 
    { 
     cout << "Movie not found." << endl; 
    } 
} 
+0

ありがとうございました。私はこれを他にどのように行うべきか分かりませんでした。私はあなたのフィードバックに感謝します。 –

関連する問題