def calc(op,l,r) case op when '+' then l+r when '*' then l*r when '-' then l-r when '/' then l/r when '%' then l%r end end while gets do a = $_.scan(%r!\d+|[-+/*%]!) while (l, ope, r = a.shift(3)) and ope do a.unshift calc(ope,l.to_i,r.to_i) end puts l end