栈
代码用来检验(){}【】是否匹配正确,用栈存进后取出对比,不知道哪里错了一直没有检验正确,请问是为什么?#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;
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!='\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=='('||size=='['||size=='{')
{
*(L->top) = size;
L->top ++;
}
if(size==')'||size==']'||size=='}')
{
L->top --;
if(!L->top)
{
return re;
}
switch(*(L->top))
{
case '(':
if(size==')')
{
break;
}
else
{
return re;
}
case '{':
if(size=='}')
{
break;
}
else
{
return re;
}
case '[':
if(size==']')
{
break;
}
else
{
return re;
}
}
}
}
if(L->top == L->base)
{
re = 1;
}
return re;
} #include<stack> 不就好了么……{:10_277:} 永恒的蓝色梦想 发表于 2020-4-26 20:01
#include 不就好了么……
C语言没有这玩意,想用得自己写一个,这是C++中的东西
^_^ 人造人 发表于 2020-4-26 20:04
C语言没有这玩意,想用得自己写一个,这是C++中的东西
^_^
{:10_247:}
乖乖去用C++{:10_258:}
页:
[1]