中缀转换为后缀问题——高手帮忙找找错误!谢谢了!
/*stack.h*/static char prio[7][7] =
{
'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=',' ',
'>','>','>','>',' ','>','>',
'<','<','<','<','<',' ', '='
};
#include "iostream.h"
#include "stdlib.h"
#define stack_size 100
#define ok 1
#define False 0
typedef char etype;
typedef int status;
typedef struct
{
char * base;
int top;
int st_size;
}sqstack;
status init_stack(sqstack & s);
status get_e(sqstack s,etype &e);
status push(sqstack & s,etype e);
status out_e(sqstack & s,etype & e);
void display(sqstack s);
int adj_operator(etype e); //判断是否是操作符
void mid_chang_back(etype * mid, etype * back); //中缀转换为后缀
char comp_turns(char ch1,char ch2); //判断优先级
/*stack.cpp*/
#include "stack.h"
status init_stack( sqstack & s)//初始化函数
{
s.base=new etype[stack_size];
if(!s.base) return False;
s.top=-1;
s.st_size=stack_size;
return ok;
}
status get_e(sqstack s,etype &e)//获得e
{
if (s.top==-1) return False;
e=s.base[s.top];
return ok;
}
status push(sqstack & s,etype e)//进栈
{
if(s.top>=s.st_size)return False;
s.top++;
s.base[s.top]=e;
return ok;
}
status out_e(sqstack & s,etype & e)//出栈
{
if(s.top==-1)return False;
e=s.base[s.top];
s.top--;
return ok;
}
void display(sqstack s)//输出栈
{
while(s.top>=0)
{
cout<<s.base[s.top]<<" ";
s.top--;
}
cout<<endl;
}
int adj_operator(char e)//判断是否是符号
{
char p[]={'+','-','*','/','(',')','#'};
for(int i=0;i<7;i++)
if(e==p[i])return i+1;
return 0;
}
char comp_turns(char ch1,char ch2 )//判断优先级
{
return prio[adj_operator(ch1)-1][adj_operator(ch2)-1];
}
从这里开始-》这个中缀转换为后缀的函数有什么错误么 ?
void mid_chang_back(etype * mid,etype * back)//中缀转换为后缀
{
sqstack s;
init_stack(s);
push(s,'#');
etype ch='a';
etype *p=back;
while(*mid!='#'||ch!='#')
{
if(!adj_operator(*mid))*p++=*mid++;
else
{
get_e(s,ch);
if(comp_turns(*mid,ch)=='>')//比栈顶高或等
{
push(s,*mid++);
}
if(comp_turns(*mid,ch)=='<')
{
while(comp_turns(*mid,ch)=='<')//比栈顶优先级低
{
out_e(s,ch);
*p++=ch;
}
push(s,*mid);
}
if(comp_turns(*mid,ch)=='=')
{
out_e(s,ch);mid++;
}
}
}
}