Matching Brackets in the expression – C++

This was an extra programming exercise within university that I completed not so long ago. It was a Microsoft interview question also. I chose to implement it in c++.

The actual question which I was presented with:

Question

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

using namespace std;

//setting up the stack
stack<char> stacking;

void printMyInfo()
{
	cout<<"***************************************"<<endl;
	cout<<"* Student Name: Zara McKeown          *"<<endl;
	cout<<"* Registration:                       *"<<endl;
	cout<<"* Course: Data Structures             *"<<endl;
	cout<<"***************************************"<<endl<<endl;
}//printMyInfo

int checkBraces(char *characters)
{
    // Continues to push or pop using a for loop
    // \0 means null termination character
    for (int i = 0; characters[i] != '\0'; i++)
    {
        //seeing if the current character is a ( bracket
        if(characters[i] == '(')
        {
            //pushing the character into the stack
            stacking.push(characters[i]);
        }//if
        
        //seeing if the current character is a { bracket
        else if(characters[i] == '{')
        {
            //pushing the character into the stack
            stacking.push(characters[i]);
        }//else if
        
        //seeing if the current character is a [ bracket
        else if(characters[i]=='[')
        {
            //pushing the character into the stack
            stacking.push(characters[i]);
        }//else if
        
        //Checking to see if the next character is ) bracket
        else if(characters[i] == ')')
        {
            //making sure there is something in the stack already
            //if not it returns false
            if(stacking.empty())
            {
                cout << "0";
                return false;
                
            }//if
            
            //checking to see if there if the last thing added to the stack was an ( bracket
            else if(stacking.top() == '(')
            {
                //because the program has found a match. It now can remove the ( bracket.
                // So it can move onto the next if there is one.
                stacking.pop();
                
            }//else if
            
            //if the ( bracket is not found it will then return false.
            else
            {
                cout << "0";
                return false;
            }//else
        }//else if
        
        
        //Checking to see if the next character is } bracket
        else if(characters[i] == '}')
        {
            //making sure there is something in the stack already
            //if not it returns false
            if(stacking.empty())
            {
                
                cout << "0";
                return false;
            }//if
            
            //checking to see if there if the last thing added to the stack was an { bracket
            else if(stacking.top() == '{')
            {
                //because the program has found a match. It now can remove the ( bracket.
                // So it can move onto the next if there is one.
                stacking.pop();
                
            }//else if
            
            //if the { bracket is not found it will then return false.
            else
            {
                cout << "0";
                return false;
            }//else
        }//else if
        
        
        //Checking to see if the next character is ] bracket
        else if(characters[i] == ']')
        {
            //making sure there is something in the stack already
            //if not it returns false
            if(stacking.empty())
            {
                cout << "0";
                return false;
            }//if
            
            //checking to see if there if the last thing added to the stack was an [ bracket
            else if(stacking.top() == '[')
            {
                //because the program has found a match. It now can remove the [ bracket.
                // So it can move onto the next if there is one.
                stacking.pop();
                
            }//else if
            
            //if the [ bracket is not found it will then return false.
            else
            {
                cout << "0";
                return false;
            }//else
        }//else if
        
    }//for
    
    cout << "1";
    return true;

}//int

int main()
{
    char str1[] = "{1+[2+(3+4)]}";
    
    char str2[] = "{(3+5)-[2*1]}";
    
    char str3[] = "{3+5}-[2+1]";
    
    char str4[] ="(2+[3+5)+1]";
    
    char str5[] ="";
    
    printMyInfo();
    
    cout << "This program checks braces ([{}])" << endl;
    
    cout << endl;

    cout << "Are all the braces matching in the expression " << str1 << "? :";

    checkBraces(str1);
    
    cout << endl;
    
    cout << "Are all the braces matching in the expression " << str2 << "? :";

    checkBraces(str2);
    
    cout << endl;
    
    cout << "Are all the braces matching in the expression " << str3 << "? :";

    checkBraces(str3);
    
    cout << endl;

    cout << "Are all the braces matching in the expression " << str4 << "? :";

    checkBraces(str4);
    
    cout << endl;
    
    cout << "Are all the braces matching in an empty expression " << "? :";
    
    checkBraces(str5);
    
    cout << endl;
    
    cout<<endl<<"Program terminated."<<endl;//have these two lines before "return 0;"
    system("pause");//press any key to close the black screen

    return 0;
}//main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s