此函数验证式子是否正确_第1页
此函数验证式子是否正确_第2页
此函数验证式子是否正确_第3页
此函数验证式子是否正确_第4页
此函数验证式子是否正确_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论