rendered paste bodyrequire "stack.rb"def rpn_calc(stack, token) case token when "+" stack.push(stack.pop + stack.pop) when "-" op_a = stack.pop op_b = stack.pop stack.push(op_b - op_a) when "*" stack.push(stack.pop * stack.pop) when "/" op_a = stack.pop op_b = stack.pop stack.push(op_b / op_a) endendprint "> "while gets priority = {"*" => 2, "/" => 2, "+" => 1, "-" => 1, "(" => 0, ")" => 0} input = $_ stack = Stack.new rpn_stack = Stack.new input.scan(/[\d\.]+|[\+\-\*\/\(\)]{1}?/).each{ |token| case token when "+","-","*","/" rpn_calc(rpn_stack, stack.pop) while not stack.empty? and priority[stack.peek] >= priority[token] stack.push(token) when "(" stack.push("(") when ")" rpn_calc(rpn_stack, stack.pop) until stack.peek == "(" stack.pop else rpn_stack.push(token.to_f) end } rpn_calc(rpn_stack, stack.pop) until stack.empty? puts rpn_stack.pop if not rpn_stack.empty? print "> "end