#include "iostream"
#include "stack"
using namespace std;
bool pipei(char* str)
{
stack<char> st;
char* p=str;
if (!str)
{
return false;
}
int index =0;
while(*p)
{
switch(*p)
{
case '{':
st.push(*p);
p++;
break;
case '[':
st.push(*p);
p++;
break;
case '(':
st.push(*p);
p++;
break;
case ')':
if (st.top() =='(')
{
st.pop();
p++;
break;
}
else
{
return false;
}
case ']':
if (st.top() =='[')
{
st.pop();
p++;
break;
}
else
return false;
case '}':
if (st.top() =='{')
{
st.pop();
p++;
break;
}
return
false;
}
}
if (st.empty())
{
return true;
}
return false;
}
void main()
{
char str[20];
cin>>str;
if (pipei(str))
{
cout<<"匹配成功!"<<endl;
}
else
cout<<"匹配失败!"<<endl;
}
括号匹配
最新推荐文章于 2024-09-22 10:16:35 发布