| | |
Object Orientated C+++.net Calculator!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 3
Reputation:
Solved Threads: 0
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.
>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.
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.
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 :-) :
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.
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)
additive-expression: multiplicative-expression additive-expression + multiplicative-expression additive-expression – multiplicative-expression multiplicative-expression: segment-expression multiplicative-expression * segment-expression multiplicative-expression / segment-expression multiplicative-expression % segment-expression segment-expression: pm-expression segment-expression :> pm-expression pm-expression: cast-expression pm-expression .* cast-expression pm-expression –>* cast-expression cast-expression: unary-expression ( type-name ) cast-expression
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.
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
[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.]
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
[QUOTE=priyad2;556726]
•
•
•
•
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
- VB.NET Calculator (VB.NET)
- Assignment - Object Orientated Analysis (OOA) (C++)
- What's diffrence between VB and VB.Net ? (VB.NET)
- .NET vs VB6 - differences? (VB.NET)
- Difficulty understanding Object Orientated File Streaming (C++)
Other Threads in the C++ Forum
- Previous Thread: birthdate- day??
- Next Thread: character counting program is driving me nuts!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






