Peregrine_Falcon's blog

By Peregrine_Falcon, history, 18 months ago, ,

Convert Infix to Postfix Notation

Initially we’ve a string S which represent the expression in infix format. Now we need a character stack.

*We’ll iterate through the string S, from left to right.

• Whenever we encounter an operand we’ll print it.

• If we encounter an ‘(‘ we’ll push it to the stack.

• If we encounter an ‘)’ we’ll take the following actions:

1. We’ll continue to pop top of the stack until we find the ’(‘
2. Before we pop top of the stack, we’ll print every character of top of the stack except ‘)’.
• If we encounter an operator, we’ll take the following actions:

1. We’ll continue to pop top of the stack until we find that S[ i ] is greater than the top of the Stack (by the rules of precedence) Ex: if we found an Multiplication operator on S [ i ] We’ll continue searching for addition or subtraction operator on top of the stack. We’ll stop searching until it’s empty or we’ve found one. Before that we’ll pop top of the Stack. And before pop, we’ll print every operators.
• When the traversal will finished, we’ll continue to pop top of the stack until it's empty, and before pop we’ll print every operators.

You can see my Code

Convert Infix to Prefix Notation

To convert an infix to Prefix, first we’ve to know how to convert Infix to postfix notation.

Initially we’ve a string S which represent the expression in infix format.

• Reverse the string S. After reversing A+B*C will become C*B+A. Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.

• Obtain the postfix expression of the modified string S. We’ve to handle the ‘(‘ as ‘)’ and ‘)’ as ‘(‘

• Just reverse the postfix expression we’ve.

You can see my Code

Here are some basic practice problems: WhatFix Notation Equation Convert the Expression

Thank you for reading. If there anything else, please let me know.

• +1

 » 18 months ago, # |   0 I am new in programming.Can you give me link of tutorial of this?
•  » » 18 months ago, # ^ |   +1
•  » » » 18 months ago, # ^ |   0 Thanks
 » 18 months ago, # |   0 Thank you.It may be more useful to add, to the blog above, a link to a page that explanes those idioms (infix, prefix and postfix notations), like the first link in the reply to the previous comment, or to explane them before the solving approach.
•  » » 18 months ago, # ^ |   +1 Ok, I'm gonna add some basic practice problem too, with the link you asked.
•  » » » 18 months ago, # ^ |   0 Thank you very much :D
•  » » » » 18 months ago, # ^ |   +1 Always welcome. And also thank you for the suggestion.
 » 5 months ago, # |   0 Go HereAfter watching this you will become master in converting between these two .
 » 5 months ago, # |   0 Please check your code with the expression : 2^3^4 It's providing the wrong output, probably due to the precedence function as precedence should also include the left to right precedence of exponential operator and right to left precedence for other operators in case of multiple operators of the same kind.