
-----------------------------------
CyCLoBoT
Tue May 13, 2003 4:06 pm

Help with Arrangements of Operators
-----------------------------------
When the user enters in an expression like a+b-c, how can I code it so that the output would be -+abc (its a prefix notation). Anyone know how this can be done?

-----------------------------------
Martin
Tue May 13, 2003 4:12 pm


-----------------------------------
I think this is what you mean

var commands : string := "+-/*"

var com, opp : string := ""

var equation : string
get equation
for i:1..length (equation)
  if index (commands, equation(i)) > 0 then
    opp += equation (i)
  else
    com += equation (i)
  end if
end for

put opp, com

-----------------------------------
Homer_simpson
Tue May 13, 2003 4:29 pm


-----------------------------------
the math formula solver almost does the same thing(turing submittions)

-----------------------------------
CyCLoBoT
Tue May 13, 2003 4:36 pm


-----------------------------------
but it doesn't follow the order of operation  :(

-----------------------------------
Homer_simpson
Tue May 13, 2003 4:42 pm


-----------------------------------
well i'm working on it...

-----------------------------------
CyCLoBoT
Tue May 13, 2003 5:34 pm


-----------------------------------
what if the expression contains brackets like (a+b)-(c+d). How do I output the prefix notation. for the example the prefix should be -+ab+cd(I think :?: )

-----------------------------------
bugzpodder
Sat May 24, 2003 6:56 pm


-----------------------------------
that site wont do you any good anywayz, so i am not going to post it here.  anwayz, 

(a+b)-(c+d)

if it is a simple expression (such as "a", "b", "x") return it
if it has bracket as the first and last character, remove it and proceed
have the function check for the *last* operator out side of the brackets, that would be the minus in the middle.  then you return the sign plus  f(LHS) plus f(RHS) where LHS is the part before the  operator, and RHS would the the part after the operator.  for example:
in quotations are the string returned, square brackets is the string parameter
f[(a+b)-(c+d)]
"-"+f[(a+b)]+f[(c+d)]
"-"+f[a+b]+f[c+d]
"-"+"+"+f[a]+f[b]+"+"+f[c]+f[d]
"-+ab+cd"
which is what you want, hopefully

-----------------------------------
bugzpodder
Sat May 24, 2003 6:59 pm


-----------------------------------
oops you are gonna run into order of operation problems, so find the last +/- first, if none exists, find *//

-----------------------------------
CyCLoBoT
Sat May 24, 2003 7:34 pm


-----------------------------------
bugzpodder I understand what u are trying to say but I am having difficulty coding it though. Would u mind telling me how I can start this with the coding?

-----------------------------------
bugzpodder
Mon May 26, 2003 7:14 pm


-----------------------------------
i'll get you started.  i'll break my no-turing rule for once:


fcn convert(infix:string):string
  if length(infix)=1 then
     result infix
  elsif infix(1)="(" and infix(*)=")" then
     result infix(2..*-1)
  end if
 var cnt:=0
  for decreasing i:length(infix)..1   
     if cnt=0 and (infix(i)="+" or infix(i)="-") then
           result infix(i)+convert(infix(1..i-1))+convert(infix(i+1..*))
     elsif infix(i)=")" then
           cnt+=1
     elsif infix(i)="(" then
           cnt-=1
     end if
   end for
    
     %handle the rest


end convert

