home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / pascal / 6460 < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.4 KB  |  56 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!utcsri!torn!newshost.uwo.ca!uwovax.uwo.ca!4224_5276
  3. From: 4224_5276@uwovax.uwo.ca (Chris Walden)
  4. Subject: Re: Help Converting :='x+y' to :=x+y
  5. Organization: University of Western Ont, London
  6. Date: Tue, 10 Nov 1992 14:47:47 GMT
  7. Message-ID: <1992Nov10.104747.1@uwovax.uwo.ca>
  8. References: <1dnfelINNp09@charnel.ecst.csuchico.edu>
  9. Sender: news@julian.uwo.ca (USENET News System)
  10. Nntp-Posting-Host: hydra.uwo.ca
  11. Lines: 43
  12.  
  13. In article <1dnfelINNp09@charnel.ecst.csuchico.edu>, psmedsha@ecst.csuchico.edu (Paul Smedshammer) writes:
  14. > I don't know if this is a simple thing to do but I have not
  15. > been able to figure it out.  I would like to take a string 
  16. > of characters that represent an equation and implement that
  17. > equation.  For example:
  18. >      Var  Equation : String[20];
  19. >           x, y, Solution : Real;
  20. >      Begin
  21. >           Equation := 'x + y';
  22. >           x := 1;
  23. >           y := 2;
  24. >              {'now right here I would like to be able
  25. >              to use the equation in the string Equation
  26. >              like this:
  27. >           Solution := Somefunction(Equation);
  28. >              Is this easily done?'}
  29. >           Writeln('The solution is:',Equation,'=',Solution);
  30. >      End.
  31. > I would like the user to be able to input an equation and
  32. > then the program will perform different mathematics on that
  33. > equation.  But to be able to do the mathematics I need to 
  34. > get the input equation into the form " Solution := x + y".
  35. > I would appreciate any help I can get on this.  I'm stuck now
  36. > to changing the code for each different equation I use.  Thanks
  37. > for your time.
  38.  
  39. This is my humble suggestion in how to implement it... have a function like:
  40.  
  41. SolveEqn(Equation:string; Vbles:VbleRec):real   ... where Equation is the
  42. string equation and Vbles (type Vblerec) is a record of the values of the
  43. variables.  You would have to limit variable to a set number (ie a..z or
  44. something).  Then recursively call the function.  You will have to check for
  45. ()'s (if you allow them) and each operation (*-+/^..??) that you allow.
  46.  
  47. The two different ways I can suggest, would be to a) parse left to right,
  48. recursing for "higher" order of operations, or b) scan for order of operations,
  49. start at the highest, and actually simplify the string equation as you go.
  50.  
  51. Good luck... write me if you need any more suggestions!
  52.  
  53. Chris Walden  4224_5276@uwovax.uwo.ca
  54.