All pastes #1753587 Raw Edit

Unnamed

public ruby v1 · immutable
#1753587 ·published 2010-01-16 15:07 UTC
rendered paste body
require "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