944,091 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4924
  • C++ RSS
Nov 7th, 2004
0

Object Orientated C+++.net Calculator!

Expand Post »
Hi i have just begun to develop a calculator in C++.net. I have created 10 buttons on the form 0 to 9 and the usual operands + - * / = etc. I want the code to work out the preceidence i.e. * before / etc but cant seem to understand how to do this. The code currently uses a string to concatinate the numbers then when an operand is selected it is converted to an double value and stored as a variable. Can anyone help me please as i am really confused about the matter or preceidence and how to begin to work it out in c++.net i dont know how to get the code to look at each operand in a string of chars.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
teked is offline Offline
3 posts
since Nov 2004
Nov 7th, 2004
0

Re: Object Orientated C+++.net Calculator!

>i.e. * before / etc
Multiplication and division have the same precedence.

>i dont know how to get the code to look at each operand in a string of chars.
You would save yourself a lot of trouble by using a stack to either build a parse tree, or convert the infix notation to postfix. That way you solve two problems: operator precedence, and parenthesis ordering of the expression if any. A search on google will give you plenty of information on converting infix to postfix.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 7th, 2004
0

Re: Object Orientated C+++.net Calculator!

One good way to do this is to produce a 'BNF' for your expressions. Lookup BNF or 'Backus Nauer Form' to get a clear definition, but it is what compiler writiers use to specify the syntax of their language, and from it, you can easily build a parser. This way, the operator precedence just 'falls out' from the description.

For a calculator with just */+- this may be overkill, but heck maybe you'll want to expand the calculator to allow parens () or other stuff too!

Here's a fragment of the C++ BNF dealing with expressions, just to give you an idea; this isn't pure BNF form, but heck it's what's in the C++ help :-) :
C++ Syntax (Toggle Plain Text)
  1. additive-expression:
  2. multiplicative-expression
  3. additive-expression + multiplicative-expression
  4. additive-expression – multiplicative-expression
  5.  
  6. multiplicative-expression:
  7. segment-expression
  8. multiplicative-expression * segment-expression
  9. multiplicative-expression / segment-expression
  10. multiplicative-expression % segment-expression
  11.  
  12. segment-expression:
  13. pm-expression
  14. segment-expression :> pm-expression
  15.  
  16. pm-expression:
  17. cast-expression
  18. pm-expression .* cast-expression
  19. pm-expression –>* cast-expression
  20.  
  21. cast-expression:
  22. unary-expression
  23. ( type-name ) cast-expression
And so on. You can see how you could build routines called, say, 'AdditiveExpression' and have it in turn call other routines and itself recursively to see if the expression is MultiplicativeExpression, or AdditiveExpression followed by a plus or minus followed by a MultiplicativeExpression.

Like I say, this may be overkill for a simple 'four-banger' calculator, but it would allow you to make a much more powerful calculator with parens and exponents and mod and the like.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Mar 10th, 2008
0

Re: Object Orientated C+++.net Calculator!

[QUOTE=teked;65947]Hi i have just begun to develop a calculator in C++.net. I have created 10 buttons on the form 0 to 9 and the usual operands + - * / = etc. I want the code to work out the preceidence i.e. * before / etc but cant seem to understand how to do this. The code currently uses a string to concatinate the numbers then when an operand is selected it is converted to an double value and stored as a variable. Can anyone help me please as i am really confused about the matter or preceidence and how to begin to work it out in c++.net i dont know how to get the code to look at each operand in a string of chars.]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
priyad2 is offline Offline
3 posts
since Mar 2008
Mar 10th, 2008
0

Re: Object Orientated C+++.net Calculator!

[QUOTE=priyad2;556726]
Click to Expand / Collapse  Quote originally posted by teked ...
Hi i have just begun to develop a calculator in C++.net. I have created 10 buttons on the form 0 to 9 and the usual operands + - * / = etc. I want the code to work out the preceidence i.e. * before / etc but cant seem to understand how to do this. The code currently uses a string to concatinate the numbers then when an operand is selected it is converted to an double value and stored as a variable. Can anyone help me please as i am really confused about the matter or preceidence and how to begin to work it out in c++.net i dont know how to get the code to look at each operand in a string of chars.]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
priyad2 is offline Offline
3 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: birthdate- day??
Next Thread in C++ Forum Timeline: character counting program is driving me nuts!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC