后缀算法的后缀,用于一元运算符 [英] Infix to postfix algorithm that takes care of unary operators

查看:120
本文介绍了后缀算法的后缀,用于一元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

算法的I / p将是这样的表达式:

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

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

即标准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.

如果我使用标准转换算法,则 无法区分一元运算符和二进制运算符
就像a *(-b)会给我ab- *一样,它将以错误的方式求值。

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.

推荐答案

如果运算符是表达式中的第一件事, or 紧跟在另一个运算符之后, or 紧跟在左括号之后,则它是一元运算符。

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.

这篇关于后缀算法的后缀,用于一元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆