Advertisement
VladikOtez

Sample Eval

Sep 14th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.35 KB | None | 0 0
  1. def calc_rev_pol(expr)
  2.   return 0 if expr.empty?
  3.   ary = expr.scan(/\d+\.?\d?|\D/).delete_if{|token| token == ' '}
  4.   stack = []
  5.   ary.each do |token|
  6.     if %w(+ - / *).include?(token)
  7.       val2 = stack.pop.to_f
  8.       val1 = stack.pop.to_f
  9.       stack.push(val1.send(token, val2))
  10.     else
  11.       stack.push(token)
  12.     end
  13.   end
  14.   stack.pop.to_f
  15. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement