您的位置:首页 > 其它

第五周 项目3 - 括号的匹配

2017-10-15 21:32 141 查看
#include <stdio.h>  

#include "sqstack.h"  

int main()  

{  

    char c;  

    char st[50];  

    int d=1, i;  

    SqStack *s;  

    InitStack(s);  

    printf("请输入表达式:");  

    scanf("%s", st);  

    for(i=0; st[i]!='\0'&&d; i++)  

    {  

        switch(st[i])  

        {  

        case'(':  

        case'[':  

        case'{':  

            Push(s, st[i]);  

            break;  

        case')':  

            Pop(s, c);  

            if(c!='(') d=0;  

            break;  

        case']':  

            Pop(s, c);  

            if(c!='[') d=0;  

            break;  

        case'}':  

            Pop(s,c);  

            if(c!='{') d=0;  

            break;  

        }  

    }  

    if(StackEmpty(s)&&d==1)  

        printf("配对正确!!\n");  

    else  

        printf("配对错误!!\n");  

    return 0;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: