1、 void WriteExpr(char* E); void Assign(char v,int c); static int Value(char* E); static BinaryTreeAndExpr* CompoundExpr(char p,char* E1,char* E2); void Release(); void ReleaseRecursion(TreeNode* &p); void ReadExprRecursion(TreeNode* &p,char* E); void WriteExprRecursion(TreeNode* p,char* E); void Assi
2、gnRecursion(TreeNode* p,char v,int c); int ValueRecursion(TreeNode* p); int Priority(char c1,char c2); bool IsOperator(char c); int Evaluation(int a,char op,int b); TreeNode* root; int Expr_i,Expr_len;#endif2、Expression.cpp文件的实现#includecmath#includeExpression.husing namespace std;/-树节点类成员函数TreeNode:
3、TreeNode(char _data,TreeNode* _left,TreeNode* _right) Data=_data; left=_left; right=_right;TreeNode()char TreeNode:GetData() return Data;void TreeNode:SetLeft(TreeNode* _left)SetRight(TreeNode* _right)TreeNode* TreeNode:GetLeft() return left;GetRight() return right;SetData(char _data)/- 二叉树几及表达式类成员函
4、数BinaryTreeAndExpr:BinaryTreeAndExpr():root(NULL) Expr_i=Expr_len=0;BinaryTreeAndExpr()void BinaryTreeAndExpr:Release() if(root!=NULL) ReleaseRecursion(root); delete(root); root=NULL; ReleaseRecursion(TreeNode* &p) if(p-GetLeft()! TreeNode* p1; p1=p-GetLeft(); ReleaseRecursion(p1); delete(p1); else
5、if(p-GetRight()! TreeNode*p2; p2=p-GetRight(); ReleaseRecursion(p2); delete(p2); p=NULL;TreeNode* BinaryTreeAndExpr:GetRoot() return root;ReadExpr(char* E)=NULL) Release();root=NULL; Expr_i=0; Expr_len=strlen(E); if(Expr_len=0) return ; ReadExprRecursion(root,E);ReadExprRecursion(TreeNode* &p,char*
6、E) if(Expr_i=Expr_len)return ; p=(TreeNode*)new TreeNode(EExpr_i+,NULL,NULL); char temp=p-GetData(); if(!IsOperator(temp) return ; else TreeNode* q1,* q2; ReadExprRecursion(q1,E); p-SetLeft(q1); ReadExprRecursion(q2,E);SetRight(q2);WriteExpr(char* E) if(root=NULL) E0=0;return ; WriteExprRecursion(ro
7、ot,E);WriteExprRecursion(TreeNode* p,char* E) char c1,c2,c3100,c4100;GetLeft()=NULL | p-GetRight()=NULL) E0=p- E1= return ; c1=p-GetLeft()- c2=p-GetRight()-IsOperator(c1) & !IsOperator(c2) E0=c1;E1=p-E2=c2;E3= else if(IsOperator(c1) &IsOperator(c2) WriteExprRecursion(p-GetLeft(),c3); if(Priority(p-G
8、etData(),p-GetData()0) E0=( for(int i=0;i Ei+3=p- Ei+4= elsei+) Ei=c3i; Ei=p- Ei+1=p- Ei+2= else if(! IsOperator(c2)GetRight(),c3); E0=p- E1=p- E2=i+) Ei+3=c3i; Ei+3=i+) Ei+2=c3i;GetRight(),c4); Ei= int j=strlen(E); Ej=p- Ej+1=strlen(c4);i+) Ej+2+i=c4i; Ej+2+i= Ej+3+i=i+) Ej+1+i=c4i; Ej+1+i=int Bina
9、ryTreeAndExpr:Priority(char c1,char c2) switch(c1) case +- return -1;* switch(c2) case return 1;/ return 1; return 0;bool BinaryTreeAndExpr:IsOperator(char c) return !(c=97 & c=48 &=57);Assign(char v,int c) AssignRecursion(root,v,c);AssignRecursion(TreeNode* p,char v,int c) if(p! if(p-GetData()=v) p
10、-SetData(c+48); AssignRecursion(p-GetLeft(),v,c);GetRight(),v,c);BinaryTreeAndExpr* BinaryTreeAndExpr:CompoundExpr(char p,char* E1,char* E2) BinaryTreeAndExpr BTAE1,BTAE2,*BTAE3; BTAE1.ReadExpr(E1); BTAE2.ReadExpr(E2); TreeNode* q=(TreeNode*)new TreeNode(p,NULL,NULL); q-SetLeft(BTAE1.GetRoot();SetRi
11、ght(BTAE2.GetRoot(); BTAE3=(BinaryTreeAndExpr*)new BinaryTreeAndExpr; BTAE3-SetRoot(q); return BTAE3;SetRoot(TreeNode* _root) root=_root;Value(char* E) BinaryTreeAndExpr btae; btae.ReadExpr(E); return btae.ValueRecursion(btae.GetRoot();ValueRecursion(TreeNode* p) char c1,c2; int temp1,temp2; c1=p- r
12、eturn (c1 c1=122) temp1=0; else temp1=c1-48; if(c2 c2GetData(),temp2); temp1=ValueRecursion(p-GetLeft(); temp2=ValueRecursion(p-GetRight();Evaluation(int a,char op,int b) switch(op) return a+b; break; return a-b; return a*b; return a/b; return pow(a,b);3、ExpressionMain.cpp文件的实现int main() BinaryTreeA
13、ndExpr btae,*btae1; char E1100,E2100,P,V; int switchs,c,switchs2; bool run=true,run2=true; while(run) coutswitchs; switch(switchs) case 4: run=false; break; case 1: cout请输入相关数据.前缀表达试 getchar(); scanf(%s %c %s,E1,&P,E2); btae1=BinaryTreeAndExpr:CompoundExpr(P,E1,E2); while(run2) coutswitchs2; if(swit
14、chs2=1) coutAssign(V,c); else run2=false; btae1-WriteExpr(E1);中缀表达试:E1E1; btae.ReadExpr(E1); btae.Assign(V,c); btae.WriteExpr(E2);E2 case 3:中缀表达试为:计算结果为:Value(E1) default:你的输入无效!请重新输入. btae.Release(); if(run) cout四、 调式分析:1、 调式时递归函数不正确,出现一些逻辑错误,经改正后最终得到了正确的结果。2、 体会,学会了二叉树的使用。五、 用户使用说明:按相关提示输入,即可得到结果。六、 测试结果:见下页:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1