Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Develop an expression manager that can do a balanced symbol check: string ex

ID: 3812109 • Letter: C

Question

C++

Develop an expression manager that can do a balanced symbol check: string expression = "{(0+1)*(2+3)}"; string expression = "{(0+1)+[4*(2+3)]}"; string expression ="{(0+1)+[4*(2+3)}}"; As you notice the expressions contain the following symbols: { , }, ( , ), [ , ]. Write a program that after it reads the above expressions will check and report whether the expression is balanced or not. Remember that { , }, ( , ), [ , ] are the only symbols that will be checked. All other characters will be ignored. Remember to use the stack STL as you process each expression. Call the method that processes the expression as follows: bool isItBalanced(string inputExpr) If there is a mismatch in the expression make sure you report the symbol of the mismatch and its actual position in the expression.

Run: Enter your expression: {(0+1)*(2+3)} Expected output: {(0+1)*(2+3)} == 1 Enter your expression: {(0+1)+[4*(2+3)]} Expected output: {(0+1)+[4*(2+3)]} == 1 Enter your expression: {(0+1)+[4*(2+3)}} Mismatch found : } at 15 Expected output: {(0+1)+[4*(2+3)}} == 0

Explanation / Answer

#include<iostream>
#include<stack>
#include<string>

using namespace std;

bool match(char char_1, char char_2);

class expression_manager
{
   stack<char> s;
public:
   expression_manager()
   {
      
   }
  
   bool isItBalanced(string inputExpr)
   {
       for (int i = 0; i < inputExpr.length(); i++)
       {
           /*push onto stack If the exp[i] is a starting parenthesis */
           if (inputExpr[i] == '(' || inputExpr[i] == '{' || inputExpr[i] == '[')
           {
               s.push(inputExpr[i]);
           }
           /* If exp[i] is a ending parenthesis then check for empty stack or
           paranthesis matching then pop it from Stack*/
           if (inputExpr[i] == ')' || inputExpr[i] == '}' || inputExpr[i] == ']')
           {
               if (s.empty() || !match(s.top(), inputExpr[i]))
               {
                   if (!s.empty())
                   {
                       cout << "Miss match found : "<<inputExpr[i] << " at " << i << endl;
                   }
                   return false;
               }
               else
               {
                   s.pop();
               }
           }
       }
       return true;
   }
};

/* Match for relevent paranthesis */
bool match(char char_1, char char_2)
{
   if (char_1 == '(' && char_2 == ')')
       return true;
   else if (char_1 == '{' && char_2 == '}')
       return true;
   else if (char_1 == '[' && char_2 == ']')
       return true;
   else
       return false;
}

int main()
{
   //string expression = "{(0+1)*(2+3)}";
   string expression = "{(0+1)+[4*(2+3)}}";
   expression_manager expr;
   cout << "Enter your expression: ";
   cin >> expression;
   if (expr.isItBalanced(expression))
   {
       cout << expression << " == 1" << endl;
   }
  
}

---------------------------------------------------------------------------

//otput

Enter your expression: {(0+1)+[4*(2+3)]}
{(0+1)+[4*(2+3)]} == 1

Enter your expression: {(0+1)+[4*(2+3)}}
Miss match found : } at 15