home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!ukma!darwin.sura.net!udel!sbcs.sunysb.edu!csws2.ic.sunysb.edu!clane
- From: clane@csws2.ic.sunysb.edu (Charles F Lane)
- Subject: Re: Help Converting : ='x+y' to :=x+y
- Message-ID: <1992Nov13.003852.26113@sbcs.sunysb.edu>
- Sender: usenet@sbcs.sunysb.edu (Usenet poster)
- Nntp-Posting-Host: csws2.ic.sunysb.edu
- Organization: State University of New York at Stony Brook
- References: <1dnfelINNp09@charnel.ecst.csuchico.edu>
- Date: Fri, 13 Nov 1992 00:38:52 GMT
- Lines: 56
-
- In article <1dnfelINNp09@charnel.ecst.csuchico.edu> psmedsha@ecst.csuchico.edu (Paul Smedshammer) writes:
- >I don't know if this is a simple thing to do but I have not
- >been able to figure it out. I would like to take a string
- >of characters that represent an equation and implement that
- >equation. For example:
- >
- > Var Equation : String[20];
- > x, y, Solution : Real;
- > Begin
- > Equation := 'x + y';
- > x := 1;
- > y := 2;
- > {'now right here I would like to be able
- > to use the equation in the string Equation
- > like this:
- > Solution := Somefunction(Equation);
- > Is this easily done?'}
- > Writeln('The solution is:',Equation,'=',Solution);
- > End.
-
- Just use a case statement after detecting the operator (+,-,*,/)
- in the string:
-
- var Operator : char; {Add this to your other vars}
-
- for i := 1 to ord(Equation[0]) do
- if Equation[i] in ['+','-','*','/']
- then Operator := Equation[i]
- else Operator := 'Z';
-
- case Operator of
- '+' : Solution := x+y;
- '-' : Solution := x-y;
- '*' : Solution := x*y;
- '/' : Solution := x/y;
- else {print some error message that there's no operator in the string};
- end;
-
- You may want to make your operator detection algorithm a bit more robust
- than the above, but I kept it simple for unerstandability. I'm a bit
- confused about your assignments to x and y. You'll need to detect the two
- numbers in the string, as well -- unless the numbers are already known and
- all you want the user to do is indicate an equation to operate on the numbers.
-
- [stuff deleted]
- >------------------------------------------------------------------------------
- >psmedsha@ecst.csuchico.edu (Paul Smedshammer)
- >
- >Graduate Student in Mechanical Engineering
- >California State University at Chico
- >------------------------------------------------------------------------------
-
- --
- | "So who is this Al Gorithm guy they keep
- Charles F. Lane | mentioning in my Computer Science classes?"
- clane@Libws1.ic.sunysb.edu|
-