Infix to postfix algorithm that takes care of unary operators

后端 未结 3 1839
南笙
南笙 2021-01-05 13:49

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

3条回答
  •  有刺的猬
    2021-01-05 14:29

    You could simply convert -6 to 06- to eliminate unary operators completely. I like this approach since it is more orthogonal and you do not need to take care of special cases when processing.

    An alternative approach is to use different symbols for the unary and the binary versions of operators using the same symbol, eg. - remains binary minus and ~ becomes negation sign.

提交回复
热议问题