class CEpress
{
private:
string m_sString;
public:
CEpress(string sstring)
{
m_sString = sstring;
}
bool isValid()
{
char ch=0;
bool pos =true;
stack<char>m_sTac;
for (int i = 0; i < m_sString.length(); i++)
{
ch = m_sString.at(i);
if ('(' == ch || '[' == ch)
{
m_sTac.push(ch);
}
if (']' == ch || ')' == ch)
{
if (m_sTac.empty())
{
pos = false;
break;
}
else
{
if (']' == ch&&'[' == m_sTac.top())
{
m_sTac.pop();
}
else if (')' == ch&&'(' == m_sTac.top())
{
m_sTac.pop();
}
else
{
pos = false;
break;
}
}
}
}
if (true == pos && !m_sTac.empty())
{
pos = false;
}
return pos;
}
};
int main()
{
CEpress c1 = { ")[]" };
CEpress c2 = { "([]" };
CEpress c3 = { "()[" };
CEpress c4 = { "([]())" };
c1.isValid() == true ? cout << "c1 is right" << endl : cout << "c1 is false" << endl;
c2.isValid() == true ? cout << "c2 is right" << endl : cout << "c2 is false" << endl;
c3.isValid() == true ? cout << "c3 is right" << endl : cout << "c3 is false" << endl;
c4.isValid() == true ? cout << "c4 is right" << endl : cout << "c4 is false" << endl;
return 0;
}
括号识别
最新推荐文章于 2021-09-22 00:06:22 发布