- /*
- *=====================================================================================
- *
- *Filename:eval.cpp
- *
- *Description:表达式,《c++沉思录》第8章
- *
- *Version:1.0
- *Created:2009年01月07日15时51分53秒
- *Revision:none
- *Compiler:gcc
- *
- *Author:LiWeiJian(mn),lwj1396@163.com
- *Company:hunanuniversity
- *
- *=====================================================================================
- */
- #include<iostream>
- #include<string>
- usingnamespacestd;
- classExpr_node
- {
- friendclassExpr;
- intuse;//引用计数
- public:
- virtualvoidprint(ostream&)const=0;
- virtualintdocompute()const=0;
- protected:
- Expr_node():use(1)
- {
- //cout<<"Expr_node(),use="<<use<<endl;
- }
- virtual~Expr_node()
- {
- //cout<<"~Expr_node()"<<this<<endl;
- }
- };
- classExpr
- {
- friendostream&operator<<(ostream&,constExpr&);
- Expr_node*p;
- public:
- Expr(int);//创建一个Int_node
- Expr(conststring&,Expr);//创建一个Unary_node
- Expr(conststring&,Expr,Expr);//创建一个Binary_node
- Expr(constExpr&t)
- {
- t.p->use++;
- //构造与赋值不一样
- //if(--p->use==0)
- //deletep;
- p=t.p;
- //cout<<"Expr"<<this<<"的use="<<p->use<<endl;
- }
- Expr&operator=(constExpr&);
- ~Expr()
- {
- if(--p->use==0)
- {
- //cout<<"~Expr()"<<this<<endl;
- deletep;
- }
- }
- intcompute()const
- {
- returnp->docompute();
- }
- };
- classInt_node:publicExpr_node
- {
- friendclassExpr;
- intn;
- Int_node(intk):n(k){}
- voidprint(ostream&o)const{o<<n;}
- intdocompute()const{returnn;}
- };
- classUnary_node:publicExpr_node
- {
- friendclassExpr;
- stringop;
- Expropnd;
- Unary_node(conststring&a,Exprb):
- op(a),opnd(b)
- {
- }
- voidprint(ostream&o)const
- {
- o<<"("<<op<<opnd<<")";
- }
- intdocompute()const
- {
- //暂定只能是负号
- if(op=="-")
- return-opnd.compute();
- elseif(op=="+")
- return+opnd.compute();
- else
- cout<<"error"<<endl;
- return-1;
- }
- };
- classBinary_node:publicExpr_node
- {
- friendclassExpr;
- stringop;
- Exprleft;
- Exprright;
- Binary_node(conststring&a,Exprb,Exprc):
- op(a),left(b),right(c)
- {
- }
- voidprint(ostream&o)const
- {
- o<<"("<<left<<op<<right<<")";
- }
- intdocompute()const
- {
- intleftval=left.compute();
- intrightval=right.compute();
- if(op=="-")returnleftval-rightval;
- if(op=="+")returnleftval+rightval;
- if(op=="*")returnleftval*rightval;
- if(op=="/"&&rightval!=0)returnleftval/rightval;
- return-1;
- }
- };
- Expr::Expr(intn)
- {
- //cout<<"Expr("<<n<<")"<<endl;
- p=newInt_node(n);
- //cout<<"Expr的地址"<<this<<endl;
- //cout<<"expr.expr_node的地址:"<<p<<endl;
- }
- Expr::Expr(conststring&op,Exprt)
- {
- //cout<<"Expr("<<op<<""<<t<<")"<<endl;
- p=newUnary_node(op,t);
- //cout<<"Expr的地址"<<this<<endl;
- //cout<<"expr.expr_node的地址:"<<p<<endl;
- }
- Expr::Expr(conststring&op,Exprleft,Exprright)
- {
- //cout<<"Expr("<<left<<""<<op<<""<<right<<")"<<endl;
- p=newBinary_node(op,left,right);
- //cout<<"Expr的地址"<<this<<endl;
- //cout<<"expr.expr_node的地址:"<<p<<endl;
- }
- Expr&
- Expr::operator=(constExpr&rhs)
- {
- rhs.p->use++;
- if(--p->use==0)
- deletep;
- p=rhs.p;
- return*this;
- }
- ostream&operator<<(ostream&o,constExpr&e)
- {
- e.p->print(o);
- returno;
- }
- intmain()
- {
- Expre=Expr("*",Expr("+",8,7),Expr("/",56,7));
- cout<<e<<"="<<e.compute()<<endl;
- }