《C#高级编程》源代码的进.doc_第1页
《C#高级编程》源代码的进.doc_第2页
《C#高级编程》源代码的进.doc_第3页
《C#高级编程》源代码的进.doc_第4页
《C#高级编程》源代码的进.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

/*程序名称:表达式计算器编译环境:Microsoft Visual C+ 6.0时间:200801*/*说明:采用树形结构处理表达式,按优先级运算结果,一个加,减,乘,除或数值为一个节点优先级如下:函数:4括号:3乘除:2加减:1*/#include #include #include #include #include using namespace std;const char NUM=0,1,2,3,4,5,6,7,8,9,.;const char OPERATION=+,-,*,/;const double PI=3.14159265358979;const double EE=2.71828182818281;class Fun /处理系统数学函数的类public:Fun(string o,int t,double l=0.0,double r=0.0):op(o),type(t),lvalue(l),rvalue(r)static string FUN;double calc();private:int type; /666 0 1 sin90 2 3! 3 3C2 string op; /函数类型double lvalue; /函数左边的值double rvalue; /函数右边的值static int FunNum;int Fun:FunNum=10;string Fun:FUN=!,sin,cos,tan,log,ln,C,A,-;/*函数说明:1:log是以10为底的工程对数2:ln 是以e为底的自然对数3:C 计算组合数 输入规则 如计算 3取2的组合 输入表达式 3C24:A 计算排列数 输入规则 如计算 3取2的排列 输入表达式 3A25:! 计算阶乘6: x的y次方 输入 xy*/int factorial(int n) /阶乘函数int i,s=1;for(i=1;iGetValue();rvalue=Right-GetValue();Expression=0;char op=GetOperation();switch(op)case +:return lvalue+rvalue;case -:return lvalue-rvalue;case *:return lvalue*rvalue;case /:return lvalue/rvalue;default:return 0;elsereturn value;bool Isnum(char c)for(int i=0;isizeof(NUM);i+)if(c=NUMi)return true;return false;bool Isoperation(char c)for(int i=0;isizeof(OPERATION);i+)if(c=OPERATIONi)return true;return false;Unit * Analyse(string exp) /分析表达式并生成链表int pri=0; /当前优先级int stat=1; /当前的读入状态 括号 0 运算符 1 其他 2Unit * head=NULL,* p=NULL;int i=0,explen;explen=exp.size();for(i=0;iNext=new Unit(pri+add_pri,c, ,0,0);p=p-Next;p-Pre=temp;else /其他的当做函数处理stat=2;string function=;while(iNext=new Unit(pri, ,function,0,2);p=p-Next;p-Pre=temp;return head;Unit * Calc(Unit * head) /计算双向链表基本单元的值Unit * p=head;while(p!=NULL)if(p-Type!=0) /非操作符string temp=p-Code;string op;double lvalue=0,rvalue=0;int l_point=0,r_point=0;int i=0,type=0;char ch;while(itemp.size() & (ch=temp.at(i),Isnum(ch)if(ch=.)l_point+;i+;continue;if(! l_point)lvalue*=10;lvalue+=(ch-0)*pow(10,-l_point);i+;if(l_point)l_point+;while(itemp.size() & (ch=temp.at(i),! Isnum(ch)op+=ch;type=1;i+;while(ivalue=f-calc();p=p-Next;return head;Node * Tree(Unit * head) /生成表达式树Node * root=NULL,* proot=NULL,* pbranch=NULL;Unit * p=head;int now_pri; /当前优先级bool hadop=false;while(p!=NULL)if(p-Type=0) /如果是一个操作符hadop=true;if(root=NULL)proot=new Node(p-Operation,p-PRI,1);root=proot;pbranch=root;now_pri=p-PRI;proot-Left=new Node( ,0,0,p-Pre-value);proot-Right=new Node( ,0,0,p-Next-value);elseif(p-PRIOperation,p-PRI,1); /新的树根proot-Left=root; /根的变换proot-Left-Head=proot;proot-Right=new Node( ,0,0,p-Next-value);proot-Right-Head=proot;root=proot;pbranch=proot; /右树枝的变换/pbranch-Right=new Node( ,0,0,p-Pre-value); /树枝右边取值else if(p-PRI=now_pri) /优先级相同,先算左边的Node * temp;temp=new Node(p-Operation,p-PRI,1);temp-Left=pbranch;if(pbranch-Head=NULL)proot=temp;root=proot;pbranch-Head=proot;elseNode * temp0;temp0=pbranch-Head;temp0-Right=temp;temp-Head=temp0;pbranch-Head=temp;pbranch=temp;pbranch-Right=new Node( ,0,0,p-Next-value);pbranch-Right-Head=pbranch;elseNode * temp;temp=new Node(p-Operation,p-PRI,1);pbranch-Right=temp;temp-Head=pbranch;pbranch=pbranch-Right;pbranch-Left=new Node( ,0,0,p-Pre-value);pbranch-Left-Head=pbranch;pbranch-Right=new Node( ,0,0,p-Next-value);pbranch-Right-Head=pbranch;now_pri=p-PRI;p=p-Next;if(! hadop)root=new Node( ,0,0,head-value);return root;int main()string exp;ifstream infile(test.txt,ios:in);while(! getline(infile,exp).eof()if(exp=)continue;Unit * h=Analyse(exp);h=Calc(h);Node * root=Tree(h);coutexp=GetValue()endl;return 0;备注:修正了上一篇的一个bug,支持负数运算.(3+2)*9)-(6/3)*sin90+1)/8+18*3=59.56.6+3.2*6=25.8sin60+3*2=6.866033+2*6/3+12/6=9sin30*2+3*6-sin90*(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论