【来自华为机试】检测括号匹配,匹配返回1,否0。
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
charcha=NULL;
intflag=1;
stacka;
while(true){
cha=getchar(); //getchar能读取换行和空格,cin不能!
if(cha=='\n')break;
if(cha=='('||cha=='[')
a.push(cha);
else if(cha==')'){
if(a.top()=='(')
a.pop();
else flag=0;
}
else if(cha==']'){
if(a.top()=='[')
a.pop();
else flag=0;
}
}
if(!a.empty())
flag=0;
cout<<flag<<endl;
//system("pause");
returnflag;
}