RPN calculator · RPN計算機
RPN calculator
In Reverse Polish Notation the operator comes after its two numbers: 2 3 + means 2 + 3, and 2 3 4 * + means 2 + (3 * 4) = 14. No brackets needed.
Use a stack (a list). Read tokens left to right: push numbers; when you hit an operator, pop the top two, combine them (mind the order — the first value popped is the right-hand side), and push the result. The answer is what's left on top.
RPN電卓
逆ポーランド記法では演算子が2つの数値の後に来ます: 2 3 + は 2 + 3 を意味し、 2 3 4 * + は 2 + (3 * 4) = 14 を意味します。 括弧は不要です。
スタック(リスト)を使います。トークンを左から右へ読み進めます: 数はスタックに積みます。演算子に出会うと、上の2つを取り出し、組み合わせます(順序に注意 — 最初に取り出した値が右辺)。結果をスタックに戻します。答えは残ったトップの値です。
Write rpn_eval(tokens) that evaluates a list of Reverse Polish Notation tokens — integers written as strings and the operators +, -, *, /. Use a stack; / should truncate toward zero. Return the final integer. · rpn_eval(tokens) を書いて、Reverse Polish Notationのトークンのリストを評価してください——文字列で表された整数と演算子 +, -, *, /。スタックを使用し、/ はゼロ方向に丸める必要があります。最終的な整数を返してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。