中缀表达式直接转换为表达式二叉树、前缀表达式、后缀表达式,表达式求值

#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;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值