#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
struct node
{
char date;
struct node *next;
};
struct node *top;
void push (char x)
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->date=x;
p->next=top;
top=p;
}
char pop()
{
struct node *p;
char x;
if (top==NULL)
printf("栈下溢错误!\n");
p=top;
x=p->date;
top=top->next;
free(p);
return(x);
}
void main()
{
char x,y;
printf("括号匹配,请输入括号:\n");
scanf("%c",&x);
while(x!='\n')
{
if(x=='('||x=='['||x=='{')
{
push(x);
scanf("%c",&x);
}
if(x==')'||x==']'||x=='}')
{
if (top==NULL)
{
printf("不匹配!\n");
exit(1);
}
y=pop();
if((y=='('&&x==')')||(y=='['&&x==']')||(y=='{'&&x=='}'))
{
scanf("%c",&x);
}
else
{
printf("不匹配!\n");
exit(0);
}
}
}
if (top!=NULL)
{
printf("不匹配!\n");
exit(1);
}
printf("括号匹配成功!\n");
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交