四则运算二叉树

上传人:碎****木 文档编号:220862132 上传时间:2021-12-09 格式:DOCX 页数:16 大小:37.33KB
返回 下载 相关 举报
四则运算二叉树_第1页
第1页 / 共16页
四则运算二叉树_第2页
第2页 / 共16页
四则运算二叉树_第3页
第3页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《四则运算二叉树》由会员分享,可在线阅读,更多相关《四则运算二叉树(16页珍藏版)》请在金锄头文库上搜索。

1、数据构造课程设计报告设计题目:十进制四那么运算计算器实习目的通过实习,了解并初步把握设计、实现较大系统的完整过程,包括系统分析、编码 设计、系统集成、以及调试分析,娴熟把握数据构造的选择、设计、实现以及操作方法, 为进一步的应用开发打好根底。二问题描述在以二叉树表示算术表达式的根底上,设计一个十进制的四那么运算的计算器。设计要求实现整数浮点数的四那么运算。三需求分析该程序实现的是实数型的四那么运算,并在此运算上又参加了幂”运算,该程序用一二叉树表示整个输入的算术表达式:(1)实现对结点的打印,便于结果分析; (2)实现对结点的统计;(3)实现中间结果的显示,可以看打印的结点,验证运算结果的正确

2、与否。四概要设计系统用到的抽象数据类型定义:161. ADT node_type数据对象V:一个集合,该集合中的全部元素具有一样的特性数据关系R:R=VRVR=|P(x,y)(x,y 属于V)根本操作:(1) node_type(stringk);操作结果:对结点进展初始化ADT node_type 2ADT binary_tree数据对象D:一个集合,该集合中的全部元素具有一样的特性数据关系R:假设D 为空,那么为空树。假设D 中仅含有一个数据元素,那么R 为空集, 否那么 R=H,H 为如下二元关系:(1) 在 D 中存在唯一的称为根的数据元素root,它在关系H 中没有前驱(2) 除 r

3、oot 以外,D 中每个结点在关系H 下有且仅有一个前驱。根本操作:(1) print(node_type*r)CopyTimeTree(p,q); 操作结果:对结点进展打印(2) evaluate(node_type*prt); 操作结果:对一二叉树进展计算(3) counter();操作结果:计算一棵二叉树中的结点个数ADT binary_tree系统中子程序及功能要求:1. ADT node_type build_node(stringx):建立一个结点2. addition(charOperatorA,charOperatorB):推断两操作符是否相等,假设相等返回True3. Tak

4、esPrecedence(char OperatorA,char OperatorB):判别符号的优先级。AB,返回为TRUE4. copy(ADT node_type *&r1, ADT node_type *r2):拷贝整个二叉树5. isok(stringexp):验证表达式是否输入正确,假设正确返回TRUE五具体设计(C 语言源程序)/tree.h/ #ifndef_TREE_H_#define_TREE_H_/防止反复包含#include #include #include #includeusingnamespacestd;/boolIsOperator(stringmystrin

5、g)/验证操作符if(mystring=“-“|mystring=“+“|mystring=“/“|mystring=“*“|mystring=“)return(true); else return(false);boolIsOperator(charops)/重载if(ops=”+”|ops=”-”|ops=”*”|ops=”/”|ops=”|ops=”(”|ops=”)”) return(true);else return(false);boolIsOperand(charch)/验证数据if(ch=”0”)&(chdata)&!IsOperator(prt-left_child-data

6、)&!IsOperator(prt-right_child-data)floatnum=0;floatnum1=atof(prt-left_child-data.c_str(); floatnum2=atof(prt-right_child-data.c_str(); if(prt-data=“+“)num=num1+num2; else if(prt-data=“-“)num=num1-num2; else if(prt-data=“*“)num=num1*num2; else if(prt-data=“/“)num=num1/num2; else if(prt-data=“)num=pow

7、(num1,num2);coutnum”t”;/打印中间结果stringstreambob;bobdata=suzzy;prt-left_child=NULL; prt-right_child=NULL;elseif(prt-left_child=NULL&prt-right_child=NULL);elseevaluate(prt-left_child); evaluate(prt-right_child); evaluate(prt);voidclear_help(node_type*rt)if(rt!=NULL)clear_help(rt-left_child); clear_help(

8、rt-right_child); deletert;voidclear()/删除整个二叉树。clear_help(root);/voidcount_help(node_type*rt,int&a)/计数者/引用可以带回转变值。a=0;intf1,f2; if(rt!=NULL)count_help(rt-left_child,f1); count_help(rt-right_child,f2); a=f1+f2+1;intcounter()/返回结点的个数inta; count_help(root,a); returna;/二叉树类完毕。node_type*build_node(stringx

9、)/建立一个结点node_type*new_node; new_node=newnode_type(x); return(new_node);voidbinary_tree:print(node_type*p)/打印if(p!=NULL)print(p-left_child); print(p-right_child); coutdataB,返回为 TRUE。if(OperatorA=”(”) returnfalse;elseif(OperatorB=”(”) returnfalse;elseif(OperatorB=”)”) returntrue;elseif(addition(Operat

10、orA,OperatorB) returnfalse;elseif(OperatorA=”) returntrue;elseif(OperatorB=”) returnfalse;elseif(OperatorA=”*”)|(OperatorA=”/”) returntrue;elseif(OperatorB=”*”)|(OperatorB=”/”) returnfalse;elseif(OperatorA=”+”)|(OperatorA=”-”) returntrue;elsereturnfalse;voidcopy(node_type*&r1,node_type*r2)/挎贝整个二叉树if(r2=NULL) r1=NULL;elser1=build_node(r2-data);copy(r1-left_child,r2-left_child); copy(r1-right_child,r2-right_child);#endif/条件编绎完毕/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号