SDUT - 2134 数据结构实验之栈与队列四:括号匹配

#include <stdio.h>
#include <string.h>
char a[1005], s[1005];
int p = 0;
int main()
{
    char c;
    while(gets(s))
    {
        p = 0;
        int len = strlen(s), i;
        for(i = 0; i < len; i++)
        {
            c = s[i];
            if(c == '(' || c == '[' || c == '{'){a[p++] = c;}
            else
            {
                if(c == ')')
                {
                    if(p - 1 >= 0 && a[p - 1] == '(')p--;
                    else a[p++] = c;
                }
                else if(c == ']')
                {
                    if(p - 1 >= 0 && a[p - 1] == '[')p--;
                    else a[p++] = c;
                }
                else if(c == '}')
                {
                    if(p - 1 >= 0 && a[p - 1] == '{')p--;
                    else a[p++] = c;
                }
            }
        }
        if(p == 0)printf("yes\n");
        else printf("no\n");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81865499