版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、#include<iostream> #include<string> #include<stack> #include"Tree.h" /Checks if expression if ok/ bool isok(string exp) /此函数验证式子是否正确,即是否符合运算规则。 char check; int error=0; int lb=0; int rb=0; if(exp.size()=1)return false; else if(IsOperator(exp0)|IsOperator(expexp.size()-1)&
2、amp;&exp0!='('&&expexp.size()-1!=')') /此处若不加,在遇到某些式子时,会出现非法操作。 return false; for(int m=0;m<exp.size();m+) check=expm; if(IsOperand(check); /如果是数字,跳过,不管。 else if(IsOperator(check) if(check=')') rb+; if(IsOperator(expm+1)&&(expm+1='+'|expm+1='
3、-'|expm+1='*'|expm+1='/'|expm+1=''|expm+1=')') m+; if(expm=')') rb+; else if(IsOperator(expm+1) error+; else if(check='(') lb+; if(expm+1='(') m+; lb+; else if(IsOperator(expm+1) error+; else if(IsOperator(expm+1)&&expm+1='('
4、;) m+; lb+; else if(IsOperator(expm+1) error+; else error+; if(error=0&&lb=rb) return(true); else return(false); / int main() /主函数开始 binary_tree etree; stack<binary_tree>NodeStack; stack<char>OpStack; string infix; char choice='y' char c; while(choice='y'|choice=&
5、#39;Y') cout<<"nn请输入表达式,不要带空格;n" cin>>infix; cout<<"-"<<'n' cout<<"表达式为: "<<infix<<'n' if(isok(infix) for(int i=0;i<infix.size();i+) c=infixi; if(IsOperand(c) string tempstring; tempstring=tempstring+c; wh
6、ile(i+1<infix.size()&&IsOperand(infixi+1) tempstring=tempstring+infix+i; binary_tree temp; temp.root=build_node(tempstring); NodeStack.push(temp); / else if(c='+'|c='-'|c='*'|c='/'|c='') if(OpStack.empty() OpStack.push(c); else if(OpStack.top()='
7、;(') OpStack.push(c); else if(TakesPrecedence(c,OpStack.top() OpStack.push(c); else while(!OpStack.empty()&&(TakesPrecedence(OpStack.top(),c)|addition(OpStack.top(),c) binary_tree temp_tree; string thisstring="" thisstring=thisstring+OpStack.top(); OpStack.pop(); etree.root=bui
8、ld_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; copy(temp_tree.root,etree.root);
9、 NodeStack.push(temp_tree); etree.root=NULL; OpStack.push(c); else if(c='(') OpStack.push(c); else if(c=')') while(OpStack.top()!='(') binary_tree temp_tree; string thisstring="" thisstring=thisstring+OpStack.top(); OpStack.pop(); etree.root=build_node(thisstring);
10、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; copy(temp_tree.root,etree.root); NodeStack.push(temp_
11、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_tre
12、e.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); if(!OpStack.empty() etree.root=NULL; cout<<"打印结点如下: " etree.print(); bin
13、ary_tree temp; copy(temp.root,etree.root); cout<<'n' cout<<"结点个数为:"<<etree.counter()<<'n' cout<<"以下是,中间的计算结果:"<<'n' etree.evaluate(); cout<<'n' cout<<"结果是: " cout<<etree.root->data&
14、lt;<'n' cout<<"-"<<'n' cout<<"是不是要进行二叉树排序,并显示?若是升序点A,若是降序点B,若不是点C。"<<'n' char c1; cin>>c1; if(c1='A'|c1='a') binary_tree temp1; copy(temp1.root,temp.order().root); /此处代码无需再改 temp1.inprint(temp1.root);/前边程序有错,此处TEMP1为空.所以没有输出. else if(c1='B'|c1='b') binary_tree temp1; copy(temp1.root,temp.order().root); temp1.deprint(temp1.root); cout<<"nnRun Program again?Enter<y/n>:" cin>>choice; else cout<<&quo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 秋分温泉活动策划方案(3篇)
- 学校历史文化管理制度(3篇)
- 2026年聊城市中医医院“水城优才”青年人才引进参考考试题库及答案解析
- 现代装饰公司店长管理制度(3篇)
- 2026年度德州市事业单位公开招聘初级综合类岗位人员(526人)备考考试题库及答案解析
- 2026山东事业单位统考济南历城区属招聘初级综合类岗位50人参考考试题库及答案解析
- 2026浙江兴海控股集团有限公司下属企业招聘3人备考考试试题及答案解析
- 2026年甘肃省酒泉民健康复医院招聘考试备考试题及答案解析
- 2026上半年黑龙江伊春市事业单位招聘262人参考考试题库及答案解析
- 2026浙江南方水泥有限公司校园招聘备考考试试题及答案解析
- (二诊)绵阳市2023级高三第二次诊断考试历史试卷A卷(含答案)
- 2026年电力交易员岗位能力认证考核题含答案
- 2026年及未来5年市场数据中国金刚石工具行业投资分析及发展战略咨询报告
- 2025-2026学年总务主任年度述职报告
- 机电井(水源井)工程施工技术方案
- 创新创业教育学习通超星期末考试答案章节答案2024年
- 河道治理、拓宽工程 投标方案(技术方案)
- 政治审查表(模板)
- 《最奇妙的蛋》完整版
- SEMI S1-1107原版完整文档
- 2023年中级财务会计各章作业练习题
评论
0/150
提交评论