




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 怎样管理时间的班会
- 钢板桩施工安全协议书(2篇)
- 审计档案管理办法
- 2025届高考语文押题作文6篇
- 沉浸式护理体验
- 投资者知权行权维权手册(股票篇)
- 2025年5月辽宁省抚顺市新宾县上夹河镇中学九年级模拟数学测试题
- 癌症患者的系统化护理要点
- 医学生的大学规划
- 贵州省遵义市2024-2025学年高二下学期第四次月考语文检测卷(含答案)
- 基于高光谱成像的青稞品种鉴别和特征品质无损检测技术研究
- 2024年山东省政府采购评审专家考试真题100个题及答案
- 2025年合肥市公安局第一批招考聘用警务辅助人员591人高频重点提升(共500题)附带答案详解
- 医院培训课件:《医务人员职业暴露及安全防护》
- 煤质化验工职业技能竞赛理论考试题及答案
- DB52T 1512-2020 水利水电工程隧洞施工超前地质预报技术规程
- 15J403-1-楼梯栏杆栏板(一)
- 部编版四年级语文下册1-8单元词语、课文默写练习卷
- 《数学课程标准》义务教育2022年修订版(原版)
- GB/T 1148-2024内燃机铝活塞
- 宣传用品供货制供应商采购投标方案(技术方案)
评论
0/150
提交评论