#include<iostream>
#include<stack>
#include<string>
#include<queue>
#include<math.h>
using namespace std;
struct TreeNode
{
string key;
TreeNode* left;
TreeNode* right;
};
stack<string> opt;
stack<TreeNode*> subexp;
stack<char> bracket;
stack<string> fun;
stack<char> param;
bool TestBrackets(string exp,int len)
{
int i=0;
while(i<len)
{
char ch=exp[i];
if(ch=='(')
{
bracket.push(ch);
}else
{
if(ch==')')
{
if(!bracket.empty())
{
bracket.pop();
}else
{
return false;
}
}
}
i++;
}
if(bracket.empty())
{
return true;
}else
{
return false;
}
}
TreeNode* InfixExpressionToBinaryTree(string exp,int len)
{
if(!TestBrackets(exp,len))
{
cout<<" the brackets can not be matched"<<endl;
return NULL;
}else
{
int it=0;
while(it<len)
{
char ch=exp[it];
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
string str="";
do
{
str=str+ch;
it++;
ch=exp[it];
if(ch=='(')
{
stack<char>tmp;
do{
if(ch=='(')
{
tmp.push(ch);
}
if(ch==')')
{
tmp.pop();
}
str=str+ch;
it++;
ch=exp[it];
}while(it<len&&!tmp.empty());
continue;
}
}while(it<len&&ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'&&ch!=')'&&ch!='^');
TreeNode* newNode=new TreeNode;
newNode->key=str;
newNode->left=NULL;
newNode->right=NULL;
subexp.push(newNode);
continue;
}
if(ch>='0'&&ch<='9')
{
string str="";
do
{
str=str+ch;
it++;
ch=exp[it];
}while((it<len&&ch>='0'&&ch<='9')||ch=='.');
TreeNode* newNode=new TreeNode;
newNode->key=str;
newNode->left=NULL;
newNode->right=NULL;
subexp.push(newNode);
continue;
}
if(ch=='(')
{
string str="";
str=str+ch;
opt.push(str);
it++;
continue;
}
if(ch=='+'||ch=='-')
{
string str="";
str=str+ch;
if(opt.size()==0)
{
opt.push(str);
it++;
}else
{
if(opt.top()=="(")
{
opt.push(str);
it++;
}else
{
while((!opt.empty())&&opt.top()!="(")
{
string op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.push(str);
it++;
}
}
continue;
}
if(ch=='^')
{
string str="";
str=str+ch;
if(opt.size()==0)
{
opt.push(str);
it++;
}else
{
if(opt.top()=="(")
{
opt.push(str);
it++;
}else
{
while((!opt.empty())&&opt.top()!="("&&opt.top()!="+"&&opt.top()!="-"&&opt.top()!="*"&&opt.top()!="/")
{
string op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.push(str);
it++;
}
}
continue;
}
if(ch=='*'||ch=='/')
{
string str="";
str=str+ch;
if(opt.size()==0)
{
opt.push(str);
it++;
}else
{
if(opt.top()=="(")
{
opt.push(str);
it++;
}else
{
while((!opt.empty())&&opt.top()!="("&&opt.top()!="+"&&opt.top()!="-")
{
string op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.push(str);
it++;
}
}
continue;
}
if(ch==')')
{
while(!opt.empty()&&opt.top()!="(")
{
string op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.pop();
it++;
continue;
}
}
while(!opt.empty())
{
string op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
}
return subexp.top();
}
//表达式二叉树前序遍历
void PreVisitor(TreeNode* ROOT)
{
cout<<ROOT->key<<" ";
if(ROOT->left!=NULL)
{
PreVisitor(ROOT->left);
}
if(ROOT->right!=NULL)
{
PreVisitor(ROOT->right);
}
}
//表达式二叉树中序遍历,同时加括号确定运算优先级
void InfixVisitor(TreeNode* ROOT)
{
if(ROOT->left!=NULL)
{
cout<<"(";
InfixVisitor(ROOT->left);
}
cout<<ROOT->key<<" ";
if(ROOT->right!=NULL)
{
InfixVisitor(ROOT->right);
cout<<")";
}
}
//表达式二叉树后序遍历
void PostVisitor(TreeNode* ROOT)
{
if(ROOT->left!=NULL)
{
PostVisitor(ROOT->left);
}
if(ROOT->right!=NULL)
{
PostVisitor(ROOT->right);
}
cout<<ROOT->key<<" ";
}
//
long long calcuiate(TreeNode* tree,int t)
{
if(tree->left==NULL&&tree->right==NULL)
{
if(tree->key=="a")
{
return t;
}else
{
string n=tree->key;
long long op=0;
for(int i=0;i<n.length();i++)
{
op=op*10+(int)(n[i]-'0');
}
return op;
}
}else
{
if(tree->key=="+")
{
return calcuiate(tree->left,t)+calcuiate(tree->right,t);
}
if(tree->key=="-")
{
return calcuiate(tree->left,t)-calcuiate(tree->right,t);
}
if(tree->key=="*")
{
return calcuiate(tree->left,t)*calcuiate(tree->right,t);
}
if(tree->key=="/")
{
return calcuiate(tree->left,t)/calcuiate(tree->right,t);
}
if(tree->key=="^")
{
return pow(calcuiate(tree->left,t),calcuiate(tree->right,t));
}
}
}
int main()
{
string exp="(a-1)^3+4*a";
int len=exp.length();
TreeNode* ROOT=InfixExpressionToBinaryTree(exp,len);
if(ROOT!=NULL)
{
PreVisitor(ROOT);
cout<<endl;
InfixVisitor(ROOT);
cout<<endl;
PostVisitor(ROOT);
cout<<endl;
}
for(int i=0;i<3;i++)
{
cout<<calcuiate(ROOT,i)<<endl;
}
while(!subexp.empty())
{
subexp.pop();
}
char ch;
cin>>ch;
exit(ch);
return 0;
}
中缀表达式直接转换为表达式二叉树、前缀表达式、后缀表达式,表达式求值
最新推荐文章于 2021-11-13 11:26:01 发布