2011-10-17 10 views
0

私のコードでは、特定のアイテムをスタックに配置しようとしています。これを行うために、私は元のスタックからそれをポップするために、一時スタックに途中のすべての項目を移動しています。それがポップされた後、元のスタックの元の順序ですべてのアイテムを元に戻します。私のコードは、アイテムがスタックに入っていることを認識しないので、実際にスタックに入っているときは見つからないということになります。あなたは私が...メインスタック内の1つのアイテムを探す

int型私のループをデバッグする助けてくださいすることができます:ここから

#include <iostream> 
#include "Stack.h" 
#include "Gumball.h" 

using namespace std; 

int main() 
{ 
    Stack s, gumballStack; 
    Gumball g, temp; 
    char choice; 
    bool choice_flag = true; 

    do { 
    cin >> choice; 
    cin >> g.color; 
    switch(choice) 
    { 
     case 'b': 
     case 'B': 
      cout << "A" << " " << g.color << " gumball has been bought." << endl << endl; 
      g.counter = 0; 
      s.isempty(); 
      s.push(g); 
      if(!s.isfull()) 
       cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl; 
      else 
       cout << "There is no room for another gumball." << endl << endl; 
      break; 
     case 'e': 
     case 'E': 
      s.isempty(); 
      temp = s.pop(); 
      if(s.isempty() && temp.color == g.color) 
      { 
       cout << "The " << g.color << " gumball has been eaten." << endl << endl; 
      } 

を、私はエラーであると考えている:

  while(!s.isempty() && g.color != temp.color) 
      { 
       gumballStack.push(temp); 
       g.counter++; 
       s.pop(); 
       cout << " " << temp.counter << endl << endl; 
      } 
      if(!s.isempty()) 
      { 
       cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl; 
      } 
      else 
      { 
       cout << "The gumball cannot be found." << endl << endl; 
      } 
      while(!gumballStack.isempty()) 
      { 
       //gumballStack.pop(); 
       s.push(gumballStack.pop()); 
       gumballStack.pop(); 
      } 
      break; 
     case 'q': 
     case 'Q': 
      choice_flag = false; 
      break; 
    } 
} while(choice_flag); 

return 0; 
} 

.hファイル:

#ifndef STACK_H 
#define STACK_H 
#include "Gumball.h" 

// Interface file - Stack class definition 
class Stack { 
    public: 
     Stack(); 
     void push(Gumball); 
     Gumball pop(); 
     bool isempty(); 
     bool isfull(); 
    private: 
     Gumball gumballs[6+1]; 
     int top; 
}; 


#endif // STACK_H 

あなたの質問にお答えください:

(stack.h用)も.cppファイルは、私はそれはあなたが質問のほとんどに答えるだろうと思い、次のとおりです。

私は私がスタックに戻って一時を入れていますので、あなたが述べてきた問題を参照してください
#include "Stack.h" 
#include "Gumball.h" 

using namespace std; 

// Constructor to initialize the stack 
Stack::Stack() 
{ 
    top = -1; 
} 

// Function to add item x to stack 
void Stack::push(Gumball x) 
{ 
    if(!isfull()){ 
    top++; 
    gumballs[top] = x; 
    return; } 
    else 
    return; 
} 

// Function to remove and return top item of stack 
Gumball Stack::pop() 
{ 
    Gumball x; 

    if(!isempty()) { 
     x = gumballs[top]; 
     top--; 
     return x; } 
    else 
     return x; 
} 

// Function to check if stack is empty 
bool Stack::isempty() 
{ 
    if (top == -1) 
     return true; 
    else 
     return false; 
} 

// Function to check if stack is full 
bool Stack::isfull() 
{ 
    if (top == 6) 
     return true; 
    else 
     return false; 
} 

複数回...間違いなく私の意図は、それを指摘していただきありがとうございます。どのように私はそれが同じものの代わりに、私が探しているアイテムと同じではないスタックに各ガンボールを追加するには?

gumball.hファイル:

#ifndef GUMBALL_H 
#define GUMBALL_H 
#include <iostream> 

using namespace std; 

// Interface file - Gumball class definition 
class Gumball 
{ 
    public: 
     Gumball(); 
     string color; 
     int counter; 
    private: 
}; 

#endif // GUMBALL_H 

gumball.cppファイル:

#include "Gumball.h" 

Gumball::Gumball() 
{ 
    color = " "; 
    counter = 0; 
} 

私はそれがあるGumball.hと.cppファイルを追加するので、ここでウル他の質問に答えるだろうと思い

+0

のようなものを使用する必要がありますか? –

答えて

1

それは

while(!s.isempty() && g.color != temp.color) 
     { 
      gumballStack.push(temp); 
      g.counter++; 
      temp = s.pop(); //temp has been updated 
      cout << " " << temp.counter << endl << endl; 
     } 

になるが、食べガムボールは、スタック内の最後のときが空になりますので、このコードは動作しないことに注意を払う必要があります。 STLコンテナを使用しないのはなぜトムと一致し、他のソリューション(例えばSTL ::リスト)を考える中で、あなたを指している以外

、あなたはこの

if (s.isempty()) { 
    cout << "The gumball cannot be found." << endl << endl; 
} 
while(!s.isempty()) { 
    Gumball temp = s.pop(); 
    if(temp.color == g.color) { 
     cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl; 
    } else { 
     gumballStack.push(temp); 
     g.counter++; 
     if (s.isempty()) { 
      cout << "The gumball cannot be found." << endl << endl; 
     } 
    } 
} 
while(!gumballStack.isempty()) { 
     s.push(gumballStack.pop()); 
     gumballStack.pop(); 
} 
1

Stackの実装を見ずに問題が何であると言うのが難しいですか。しかし、コードのいくつかの部分が混乱していることがわかったので、どこに指示するのが役に立つかもしれないと思いました。インターフェイスをコードに変更してより明確にすると、問題が明らかになることがあります。上記の質問の

// Interface file - Stack class definition 
class Stack { 
    public: 
     Stack(); 
     void push(Gumball); //Does this push to the front or back? 
          // The stl uses push_back, and push_front, 
          // its good to keep this convention 
     Gumball pop(); //Does this pop the front or back? 
     bool isempty(); //This function doesn't change Stack, right? 
         // if so it should be marked const. 
     bool isfull(); //Mark as const? 
    private: 
     Gumball gumballs[6+1]; 
     int top; 
}; 

isempty()constネスが理想的

case 'E': 
    s.isempty(); //This should be redundent? 
       // isempty is a question, it shouldnt change s. 
    temp = s.pop(); 
    if(s.isempty() && temp.color == g.color) 
    { 
    cout << "The " << g.color << " gumball has been eaten." << endl << endl; 
    } 
    //Here isempty is being used as a question (as if it doesn't change s 
    // - I presume this is the intended use. 
    //Also, .color is not a function, it should be, as will be seen. 
    //Also, temp never gets updated in your loop. 
    while(!s.isempty() && g.color != temp.color) 
    { 
    gumballStack.push(temp); //Why are you pushing multiple copies 
           // of the same temp 
    g.counter++; //The counter should be an implementation detail, 
     // it should not be exposed like this. 
     // perhaps overload operator++()? 
     //Presumably you want g.color to update when you increase the counter? 
     //this doesn't currently happen because g.color is not a function - 
     // it points to data. 
     //I'm guessing that When you call color(), 
     // the function should check the value of the counter 
     // and obtain the appropriate color. 
    s.pop(); //Did you want to update temp here? 
    cout << " " << temp.counter << endl << endl; 
    } 

を以下では特に重要であり、あなたはイテレータで全体を書き直します。 std::findのインターフェースを見てください。

+0

私はあなたの質問などに同じ投稿で回答しました。スクロールして変更を確認してください。また、私はstd :: findまたはiteratorsをまだ使用していないことが好まれました.... – 123me

関連する問題