forked from sacruby/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator-ORIGINAL.rb
69 lines (55 loc) · 1.33 KB
/
calculator-ORIGINAL.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
#
# Simple RPN Calculator
#
class CalculatorEngine
def initialize
@memory = []
end
def run
done = false
while not done do
print "> "
input = gets
if input
input = input.chomp
if input == 'q'
done = true
else
# operators will work on the stack, numbers will be added to stack
case input
when '0','1','2','3','4','5','6','7','8','9'
# ['0','1'].include?, %w(0 1).include?
@memory.push(input.to_f)
puts @memory.last
when 'c'
@memory = []
puts "Memory Cleared"
when 'm'
puts "Memory:"
position = 0
@memory.each do |m|
puts "\t#{position}: #{m}"
position = position + 1
end
when '+'
if @memory.length >= 2
op1 = @memory.pop
op2 = @memory.pop
@memory.push(op1 + op2)
puts "= #{@memory.last}"
else
puts "Error: Not Enough Operands"
end
else
puts "Error: Unsupported Operator: #{input}" unless input.empty?
end
end
else
done = true
end
end
end
end
engine = CalculatorEngine.new
engine.run
# http://ruby-doc.org/