33,311
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char data[100];
int top;
}zy;
enum
{
match=1,
notmatch=2,
leftmore=3,
rightmore=4,
};
void initial(zy *stack)
{
stack->top=-1;
}
int isempty(zy *stack)
{
return stack->top==-1;
}
void push(zy *stack,char ch)
{
stack->data[++stack->top]=ch;
}
char pop(zy *stack)
{
return stack->data[stack->top--];
}
int ispair(char cleft,char cright)
{
return cleft=='('&& cright==')'||cleft=='['&& cright==']'||cleft=='{'&& cright=='}';
}
int ismatch(char str[])
{
char ch ,temp;
int i=0;
zy stack;
initial(&stack);
while((ch=str[i++])!='\0')
{
switch(ch)
{
case'(':
case'[':
case'{':
push(&stack,ch);
break;
case')':
case']':
case'}':
if (isempty(&stack))
return rightmore;
temp=pop(&stack);
if(!ispair(temp,ch))
return notmatch;
break;
default:
break;
}
}
if(!isempty(&stack))
return leftmore;
return match;
}
void main()
{
int a;
char str[50];
printf("please input: ");
gets(str);
a=ismatch(str);
if(a==1)
printf("match\n");
if(a==2)
printf("not match\n");
if(a==3)
printf("left more\n");
if(a==4)
printf("right more\n");
}