home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!haven.umd.edu!decuac!pa.dec.com!sousa.tay.dec.com!keptin.enet.dec.com
- From: granoff@keptin.enet.dec.com (Mark H. Granoff)
- Newsgroups: comp.lang.pascal
- Subject: Re: Help Converting :='x+y' to :=x+y
- Message-ID: <2206@sousa.tay.dec.com>
- Date: 11 Nov 92 17:46:17 GMT
- References: <1dnfelINNp09@charnel.ecst.csuchico.edu>
- Sender: newsa@sousa.tay.dec.com
- Reply-To: granoff@ranger.enet.dec.com
- Organization: Digital Equipment Corporation, Littleton, MA
- Lines: 49
-
-
- In article 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.
- >
- >[Example deleted]
- >
- >.... I'm stuck now
- >to changing the code for each different equation I use.
-
- Yes, it is simple, once you have the correct algorithm.
-
- Basically, what you need to do is convert any equation (based on the standard
- order of precedence rules for such things) from "infix" notation to "postfix"
- notation, sometimes called Reverse Polish notation. The equation can be solved
- then, using the postfix equation.
-
- For example, the infix equation A+B*C is written in postfix as ABC*+. Note,
- however, that a similar equation, (A+B)*C, where the precedence of operations
- is differenct, has a different postfix representation: AB+C*.
-
- The basic algorithm is to scan the original equation once, using a stack and
- applying the proper precendence rules, to produce the postfix equation. Then,
- you can scan the postfix equation once, again using a stack, to evaluate the
- equation.
-
- One thing to consider is if you will support multi-character variable names, or
- only single character variable names. Your decision will effect the complexity
- of your parsing algorithm.
-
- You'll need a way to set variables, e.g. A=1, or B=A+1. (You can use nearly
- the same algorithm as for evaluating equations, since, after all, everything to
- the right of the equal sign is an equation!) With some work, it is also
- relatively simple to define functions, e.g. F(X)=A^2+B^2, so you can then say
- something like F(2) and see the answer.
-
- Good luck.
-
- --
- Mark H. Granoff | Personal Computing Systems Network S/W Development Group
- ---------------------------------------------------------------------------
- Digital Equipment Corporation | Internet: granoff@keptin.enet.dec.com
- 30 Porter Road, LJO2/I4 | Usenet : ...!decwrl!keptin.enet!granoff
- Littleton, MA 01460 | AT&T : +1 508 486 2090
- ---------------------------------------------------------------------------
- Opinions herein are my own and do not necessarily reflect those of Digital.
-
-