2012-04-12 5 views
0

Ich bin ein wenig stecken auf, wie man den Stapel verwenden und warum ich sogar Stack in den Code, den ich schreibe, verwenden würde. Die Assignment sagt, ein Programm zu schreiben, das überprüft, ob die Benutzereingabe Iformed ist oder nicht. Es ist ein einfaches Programm, das drei verschiedene Auswahlen hat, aus denen der Benutzer auswählen kann. 1. Grundlegende Klammern() 2. Standard Klammern() [] {} und 3. Benutzerdefinierte Klammern. Das einzige, was das Hauptprogramm tun soll, ist zu überprüfen, ob die Benutzereingabe wohlgeformt ist oder nicht, und nur diese Nachricht auf dem Bildschirm anzuzeigen.C++ HW Hilfe mit Stack

Ich habe eine StackLS.cpp und eine Stack.h-Datei, die ich zusammen mit meiner main.cpp verwende. Ich füge unten einen Beispielcode von jedem ein.

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 

Hier ist die main.cpp, die ich bisher haben. 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') 

Wieder frage ich mich, wie ich den Stapel verwenden würde ich in diesem Programm gezeigt haben Sie (main.cpp). Ich bin ein wenig verwirrt darüber, warum ich Stapel verwenden würde und warum. Jede Hilfe wird geschätzt. Vielen Dank. Die main.cpp mag nicht richtig sein, aber ich lerne wieder und deshalb bin ich hier, um mehr zu lernen. Danke

+0

Wenn jemand "den Stapel" sagt, meinen sie normalerweise "den Aufrufstapel". Was Sie haben, ist * a * Stapel Datenstruktur, aber es ist nicht * der * Stapel. –

Antwort

1

Wenn Sie eine öffnende Klammer sehen, drücken Sie sie auf den Stapel. Wenn Sie eine schließende geschweifte Klammer sehen, stellen Sie sicher, dass es sich um das Gegenstück der geschweiften Klammer oben auf dem Stapel handelt, und entfernen Sie sie dann. Wenn Ihre Eingabe abgeschlossen ist, stellen Sie sicher, dass der Stapel leer ist.

+0

Okay. Das hilft ein wenig. Ich bin mir nicht sicher, wie ich das machen soll. Wie ich in Code schreiben würde, wenn es öffnende Klammer ist, dann drück und schließe, stelle sicher, dass es Gegenstück zu öffnender Klammer ist. Irgendwelche Ratschläge dazu? – Lea

+1

Einfach durch die Eingabe ein Zeichen nach dem anderen durchlaufen und mit den verschiedenen Arten von Klammern vergleichen. –

+0

Ich habe eine Schleife, um jeweils ein Zeichen zu machen. Ich bin verloren wie ich den Stack in der main.cpp nutze. Wie verwende ich die in jedem deklarierten Methoden. – Lea