| #include<iostream.h> #include<string.h> const int SIZE = 100; typedef char itemType; struct Stack { private : itemType items[SIZE]; int top; public : Stack(){ top = -1;}; Stack (const Stack &s); int isFull(); int isEmpty(); int push(itemType newItem); int pop(); int getTop(itemType &item); int getTopIndex() { return (top);}; itemType *getDataAddress() { return (items);}; }; //============================================================================== Stack :: Stack(const Stack &s) { top = s.getTopIndex(); itemType* data = getDataAddress(); for ( int index=0 ; index<= top ; ++index) items[index] = data[index]; } //============================================================================== int Stack :: isFull() { return (top==SIZE -1);}; //============================================================================== int Stack :: isEmpty() {return (top<0); }; //============================================================================== int Stack :: push (itemType newItem) { if( isFull()) return 0; else { ++top; items[top]=newItem; return 1; } } //============================================================================== int Stack :: pop() { if(isEmpty()) return 0; else { --top; //It will be overwritten next time return 1; } } //============================================================================= int Stack :: getTop(itemType &topItem) { if(isEmpty()) return 0; else { topItem = items[top]; return 1; } } //============================================================================== int main(){ Stack s; int noerrors=1; char str[20]; cout <<"Enter a mathematical string"; cin >>str; int len = strlen(str); for ( int j=0 ; j<len ; j++) { switch(str[j]) { case '(':{ s.push('('); break; } case ')':{ if(!s.isEmpty()) s.pop(); else { noerrors = 0; cout<<"Expression error:missing opening parenthesis"; } break; } default: break; } //end switch }//end for if(!s.isEmpty()) { noerrors=0; cout<<"expression error , too many opening parentheses"; } if(noerrors) cout<<"Valid expression"; }//end main |