Created
July 17, 2017 08:51
-
-
Save nakabonne/a043381bb39f3dfe5eea8e5b49663a9e to your computer and use it in GitHub Desktop.
逆ポーランド記法をrubyで実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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