- /*
- * =====================================================================================
- *
- * Filename: eval.cpp
- *
- * Description: 表达式,《c++沉思录》第8章
- *
- * Version: 1.0
- * Created: 2009年01月07日 15时51分53秒
- * Revision: none
- * Compiler: gcc
- *
- * Author: Li WeiJian (mn), lwj1396@163.com
- * Company: hunan university
- *
- * =====================================================================================
- */
- #include<iostream>
- #include<string>
- using namespace std;
- class Expr_node
- {
- friend class Expr;
- int use; //引用计数
- public:
- virtual void print(ostream&) const = 0;
- virtual int docompute() const=0;
- protected:
- Expr_node():use(1)
- {
- //cout<<"Expr_node(),use="<<use<<endl;
- }
- virtual ~Expr_node()
- {
- //cout<<"~Expr_node() "<<this<<endl;
- }
- };
- class Expr
- {
- friend ostream& operator<<(ostream&,const Expr&);
- Expr_node* p;
- public:
- Expr(int); //创建一个Int_node
- Expr(const string&,Expr); //创建一个Unary_node
- Expr(const string&,Expr,Expr);//创建一个Binary_node
- Expr(const Expr& t)
- {
- t.p->use++;
- //构造与赋值不一样
- //if(--p->use == 0)
- //delete p;
- p=t.p;
- //cout<<"Expr "<<this<<"的use="<<p->use<<endl;
- }
- Expr& operator=(const Expr&);
- ~Expr()
- {
- if (--p->use==0)
- {
- // cout<<"~Expr() "<<this<<endl;
- delete p;
- }
- }
- int compute() const
- {
- return p->docompute();
- }
- };
- class Int_node:public Expr_node
- {
- friend class Expr;
- int n;
- Int_node(int k):n(k){}
- void print(ostream& o) const {o<<n;}
- int docompute() const {return n;}
- };
- class Unary_node:public Expr_node
- {
- friend class Expr;
- string op;
- Expr opnd;
- Unary_node(const string& a,Expr b):
- op(a),opnd(b)
- {
- }
- void print(ostream& o) const
- {
- o<<"("<<op<<opnd<<")";
- }
- int docompute() const
- {
- //暂定只能是负号
- if(op=="-")
- return -opnd.compute();
- else if(op=="+")
- return +opnd.compute();
- else
- cout<<"error"<<endl;
- return -1;
- }
- };
- class Binary_node:public Expr_node
- {
- friend class Expr;
- string op;
- Expr left;
- Expr right;
- Binary_node(const string& a,Expr b,Expr c):
- op(a),left(b),right(c)
- {
- }
- void print(ostream& o) const
- {
- o<<"("<<left<<op<<right<<")";
- }
- int docompute() const
- {
- int leftval = left.compute();
- int rightval = right.compute();
- if(op=="-") return leftval-rightval;
- if(op=="+") return leftval+rightval;
- if(op=="*") return leftval*rightval;
- if(op=="/"&&rightval!=0) return leftval/rightval;
- return -1;
- }
- };
- Expr::Expr(int n)
- {
- // cout<<"Expr("<<n<<")"<<endl;
- p = new Int_node(n);
- //cout<<"Expr 的地址"<<this<<endl;
- //cout<<"expr.expr_node的地址:"<<p<<endl;
- }
- Expr::Expr(const string&op, Expr t)
- {
- // cout<<"Expr("<<op<<" "<<t<<")"<<endl;
- p = new Unary_node(op,t);
- //cout<<"Expr 的地址"<<this<<endl;
- //cout<<"expr.expr_node的地址:"<<p<<endl;
- }
- Expr::Expr(const string& op, Expr left, Expr right)
- {
- // cout<<"Expr("<<left<<" "<<op<<" "<<right<<")"<<endl;
- p = new Binary_node(op,left,right);
- //cout<<"Expr 的地址"<<this<<endl;
- //cout<<"expr.expr_node的地址:"<<p<<endl;
- }
- Expr&
- Expr::operator=(const Expr& rhs)
- {
- rhs.p->use++;
- if(--p->use == 0)
- delete p;
- p=rhs.p;
- return *this;
- }
- ostream& operator<<(ostream& o, const Expr& e)
- {
- e.p->print(o);
- return o;
- }
- int main()
- {
- Expr e = Expr("*",Expr("+",8,7),Expr("/",56,7));
- cout<<e<<"="<<e.compute()<<endl;
- }
表达式 《c++沉思录》第八章
最新推荐文章于 2015-08-05 16:51:03 发布