I found so much algorithms on google but, I can't find runnable algorithm that convert infix to postfix. How to convert infix equation to postfix equation on C#? Please help...
-
Take a look at this post http://stackoverflow.com/questions/1438030/infix-to-postfix-converter or this one http://www.codeproject.com/Tips/370486/Converting-InFix-to-PostFix-using-Csharp-VB-NET – Mulflar Oct 17 '16 at 11:53
1 Answers
This is a great little project for learning to program. There are two parts: parse the code into a tree, then print the tree as postfix. Each tree node is an operator with two children: 4 + (5/6)
has +
as the root node, and the children are 4 and 5/6. When you look deeper at the 5/6
node, you find a parent with two children: the parent is /
and the children are 5
and 6
.
Parsing is the hardest part. To parse an expression, look at what you need to parse, find the operator, and note what's left of the operator and what's to the right. The operator is the node, and it has two children: the Parse(stuff to the left)
and Parse(stuff to the right)
. You'll need to pay extra attention to minus (which is a unary operator and so not strictly infix). I suggest treating minus as a special case: -4
should be parsed as -4
, not a minus node with one child 4
. When you parse the equation, all the internal nodes should be operators and all the leaf nodes will be numbers.
After you've parsed it, just print. For the root node, print(node)
will print "(", the left child (print it recursively), the right child (print it recursively), the operator, then close the expression (node) with ")".
I don't have a link to the algorithm handy, but you'll actually grow a lot more if you implement it yourself.

- 6,351
- 1
- 26
- 36