括号匹配问题,出错在哪!#include#include#include#define OK 1#define ERROR -1#define OVERFLOW -2typedef struct{char *base;char *top;char stacksize;}SqStack;int InitStack(SqStack &s){s.base=(char *)malloc(100*sizeof(char));if(!s.base)exit(OVERF
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 00:28:54
括号匹配问题,出错在哪!#include#include#include#define OK 1#define ERROR -1#define OVERFLOW -2typedef struct{char *base;char *top;char stacksize;}SqStack;int InitStack(SqStack &s){s.base=(char *)malloc(100*sizeof(char));if(!s.base)exit(OVERF
括号匹配问题,出错在哪!
#include
#include
#include
#define OK 1
#define ERROR -1
#define OVERFLOW -2
typedef struct{
char *base;
char *top;
char stacksize;
}SqStack;
int InitStack(SqStack &s)
{
s.base=(char *)malloc(100*sizeof(char));
if(!s.base)
exit(OVERFLOW);
s.top=s.base;
s.stacksize=100;
printf("初始化成功 ");
return OK;
}
char Push(SqStack &s,char e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(char *)realloc(s.base,(s.stacksize+10)*sizeof(char));
if(!s.base)
exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=10;
}
*s.top++=e;
printf("进栈成功 ");
return OK;
}
char Pop(SqStack &s)
{
char e;
if(s.top==s.base)
return ERROR;
e=*--s.top;
printf("出栈成功 ");
return e;
}
char StackEmpty(SqStack &s)
{
if(s.base==s.top)
return 1;
printf("调用成功 ");
return 0;
}
void main()
{
SqStack s;
InitStack(s);
char t;
char flag=1 ;
char ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '(':Push(s,ch);
break;
case'[':Push(s,ch);
break;
case ')':if(!StackEmpty(s))
{
t=Pop(s);
if(t=='(')
break;
}
else
flag=0;
break;
case ']':
if(!StackEmpty(s))
{
t=Pop(s);
if(t=='[')
break;
}
else
flag=0;
break;
}
ch=getchar();
}
if(flag=0||!StackEmpty(s))
printf("No");
else
printf("Yes");
}
括号匹配问题,出错在哪!#include#include#include#define OK 1#define ERROR -1#define OVERFLOW -2typedef struct{char *base;char *top;char stacksize;}SqStack;int InitStack(SqStack &s){s.base=(char *)malloc(100*sizeof(char));if(!s.base)exit(OVERF
你的switch 里面,if(t==) 什么的里面的break,我知道你是想退出while循环,但你的代码不是按你想的方法工作,因为这个break在switch里面终止的只是switch.