You are given a string that represent an expression of digits and operands. E.g. 1+2*3, 1-2+4. You need to evaluate the string or the expression. NO BODMAS is followed. If the expression is of incorrect syntax return -1.
Test cases:
a) 1+2*3 will be evaluated to 9.
b) 4-2+6*3 will be evaluated to 24.
c) 1++2 will be evaluated to -1(INVALID).
Also, in the string spaces can occur. For that case we need to ignore the spaces. Like :- 1*2 -1 is equals to 1.

The idea is simple start from the first character and traverse from left to right and check for errors like two consecutive operators and operands. We also keep track of result and update the result while traversing the expression.

Following is C++ program to evaluate the given expression.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20evaluate%20a%20given%20expression%0A%23include%20%3Cioexpeam%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20if%20a%20given%20character%20is%20operand%0Abool%20isOperand(char%20c)%20%7B%20return%20(c%20%3E%3D%20’0’%20%26%26%20c%20%3C%3D%20’9′)%3B%20%7D%0A%20%0A%2F%2F%20utility%20function%20to%20find%20value%20of%20and%20operand%0Aint%20value(char%20c)%20%7B%20%20return%20(c%20-%20’0′)%3B%20%7D%0A%20%0A%2F%2F%20This%20function%20evaluates%20simple%20expressions.%20It%20returns%20-1%20if%20the%0A%2F%2F%20given%20expression%20is%20invalid.%0Aint%20evaluate(char%20*exp)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20Case%3A%20Given%20expression%20is%20empty%0A%20%20%20%20if%20(*exp%20%3D%3D%20’%5C0′)%20%20return%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20The%20first%20character%20must%20be%20an%20operand%2C%20find%20its%20value%0A%20%20%20%20int%20res%20%3D%20value(exp%5B0%5D)%3B%0A%20%0A%20%20%20%20%2F%2F%20Traverse%20the%20remaining%20characters%20in%20pairs%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20exp%5Bi%5D%3B%20i%20%2B%3D%202)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20next%20character%20must%20be%20an%20operator%2C%20and%0A%20%20%20%20%20%20%20%20%2F%2F%20next%20to%20next%20an%20operand%0A%20%20%20%20%20%20%20%20char%20opr%20%3D%20exp%5Bi%5D%2C%20opd%20%3D%20exp%5Bi%2B1%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20next%20to%20next%20character%20is%20not%20an%20operand%0A%20%20%20%20%20%20%20%20if%20(!isOperand(opd))%20%20return%20-1%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Update%20result%20according%20to%20the%20operator%0A%20%20%20%20%20%20%20%20if%20(opr%20%3D%3D%20’%2B’)%20%20%20%20%20%20%20res%20%2B%3D%20value(opd)%3B%0A%20%20%20%20%20%20%20%20else%20if%20(opr%20%3D%3D%20′-‘)%20%20res%20-%3D%20value(opd)%3B%0A%20%20%20%20%20%20%20%20else%20if%20(opr%20%3D%3D%20’*’)%20%20res%20*%3D%20value(opd)%3B%0A%20%20%20%20%20%20%20%20else%20if%20(opr%20%3D%3D%20’%2F’)%20%20res%20%2F%3D%20value(opd)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20not%20a%20valid%20operator%0A%20%20%20%20%20%20%20%20else%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20res%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20expr1%5B%5D%20%3D%20%221%2B2*5%2B3%22%3B%0A%20%20%20%20int%20res%20%3D%20evaluate(expr1)%3B%0A%20%20%20%20(res%20%3D%3D%20-1)%3F%20cout%20%3C%3C%20expr1%20%3C%3C%20%22%20is%20%22%20%3C%3C%20%22Invalid%5Cn%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Value%20of%20%22%20%3C%3C%20expr1%20%3C%3C%20%22%20is%20%22%20%3C%3C%20res%20%3C%3C%20endl%3B%0A%20%0A%20%20%20%20char%20expr2%5B%5D%20%3D%20%221%2B2*3%22%3B%0A%20%20%20%20res%20%3D%20evaluate(expr2)%3B%0A%20%20%20%20(res%20%3D%3D%20-1)%3F%20cout%20%3C%3C%20expr2%20%3C%3C%20%22%20is%20%22%20%3C%3C%20%22Invalid%5Cn%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Value%20of%20%22%20%3C%3C%20expr2%20%3C%3C%20%22%20is%20%22%20%3C%3C%20res%20%3C%3C%20endl%3B%0A%20%0A%20%20%20%20char%20expr3%5B%5D%20%3D%20%224-2%2B6*3%22%3B%0A%20%20%20%20res%20%3D%20evaluate(expr3)%3B%0A%20%20%20%20(res%20%3D%3D%20-1)%3F%20cout%20%3C%3C%20expr3%20%3C%3C%20%22%20is%20%22%20%3C%3C%20%22Invalid%5Cn%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Value%20of%20%22%20%3C%3C%20expr3%20%3C%3C%20%22%20is%20%22%20%3C%3C%20res%20%3C%3C%20endl%3B%0A%20%0A%20%20%20%20char%20expr4%5B%5D%20%3D%20%221%2B%2B2%22%3B%0A%20%20%20%20res%20%3D%20evaluate(expr4)%3B%0A%20%20%20%20(res%20%3D%3D%20-1)%3F%20cout%20%3C%3C%20expr4%20%3C%3C%20%22%20is%20%22%20%3C%3C%20%22Invalid%5Cn%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Value%20of%20%22%20%3C%3C%20expr4%20%3C%3C%20%22%20is%20%22%20%3C%3C%20res%20%3C%3C%20endl%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++ Program” highlight=”” provider=”manual”/]

Output:

Value of 1+2*5+3 is 18
Value of 1+2*3 is 9
Value of 4-2+6*3 is 24
1++2 is Invalid

The above code doesn’t handle spaces. We can handle spaces by first removing all spaces from the given string. A better solution is to handle spaces in single traversal. This is left as an exercise.

Time Complexity is O(n) where n is length of the given expression.

[ad type=”banner”]