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
|
# File 'lib/rpn_ruby.rb', line 13
def enter value
case value
when '='
return result
when '+'
@stack.push @stack.pop + @stack.pop
when '-'
subtrahend = @stack.pop
@stack.push @stack.pop - subtrahend
when '*'
@stack.push @stack.pop * @stack.pop
when '^'
exponent = @stack.pop
@stack.push @stack.pop ** exponent
when 'r'
rt = @stack.pop
@stack.push @stack.pop.root rt
when '/', 'd'
divisor = @stack.pop
@stack.push @stack.pop / divisor
when '%'
divisor = @stack.pop
@stack.push @stack.pop % divisor
else
@stack.push value.to_f
end
self
rescue Exception => @err
@stack.push 'invalid input'
return self
end
|