
Patrick B. answered 08/09/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stack>
#include <string.h>
bool checkParens(char * strMathExpression)
{
stack<int> _stack;
int x=1;
bool boolReturn=true;
int nStrLen = strlen(strMathExpression);
for (int iLoop=0; iLoop<nStrLen; iLoop++)
{
char chCurChar = strMathExpression[iLoop];
if (chCurChar=='(')
{
_stack.push(x);
}
if (chCurChar==')')
{
if (_stack.empty())
{
boolReturn=false;
break;
}
else
{
_stack.pop();
}
}
}
if (!_stack.empty())
{
boolReturn=false;
}
return( boolReturn);
}
int main()
{
char mathExpr[] = "(5 + (9-(3+2))+ 4)";
//char mathExpr[] = "(5 + (9-(3+2)+ 4)";
bool boolReturn = checkParens(mathExpr);
if (boolReturn)
{
cout << "BALANCED !!!" <<endl;
}
else
{
cout << "UNBALANCED:MIS-MATCHED PARENTHESIS " <<endl;
}
}