home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!utcsri!torn!newshost.uwo.ca!uwovax.uwo.ca!4224_5276
- From: 4224_5276@uwovax.uwo.ca (Chris Walden)
- Subject: Re: Help Converting :='x+y' to :=x+y
- Organization: University of Western Ont, London
- Date: Tue, 10 Nov 1992 14:47:47 GMT
- Message-ID: <1992Nov10.104747.1@uwovax.uwo.ca>
- References: <1dnfelINNp09@charnel.ecst.csuchico.edu>
- Sender: news@julian.uwo.ca (USENET News System)
- Nntp-Posting-Host: hydra.uwo.ca
- Lines: 43
-
- 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.
- >
- > I would like the user to be able to input an equation and
- > then the program will perform different mathematics on that
- > equation. But to be able to do the mathematics I need to
- > get the input equation into the form " Solution := x + y".
- > I would appreciate any help I can get on this. I'm stuck now
- > to changing the code for each different equation I use. Thanks
- > for your time.
-
- This is my humble suggestion in how to implement it... have a function like:
-
- SolveEqn(Equation:string; Vbles:VbleRec):real ... where Equation is the
- string equation and Vbles (type Vblerec) is a record of the values of the
- variables. You would have to limit variable to a set number (ie a..z or
- something). Then recursively call the function. You will have to check for
- ()'s (if you allow them) and each operation (*-+/^..??) that you allow.
-
- The two different ways I can suggest, would be to a) parse left to right,
- recursing for "higher" order of operations, or b) scan for order of operations,
- start at the highest, and actually simplify the string equation as you go.
-
- Good luck... write me if you need any more suggestions!
-
- Chris Walden 4224_5276@uwovax.uwo.ca
-