表达式 《c++沉思录》第八章

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  *       Filename:  eval.cpp
  5.  *
  6.  *    Description:  表达式,《c++沉思录》第8章
  7.  *
  8.  *        Version:  1.0
  9.  *        Created:  2009年01月07日 15时51分53秒
  10.  *       Revision:  none
  11.  *       Compiler:  gcc
  12.  *
  13.  *         Author:  Li WeiJian (mn), lwj1396@163.com
  14.  *        Company:  hunan university
  15.  *
  16.  * =====================================================================================
  17.  */
  18. #include<iostream>
  19. #include<string>
  20. using namespace std;
  21. class Expr_node
  22. {
  23.     friend class Expr;
  24.     int use;         //引用计数
  25.     public:
  26.         virtual void print(ostream&) const = 0;
  27.         virtual int docompute() const=0;
  28.     protected:
  29.         Expr_node():use(1)
  30.         {
  31.             //cout<<"Expr_node(),use="<<use<<endl;
  32.         }
  33.         virtual ~Expr_node()
  34.         {
  35.             //cout<<"~Expr_node() "<<this<<endl;
  36.         }
  37. };
  38. class Expr
  39. {
  40.     friend ostream& operator<<(ostream&,const Expr&);
  41.     Expr_node* p;
  42.     public:
  43.         Expr(int);                    //创建一个Int_node
  44.         Expr(const string&,Expr);     //创建一个Unary_node
  45.         Expr(const string&,Expr,Expr);//创建一个Binary_node
  46.         Expr(const Expr& t)      
  47.         {
  48.             t.p->use++;
  49.             //构造与赋值不一样
  50.             //if(--p->use == 0)
  51.                 //delete p;
  52.             p=t.p;  
  53.             //cout<<"Expr "<<this<<"的use="<<p->use<<endl;
  54.         }
  55.         Expr& operator=(const Expr&);
  56.         ~Expr()
  57.         {
  58.             if (--p->use==0)
  59.             {
  60.             //  cout<<"~Expr() "<<this<<endl;
  61.                 delete p;
  62.             }
  63.         }
  64.         int compute() const
  65.         {
  66.             return p->docompute();
  67.         }   
  68. };
  69. class Int_node:public Expr_node
  70. {
  71.     friend class Expr;
  72.     
  73.     int n;
  74.     Int_node(int k):n(k){}
  75.     void print(ostream& o) const {o<<n;}
  76.     int docompute() const {return n;}
  77. };
  78. class Unary_node:public Expr_node
  79. {
  80.     friend class Expr;
  81.     string op;
  82.     Expr opnd;
  83.     Unary_node(const string& a,Expr b):
  84.         op(a),opnd(b)
  85.     {
  86.     }
  87.     
  88.     void print(ostream& o) const
  89.     {
  90.         o<<"("<<op<<opnd<<")";
  91.     }
  92.     int docompute() const
  93.     {
  94.         //暂定只能是负号
  95.         if(op=="-")
  96.             return -opnd.compute();
  97.         else if(op=="+")
  98.             return +opnd.compute();
  99.         else
  100.             cout<<"error"<<endl;
  101.         return -1;
  102.     }
  103. };
  104. class Binary_node:public Expr_node
  105. {
  106.     friend class Expr;
  107.     string op;
  108.     Expr left;
  109.     Expr right;
  110.     Binary_node(const string& a,Expr b,Expr c):
  111.         op(a),left(b),right(c)
  112.     {
  113.     }
  114.     void print(ostream& o) const
  115.     {
  116.         o<<"("<<left<<op<<right<<")";
  117.     }
  118.     int docompute() const
  119.     {
  120.         int leftval = left.compute();
  121.         int rightval = right.compute();
  122.         if(op=="-"return leftval-rightval;
  123.         if(op=="+"return leftval+rightval;
  124.         if(op=="*"return leftval*rightval;
  125.         if(op=="/"&&rightval!=0) return leftval/rightval;
  126.         return -1;
  127.     }
  128. };
  129. Expr::Expr(int n)
  130. {
  131. //  cout<<"Expr("<<n<<")"<<endl;
  132.     p = new Int_node(n);
  133.     //cout<<"Expr 的地址"<<this<<endl;
  134.     //cout<<"expr.expr_node的地址:"<<p<<endl;
  135. }
  136. Expr::Expr(const string&op, Expr t)
  137. {
  138. //  cout<<"Expr("<<op<<" "<<t<<")"<<endl;
  139.     p = new Unary_node(op,t);
  140.     //cout<<"Expr 的地址"<<this<<endl;
  141.     //cout<<"expr.expr_node的地址:"<<p<<endl;
  142. }
  143. Expr::Expr(const string& op, Expr left, Expr right)
  144. {
  145. //  cout<<"Expr("<<left<<" "<<op<<" "<<right<<")"<<endl;
  146.     p = new Binary_node(op,left,right);
  147.     //cout<<"Expr 的地址"<<this<<endl;
  148.     //cout<<"expr.expr_node的地址:"<<p<<endl;
  149. }
  150. Expr& 
  151. Expr::operator=(const Expr& rhs)
  152. {
  153.     rhs.p->use++;
  154.     if(--p->use == 0)
  155.         delete p;
  156.     p=rhs.p;
  157.     return *this;
  158. }
  159. ostream& operator<<(ostream& o, const Expr& e)
  160. {
  161.     e.p->print(o);
  162.     return o;
  163. }
  164. int main()
  165. {
  166.     Expr e = Expr("*",Expr("+",8,7),Expr("/",56,7));
  167.     cout<<e<<"="<<e.compute()<<endl;
  168. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值