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 3 + تعني 2 + 3، و2 3 4 * + تعني 2 + (3 * 4) = 14. لا حاجة للأقواس.
استخدم مكدس (قائمة). اقرأ الرمز من اليسار إلى اليمين: ادفع الأعداد؛ عندما تواجه عاملاً، اسحب أعلى عددين، اجمعهما (انتبه للترتيب — القيمة الأولى المسحوبة هي الجانب الأيمن)، ودفع النتيجة. الجواب هو ما يتبقى في الأعلى.
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) تُقيّم قائمة رموز Notation بولندية العكسية — أعداد صحيحة مكتوبة كنصوص وعوامل +، -، *، /. استخدم مكدس؛ / يجب أن تقرب نحو الصفر. ارجع العدد الصحيح النهائي.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.