Thread: evaluating unary negation operator in an infix expression??

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Question problem with stacks!

    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...
    Code:
    #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;
    }
    Yev
    Last edited by YevGenius; 05-09-2004 at 05:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM