从终端读入以‘@’为结束符的字符序列,检查该字符序列中的 ( 与 )、 [ 与 ] 、{ 与 } 是否匹配(个数相等且位置不相交)。

#include<stdio.h>
#include<string.h>
#define size 100
char stack[size];
int top = 0;
int flag = 1;
int push(char ch)
{
if (top >= size - 1)
{
return 0;
}
else
{
stack[top] = ch;
top = top + 1;
return 1;
}
}
char pop()
{
if (top <= 0)
{
return '\0';
}
else
{
char a;
a = stack[top - 1];
top = top - 1;
return a;
}
}
int main()
{
char b[100] = { 0 };
gets_s(b);
int c;
int m = 0;
int n = 0;
c = strlen(b);
int i;
int t = 0;
for (i = 0;b[i]!='@';i++)
{
char sh,th;
sh = b[i];
switch(sh)
{
case '(':m = m + 1;push(sh);break;
case '[':m = m + 1;push(sh);break;
case '{':m = m + 1;push(sh);break;
case ')':n = n + 1;th = pop();if (th != '(') { printf("NO");t = 1; }break;
case ']':n = n + 1;th = pop();if (th != '[') { printf("NO");t = 1; }break;
case '}':n = n + 1;th = pop();if (th != '{') { printf("NO");t = 1; }break;
}
if (t == 1) break;

}

if (i == c-1&&m==n)
{
    printf("YES");
}
else if (i == c - 1 && m != n)
{
    printf("NO");
}
return 0;

}
有案例过不了

写复杂了
可以参考
https://leetcode-cn.com/problems/valid-parentheses/solution/you-xiao-de-gua-hao-by-leetcode-solution/