【问题标题】:clarification regarding convertion of postfix关于后缀转换的说明
【发布时间】:2019-11-16 08:28:18
【问题描述】:

所以这个表达式,A - B + C * (D / E) 结果为 A B - C D E / * +

我认为在将中缀转换为后缀时,您必须将运算符保留在堆栈中,直到在其后面或表达式末尾看到优先级较低的运算符,然后将其全部写下来。

但是减号和加号的优先级相同,那为什么要写下减号而不把它放在堆栈中呢?

我认为我对转换方法的理解有问题。请提供解释,最好提供分步解决方案。我真的很困惑减号会发生什么,因为我对转换的理解不同。非常感谢。

【问题讨论】:

    标签: stack postfix-notation infix-notation


    【解决方案1】:

    Shunting yard algorithm 规则状态:

    if the token is an operator, then:
        while ((there is a function at the top of the operator stack)
               or (there is an operator at the top of the operator stack with greater precedence)
               or (the operator at the top of the operator stack has equal precedence and is left associative))
              and (the operator at the top of the operator stack is not a left parenthesis):
            pop operators from the operator stack onto the output queue.
    

    注意第二个or 条件:运算符堆栈顶部的运算符具有相同的优先级并且是左关联的

    +- 运算符具有相同的优先级,并且是左关联的。因此,当您看到 + 时,您将删除 - 运算符。

    另见https://www.geeksforgeeks.org/operator-precedence-and-associativity-in-c/。尽管这是 C 特有的,但大多数语言对通用运算符使用相同的优先级和关联性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 2019-05-06
      相关资源
      最近更新 更多