Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Help with Arrangements of Operators
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?

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 *//

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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 10 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: