检验括号匹配

检验括号匹配,第1张

检验括号匹配
#include
#include
#include
#define MAXSIZE 1000
typedef struct 
{
    char *top;
    char *base;
    int stacksize;
}ST;
void InitStack(ST &S)
{
    S.base=new char[MAXSIZE];
    if(!S.base) exit(0);
    S.top=S.base;
    S.stacksize=MAXSIZE;
}
void Push(ST &S,char e)
{
    if(S.top-S.base==MAXSIZE) exit(0);
    *(S.top)=e;
    S.top++;
}
void Pop(ST &S,char &e)
{
    if(S.top==S.base) exit(0);
    S.top--;
    e=*S.top;
}
int kong(ST S)
{
    if(S.top==S.base) return 1;
    else
    {
        return 0;
    }
}
char tou(ST S)
{
    return *(S.top-1);
}
int panduan(ST S)
{
    char c,e,c1=')',c2='}',c3=']';
    int a=0;
    while((c=getchar())!='n')
    {
        if(c=='(') Push(S,c1);
        else if(c=='{') Push(S,c2);
        else if(c=='[') Push(S,c3);
        else if(c==tou(S)) Pop(S,e);
        else if(c!=tou(S))
        {
            a=0;
            break;
        }
    }
    if(kong(S)==1) a=1;
    return a;
}
int main()
{
    ST S;
    InitStack(S);
    if(panduan(S)==1) printf("shi");
    else printf("bushi");
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5581062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存