From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d5ed2292349acc76 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-02-19 11:55:21 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.icl.net!opentransit.net!fr.clara.net!heighliner.fr.clara.net!freenix!sn-xit-01!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada Subject: Re: Calculator and Operator Precedence Date: Tue, 19 Feb 2002 15:00:31 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <3C727CCF.505D83E0@brighton.ac.uk> X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 X-Complaints-To: newsabuse@supernews.com Xref: archiver1.google.com comp.lang.ada:20133 Date: 2002-02-19T15:00:31-05:00 List-Id: "jon" 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 ::= [+ ] ::= [* ] ::= () | 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.