主题:求助!程序能通过编译但无法运行!
#include<iostream>
using namespace std;
typedef char SElemType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
bool InitStack(SqStack &s)
{
s.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!s.base)
return 0;
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return 1;
}//构建一个空栈s
bool Push(SqStack &s,SElemType e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(SElemType*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s.base)
return 0;
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++=e;
return 1;
}//添加元素
bool Pop(SqStack &s)
{
SElemType e;
if(s.top==s.base)
return 0;
e=*--s.top;
return 1;
}//出栈
char GetTop(SqStack s)
{
SElemType e;
if(s.top==s.base)
return 0;
e=*(s.top-1);
return e;
}//取栈顶元素
bool StackEmpty(SqStack s)
{
if(s.base==s.top)
return 1;
else
return 0;
}//判空
int youxianji(char ch,SqStack a)
{
if(a.base==a.top)
return 1;
else
{
if(ch>='0'&&ch<='9')
return 2;
else if(ch>='a'&&ch<='z')
return 2;
else if(ch>='A'&&ch<='Z')
return 2;
else if(ch=='+'||ch=='-')
{
if(*(a.top-1)=='(')
return 1;
else
return 0;
}
else if(ch=='*'||ch=='/')
{
if(*(a.top-1)==')'||*(a.top-1)=='*'||*(a.top-1)=='/')
return 0;
else
return 1;
}
else if(ch=='(')
{
if(*(a.top-1)==')')
return 3;
else
return 1;
}
else if(ch==')')
{
if(*(a.top-1)=='(')
return 3;
else
return 0;
}
}
}
bool ExpressionConvert(char *src, char *dest,SqStack &q)
{
int p=0;
for(int i=0;src[i]!='\0';i++)
{
if(youxianji(src[i],q)==2)
{
dest[p]=src[i];
p++;
continue;
}
else if(youxianji(src[i],q)==0)
{
dest[p]=GetTop(q);
Pop(q);
p++;
i--;
continue;
}
else if(youxianji(src[i],q)==1)
{
Push(q,src[i]);
continue;
}
else if(youxianji(src[i],q)==3)
{
cout<<"表达式错误!"<<endl;
return 0;
}
}
if(( StackEmpty (q)))
{
dest[p]='\0';
return 1;
}
else
return 0;
}
void main()
{
cout<<"请输入中缀表达式:";
char src[30], dest[30];
SqStack q;
cin>>src;
q.base=NULL;
InitStack(q);
if(ExpressionConvert(src,dest,q))
{
for(int k=0;dest[k]!='\0';k++)
cout<<dest[k];
cout<<endl;
}
else
cout<<"错误!"<<endl;
}
请问程序有什么问题。帮我修改一下好吗!!
using namespace std;
typedef char SElemType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
bool InitStack(SqStack &s)
{
s.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!s.base)
return 0;
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return 1;
}//构建一个空栈s
bool Push(SqStack &s,SElemType e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(SElemType*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s.base)
return 0;
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++=e;
return 1;
}//添加元素
bool Pop(SqStack &s)
{
SElemType e;
if(s.top==s.base)
return 0;
e=*--s.top;
return 1;
}//出栈
char GetTop(SqStack s)
{
SElemType e;
if(s.top==s.base)
return 0;
e=*(s.top-1);
return e;
}//取栈顶元素
bool StackEmpty(SqStack s)
{
if(s.base==s.top)
return 1;
else
return 0;
}//判空
int youxianji(char ch,SqStack a)
{
if(a.base==a.top)
return 1;
else
{
if(ch>='0'&&ch<='9')
return 2;
else if(ch>='a'&&ch<='z')
return 2;
else if(ch>='A'&&ch<='Z')
return 2;
else if(ch=='+'||ch=='-')
{
if(*(a.top-1)=='(')
return 1;
else
return 0;
}
else if(ch=='*'||ch=='/')
{
if(*(a.top-1)==')'||*(a.top-1)=='*'||*(a.top-1)=='/')
return 0;
else
return 1;
}
else if(ch=='(')
{
if(*(a.top-1)==')')
return 3;
else
return 1;
}
else if(ch==')')
{
if(*(a.top-1)=='(')
return 3;
else
return 0;
}
}
}
bool ExpressionConvert(char *src, char *dest,SqStack &q)
{
int p=0;
for(int i=0;src[i]!='\0';i++)
{
if(youxianji(src[i],q)==2)
{
dest[p]=src[i];
p++;
continue;
}
else if(youxianji(src[i],q)==0)
{
dest[p]=GetTop(q);
Pop(q);
p++;
i--;
continue;
}
else if(youxianji(src[i],q)==1)
{
Push(q,src[i]);
continue;
}
else if(youxianji(src[i],q)==3)
{
cout<<"表达式错误!"<<endl;
return 0;
}
}
if(( StackEmpty (q)))
{
dest[p]='\0';
return 1;
}
else
return 0;
}
void main()
{
cout<<"请输入中缀表达式:";
char src[30], dest[30];
SqStack q;
cin>>src;
q.base=NULL;
InitStack(q);
if(ExpressionConvert(src,dest,q))
{
for(int k=0;dest[k]!='\0';k++)
cout<<dest[k];
cout<<endl;
}
else
cout<<"错误!"<<endl;
}
请问程序有什么问题。帮我修改一下好吗!!