
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Stack_Size 50
typedef struct
{
char elem[Stack_Size];//用来存放栈中元素的一维数组
int top;/*用来存放栈顶元素的下标,top为-1代表空栈*/
}SeqStack;
//初始化顺序栈
void chushi(SeqStack *S)
{//构造一个空栈
S->top=-1;
}
//顺序栈入栈操作
int push(SeqStack *S,char x)
{
if(S->top==Stack_Size-1)return 0;//栈已满
S->top++;
S->elem[S->top]=x;
return 1;
}
//出栈操作
int pop(SeqStack *S,char *x)
{/*将栈S的栈顶元素弹出,放到x所指的存储空间中*/
if(S->top==-1)
return 0;
else
{
*x=S->elem[S->top];
S->top--;
return 1;
}
}
int isempty(SeqStack *S)
{
if(S->top==-1)return 1;
else return 0;
}
//
int Match(char s1,char s2)
{
if((s1=='['&&s2==']')||(s1=='('&&s2==')')||(s1=='{'&&s2=='}'))
return 1;
else return 0;
}
int GetTop(SeqStack *S,char *x)
{
/*将站的栈顶元素弹出,放到x所指的存储空间中,但栈顶指针保持不变*/
if(S->top==-1)
return 0;
else
{
*x=S->elem[S->top];
return 1;
}
}
//括号匹配算法
void mat(char *str)
{
//str[]中为输入的字符串,利用堆栈技术来检查该字符串中的括号是否匹配*/
SeqStack S;int i;char ch;
chushi(&S);
for(i=0;str[i]!='\0';i++)/*ge对字符串中的字符逐一扫描*/
{
switch(str[i]){
case '(':
case '[':
case '{':
push(&S,str[i]);
break;
case ')':
case ']':
case '}':
if(isempty(&S))
{
printf("no");return;
}
else
{
GetTop(&S,&ch);
if(Match(ch,str[i]))
pop(&S,&ch);
else
{
printf("no");return;
}
}
}
}
if(isempty(&S))
printf("yes");
else printf("no");
}
int main()
{ char str[100];
gets(str);
mat(str);
return 0;
}