From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Calculator and Operator Precedence
Date: Tue, 19 Feb 2002 15:00:31 -0500
Date: 2002-02-19T15:00:31-05:00 [thread overview]
Message-ID: <u75bd433877688@corp.supernews.com> (raw)
In-Reply-To: 3C727CCF.505D83E0@brighton.ac.uk
"jon" <jkjw@brighton.ac.uk> wrote in message
news:3C727CCF.505D83E0@brighton.ac.uk...
> i ve built a basic calculator which asks user for expression using a
> console.
>
> eg
>
> Enter expression : 1 + 2 + 3 * 4
>
> now i need to make it so it can handle Operator Precendence
You'll need to define a grammar, something like
<exp> ::= <factor> [+ <exp>]
<factor> ::= <term> [* <factor>]
<term> ::= (<exp>) | <digit>
I like to use an Interpreter pattern. As you scan the expression, you can
emit expression objects. To return a value you "evaluate" the ultimate
expression object:
Exp : constant Exp_Access := Parse (Line);
Value : constant Integer := Eval (Exp);
http://www.acm.org/archives/wa.cgi?A2=ind9810&L=patterns&P=R2
http://www.acm.org/archives/patterns.html
If you're worried about memory leaks, you can use "smart"
(reference-counted) pointers or "auto" pointers (a la C++) or "control"
types.
next prev parent reply other threads:[~2002-02-19 20:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-02-19 16:26 Calculator and Operator Precedence jon
2002-02-19 17:24 ` Stephen Leake
2002-02-19 20:00 ` Matthew Heaney [this message]
2002-02-20 8:03 ` Phil Thornley
2002-02-20 15:28 ` Jon
2002-02-20 15:45 ` John English
2002-02-22 16:10 ` Steven Deller
2002-02-22 16:55 ` Jean-Marc Bourguet
2002-02-25 9:17 ` Dmitry A. Kazakov
2002-02-22 20:14 ` Vadim Godunko
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox