正文

括号匹配问题2005-05-07 11:34:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/869.html

分享到:

#include<stdio.h>
#include<stdlib.h>
#define N 20
typedef struct
{
char data[N];
int top;
}stack;
stack *L;
void initstack(stack *S)
{
S->top=-1;
}
int  Isemptystack(stack *S)
{
return S->top==-1;
}
int Isfullstack(stack *S)
{
return S->top==N-1;
}
void pushstack(stack *S,char ch)
{
if(Isfullstack(S))
{
printf("overflow!!!");
exit(0);
}
S->data[++S->top]=ch;
}
void popstack(stack *S)
{
if(Isemptystack(S))
{
printf("the stack is empty!!");
exit(0);
}
S->top--;
}
void ismatch(char a[N],stack *S)
{
int i;
for(i=0;i<N;i++)
{
if(a[i]=='(')
{
if(S->data[S->top]==')')
{printf("the wrong input format!\n");
exit(0);
}
pushstack(S,a[i]);
}
else if(a[i]==')')
{
if(Isemptystack(S))
{
printf("the wrong input format!\n");
exit(0);
}
popstack(S);
}
}
if(Isemptystack(S))
printf("success for match!\n");
else printf("cannot match\n");
}
main()
{
char array[N];
initstack(L);
printf("please input the expression(the lenghth<N):\n");
scanf("%s",array);
ismatch(array,L);
}

阅读(3902) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册