资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验二四则运算(二叉树)一实习目的通过实习,了解并初步掌握设计、 实现较大系统的完整过程,包括系统分析、编码设计、系统集成、以及调试分析,熟练掌握数据结构的选择、设计、实现以及操作方法,为进一步的应用开发打好基础。二实验要求设计一个混合四则运算程序要求: (1)用二叉树方法设计;(2)可接受空格输入;(3)设计二叉树上节点中数据的排序函数;(4)设计计数二叉树中节点个数的函数;(5)打印输出各关节的中间计数结果和最终计数的结果。三实验代码 #ifndef _TREE_H_ #define _TREE_H_ #include #include #include #include using namespace std; bool IsOperator(string mystring) if(mystring=“-“|mystring=“+“|mystring=“/“|mystring=“*“|mystring=“) return(true); else return(false); bool IsOperator(char ops) if(ops=+|ops=-|ops=*|ops=/|ops=|ops=(|ops=) return(true); else return(false); bool IsOperand(char ch) if(ch=0) float num1=atof(prt-left_child-data.c_str(); float num2=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(num1,num2); coutdata=suzzy; prt-left_child=NULL; prt-right_child=NULL; else if(prt-left_child=NULL else evaluate(prt-left_child); evaluate(prt-right_child); evaluate(prt); void clear_help(node_type *rt) if(rt!=NULL) clear_help(rt-left_child); clear_help(rt-right_child); delete rt; void clear() clear_help(root); void count_help(node_type *rt,int int f1,f2; if(rt!=NULL) count_help(rt-left_child,f1); count_help(rt-right_child,f2); a=f1+f2+1; int counter() int a; count_help(root,a); return a; ; node_type *build_node(string x) node_type *new_node; new_node=new node_type(x); return(new_node); void binary_tree:print(node_type *p) if(p!=NULL) print(p-left_child); print(p-right_child); coutdatadata); copy(r1-left_child,r2-left_child); copy(r1-right_child,r2-right_child); #endif #include #include #include #include bool isok(string exp) char check; int error=0; int lb=0; int rb=0; if(exp.size()=1 |exp.size()=0 else if(IsOperator(exp0) for(int m=0;mNodeStack; stackOpStack; string infix; char choice=y; char c; while(choice=y|choice=Y) coutright_child=temp_tree.root; temp_tree.root=NULL; copy(temp_tree.root,NodeStack.top().root); etree.root-left_child=temp_tree.root; NodeStack.pop(); temp_tree.root=NULL; copy(temp_tree.root,etree.root); NodeStack.push(temp_tree); etree.root=NULL; OpStack.push(c); else if(c=() OpStack.push(c); if(infixi+1=-) binary_tree temp; temp.root=build_node(“0“); NodeStack.push(temp); OpStack.push(-); +i; else if(c=) while(OpStack.top()!=() binary_tree temp_tree; string thisstring=“; thisstring=thisstring+OpStack.top(); OpStack.pop(); etree.root=build_node(thisstring); copy(temp_tree.root,NodeStack.top().root); NodeStack.pop(); etree.root-right_child=temp_tree.root; temp_tree.root=NULL; copy(temp_tree.root,NodeStack.top().root); etree.root-left_child=temp_tree.root; NodeStack.pop(); if(!IsOperator(etree.root-right_child-data) a = etree.root-left_child-data; b = etree.root-right_child-data; float num1=atof(a.c_str(); float num2=atof(b.c_str(); if(num1 left_child-data = b; etree.root-right_child-data = a; temp_tree.root=NULL; copy(temp_tree.root,etree.root); NodeStack.push(temp_tree); etree.root=NULL; OpStack.pop(); while(!OpStack.empty() binary_tree temp_tree; string thisstring=“; thisstring=thisstring+OpStack.top(); OpStack.pop(); etree.root=build_node(thisstring); copy(temp_tree.root,NodeStack.top().root); NodeStack.pop(); etree.root-right_child=temp_tree.root; temp_tree.root=NULL; copy(temp_tree.root,NodeStack.top().root); etree.root-left_child=temp_tree.root; NodeStack.pop(); temp_tree.root=NULL; if(!IsOperator(etree.root-right_child-data) a = etree.root-left_child-data; b = etree.root-right_child-data; float num1=atof(a.c_str(); float num2=atof(b.c_str(); if(num1 left_child-data = b; etree.root-right_child-data = a; copy(temp_tree.root,etree.root); NodeStack.push(temp_tree); if(!OpStack.empty() etree.root=NULL; coutdata: “; cinchoice; else cout: “; cinchoice; return 0; 四实验结果
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号