表达式类型的实现二叉树Word文档格式.docx

上传人:b****6 文档编号:18728341 上传时间:2022-12-31 格式:DOCX 页数:17 大小:277.05KB
下载 相关 举报
表达式类型的实现二叉树Word文档格式.docx_第1页
第1页 / 共17页
表达式类型的实现二叉树Word文档格式.docx_第2页
第2页 / 共17页
表达式类型的实现二叉树Word文档格式.docx_第3页
第3页 / 共17页
表达式类型的实现二叉树Word文档格式.docx_第4页
第4页 / 共17页
表达式类型的实现二叉树Word文档格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

表达式类型的实现二叉树Word文档格式.docx

《表达式类型的实现二叉树Word文档格式.docx》由会员分享,可在线阅读,更多相关《表达式类型的实现二叉树Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。

表达式类型的实现二叉树Word文档格式.docx

voidWriteExpr(char*E);

voidAssign(charv,intc);

staticintValue(char*E);

staticBinaryTreeAndExpr*CompoundExpr(charp,char*E1,char*E2);

voidRelease();

voidReleaseRecursion(TreeNode*&

p);

voidReadExprRecursion(TreeNode*&

p,char*E);

voidWriteExprRecursion(TreeNode*p,char*E);

voidAssignRecursion(TreeNode*p,charv,intc);

intValueRecursion(TreeNode*p);

intPriority(charc1,charc2);

boolIsOperator(charc);

intEvaluation(inta,charop,intb);

TreeNode*root;

intExpr_i,Expr_len;

#endif

2、Expression.cpp文件的实现

#include<

iostream>

cmath>

#include"

Expression.h"

usingnamespacestd;

//----------------------树节点类成员函数

TreeNode:

:

TreeNode(char_data,TreeNode*_left,TreeNode*_right)

Data=_data;

left=_left;

right=_right;

}

~TreeNode()

charTreeNode:

GetData()

returnData;

voidTreeNode:

SetLeft(TreeNode*_left)

SetRight(TreeNode*_right)

TreeNode*TreeNode:

GetLeft()

returnleft;

GetRight()

returnright;

SetData(char_data)

//----------------------------二叉树几及表达式类成员函数

BinaryTreeAndExpr:

BinaryTreeAndExpr():

root(NULL)

Expr_i=Expr_len=0;

~BinaryTreeAndExpr()

voidBinaryTreeAndExpr:

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);

elseif(p->

GetRight()!

TreeNode*p2;

p2=p->

GetRight();

ReleaseRecursion(p2);

delete(p2);

p=NULL;

TreeNode*BinaryTreeAndExpr:

GetRoot()

returnroot;

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*E)

if(Expr_i==Expr_len)return;

p=(TreeNode*)newTreeNode(E[Expr_i++],NULL,NULL);

chartemp=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){E[0]='

\0'

;

return;

WriteExprRecursion(root,E);

WriteExprRecursion(TreeNode*p,char*E)

charc1,c2,c3[100],c4[100];

GetLeft()==NULL||p->

GetRight()==NULL)

E[0]=p->

E[1]='

return;

c1=p->

GetLeft()->

c2=p->

GetRight()->

IsOperator(c1)&

&

!

IsOperator(c2))

E[0]=c1;

E[1]=p->

E[2]=c2;

E[3]='

elseif(IsOperator(c1)&

IsOperator(c2))

WriteExprRecursion(p->

GetLeft(),c3);

if(Priority(p->

GetData(),p->

GetData())>

0)

{

E[0]='

('

for(inti=0;

i<

strlen(c3);

i++)E[i+1]=c3[i];

E[i+1]='

)'

E[i+2]=p->

E[i+3]=p->

E[i+4]='

}

else

i++)E[i]=c3[i];

E[i]=p->

E[i+1]=p->

E[i+2]='

elseif(!

IsOperator(c2))

GetRight(),c3);

E[0]=p->

E[1]=p->

E[2]='

i++)E[i+3]=c3[i];

E[i+3]='

i++)E[i+2]=c3[i];

GetRight(),c4);

E[i]='

intj=strlen(E);

E[j]=p->

E[j+1]='

strlen(c4);

i++)E[j+2+i]=c4[i];

E[j+2+i]='

E[j+3+i]='

i++)E[j+1+i]=c4[i];

E[j+1+i]='

intBinaryTreeAndExpr:

Priority(charc1,charc2)

switch(c1)

case'

+'

-'

return-1;

*'

switch(c2)

case'

return1;

/'

^'

return1;

return0;

boolBinaryTreeAndExpr:

IsOperator(charc)

return!

(c>

=97&

c<

=122||c>

=48&

=57);

Assign(charv,intc)

AssignRecursion(root,v,c);

AssignRecursion(TreeNode*p,charv,intc)

if(p!

if(p->

GetData()==v)p->

SetData(c+48);

AssignRecursion(p->

GetLeft(),v,c);

GetRight(),v,c);

BinaryTreeAndExpr*BinaryTreeAndExpr:

CompoundExpr(charp,char*E1,char*E2)

BinaryTreeAndExprBTAE1,BTAE2,*BTAE3;

BTAE1.ReadExpr(E1);

BTAE2.ReadExpr(E2);

TreeNode*q=(TreeNode*)newTreeNode(p,NULL,NULL);

q->

SetLeft(BTAE1.GetRoot());

SetRight(BTAE2.GetRoot());

BTAE3=(BinaryTreeAndExpr*)newBinaryTreeAndExpr;

BTAE3->

SetRoot(q);

returnBTAE3;

SetRoot(TreeNode*_root)

root=_root;

Value(char*E)

BinaryTreeAndExprbtae;

btae.ReadExpr(E);

returnbtae.ValueRecursion(btae.GetRoot());

ValueRecursion(TreeNode*p)

charc1,c2;

inttemp1,temp2;

c1=p->

return(c1>

c1<

=122)?

0:

c1-48;

if(c1>

=122)temp1=0;

elsetemp1=c1-48;

if(c2>

c2<

=122)temp2=0;

elsetemp2=c2-48;

returnEvaluation(temp1,p->

GetData(),temp2);

temp1=ValueRecursion(p->

GetLeft());

temp2=ValueRecursion(p->

GetRight());

Evaluation(inta,charop,intb)

switch(op)

returna+b;

break;

returna-b;

returna*b;

returna/b;

returnpow(a,b);

3、ExpressionMain.cpp文件的实现

intmain()

BinaryTreeAndExprbtae,*btae1;

charE1[100],E2[100],P,V;

intswitchs,c,switchs2;

boolrun=true,run2=true;

while(run)

cout<

<

"

请选择功能,功能如下:

endl;

1.构造复合表达试并输出相应结果."

2.输入前缀表达试并构造中缀表达试."

3.对前缀表达试求值."

4.退出."

cin>

>

switchs;

switch(switchs)

case4:

run=false;

break;

case1:

cout<

请输入相关数据.前缀表达试"

getchar();

scanf("

%s%c%s"

E1,&

P,E2);

btae1=BinaryTreeAndExpr:

CompoundExpr(P,E1,E2);

while(run2)

{

cout<

如有变量要赋值请输入1,否则输入2"

cin>

switchs2;

if(switchs2==1)

{

cout<

请输入相关数据."

getchar();

scanf("

%c%d"

&

V,&

c);

btae1->

Assign(V,c);

}

elserun2=false;

}

btae1->

WriteExpr(E1);

中缀表达试:

E1<

Release();

delete(btae1);

run2=true;

case2:

cin>

E1;

btae.ReadExpr(E1);

btae.Assign(V,c);

btae.WriteExpr(E2);

E2<

case3:

中缀表达试为:

计算结果为:

Value(E1)<

default:

你的输入无效!

请重新输入."

btae.Release();

if(run)cout<

四、调式分析:

1、调式时递归函数不正确,出现一些逻辑错误,经改正后最终得到了正确的结果。

2、体会,学会了二叉树的使用。

五、用户使用说明:

按相关提示输入,即可得到结果。

六、测试结果:

见下页:

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1