RPN calculator
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
ใน Reverse Polish Notation operator มา หลัง ตัวเลขสองตัว: 2 3 + หมายความว่า 2 + 3, และ 2 3 4 * + หมายความว่า 2 + (3 * 4) = 14. ไม่ต้องใช้วงเล็บ
ใช้ stack (a list). อ่าน tokens จากซ้ายไปขวา: push numbers; เมื่อเจอ operator, pop สองค่าบนสุด, ผสมผสาน (ระวังลำดับ — ค่าแรกที่ถูก pop คือด้านขวา), และ push ผลลัพธ์. คำตอบคือสิ่งที่เหลืออยู่บนยอด
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 tokens — ตัวเลขเขียนเป็น string และ operator +, -, *, / ใช้ stack; / ควรตัดทอนเข้าหาศูนย์ กลับค่า integerสุดท้าย
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่