//使用栈的相关知识判断输入的以@为结尾的字符串中()配对,若成功配对则输出配对数,若配对失败则输出no
#include<stdio.h>
#include<stdlib.h>
#define max 100
typedef struct stack
{
char data[max];
int top;
}stack;
stack *init()
{
stack *s=(stack*)malloc(sizeof(stack));
s->top=-1;
return s;
}
int push(stack *s,char x)
{
if(s->top==max-1)
return -1;
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int pop(stack *s,char x)
{
x=s->data[s->top];
s->top--;
return 1;
}
int result()
{
stack *s=init();
char x[1024];
gets(x);
int f=0,g=0;
int i=0;
int n=0,m=0;
while((x[i])!='@')
{
if((x[i])=='(')
{
f=push(s,x[i]);
m++;
}
if((x[i])==')')
{
g=pop(s,x[i]);
n++;
}
i++;
return 1;
}
if(m==n)
{
printf("%d",m);
}
else
{
printf("no");
}
return 0;
}
int main()
{
int f;
f=result();
return 0;
}
//代码参考了weixin_39994627的括号配对用栈实现c语言_用栈判断圆括号是否正确配对