Thread: calculating postfix problem

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    9

    calculating postfix problem

    I don't know what's going wrong with this. Here's the code that I think is messing it up: the main evaluation process, of course.
    postfix: 8 4 + = 8 + 4
    Code:
    int evaluatePostfixExpression(char postfix[])
    {
        int i, length;
        int x,y;
        STACK stack;
    
        initStack(&stack); // initializze stack
    	get_postfix(postfix); // get postfix expression from user
        length = strlen(postfix);  
    
        // if strlen if postfix is more than zero
        if ( length )
        {     
    		
            for ( i=0; i<length; i++ )
            {
    			/* if current operator in postfix is a digit */
                if ( isdigit(postfix[i]) )
                {
    				push(&stack, postfix[i]);
                }
                
                /* if current operator in postfix is an operator */
                else if ( isOperator(postfix[i]) )
                {
    				x = pop(&stack);
    				y = pop(&stack);
    
    				push(&stack, calculate(y, x, postfix[i]) );
    			}
    		}
    
    		return(pop(&stack));
    	}
    
    	else 
    		return -1;
    }
    subtraction and mod seem to be working. Addition, multiplication and division are returning answers where 0=96, 1=97 etc. The exponent might as well be a random number generator :P

    Calculate and isOperator functions just in case:

    Code:
    int calculate(int op1, int op2, char the_operator)
    {
    	if(the_operator == '+')
    		return(op1 + op2);
    	else if(the_operator == '-')
    		return(op1 - op2);
    	else if(the_operator == '*')
    		return(op1 * op2);
    	else if(the_operator == '/')
    		return(op1 / op2);
    	else if(the_operator == '^')
    		return(op1 ^ op2);
    	else if(the_operator == '%')
    		return(op1 % op2);
    }
    
    int isOperator(char c) // is c and operator
    {
         if ( c == '+' || c == '-' || c == '*' ||
               c == '/' || c == '%' || c == '^' )
         {
              return TRUE;
         }
         else
              return FALSE;
    }
    edit: http://k5-k.element80.net/files/saa1213.cpp for the entire file.

    more edit: It was taking the ascii values. So if I subtract 48 from each op1 and op2 it fixes the strange output problems with simple input. Of course, then I had to add 48 when calling the calculate function so I can switch between working with ascii values and integers. So it's almost working. One more thing to fix. Don't worry about it. Might be useful later on for people who use the search, as there was lots on infix to postfix, but not much on postfix.
    Last edited by Axolotl; 04-01-2004 at 06:45 PM.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Quote Originally Posted by Axolotl
    Don't worry about it. Might be useful later on for people who use the search, as there was lots on infix to postfix, but not much on postfix.
    What were the expressions that gave the bad output?

    At the moment you are only handling single digit numbers so numbers of more than 1 digit may result in some unexpected output.
    Last edited by major_blagger; 04-01-2004 at 07:30 PM.

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ok first have you used the conversion functions on the input (i.e. from string or char to int) and the such. I can't remember what they are right now as I have to run. Also can you please say what type of errors exactly you get along with example answers. I don't understand what you explained.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Newbie calculating problem
    By Andy123 in forum C Programming
    Replies: 5
    Last Post: 02-09-2006, 12:57 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM