#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");
}
C语言实现括号匹配
最新推荐文章于 2023-10-30 15:11:31 发布