📜  数据结构-表达式解析

📅  最后修改于: 2021-01-11 06:44:24             🧑  作者: Mango

• 中缀符号
• 前缀(波兰语)表示法
• 后缀(反向波兰语)表示法

后缀符号

Sr.No. Infix Notation Prefix Notation Postfix Notation
1 a + b + a b a b +
2 (a + b) ∗ c ∗ + a b c a b + c ∗
3 a ∗ (b + c) ∗ a + b c a b c + ∗
4 a / b + c / d + / a b / c d a b / c d / +
5 (a + b) ∗ (c + d) ∗ + a b + c d a b + c d + ∗
6 ((a + b) ∗ c) – d – ∗ + a b c d a b + c ∗ d –

解析表达式

关联性

Sr.No. Operator Precedence Associativity
1 Exponentiation ^ Highest Right Associative
2 Multiplication ( ∗ ) & Division ( / ) Second Highest Left Associative
3 Addition ( + ) & Subtraction ( − ) Lowest Left Associative

＆plus;中b * c ，将首先对表达式部分b * c求值，并将乘法优先于加法。我们在此处对＆plus;使用括号首先要评估的b ，例如(a＆plus; b)* c

后缀评估算法

``````Step 1 − scan the expression from left to right
Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and perform operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation
``````