From: "Михаил Горбунов" Date: 2007-12-03T21:13:28+09:00 Subject: Postfix to Infix (#148) ------=_Part_14526_5119337.1196684011257 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Sorry, in previous post I forgot about parenthess in case of - and /. # this program understands simple operations and also sin, cos, tg and ctg # to use, provide string in RPN as argument # example: ruby postf2inf '3 5 + sin 5 3 cos + /' # result in "sin(3 + 5) / (5 + cos(3))" str_postf=ARGV[0] str_inf=[] prior = {'+'=>1,'-'=>1,'*'=>3,'/'=>3} stack = str_postf.split(' ') 0.upto stack.size-1 do |l| if ['+','-','*','/'].include?(stack[l]) arg1 = str_inf.pop arg2 = str_inf.pop arg1[1]='('+arg1[1]+')' if arg1[0]