exp = '1+(2-3)/(4*5)'
rpn = []
stack = []
operator = {
'(' => 0,
'+' => 1,
'-' => 1,
'*' => 2,
'/' => 2,
}
def isdigit(c)
if c =~ /[0-9]/
return true
end
return false
end
exp.each_char do |e|
if isdigit(e)
rpn << e
next
end
if e.class == String
case e
when ')'
while stack[-1] != '('
rpn << stack.pop
end
stack.pop
next
when '('
stack << e
next
end
if stack != []
while operator[stack[-1]] >= operator[e]
rpn << stack.pop
if stack == []
break
end
end
end
stack << e
end
end
while stack.length != 0
rpn << stack.pop
end
rpn.each do |e|
print "#{e} "
end
print "\n"
ICAgIGV4cCA9ICcxKygyLTMpLyg0KjUpJwogICAgcnBuID0gW10KICAgIHN0YWNrID0gW10KICAgIG9wZXJhdG9yID0gewogICAgJygnID0+IDAsCiAgICAnKycgPT4gMSwKICAgICctJyA9PiAxLAogICAgJyonID0+IDIsCiAgICAnLycgPT4gMiwKICAgIH0KICAgICAKICAgIGRlZiBpc2RpZ2l0KGMpCiAgICBpZiBjID1+IC9bMC05XS8KICAgIHJldHVybiB0cnVlCiAgICBlbmQKICAgIHJldHVybiBmYWxzZQogICAgZW5kCiAgICAgCiAgICBleHAuZWFjaF9jaGFyIGRvIHxlfAogICAgaWYgaXNkaWdpdChlKQogICAgcnBuIDw8IGUKICAgIG5leHQKICAgIGVuZAogICAgaWYgZS5jbGFzcyA9PSBTdHJpbmcKICAgIGNhc2UgZQogICAgd2hlbiAnKScKICAgIHdoaWxlIHN0YWNrWy0xXSAhPSAnKCcKICAgIHJwbiA8PCBzdGFjay5wb3AKICAgIGVuZAogICAgc3RhY2sucG9wCiAgICBuZXh0CiAgICB3aGVuICcoJwogICAgc3RhY2sgPDwgZQogICAgbmV4dAogICAgZW5kCiAgICBpZiBzdGFjayAhPSBbXQogICAgd2hpbGUgb3BlcmF0b3Jbc3RhY2tbLTFdXSA+PSBvcGVyYXRvcltlXQogICAgcnBuIDw8IHN0YWNrLnBvcAogICAgaWYgc3RhY2sgPT0gW10KICAgIGJyZWFrCiAgICBlbmQKICAgIGVuZAogICAgZW5kCiAgICBzdGFjayA8PCBlCiAgICBlbmQKICAgIGVuZAogICAgIAogICAgd2hpbGUgc3RhY2subGVuZ3RoICE9IDAKICAgIHJwbiA8PCBzdGFjay5wb3AKICAgIGVuZAogICAgIAogICAgcnBuLmVhY2ggZG8gfGV8CiAgICBwcmludCAiI3tlfSAiCiAgICBlbmQKICAgIHByaW50ICJcbiI=