Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
|
|
@ -0,0 +1,26 @@
|
|||
def rpn_calc (tokens)
|
||||
stack = [] of Float64
|
||||
tokens.each do |token|
|
||||
if token.is_a?(Float64)
|
||||
stack.push token
|
||||
else
|
||||
x = stack.pop
|
||||
y = stack.pop
|
||||
case token
|
||||
when "+" then stack.push(y + x)
|
||||
when "-" then stack.push(y - x)
|
||||
when "*" then stack.push(y * x)
|
||||
when "/" then stack.push(y / x)
|
||||
when "^" then stack.push(y ** x)
|
||||
else raise "unknown operator: #{token}"
|
||||
end
|
||||
end
|
||||
puts "%4s %s" % {token, stack}
|
||||
end
|
||||
end
|
||||
|
||||
def tokenize (s)
|
||||
s.split.map {|t| t.to_f rescue t }
|
||||
end
|
||||
|
||||
rpn_calc tokenize "3 4 2 * 1 5 - 2 3 ^ ^ / +"
|
||||
Loading…
Add table
Add a link
Reference in a new issue