Unary minus processing for bypass algorithm

Is there a better way to handle unary "-" when converting an infix expression to a postfix?

The prefix of each unary "-" with 0. will be obvious. Does anyone know of a better implementation? Thanks!

+8
algorithm shunting-yard rpn
source share
1 answer

As I did many years ago, a new operator was invented for my postfix expression. Therefore, when I came across a unary minus in the infix, I would convert it to # . So my postfix for a + -b became ab#+ .

And, of course, my evaluator should have known that # only pops one operand.

The view depends on how you use the postfix expression after creating it. If you want to display it, your special # operator probably confuses people. But if you just use it inside (I was), then it works great.

+9
source share

All Articles