# Expression evaluation in C++

In this tutorial, we are going to learn the evaluation of an expression in C++. An expression can have operands and operators. In C++, the order of operands and operators is very important. Let’s see how we can evaluate an expression with some examples.

## Expression evaluation in C++ with examples

When we are evaluating an expression, we first find the operator with the highest precedence. We calculate the part consisting of that operator and do the same for the operator with the second-highest precedence and so on. When we encounter two or more than two operators with the same precedence, we look for their associativity. If it is left to right, we first calculate for the operator on the left and move to its right and do the same for the rest of the operators. For right to left associativity, we first calculate for the operator on the right and then move to its left and do the same for the rest of the operators.

If you don’t know about operator precedence and associativity, spend some of your time here: Operators in C++

Let’s understand expression evaluation in C++ with a few examples.

#### Example 1: How to evaluate an expression in C++

Expression: 23-56/8*6+34%2

Evaluation: In the above expression, there are five operators:-(+, -, /, *, %).

Precedence of (*, /, %) greater than (+, -). Since precedence of (*, /, %) is the same, we check for their associativity which is left to right. Therefore, we calculate for the (/) which gives the result: 23-7*6+34%2.

Then we calculate for (*) which gives the result: 23-42+34%2.

Then we calculate for (%) which gives the result: 23-42+0.

Now since (+) and (-) have the same precedence and their associativity is from left to right, we first calculate for (-) which gives the result: -19+0.

Then we calculate for (+) which gives the result: -19

The final answer is -19.

#### Example 2: evaluate an expression in C++

Expression: 45+3*9-57%13/++a (where a is a variable with value 5)

Evaluation: In the above expression, there are six operators:-(+, -, *, /, %, ++(prefix))

Precedence of the pre-increment operator is the highest. Therefore it will be calculated first and the result would be: 45+3*9-57%13/6.

Then we calculate for (*) which gives the result: 45+27-57%13/6.

Then we calculate for (%) which gives the result: 45+27-5/6.

Then we calculate for (/) which gives the result: 45+27-0.

Then we calculate for (+) which gives the result: 72-0.

Then we calculate for (-) which gives the result: 72.

The final answer is 72.

#### Example C++ program

See the example program to understand the concept better.

#include <iostream> using namespace std; int main() { int result1,result2; //expression 1 result1=23-56/8*6+34%2; int a=5; //expression 2 result2=45+3*9-57%13/++a; cout<<"Result1 is "<<result1<<endl; cout<<"Result2 is "<<result2<<endl; return 0; }

The output of the above program will be:

Result1 is -19 Result2 is 72

Thank you.

How to find this expression?

{5+(3-9)*8}

This Arithmatic expression contain group symbols. Please tell how I solve it. Thanks

please solve this

“Evalauate an expression from string ‘1098+47’ or ‘3*6’ or ‘150/3’

I use the GizmoBase c++ framework

”’char bnf[]=” \

signed_integer ::= ‘+’? integer | ‘-‘ integer fNeg ; \

group ::= ‘(‘ expr ‘)’; \

expr1 ::= signed_integer | group; \

expr2 ::= expr1 ((‘*’ expr1 fMul) | (‘/’ expr1 fDiv))*; \

expr ::= expr2 ((‘+’ expr2 fAdd) | (‘-‘ expr2 fSub))*; \

“;

class gzGenericParser : public gzParserFunction , public gzReference

{

public:

gzGenericParser()

{

registerFunction(“integer”,&gzGenericParser::parseInteger);

registerFunction(“fMul”,&gzGenericParser::fMul);

registerFunction(“fDiv”,&gzGenericParser::fDiv);

registerFunction(“fAdd”,&gzGenericParser::fAdd);

registerFunction(“fSub”,&gzGenericParser::fSub);

registerFunction(“fNeg”,&gzGenericParser::fNeg);

gzString bnfURL;

bnfURL.format(“mem:%d,%d”,bnf,sizeof(bnf)-1);

registerRulesFromURL(bnfURL);

}

virtual ~gzGenericParser()

{

}

gzParseResult fMul()

{

//gzTrace::TraceMessage(“Mul:%ld\n”,getItemID());

m_stack.push(m_stack.pop()*m_stack.pop());

return GZ_PARSE_OK;

}

gzParseResult fSub()

{

//gzTrace::TraceMessage(“Sub:%ld\n”,getItemID());

m_stack.push(-(m_stack.pop()-m_stack.pop()));

return GZ_PARSE_OK;

}

gzParseResult fDiv()

{

//gzTrace::TraceMessage(“Div:%ld\n”,getItemID());

m_stack.push(1/(m_stack.pop()/m_stack.pop()));

return GZ_PARSE_OK;

}

gzParseResult fAdd()

{

//gzTrace::TraceMessage(“Add:%ld\n”,getItemID());

m_stack.push(m_stack.pop()+m_stack.pop());

return GZ_PARSE_OK;

}

gzParseResult fNeg()

{

//gzTrace::TraceMessage(“Neg:%ld\n”,getItemID());

m_stack.push(-m_stack.pop());

return GZ_PARSE_OK;

}

gzParseResult parseInteger()

{

gzUInt32 tokenPos=getTokenPos();

gzParseResult result=gzBasicParser::parseInteger();

if(result==GZ_PARSE_OK)

{

gzString number=getTokenData(tokenPos,getTokenPos()-tokenPos);

//gzTrace::TraceMessage(“Num:%ld:%ld\n”,(gzUInt32)number.num(),getItemID());

m_stack.push((gzDouble)number.num());

}

return result;

}

gzQueue m_stack;

};

GZ_DECLARE_REFPTR(gzGenericParser);

GZ_DECLARE_RECYCLE_DATA_IMP;

int main()

{

gzMessage::setMessageLevel(GZ_MESSAGE_MEM_DEBUG|GZ_MESSAGE_API_INTERNAL);

gzString expression=”(2-3*5)/6+3*4″;

for (gzUInt32 i = 0; i setAdapter(adapter);

//puff->setEnableTrace(TRUE);

//puff->setEnablePerfMon(TRUE);

gzParseResult result = puff->parseRule(“expr”);

if(puff->hasError())

GZMESSAGE(GZ_MESSAGE_DEBUG, “Error in parser ‘%s'”, puff->getError());

gzDouble val = puff->m_stack.pop();

}

gzDumpPerformanceInfo();

gzCleanupReference::cleanUpReferences();

return 0;

}

”’