jkaihsu
3/7/2013 - 8:23 AM

Create an RPNCalculator class which can evaluate expressions written in Reverse Polish notation. It should have an evaluate instance meth

Create an RPNCalculator class which can evaluate expressions written in Reverse Polish notation.

It should have an evaluate instance method which takes as its input a valid RPN expression and returns its evaluation. Your calculator only needs to handle addition, multiplication, and subtraction (not division).

Operators and numbers should be separated by a single space.

class RPNCalculator

  def evaluate(string)
    string = string.split
    numbers = []
    operators = []
    a = 0
    string.each do |x|


      case x
        when /\d/
          numbers.push(x.to_i)
        when "+", "-", "*", "/", "**"
          operators = numbers.pop(2)
          puts numbers.push(operators[0].send(x, operators[1]))
      end

    end

    a = numbers[0]

  end

end