数据结构之括号匹配

#include <iostream>
#include <cstring>
#include <stack>
#include <cstdio>

using namespace std;

int main()
{
    stack<char> st;
    int i=0;
    char c;
    bool flag=true;
    while(scanf("%c",&c)&&c!='#'){
        if(c=='['||c=='{'||c=='(')
        {
            st.push(c);
        }
        else
        {
            if(!st.empty())
            {
                char cc=st.top();
                st.pop();
                if((cc!='('&&c==')')||(cc!='['&&c==']')||(cc!='{'&&c=='}'))
                {
                    flag=false;
                }
            }
            else
            {
                flag=false;
            }
        }
    }
    if(!st.empty())flag=false;
    if(flag)cout<<"匹配"<<endl;
    else cout<<"不匹配"<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/jasonhaven/p/7355043.html