Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infix to postfix algorithm that takes care of unary operators

The I/p to the algo will be an expression like this:

a+(-b)
a*-b+c

i.e any expression that a standard C compiler would support.

Now I've the input already formatted as a stream of tokens , the tokens contain info whether its an operator or an operand. The algorithm should take this in and give me a postfix expression that I can evaluate.

If I use the standard conversion algo, I cant differentiate between an unary and a binary op. Like a*(-b) would give me ab-* ,which would evaluate in the wrong way.

like image 419
Aditya Avatar asked Jun 22 '13 18:06

Aditya


People also ask

What is the algorithm for infix to postfix?

To convert Infix expression to Postfix expression, we will use the stack data structure. By scanning the infix expression from left to right,if we get any operand, simply add it to the postfix form, and for the operator and parenthesis, add them in the stack maintaining the precedence of them.

Which will you use to convert infix to postfix prefix operators?

Using the stack data structure is the best method for converting an infix expression to a postfix expression. It holds operators until both operands have been processed, and it reverses the order of operators in the postfix expression to match the order of operation.

What is a postfix unary operator?

Postfix operators are unary operators that work on a single variable which can be used to increment or decrement a value by 1(unless overloaded). There are 2 postfix operators in C++, ++ and --.

What is the need to convert from infix to postfix?

Infix expressions are readable and solvable by humans. We can easily distinguish the order of operators, and also can use the parenthesis to solve that part first during solving mathematical expressions. The computer cannot differentiate the operators and parenthesis easily, that's why postfix conversion is needed.


2 Answers

If an operator is the first thing in your expression, or comes after another operator, or comes after a left parenthesis, then it's an unary operator.

You have to use other symbols for unary operators in your output string, because otherwise it is not possible to distinguish between binary and unary variants in the postfix notation.

like image 196
n. 1.8e9-where's-my-share m. Avatar answered Sep 21 '22 09:09

n. 1.8e9-where's-my-share m.


In your input, when you have 2 consecutive operators, the second operator will be unary. If you have more consecutive operators, all but the first will be unary operators.

Transform all your unary - operators to an operand -1 and an operator *, and remove all unary + operators.

If the first element is an operator, it is an unary operator.

Parenthesis are a special case, but you can do a first pass in which you ignore them. In the following example - is consecutive to *.

4*(-(5))

and your tokens would become:

4
*
(
-1
*
(
5
)
)
like image 43
Antonio Avatar answered Sep 22 '22 09:09

Antonio