im writing a stack to check for parenthesis but the output alway read " expression not match" even if the stack is balanced. hope someone can see what is wrong with my program.below is the coding
Code:#include <iostream> #define STACKSIZE 80 #define TRUE 1 #define FALSE 0 using namespace std; void StackInit(); int IsStackEmpty(); int IsStackFull(); int StackPush(char c); char StackPop(); char ViewStackTop(); char myStack[STACKSIZE]; int top; int main() { char *c; c = myStack; int balance; cout << " Please enter an equation:"; cin.getline(myStack, STACKSIZE); for (int i = 0; i < STACKSIZE; i++) { if(*c == '(' || *c == '{' || *c == '[') { StackPush(*c); } else { if(*c == ')' || *c == '}' || *c == ']') { if(IsStackEmpty() == TRUE) { balance = FALSE; } else { StackPop(); } } } } if(balance = FALSE) { cout << "Result: Expression not match"; } else { if(!IsStackEmpty()) { cout << "Result: Expression not match"; } else { cout << "Result Expression is vaild"; } } system("pause"); return 0; } void StackInit() { top = -1; } int IsStackEmpty() { if (top == -1) return TRUE; return FALSE; } int IsStackFull() { if (top == (STACKSIZE - 1)) return TRUE; return FALSE; } int StackPush(char c) { if (top == (STACKSIZE - 1)) return FALSE; myStack[++top] = c; return TRUE; } char StackPop() { if (top == -1) return '\0'; return myStack[top--]; } char ViewStackTop() { if (top == -1) return '\0'; return myStack[top]; }


LinkBack URL
About LinkBacks




Reply With Quote


Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum