logo CodeStepByStep logo


Language/Type: C++ recursion string return
Related Links:
Author: Marty Stepp (on 2016/10/10)

Write a recursive function named evaluateMathExpression that accepts a string parameter representing a math expression on integers and returns the result of that math expression. The expression will consist of single-digit integers and possible operators. All operators will be surrounded by parentheses; we would say that the expression is fully parenthesized. The operators will be either + or *. For example, the call of evaluateMathExpression("((1+2)*(3+1)+(1*(2+2)))") should return xxxx . You may assume that the string is non-empty and does not contain any other characters.

Constraints: You may use loops if you like, but your overall algorithm must be recursive. (It is possible to solve the problem without any loops.) Do not declare any global variables or any auxiliary data structures. You can declare as many primitive variables and strings as you like.

Type your C++ solution code here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.