-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculator.rb
78 lines (66 loc) · 1.34 KB
/
calculator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Simple RPN Calculator
class CalculatorEngine
attr_accessor :memory
def initialize
@memory = []
end
def clear_memory
self.memory = []
end
def push_into_memory(v)
memory.push(v)
end
def pull_from_memory
memory.pop
end
def last_entry_in_memory
memory.last
end
def print_memory
memory.each_with_index do |memory, index|
print_message "\t#{index}: #{memory}"
end
end
def print_message(message)
puts message
end
def get_input
print "> "
input = gets
input.chomp
end
def add_operation
if memory.length >= 2
op1 = pull_from_memory
op2 = pull_from_memory
push_into_memory(op1 + op2)
print_message "= #{last_entry_in_memory}"
else
print_message "Error: Not Enough Operands"
end
end
def run
while true do
input = get_input
case input
when 'q'
exit
#when '0','1','2','3','4','5','6','7','8','9'
when /\d/
push_into_memory input.to_f
print_message last_entry_in_memory
when 'c'
clear_memory
print_message "Memory Cleared"
when 'm'
print_memory
when '+'
add_operation
else
print_message "Error: Unsupported Operator: #{input}" unless input.empty?
end
end
end
end
engine = CalculatorEngine.new
engine.run