Hello,

Thank you for opening the post. I am trying to implement a unary negation operator function to use in my program that uses 2 stacks: a number and operator stack. I get a runtime error in Visual Studio saying "Runtime Check Failure #2: stack around the variable 'nst' was corrupted." If anyone could offer any assistance, I would appreciate it. Thanks a lot in advance.

Below is the full code...

YevCode:#include <iostream> #include <stdlib.h> #include <string> using namespace std; int prec( char op ); long evaluate( char op, long x, long y ); long expon(long x, long y); // function prototype for exponent operator long negate(long y); // function prototype for negation operator int main( ) { long nst[41]; // The number stack. long ntop = -1; // The top of the number stack. char ost[41]; // The operator stack. long otop = -1; // The top of the operator stack. string ebuff; // Buffers the arithmetic expression. int ie; // Indexes the arithmetic expression. // Prompt the user for an expresion and read it. cout << "enter arithmetic expression:\n"; cin >> ebuff; // Let's stick a special character at the end of the string. ebuff += ']'; // Put the beginning of line character in the operator stack. otop = 0; ost[0] = '['; // Scan throughout the expression one character at a time. for( ie = 0; ; ie++ ) { // Stack the numbers immediately. if( ebuff[ie] >= '0' && ebuff[ie] <= '9' ) { ntop++; nst[ntop] = ebuff[ie] - '0'; continue; } // We have an operator. If it is a left parentheses, stack it. if( ebuff[ie] == '(' ) { otop++; ost[otop] = '('; continue; } // Perform as may operations from the stack as the // precedence allows. while( prec( ost[otop] ) >= prec( ebuff[ie] ) ) { // Left paren or [ in stack means we have nothing // left to evaluate. if( ost[otop] == '[' || ost[otop] == '(' ) break; // Perform the indicated operation. nst[ntop-1] = evaluate( ost[otop], nst[ntop-1], nst[ntop] ); ntop--; otop--; } // If we broke out because of matching the beginning of the line, // the number on the top of the stack is the result. if( ebuff[ie] == ']' ) { cout << "value of expression is: " << nst[ntop] << endl; return 0; } // If we broke out due to matching parens, pop the paren // out of the stack. if( ost[otop] == '(' && ebuff[ie] == ')' ) { otop--; continue; } // Stack the operator that we could not evaluate. otop++; ost[otop] = ebuff[ie]; continue; } } // Function to return the precedence value for an operator. int prec( char op ) { switch( op ) { case '[': return 0; case ']': return 0; case '~': return 1; // negation is done last (PROBLEM AREA) case '(': return 2; case ')': return 2; case '+': return 5; case '-': return 5; case '*': return 10; case '/': return 10; case '^': return 11; // return 11 since exponent function precedes all others default: cout << "Illegal operator\n"; exit( 1 ); return 0; } } // Applies an operator to numbers. long evaluate( char op, long x, long y) { // Based on the operator, perform the operation. switch( op ) { case '+': return x + y; case '-': return x - y; case '*': return x * y; case '/': return x / y; case '~': return negate(y); // negate quantity at top of stack; case '^': return expon(x,y); default: cout << "Illegal operator" << endl; exit( 1 ); return 0; } } long negate(long y) { y = -y; // negate top of stack return y; } long expon(long x, long y) { int ans=1; for (; y!=0; y--) { ans = ans*x; } return ans; }