📅  最后修改于: 2022-03-11 14:44:50.986000             🧑  作者: Mango
#include
#include
using namespace std;
bool isPar(char* x,int n)
{
stack s;
for (int i = 0; i < n; i++)
{
if (x[i] == '(' || x[i] == '{' || x[i] == '[')
s.push(x[i]);
if (!s.empty())
{
if (x[i] == ')')
{
if (s.top() == '(')
{
s.pop();
continue;
}else
break;
}
//
if (x[i] == ']')
{
if (s.top() == '[')
{
s.pop();
continue;
}
else
break;
}
//
if (x[i] == '}')
{
if (s.top() == '{')
{
s.pop();
continue;
}
else
break;
}
}
else
{
return false;
}
}
return s.empty() ? true:false;
}
int main()
{
char x[100];
cout << "String: " << endl;
cin >> x;
if (isPar(x,strlen(x)))
cout << "Balanced!" << endl;
else
cout << "Not Balanced!" << endl;
return 0;
}