括弧匹配检验

 

#include<stack>

#include<string>
#include<iostream> using namespace std; string s; stack<int>str; int main() { cin>>s; for(int i=0;i<s.size();++i) { if(s[i]=='(')str.push(1); if(s[i]=='[')str.push(2); if(s[i]==')') { if(str.top()==2) { cout<<"Wrong"; return 0; } else str.pop(); } if(s[i]==']') { if(str.top()==1) { cout<<"Wrong"; return 0; } else str.pop(); } } if(!str.empty())cout<<"Wrong"; else cout<<"OK"; return 0; }
posted @ 2017-03-27 19:47  Grary  阅读(280)  评论(0)    收藏  举报
博客园 首页 私信博主 编辑 关注 管理 新世界