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

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!ukma!darwin.sura.net!udel!sbcs.sunysb.edu!csws2.ic.sunysb.edu!clane
  3. From: clane@csws2.ic.sunysb.edu (Charles F Lane)
  4. Subject: Re: Help Converting : ='x+y' to :=x+y
  5. Message-ID: <1992Nov13.003852.26113@sbcs.sunysb.edu>
  6. Sender: usenet@sbcs.sunysb.edu (Usenet poster)
  7. Nntp-Posting-Host: csws2.ic.sunysb.edu
  8. Organization: State University of New York at Stony Brook
  9. References: <1dnfelINNp09@charnel.ecst.csuchico.edu>
  10. Date: Fri, 13 Nov 1992 00:38:52 GMT
  11. Lines: 56
  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. >
  19. >     Var  Equation : String[20];
  20. >          x, y, Solution : Real;
  21. >     Begin
  22. >          Equation := 'x + y';
  23. >          x := 1;
  24. >          y := 2;
  25. >             {'now right here I would like to be able
  26. >             to use the equation in the string Equation
  27. >             like this:
  28. >          Solution := Somefunction(Equation);
  29. >             Is this easily done?'}
  30. >          Writeln('The solution is:',Equation,'=',Solution);
  31. >     End.
  32.  
  33. Just use a case statement after detecting the operator (+,-,*,/)
  34. in the string:
  35.  
  36.   var Operator : char;  {Add this to your other vars}
  37.  
  38.   for i := 1 to ord(Equation[0]) do
  39.    if Equation[i] in ['+','-','*','/']
  40.    then Operator := Equation[i]
  41.    else Operator := 'Z';
  42.  
  43.   case Operator of
  44.    '+' : Solution := x+y;
  45.    '-' : Solution := x-y;
  46.    '*' : Solution := x*y;
  47.    '/' : Solution := x/y;
  48.    else {print some error message that there's no operator in the string}; 
  49.   end;
  50.  
  51. You may want to make your operator detection algorithm a bit more robust
  52. than the above, but I kept it simple for unerstandability.  I'm a bit 
  53. confused about your assignments to x and y.  You'll need to detect the two
  54. numbers in the string, as well -- unless the numbers are already known and
  55. all you want the user to do is indicate an equation to operate on the numbers.
  56.  
  57. [stuff deleted]
  58. >------------------------------------------------------------------------------
  59. >psmedsha@ecst.csuchico.edu (Paul Smedshammer)
  60. >
  61. >Graduate Student in Mechanical Engineering
  62. >California State University at Chico
  63. >------------------------------------------------------------------------------
  64.  
  65. -- 
  66.                           | "So who is this Al Gorithm guy they keep
  67. Charles F. Lane           |  mentioning in my Computer Science classes?"
  68. clane@Libws1.ic.sunysb.edu|
  69.