Advertisement
MaskerQwQ

括号匹配

Nov 15th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<string.h>
  3. #include<stack>
  4.  
  5. using namespace std;
  6.  
  7. bool ParenthesisMatch(string l){
  8.     stack<char>s;
  9.     int flag;
  10.     for(int i=0;i<l.length();i++){
  11.         if(l[i]=='('){
  12.             s.push(l[i]);
  13.         }
  14.         if(l[i]==')'){
  15.             if(s.empty()){
  16.                 cout<<"此括号串不匹配"<<endl;    
  17.                 return false;
  18.             }
  19.             if(s.top()=='('){
  20.                 s.pop();
  21.             }else{
  22.                 cout<<"此括号串不匹配"<<endl;    
  23.                 return false;
  24.             }
  25.         }
  26.     }
  27.     if(s.empty()){
  28.         cout<<"此括号串匹配"<<endl;
  29.     }else{
  30.         cout<<"此括号串不匹配"<<endl;
  31.     }
  32. }
  33. int main(){
  34.     string t;
  35.     cin>>t;
  36.     ParenthesisMatch(t);
  37.     return 0;  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement