class RPN
attr_reader :stack, :result
def initialize
@stack = []
@result = 0
end
def clear_stack
@stack = []
end
def pop()
@stack.pop
end
def push(data)
@stack << data.to_f
end
def operation(op)
raise "Not enough numbers in the stack" if (@stack.length < 2)
term1 = pop
term2 = pop
@result = eval("#{term2}#{op}#{term1}")
@stack << @result
end
def evaluate(str)
puts "Evaluating #{str}"
str.split(' ').each do |el|
'+-*/'.include?(el) ? operation(el) : push(el)
end
clear_stack
@result
end
end
c = RPN.new
p c.evaluate('1 2 3 4 5 6 7 8 9 10 + + + + + + + + +')
Y2xhc3MgUlBOCiAgICBhdHRyX3JlYWRlciA6c3RhY2ssIDpyZXN1bHQKICAgIGRlZiBpbml0aWFsaXplCiAgICAgICAgQHN0YWNrID0gW10KICAgICAgICBAcmVzdWx0ID0gMAogICAgZW5kCiAgICBkZWYgY2xlYXJfc3RhY2sKICAgICAgQHN0YWNrID0gW10KICAgIGVuZAogICAgZGVmIHBvcCgpCiAgICAgICBAc3RhY2sucG9wCiAgICBlbmQKICAgIGRlZiBwdXNoKGRhdGEpCiAgICAgIEBzdGFjayA8PCBkYXRhLnRvX2YKICAgIGVuZAogICAgZGVmIG9wZXJhdGlvbihvcCkKICAgICAgICByYWlzZSAiTm90IGVub3VnaCBudW1iZXJzIGluIHRoZSBzdGFjayIgaWYgKEBzdGFjay5sZW5ndGggPCAyKQogICAgICAgIHRlcm0xID0gcG9wCiAgICAgICAgdGVybTIgPSBwb3AKICAgICAgICBAcmVzdWx0ID0gZXZhbCgiI3t0ZXJtMn0je29wfSN7dGVybTF9IikKICAgICAgICBAc3RhY2sgPDwgQHJlc3VsdAogICAgZW5kCiAgICBkZWYgZXZhbHVhdGUoc3RyKQogICAgICAgIHB1dHMgIkV2YWx1YXRpbmcgI3tzdHJ9IgogICAgICAgIHN0ci5zcGxpdCgnICcpLmVhY2ggZG8gfGVsfAogICAgICAgICAgICcrLSovJy5pbmNsdWRlPyhlbCkgPyBvcGVyYXRpb24oZWwpIDogcHVzaChlbCkKICAgICAgICBlbmQKICAgICAgICBjbGVhcl9zdGFjawogICAgICAgIEByZXN1bHQKICAgIGVuZAplbmQKCmMgPSBSUE4ubmV3CnAgYy5ldmFsdWF0ZSgnMSAyIDMgNCA1IDYgNyA4IDkgMTAgKyArICsgKyArICsgKyArICsnKQo=