用了c++ stl做,适合考试时赶一赶 (原来那份C语言实现的也还是建议平时练习、了解
#include<iostream>
#include<stack>
using namespace std;
int main(){
string s;
cin>>s;
stack<char> k;
int l=s.length();
for(int i=0;i<l;i++){
if(s[i]=='('||s[i]=='['){
k.push(s[i]);
}
else if(s[i]==')'||s[i]==']'){
if(k.empty()){
cout<<"lack of left parenthesis";
return 0;
}
else{
char ch=k.top();
if(ch=='('&&s[i]==')') k.pop();
else if(ch=='['&&s[i]==']') k.pop();
else{
cout<<"isn't matched pairs";
return 0;
}
}
}
else continue;
}
if(!k.empty()) cout<<"lack of right parenthesis";
else cout<<"matching";
return 0;
}