后缀转中缀:24*95+-变成(2*4)95+-变成(2*4)(9+5)-变成(2*4)-(9+5)=-6
注意碰到符号时是符号前两位数字的运算
中缀转后缀:
后缀转二叉树:碰到数字放入栈中,碰到符号,就以符号为根创建以栈顶数字为右子树,下一个数为左子树的数放入栈中(成为子树的数字当然要pop)一直继续下去
// postfix.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<stack>
#include<iomanip>
using namespace std;
//符号
class character{
public:
character();
character(char c);
friend bool operator>=(const character&l,const character&r){
return l.pr>=r.pr;
}
char getcharacter();//返回符号
private:
char c;
// int inpr;//输入的符号优先级
int pr;//栈顶符号优先级
};
character::character(){}
character::character(char c){
this->c=c;
if(c=='+'||c=='-'){//inpr=1;
pr=1;}
else if(c=='*'||c=='%'||c=='/'){//inpr=2;
pr=2;}
else if(c=='^'){//inpr=4;
pr=3;}
//else if(c="("){inpr=5;pr=-1;}
else{//inpr=0;
pr=0;}
}
char character::getcharacter(){return c;}
class tnode{
public:
char nodeValue;
tnode*left,*right;
tnode(){}
tnode(const char&item,tnode*lp=NULL,tnode*rp=NULL):nodeValue(item),left(lp),right(rp){}
};
//中缀转后缀,中缀为exp21,后缀为exp2
class topostfix{
public:
topostfix();
topostfix(string &exp);
string postfix();
tnode* buildTree();
void print(tnode*p,int depth);
public:
string exp1;//中缀
string exp2;//后缀
stack<character> cs;//符号栈
void judgepr(character&c);
bool isch(char c);//判断符号是不是有效运算符
};
void topostfix::judgepr(character&c){
//如果要输入的符号优先级不如符号栈顶的元素,移除栈顶到后缀表达式后面
while(!cs.empty()&&cs.top()>=c&&c.getcharacter()!='('){
exp2+=cs.top().getcharacter();
cs.pop();
//exp2+='';
}
}
bool topostfix::isch(char c){
return c=='+'||c=='-'||c=='*'||c=='%'||c=='/'||c=='^';
}
topostfix::topostfix(){}
topostfix::topostfix(string &exp){
exp1=exp;
}/*
void topostfix::setexp(string &exp1){
this->exp1=exp1;
exp2="";
}*/
string topostfix::postfix(){
character c;
int i;
char ch;
for( i=0;i<exp1.length();i++){
ch=exp1[i];
if(isdigit(ch)){
exp2+=ch;
}
else if(isch(ch)||ch=='('){
c=character(ch);
judgepr(c);
cs.push(c);
}
else if(ch==')'){
c=character(ch);
if(!cs.empty()) {
while((cs.top()).getcharacter() != '(' && !cs.empty()){
exp2+=(cs.top()).getcharacter();
cs.pop();
}
cs.pop();
}
else cerr<<"error!";
}
}
while(!cs.empty()){
c=cs.top();
cs.pop();
if(c.getcharacter()!='('||c.getcharacter()!=')')
exp2+=c.getcharacter();
}
return exp2;
}
tnode *topostfix::buildTree(){
tnode *p;
stack<tnode*> ns;
char c;
int i=0;
c=exp2[i++];//先c=exp2[i],然后i++
while(i<=exp2.length()){
if(!isch(c)){
p=new tnode(c);
ns.push(p);//如果是数字,放入栈中
c=exp2[i++];
}
else{
p=new tnode(c);
//如果是符号,取出栈顶两元素,以符号为根节点,第一个栈顶为右子树,第二个栈顶为左子树
if(!ns.empty()){
p->right=ns.top();
ns.pop();
}
if(!ns.empty()){
p->left=ns.top();
ns.pop();
}
ns.push(p);
c=exp2[i++];
}
}
return p;
}
int depth(tnode *root){
int dep,dl,dr;
if(root==NULL)dep=-1;
else{
dl=depth(root->left);
dr=depth(root->right);
dep=1+(dl>dr?dl:dr);
}
//cout<<dep;
return dep;
}
void topostfix:: print(tnode *p,int depth){
if(p!=NULL){
print(p->right,depth+1);
cout<<setw(4*depth)<<" ";
cout<<p->nodeValue<<endl;
print(p->left,depth+1);
}
}
int main(int argc, char* argv[])
{
string str="3*(2+5)";
topostfix exp(str);
cout<<"转后缀"<<endl;
cout<<exp.postfix()<<endl;
cout<<"表达式二叉树为:"<<endl;
tnode *root;
root=exp.buildTree();
exp.print(root,depth(root));
return 0;
}