Results 1 to 5 of 5

Thread: Expression: Convert infix notation to postfix notation.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484

    Expression: Convert infix notation to postfix notation.

    Hi,

    Compilers usually convert expressions to postfix notation. If i have an expression 1 + 2, then the postfix notation version would be 1 2 +, and if i have 7 / 4, i will get 7 4 /. I have to write a program to convert infix notation to postfix notation. However i can't get it to work. Pls help me check if there are any logical errors and such. If you want to read the algorithm first, it's below, thnx.

    The algorithm is to have two arrays and a stack. The program should read the expression into char array infix, then create the postfix expression in char array postfix.

    1. First push a left parenthesis onto the stack.
    2. Then append a right parenthesis to the end if infix.
    3. While stack is not empty, read infix from left to right and do
    the following:

    If current character in infix is a digit, copy it to the next element of postfix.

    If current char in infix is left parenthesis, pus it onto the stack.

    If current char in infix is an operator, pop operators ( if any) at top of stack while they have equal or higher precedence than current operator, then insert the popped operators in postfix.
    Push current character in infix onto stack.

    If current character in infix is a right parenthesis, pop operators form the top of stack and insert them in postfix until a left parenthesis is at top of stack. Then pop and discard the left parenthesis from the stack.

    thnx in advance

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the algorithm sounds ok to me
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    yes but what about my code? I really want to know what's wrong.

    thnx a lot

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    All i can say is that it looks heavy, a few tips:
    use an array instead of a stack (have a pointer go up and down) and don't push the operators on it, push 1 or 2.
    Use mainly one switch and drop the same cases by leaving break out, have pointers running trough both infix and postfix arrays.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    But what is wrong wif my program anyway...

    Besides, using a stack is the standard way of doing the problem.

    pls give a hand. How about just look at the functoin 'convertToPostfix()' and help me check if there are any logic errors, assume other functions to be correct first?

    thnx
    Last edited by iflash; Feb 17th, 2002 at 08:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width