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.

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 :-) :

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

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.

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.]

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.]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.