后缀算法的后缀,用于一元运算符

编程入门 行业动态 更新时间:2024-10-13 16:17:25
本文介绍了后缀算法的后缀,用于一元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

算法的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.

更多推荐

后缀算法的后缀,用于一元运算符

本文发布于:2023-11-29 12:16:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后缀   算法   运算符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!