Infix to postfix algorithm that takes care of unary operators

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 another 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.


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
)
)