栈的应用-括号匹配
#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef struct stack
{
char store[N];
int top;
}Stack;
void init_stack(Stack *s);
void push_stack(Stack *s,char data);
char pop_stack(Stack *s);
int match(char x, char y);
int main()
{
char *table;
int flag=1;
Stack*s=(Stack*)malloc(sizeof(Stack));
init_stack(s);
table=(char*)malloc(100);
gets(table);
while(*table)
{ if(*table!='('&&*table!='['&&*table!='{'&&*table!=')'&&*table!=']'&&*table!='}')
{
printf("input error\n");
break;
}
if(*table=='('||*table=='['||*table=='{')
push_stack(s,*table);
else
{
if(!match(pop_stack(s),*table))
{
flag=0;
break;
}
}
table++;
}
if(s->top==-1 && flag==1)
printf("kuo hao pi pei\n");
else
printf("kuo hao bu pi pei\n");
}
void init_stack(Stack *s)
{
s->top=-1;
}
void push_stack(Stack *s,char data)
{
if(s->top==N-1)
{
printf("stack is full now push failed\n");
return ;
}
s->store[++s->top]=data;
}
char pop_stack(Stack *s)
{ char temp;
if(s->top==-1)
{
printf("stack is empty,pop failed\n");
return -1;
}
temp=s->store[s->top--];
return temp;
}
int match(char x,char y)
{ int result=0;
if((x=='(' && y==')')||(x=='{' && y=='}')||(x=='[' && y==']'))
result=1;
return result;
}