2012-04-12 1 views
0

私はスタックを使用する方法にちょっと固執していますし、私が書いているコードでスタックを使用する理由もあります。このアサインメントでは、ユーザーの入力が適切かどうかをチェックするプログラムを記述しています。それは、使用が選択できる3つの異なる選択肢を有する単純なプログラムである。 1.基本括弧()2.標準括弧()[] {}および3.ユーザ定義の括弧。メインプログラムが行うべき唯一のことは、ユーザーの入力が正しいかどうかをチェックし、そのメッセージのみを画面に表示することです。C++ HWヘルプスタックを使用して

私は私のmain.cppと一緒に使用しているStackLS.cppとStack.hファイルを持っています。下のサンプルコードをそれぞれ貼り付けます。

StackLS.h

typedef int elemType; // flexible data type 

class StackLS 
{ 
private: 
// inner class node 

class Node 
{ 
public: 
    elemType data; // data portion 
    Node *next; // link to the seccessor 
}; // end Node 

// data members 
Node *topItem; // pointer to the top element of this stack 

// utilities 

     public: 
// constructors 
StackLS(void); // default constructor 
StackLS(const StackLS& aStack); // copy constructor 

// observers 
bool isEmpty(void) const; 
// returns true if this stack is empty 
//   false otherwise 

bool isFull(void) const; 
// returns true if this stack is full 
//   false otherwise 

elemType top(void) const; 
// precondition: this stack is not empty 
// returns top element in this stack 

// transformers 
void push(const elemType& item); 
// precondition: this stack is not full 
// adds item to this stack 

void pop(void); 
// removes top element from this stack if exist 
// remains empty otherwise 

void makeEmpty(void); 
// makes this stack empty 

// destructor 
~StackLS(void); 
}; // end StackLS 

StackLS.cppは

 // constructors 
     StackLS::StackLS(void) 
    // default constructor 
    { 
topItem = 0; 
     } // end default constructor 

     StackLS::StackLS(const StackLS& aStack) 
    // copy constructor 
     { 
     } // end copy constructor 

     // observers 
     bool StackLS::isEmpty(void) const 
     // returns true if this stack is empty 
     //   false otherwise 
     { 
    return topItem == 0; 
     } // end isEmpty 

     bool StackLS::isFull(void) const 
     // returns true if this stack is full 
     //   false otherwise 
     { 
return false; 
     } // end isFull 

     elemType StackLS::top(void) const 
     // precondition: this stack is not empty 
     // returns top element in this stack 
     { 
// return (*topItem).data; 
return topItem->data; 
     } // end top 

     // transformers 
     void StackLS::push(const elemType& item) 
     // precondition: this stack is not full 
      // adds item to this stack 
      { 
Node *newNode = new Node; 
newNode->data = item; 
newNode->next = topItem; 
topItem = newNode; 
     } // end push 

     void StackLS::pop(void) 
     // removes top element from this stack if exist 
     // remains empty otherwise 
     { 
if (topItem != 0) 
{ 
    Node *temp = topItem; 
    topItem = topItem->next; 
    delete temp; 
} 
     } // end pop 

     void StackLS::makeEmpty(void) 
     // makes this stack empty 
     { 
    while (topItem != 0) 
{ 
    Node *temp = topItem; 
    topItem = topItem->next; 
    delete temp; 
    } 
     } // end makeEmpty 

     // destructor 
      StackLS::~StackLS(void) 
     { 
//while (!isEmpty()) 
// pop(); 
while (topItem != 0) 
{ 
    Node *temp = topItem; 
    topItem = topItem->next; 
    delete temp; 
} 
     } // end destructor 

ここで私がこれまで持っていmain.cppにあります。

 #include <iostream> 
     #include <string> 
     #include "StackLS.h" 
     using namespace std; 

     do { 

     int main() 
     { 
char answer; 
char n; 
StackLS stack; 

cout << " ********** MENU ********** " << endl; 
cout << " 1. Basic Brackets() " << endl; 
cout << " 2. Standard Brackets()[]{} " << endl; 
cout << " 3. User-Defined brackets " << endl; 
cout << " Please enter your choice: " << endl; 

switch (choice){ 
case 1: 
    cout << "Current Setting:() " << endl; 
    cout << "Enter your expression followed by a ; : " << endl; 
    do { 

    cin >> answer; 
     while (answer != ;) 
    } 


      } // end main 

     } 
while (choice != 'n' || 'N') 

main.cppには、再び私はこのプログラム(main.cppに)であなたを示したスタックを使用する方法を疑問に思って。私はなぜスタックを使うのか、そしてなぜそれを使うのか、少し混乱しています。どんな助けもありがとうございます。ありがとう。 main.cppは正しくないかもしれませんが、私は学んでいるので、私はもっと学ぶためにここにいるのです。ありがとう

+0

誰かが「スタック」と言うと、通常は「コールスタック」を意味します。あなたが持っているのは*スタックデータ構造ですが、*スタックではありません。 –

答えて

1

オープニングブレースが見えたら、それをスタックに押します。閉じる中括弧が見えるときは、それがスタックの上にある中括弧の対応物であることを確認してから、それを飛ばしてください。入力が完了したら、スタックが空であることを確認します。

+0

よろしくお願いします。それは少し助けます。私はそれをどうやって行うのか分からない。開いているブレースを押して閉じていれば、コードに入れる方法は、ブレースを開く方法に相当します。それについてのアドバイスは? – Lea

+1

一度に入力を1文字ずつループし、異なる種類の中カッコと比較するだけです。 –

+0

私は一度に1文字を処理するループを持っています。私はmain.cppでスタックをどのように使うのか迷っています。どのように私はそれぞれで宣言されたメソッドを使用します。 – Lea

関連する問題