Skip to content

Instantly share code, notes, and snippets.

@nakabonne
Created July 17, 2017 08:51
Show Gist options
  • Save nakabonne/a043381bb39f3dfe5eea8e5b49663a9e to your computer and use it in GitHub Desktop.
Save nakabonne/a043381bb39f3dfe5eea8e5b49663a9e to your computer and use it in GitHub Desktop.
逆ポーランド記法をrubyで実装
x = gets.chomp.split(' ')
s = []
x.each do |n|
case n
when '+'
one = s.pop
two = s.pop
s.push(one+two)
when '-'
one = s.pop
two = s.pop
s.push(two-one)
when '*'
one = s.pop
two = s.pop
s.push(one*two)
else
s.push(n.to_i)
end
end
puts s.first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment