Guest User

Untitled

a guest
Oct 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. class RPN
  2.  
  3. attr_accessor :calculator
  4.  
  5. def initialize
  6. @calculator = []
  7. end
  8.  
  9.  
  10. def push(arg)
  11. @calculator << arg
  12. end
  13.  
  14. def value
  15. ops = @calculator.last
  16. num1 = @calculator[-3]
  17. num2 = @calculator[-2]
  18. result = choose_method(num1, num2, ops)
  19. end
  20.  
  21. def choose_method(num1,num2, ops)
  22. if ops == "+"
  23. plus(num1, num2)
  24. elsif ops == "-"
  25. minus(num1, num2)
  26. elsif ops == "*"
  27. times(num1, num2)
  28. elsif ops == "/"
  29. divide(num1, num2)
  30. end
  31. end
  32.  
  33. def plus(num1,num2)
  34. num1 + num2
  35. end
  36.  
  37. # def minus(num1,num2)
  38. # num1 - num2
  39. # end
  40.  
  41. def minus
  42. result = 0
  43. @calculator.each do |i|
  44. result -= i
  45. end
  46. result
  47. end
  48.  
  49. def times(num1,num2)
  50. num1 * num2
  51. end
  52.  
  53. def divide(num1,num2)
  54. num1.to_f / num2
  55. end
  56.  
  57. end
  58.  
  59. happytesting = RPN.new
  60. happytesting.push(2)
  61. happytesting.push(3)
  62. happytesting.push("*")
  63. puts happytesting.value
  64. # puts happytesting.calculator[-2]
Add Comment
Please, Sign In to add comment