|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码用来检验(){}【】是否匹配正确,用栈存进后取出对比,不知道哪里错了一直没有检验正确,请问是为什么?
#include<stdio.h>
#include<stdlib.h>
typedef struct In
{
char *top;
char *base;
int length;
}N;
int Compare(N *L,char size[]);
void main()
{
N *L;
int re = 0;
char size[200];
L = (N*)malloc(sizeof(N));
if(!L)
{
printf("L space is ERROR");
exit(0);
}
printf("the formula you want to add:");
gets(size);
while(size[re]!='\0')
{
re++;
}
L->length = re;
re = Compare(L,size);
if(re==1)
{
printf("the formula is right\n");
}
else
{
printf("the formula is wrong\n");
}
}
int Compare(N *L,char size[])
{
int i = 0,re = 0;
L->base = (char*)malloc((L->length)*sizeof(char));
if(!L->base)
{
printf("L->base space is ERROR\n");
exit(0);
}
L->top = L->base;
for(i=0;i<L->length;i++)
{
if(size[i]=='('||size[i]=='['||size[i]=='{')
{
*(L->top) = size[i];
L->top ++;
}
if(size[i]==')'||size[i]==']'||size[i]=='}')
{
L->top --;
if(!L->top)
{
return re;
}
switch(*(L->top))
{
case '(':
if(size[i]==')')
{
break;
}
else
{
return re;
}
case '{':
if(size[i]=='}')
{
break;
}
else
{
return re;
}
case '[':
if(size[i]==']')
{
break;
}
else
{
return re;
}
}
}
}
if(L->top == L->base)
{
re = 1;
}
return re;
} |
|