Thread: Infix - Postfix

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Infix - Postfix

    Code:
    switch (input[i])
         {
                case '(': 
                     operator_stack.push(input[i]);
                     break;
                     
    
                case '+':
                case '-':
                case '*':
                case '/':
                case '^':
                     if (operator_stack.empty())
                        operator_stack.push(input[i]);
                     else
                     {
                        int operator_precedence = 0;
                        int top_operator_precedence = 0;
                         
                        if (input[i] == '^' ) 
                            operator_precedence = 3;
    
                        if (input[i] == '*'  || input[i] == '/')
                            operator_precedence = 2;
    
                        if (input[i] == '+'  || input[i] == '-')
                            operator_precedence = 1;                        
                       
                        if (operator_stack.top() == '^' ) 
                            top_operator_precedence = 3;
    
                        if (operator_stack.top() == '*'  || operator_stack.top() == '/')
                            top_operator_precedence = 2;
    
                        if (operator_stack.top() == '+'  || operator_stack.top() == '-') 
                            top_operator_precedence = 1;  
                       
                        if (operator_precedence >= top_operator_precedence) 
                            operator_stack.push (input[i]);
                        else
                            value_stack.push (input[i]);    
                     }   
                     break;
                               
                     
                case ')':
                     while (operator_stack.top() != '(' )
                     {
                           value_stack.push (operator_stack.top());
                           operator_stack.pop();
                     }                
                     break;
                                     
                     
               default:
                       value_stack.push (input[i]);
    The logic here is wrong, when I try the example from wikipedia 3+4*2/(1−5)^2^3
    I keep geting 34215-23^^(/*+ insetad of 342*15-23^^/+

    Where did I go wrong ?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
                        if (operator_precedence >= top_operator_precedence) 
                            operator_stack.push (input[i]);
                        else
                            value_stack.push (input[i]);
    ^^ Has issues
    Don't quote me on that... ...seriously

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Start with the simple test 3+4+5

    Also - do you remove '(' from the original stack when the ')' is encountered?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  3. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  4. Converting from infix to postfix
    By jcramer in forum C Programming
    Replies: 4
    Last Post: 03-27-2004, 09:23 PM
  5. Infix to Postfix
    By dat in forum C Programming
    Replies: 6
    Last Post: 06-16-2003, 08:46 AM