Here's my new code and it works but doesn't do what I want it to do. I want it to display matched symbols founds when use properly and unmatched when not.
Code:
#include <iostream>
using namespace std;
const int DefaultListSize = 50;
typedef char Elem;
class Astack
{
private:
int top; /*Index for top element*/
int size; /*Maximum size of stack*/
Elem*listArray; /*Array holding stack elements*/
public:
Astack(int sz =DefaultListSize) /*Constructor*/
{size = sz; top = 0; listArray = new Elem[sz];}
~Astack() { delete [] listArray;} /*Destructor*/
void clear() {top = 0;}
bool push(const Elem& item){
if(top == size) return false; /* Stack is full*/
else {listArray [top++] = item;
return true;
}
}
bool pop(Elem& item){ /*Pop top element*/
if(top == 0) return false;
else {item = listArray[--top];
return true;
}
}
bool topValue(Elem& item) const { /*Return top element*/
if (top == 0) return false;
else {item = listArray[top - 1];
return true;
}
}
int length() const {return top;}
bool IsEmpty() const {if(top == 0) return true;
else return false;
}
};
int main(){
Astack S;
char s;
cout <<"Enter data:";
cin >> s;
while(s != '#'){
if(s == '(' || s == '[' || s == '{'){
S.push(s);
}
else
cin.ignore(s);
if(s == ')' || s == ']' || s == '}'){
cout << S.pop(s);
}
if(S.IsEmpty())
cout << "The expression has only matching grouping symbols" << endl;
else
cout << "The expression has some unmatched grouping symbols" << endl;
}
return 0;
}