s = gets.chomp.gsub(/(.*?)([*+])(.+?)/){$2=='*'?"#$1+#$3)":"#$1*#$3)"} s = '('*s.count(')') + s p eval s