Author |
Message |
CyCLoBoT
|
Posted: 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? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Martin
|
Posted: Tue May 13, 2003 4:12 pm Post subject: (No 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 |
|
|
|
|
|
|
Homer_simpson
|
Posted: Tue May 13, 2003 4:29 pm Post subject: (No subject) |
|
|
the math formula solver almost does the same thing(turing submittions) |
|
|
|
|
|
CyCLoBoT
|
Posted: Tue May 13, 2003 4:36 pm Post subject: (No subject) |
|
|
but it doesn't follow the order of operation |
|
|
|
|
|
Homer_simpson
|
Posted: Tue May 13, 2003 4:42 pm Post subject: (No subject) |
|
|
well i'm working on it... |
|
|
|
|
|
CyCLoBoT
|
Posted: Tue May 13, 2003 5:34 pm Post subject: (No 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 ) |
|
|
|
|
|
bugzpodder
|
Posted: Sat May 24, 2003 6:56 pm Post subject: (No 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 |
|
|
|
|
|
bugzpodder
|
Posted: Sat May 24, 2003 6:59 pm Post subject: (No subject) |
|
|
oops you are gonna run into order of operation problems, so find the last +/- first, if none exists, find *// |
|
|
|
|
|
Sponsor Sponsor
|
|
|
CyCLoBoT
|
Posted: Sat May 24, 2003 7:34 pm Post subject: (No 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? |
|
|
|
|
|
bugzpodder
|
Posted: Mon May 26, 2003 7:14 pm Post subject: (No 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
|
|
|
|
|
|
|
|