All pastes #2091680 Raw Edit

Anonymous

public text v1 · immutable
#2091680 ·published 2011-10-20 03:51 UTC
rendered paste body
#include <stack>
#include <iostream>
using namespace std;
int isBracketsMatched(string a) {
    stack <char> stk; 
    size_t len = a.length();
    //cout << len << "\n";
    char inv = ' ';
    for (size_t indx = 0; indx < len; indx++)
    {
        char ch = a[indx];
        
        if (ch == '[' || ch == '{' || ch == '(')
        {
            stk.push (ch);
        }
        else if (ch == ']' || ch == '}' || ch == ')')
        {
	    if (ch == ']')
	    {
		inv = '[';
	    }
	    else if (ch == '}')
	    {
		inv = '{';
	    }
	    else
	    {
		inv = '(';
	    }
	
            if (inv == stk.top())
            {
                stk.pop();
            }
            else
                return 0; 
        }
    }
    if (stk.empty())
        return 1;
    else
    {
        cout <<"\nstack not empty";
        return 0;
    }
}

int main()
{
 cout << isBracketsMatched("[(5+4)*{(5/7)-(5+(8/4))}]");
 return 0;
}