Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RPN
- attr_accessor :calculator
- def initialize
- @calculator = []
- end
- def push(arg)
- @calculator << arg
- end
- def value
- ops = @calculator.last
- num1 = @calculator[-3]
- num2 = @calculator[-2]
- result = choose_method(num1, num2, ops)
- end
- def choose_method(num1,num2, ops)
- if ops == "+"
- plus(num1, num2)
- elsif ops == "-"
- minus(num1, num2)
- elsif ops == "*"
- times(num1, num2)
- elsif ops == "/"
- divide(num1, num2)
- end
- end
- def plus(num1,num2)
- num1 + num2
- end
- # def minus(num1,num2)
- # num1 - num2
- # end
- def minus
- result = 0
- @calculator.each do |i|
- result -= i
- end
- result
- end
- def times(num1,num2)
- num1 * num2
- end
- def divide(num1,num2)
- num1.to_f / num2
- end
- end
- happytesting = RPN.new
- happytesting.push(2)
- happytesting.push(3)
- happytesting.push("*")
- puts happytesting.value
- # puts happytesting.calculator[-2]
Add Comment
Please, Sign In to add comment