Computer Science Canada

Help with Arrangements of Operators

Author:  CyCLoBoT [ Tue May 13, 2003 4:06 pm ]
Post subject:  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?

Author:  Martin [ Tue May 13, 2003 4:12 pm ]
Post subject: 

I think this is what you mean
code:

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

Author:  Homer_simpson [ Tue May 13, 2003 4:29 pm ]
Post subject: 

the math formula solver almost does the same thing(turing submittions)

Author:  CyCLoBoT [ Tue May 13, 2003 4:36 pm ]
Post subject: 

but it doesn't follow the order of operation Sad

Author:  Homer_simpson [ Tue May 13, 2003 4:42 pm ]
Post subject: 

well i'm working on it...

Author:  CyCLoBoT [ Tue May 13, 2003 5:34 pm ]
Post subject: 

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 Question )

Author:  bugzpodder [ Sat May 24, 2003 6:56 pm ]
Post subject: 

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

Author:  bugzpodder [ Sat May 24, 2003 6:59 pm ]
Post subject: 

oops you are gonna run into order of operation problems, so find the last +/- first, if none exists, find *//

Author:  CyCLoBoT [ Sat May 24, 2003 7:34 pm ]
Post subject: 

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?

Author:  bugzpodder [ Mon May 26, 2003 7:14 pm ]
Post subject: 

i'll get you started. i'll break my no-turing rule for once:
code:


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


: