栈——括弧匹配检验

初级入门!!!

#include<bits/stdc++.h>
using namespace std;
stack<char>CJ;
char a[1000],b[1000];
int m,n=0,num,i=0;
int main()
{
    //freopen("括弧匹配检验.in","r",stdin);
    //freopen("括弧匹配检验.out","w",stdout);
    scanf("%s",a);
    while(a[i]!=' ')
    {
        if(a[i]=='[')
        m++;
        if(a[i]=='(')
        n++;
        if(a[i]==')')
        {
            if(n>0)
            n--;
            else
            {
                cout<<"Wrong";
                return 0;
            }
            if(n==0&&a[i-1]=='[')
            {
                cout<<"Wrong";
                return 0;
            }
        } 
        if(a[i]==']')
        {
            if(m>0)
            m--;
            else
            {
                cout<<"Wrong";
                return 0;
            }
            if(m==0&&a[i-1]=='(')
            {
                cout<<"Wrong";
                return 0;
            }
        } 
        i++;
    }
    if(n==0&&m==0)
    cout<<"OK";
    else
    cout<<"Wrong";
    return 0;
}

 

posted @ 2018-08-27 16:38  CJjiqirenqiu  阅读(222)  评论(0编辑  收藏  举报