problems on postfix conversion

This is a discussion on problems on postfix conversion within the C++ Programming forums, part of the General Programming Boards category; here is the algorithm and a code below in converting infix notation 1) if you encounter "(" push on it ...

  1. #16
    Notorious Turbo C killer blacksnake's Avatar
    Join Date
    Jan 2007
    Location
    philippines
    Posts
    50

    Question

    here is the algorithm and a code below in converting infix notation

    1) if you encounter "(" push on it on stack
    Code:
              if(x[i]=='('){stack.push(x[i]);}
    2) if you encounter operators (+,-,*,/) then compare the operators with the top of the stack.As long as the top of the stack has operators with higher or equal priority to the scanned operators you have to keep popping the stack and store it in result

    * * If the token is an operator, o1, then:
    Code:
              else if((x[i]=='+')||(x[i]=='-')||(x[i]=='*')||(x[i]=='/'))
    * while there is an operator, o2, at the top of the stack, and either

    o1 is associative or left-associative and its precedence is less than (lower precedence) or equal to that of o2, or
    o1 is right-associative and its precedence is less than (lower precedence) that of o2, (i got confised with the algorithm)

    pop o2 off the stack, onto the output queue;
    Code:
                      q=q+stack.pop(); //where q=is a string and stack.pop()=is an o2;
    * push o1 onto the stack.
    Code:
                      stack.push(x[i]); //where x[i] = o1
    Lastly you have to push the scanned operator on the stack
    Code:
                      stack.push(x[i]); //where x[i] = o1
    3)If you encounter any digits or alphabets then store in in result
    Code:
                        else if(isdigit(x[i])){q=q+x[i];}
    4)if you encounter ")" keep popping the stack unless you encounter "(" on the stack and store it in result
    Code:
              else if(x[i]==')'){q=q+stack.pop();}
    Lastly decrement top by 1 as "(" is of no importance to us
    Code:
              top=top-1;
    as i see, that I confused on the scanning of precedence of operators, can you show me clear understanding about the precedence of the operators?
    Turbo C Makes me Sick....i just want to learn data structures in latest c++

    Le Tormente (fr. the torment)

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The precedence of the operators:
    ( and ) aren't operators, so don't count
    * and / are equally high
    + and - are equally low

    The algorithm says to pop if the operator read in is "less than or equal to" the one on the stack. So + and - mean you have to pop every operator you see; * and / mean you have to pop other * and /.

Page 2 of 2 FirstFirst 12
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. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 08:33 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  4. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2008, 12:06 AM
  5. postfix
    By datainjector in forum C Programming
    Replies: 11
    Last Post: 11-20-2002, 08:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21