括弧匹配检验

#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; }
为什么要过别人为我安排的生活.
原文地址:https://www.cnblogs.com/qdscwyy/p/6628499.html