home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / pascal / 6498 < prev    next >
Encoding:
Internet Message Format  |  1992-11-11  |  2.7 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!haven.umd.edu!decuac!pa.dec.com!sousa.tay.dec.com!keptin.enet.dec.com
  2. From: granoff@keptin.enet.dec.com (Mark H. Granoff)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: Help Converting :='x+y' to :=x+y
  5. Message-ID: <2206@sousa.tay.dec.com>
  6. Date: 11 Nov 92 17:46:17 GMT
  7. References: <1dnfelINNp09@charnel.ecst.csuchico.edu>
  8. Sender: newsa@sousa.tay.dec.com
  9. Reply-To: granoff@ranger.enet.dec.com
  10. Organization: Digital Equipment Corporation, Littleton, MA
  11. Lines: 49
  12.  
  13.  
  14. In article psmedsha@ecst.csuchico.edu (Paul Smedshammer) writes:
  15. >I don't know if this is a simple thing to do but I have not
  16. >been able to figure it out.  I would like to take a string 
  17. >of characters that represent an equation and implement that
  18. >equation.  
  19. >
  20. >[Example deleted]
  21. >
  22. >....  I'm stuck now
  23. >to changing the code for each different equation I use. 
  24.  
  25. Yes, it is simple, once you have the correct algorithm.
  26.  
  27. Basically, what you need to do is convert any equation (based on the standard
  28. order of precedence rules for such things) from "infix" notation to "postfix"
  29. notation, sometimes called Reverse Polish notation.  The equation can be solved
  30. then, using the postfix equation.
  31.  
  32. For example, the infix equation A+B*C is written in postfix as ABC*+.  Note,
  33. however, that a similar equation, (A+B)*C, where the precedence of operations
  34. is differenct, has a different postfix representation: AB+C*.
  35.  
  36. The basic algorithm is to scan the original equation once, using a stack and
  37. applying the proper precendence rules, to produce the postfix equation.  Then,
  38. you can scan the postfix equation once, again using a stack, to evaluate the
  39. equation.
  40.  
  41. One thing to consider is if you will support multi-character variable names, or
  42. only single character variable names.  Your decision will effect the complexity
  43. of your parsing algorithm.
  44.  
  45. You'll need a way to set variables, e.g. A=1, or B=A+1.  (You can use nearly
  46. the same algorithm as for evaluating equations, since, after all, everything to
  47. the right of the equal sign is an equation!)  With some work, it is also
  48. relatively simple to define functions, e.g. F(X)=A^2+B^2, so you can then say
  49. something like F(2) and see the answer.
  50.  
  51. Good luck.
  52.  
  53. --
  54. Mark H. Granoff | Personal Computing Systems Network S/W Development Group  
  55. ---------------------------------------------------------------------------
  56. Digital Equipment Corporation | Internet: granoff@keptin.enet.dec.com
  57. 30 Porter Road, LJO2/I4       | Usenet  : ...!decwrl!keptin.enet!granoff
  58. Littleton, MA 01460           | AT&T    : +1 508 486 2090
  59. ---------------------------------------------------------------------------
  60. Opinions herein are my own and do not necessarily reflect those of Digital.
  61.  
  62.