Jump to content

Infix-Postfix translation?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
LanguidLegend

LanguidLegend

    Newbie

  • Members
  • Pip
  • 1 posts
Hello, I am making a simple Java calculator program, which takes input in the form of simple algebraic expression, translates it to its counter-form (infix-to-postfix & vice versa), and evaluates it.
So I have been racking my brains all day today trying to make a method which takes a String of infix and returns it's postfix form, yet I'm always taking one step forward and two back haha.
If someone else has accomplished this, can you post it or link me to where there it can be found online?

Essentially, I want the output to be in the form of
Original: ((12-4-5)*^3*^154*(12+^3)/55+13*5)

Postfix: 12 4 - 5 - 3 ^ * 154 ^ * 12 3 ^ + * 55 / 13 5 * +

I'm eternally grateful to whoever can help me out with this headache. ^^

#2
Nathandelane

Nathandelane

    Newbie

  • Members
  • PipPip
  • 22 posts
Hello fellow developer. For about two years now, I have also racked my brains with this problem. It is a fun one to solve. I have solved it several different ways. Basically what you need to do are the following:
  • Tokenize the infix expression into numbers, operators, perentheses and functions.
  • Parse through the tokens and rotate them into infix format. This is usually best done using two stacks, one for operators and on for the final postfix result.

This is part of what is called the Shunting Yard algorithm, which has long been proven to be the fastest method for translating an infix-notation-expression into a postfix-notation-expression. More on the Shunting Yard algorithm can be found here.

Good luck. You may choose to see what I've done. On SourceForge.net, I have a C#.NET project named pcdotnet (PC.NET) which accomplishes the Shunting Yard algorithm as well as many other functions. Don't give up. It's a fun problem to solve. Personal Calculator is another of my projects, which is basically the same as pcdotnet, only in Java. Feel free to use them as references if you'd like.