Answer the question
In order to leave comments, you need to log in
When implementing reverse polish notation, what about the unary minus sign?
I'm trying to solve one katu where you need to spread out a line with a mathematical expression.
And I found that this is solved with the help of the OPN, and took it as a basis, but with a unary minus, it is proposed either to replace the character or to convert the expression, for example, for -3 to OPN -> 0 3 - . I settled on replacing the symbol, I will replace it with ±.
The problem with expressions like: -(-1 + 2) , in my Polish implementation it will be like ± ± 1 2 +. how to insert the unary correctly?
And I'll attach my code . I just really want to solve this problem with the parser already. I torture her for a couple of days. There are only 2 tests left in the kata, I need to somehow take into account the unary one in front of the brackets, I don’t know how. As I understand it, I do not insert ± there.
Answer the question
In order to leave comments, you need to log in
If the notation is reversed, then the ± sign, like binary operations, must come after the operands. Then -(-1 + 2) becomes 1 ± 2 + ±. By ±, you need to change the sign of the number at the top of the stack.
Accordingly, in the parser, if you see a unary minus before the brackets, then translate the brackets into the notation, and then add ±.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question