Nik*_*nka 0 language-agnostic algorithm infix-notation prefix postfix-notation
我有一个前缀表达式,它只有 4 个二元运算符 (+,-,*,/) 。评估此类表达式的直接方法是将其转换为后缀表达式,然后评估该表达式。但是我正在寻找一种直接执行此操作而不将其转换为任何其他表达式的算法?
简单递归:
Evaluate(input):
Read a token from input.
If the token is a value:
Return the value of the token
If the token is a binary operator:
Let first_argument = Evaluate(input)
Let second_argument = Evaluate(input)
Return apply(operator, first_argument, second_argument)
Run Code Online (Sandbox Code Playgroud)