"
Use the Stack classes in a program that reads a string, one character at a time, and determines whether the string contains balanced parentheses. That is, for each left parenthesis (if there are any) there is exactly one matching right parenthesis later in the string.
"
__________
//Stack.h
Expand|Select|Wrap|Line Numbers
- //Stack.h
- #include<iostream>
- using namespace std;
- #ifndef STACK
- #define STACK
- typedef char StackElement ;
- class Stack
- {
- public:
- Stack (int numElements =128);
- Stack (const Stack &original);
- ~Stack();
- Stack & operator= (const Stack &rightHandSide);
- bool empty() const ;
- void push (const StackElement & value);
- void display(ostream & out)const;
- StackElement top() const;
- void pop();
- void check (int c1,int c2);
- private:
- int myCapacity,myTop;
- StackElement *myArray;
- };
- #endif
//The Stack.cpp
Expand|Select|Wrap|Line Numbers
- //Stack.cpp
- #include"Stack.h"
- #include<new>
- #include <cassert>
- using namespace std;
- Stack::Stack(int numElements)
- {
- assert (numElements > 0);
- myCapacity = numElements;
- myArray = new (nothrow ) StackElement[myCapacity];
- if (myArray !=0)
- myTop = -1;
- else
- {
- cerr<<"Inadequate memory to allocate stack \n"
- "--terminating execution\n";
- exit(1);
- }
- }
- //-----
- Stack::Stack (const Stack & original)
- :myCapacity(original.myCapacity),myTop(original.myTop)
- {
- myArray = new(nothrow) StackElement[myCapacity];
- if (myArray !=0)
- for (int pos =0 ; pos<=myTop;pos++)
- myArray[pos]=original.myArray[pos];
- else
- {
- cerr<<"*Inadequate memory to allocate stack ***\n";
- exit(1);
- }
- }
- //-------------
- Stack::~Stack()
- {
- delete [] myArray;
- }
- //-------------
- Stack & Stack::operator=(const Stack & rightHandSide)
- {
- if (this != &rightHandSide)
- {
- if (myCapacity != rightHandSide.myCapacity)
- {
- delete[] myArray;
- myCapacity = rightHandSide.myCapacity;
- myArray = new StackElement[myCapacity];
- if (myArray == 0 )
- {
- cerr<<"***Inadequate memory ***\n";
- }
- }
- myTop = rightHandSide.myTop;
- for ( int pos =0 ; pos <=myTop ; pos++)
- myArray[pos] = rightHandSide.myArray[pos];
- }
- return *this;
- }
- //----------
- bool Stack::empty() const
- {
- return (myTop==-1);
- }
- //---------
- void Stack::push( const StackElement & value )
- {
- if ( myTop <myCapacity - 1)
- {
- ++myTop;
- myArray[myTop] = value;
- }
- else
- {
- cerr << " *** Stack full -- can not add new value *** \n"
- "Must increase value of STACK_CAPACITY in Stack.h \n";
- exit(1);
- }
- }
- void Stack::display(ostream & out ) const
- {
- for ( int i = myTop ; i>=0 ; i--)
- out << myArray[i] << endl;
- }
- //--------
- StackElement Stack::top() const
- {
- if (!empty() )
- return(myArray[myTop]);
- else
- {
- cerr<<" *** Stack is empty -- returning garbage value ***\n";
- StackElement garbage;
- return garbage;
- }
- }
- //---------
- void Stack::pop()
- {
- if ( !empty() )
- myTop--;
- else
- cerr<<"***Stack is empty ***\n";
- }
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using namespace std ;
- #include "Stack.h"
- int main ()
- {
- char ch;
- int count1=0,count2=0;
- int cap;
- cout <<"Enter stack capacity: ";
- cin >> cap;
- Stack s(cap);
- cout << "Stack created. Empty? " <<boolalpha<< s.empty() <<endl;
- cout <<" How many elements to add to the stack ? ";
- int numItems;
- cin>>numItems;
- cout <<"Enter stack charcter contanent: ";
- for ( int i =1 ; i<= numItems; i++ )
- cin >> ch;
- if ( ch == '(')
- s.push(ch);
- if (ch ==')')
- s.pop();
- if(s.empty())
- cout<<"The parentheses are balanced . \n";
- else
- cout<<"The parentheses are not balanced . \n";
- return(0);
- }
The problem is : when I entered '(' only, the program said it is balanec !!!!
the rogram in this cast should said :The parentheses are not balanced .
because we push one parenthesis .
and the empty Stack function is not empty ..
So i do not know What I have to do :(