我不确定您需要针对哪些表达式来证明算法。但如果它们看起来像典型的 RPN 表达式,则需要建立如下内容:
1) 算法适用于 2 个操作数(和一个运算符)
和
算法适用于 3 个操作数(和 2 个运算符)
==> 那将是你的基本情况
2) 如果算法适用于 n 个操作数(和 n-1 个运算符)
那么它必须适用于 n+1 个操作数。
==> 这将是证明的归纳部分
祝你好运 ;-)
请注意数学证明,以及它们有时令人困惑的名称。在归纳证明的情况下,有时仍期望通过演绎逻辑“弄清楚”某些东西(某些事实或某些规则),但是这些事实和规则放在一起构成了更广泛的真理,购买归纳;那就是:因为基本情况被确定为真,并且因为有人证明如果 X 对于“n”情况为真,那么 X 对于“n+1”情况也为真,那么我们不需要尝试每个情况,可能是一个很大的数字,甚至是无限的)
Back on the stack-based expression evaluator... One final hint (in addtion to Captain Segfault's excellent explanation you're gonna feel over informed...).
The RPN expressions are such that:
- they have one fewer operator than operand
- they never provide an operator when the stack has fewer than 2 operands
in it (if they didn;t this would be the equivalent of an unbalanced
parenthesis situation in a plain expression, i.e. a invalid expression).
Assuming that the expression is valid (and hence doesn't provide too many operators too soon), the order in which the operand/operators are fed into the algorithm do not matter; they always leave the system in a stable situtation:
- either with one extra operand on the stack (but the knowledge that one extra operand will eventually come) or
- with one fewer operand on the stack (but the knowledge that the number of operands still to come is also one less).
So the order doesn't matter.