-
Notifications
You must be signed in to change notification settings - Fork 30
/
calculator_II.rb
56 lines (53 loc) · 1.16 KB
/
calculator_II.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
def calculate(s)
stack, m_stack= [], []
s.scan(/\w+|\+|\-|\*|\//) do |token|
if token == '*' || token == '/'
m_stack << stack.pop if m_stack.empty?
m_stack << token.to_sym
elsif token == '+' || token == '-'
stack << m_stack.pop if m_stack.any?
stack << token.to_sym
elsif m_stack.any?
op = m_stack.pop
m_stack[-1] = [m_stack[-1], token.to_i].inject(op)
else
stack << token.to_i
end
end
stack << m_stack.pop if m_stack.any?
compute(stack)
end
def compute(stack)
result, op = 0, :+
stack.each {|token| (token == :+ || token == :-) ? op = token : result = [result, token].inject(op)}
result
end
# Q-227: two phase solution
# 24/dec/2022
def calculate(s)
stack = []
op = nil
s.scan(/\w+|\+|-|\*|\//).each do |t|
if t =~ /\d/
if op == '*' || op == '/'
op = stack.pop
stack << [stack.pop, t.to_i].inject(op.to_sym)
else
stack << t.to_i
end
else
stack << t
op = t
end
end
result = 0
op = "+"
stack.each do |t|
if t == '+' || t == '-'
op = t
else
result = [result, t].inject(op.to_sym)
end
end
result
end