您的位置:首页 > 编程语言 > C语言/C++

括号匹配检验 c++

2015-04-13 13:01 260 查看
#include<iostream>
#include<stack>
using namespace std;
void main(){
int i,invalid = 0;
char a[50];
char *s=a;
stack<char> st;
st.empty();
cin >> a;
while(*s!='\0') {
if(*s == '['|| *s == '(')
st.push(*s);
else  {
if(*s == ']')   {
if(st.top() != '[')
{
invalid = 1;
break;
}
else
st.pop();
}
if(*s == ')')
{
if(st.top()!='(')
{
invalid = 1;
break;
}
else
st.pop();
}
}
s++;
}
if(!st.empty())
invalid = 1;
if(invalid)
cout<<"invalid!"<<endl;
else
cout<<"ok!"<<endl; cin>>i;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: