Object Orientated C+++.net Calculator!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 3
Reputation: teked is an unknown quantity at this point 
Solved Threads: 0
teked teked is offline Offline
Newbie Poster

Object Orientated C+++.net Calculator!

 
0
  #1
Nov 7th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Nov 7th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

 
0
  #3
Nov 7th, 2004
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 :-) :
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: priyad2 is an unknown quantity at this point 
Solved Threads: 0
priyad2 priyad2 is offline Offline
Newbie Poster

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

 
0
  #4
Mar 10th, 2008
[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.]
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: priyad2 is an unknown quantity at this point 
Solved Threads: 0
priyad2 priyad2 is offline Offline
Newbie Poster

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

 
0
  #5
Mar 10th, 2008
[QUOTE=priyad2;556726]
Originally Posted by teked View 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.]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC