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

本文介绍了一个基于C++实现的简单表达式解析器。该解析器能够处理基本的算术运算,包括加减乘除及一元操作。通过继承和多态机制,实现了表达式的动态构建与计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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:LiWeiJian(mn),lwj1396@163.com
  14. *Company:hunanuniversity
  15. *
  16. *=====================================================================================
  17. */
  18. #include<iostream>
  19. #include<string>
  20. usingnamespacestd;
  21. classExpr_node
  22. {
  23. friendclassExpr;
  24. intuse;//引用计数
  25. public:
  26. virtualvoidprint(ostream&)const=0;
  27. virtualintdocompute()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. classExpr
  39. {
  40. friendostream&operator<<(ostream&,constExpr&);
  41. Expr_node*p;
  42. public:
  43. Expr(int);//创建一个Int_node
  44. Expr(conststring&,Expr);//创建一个Unary_node
  45. Expr(conststring&,Expr,Expr);//创建一个Binary_node
  46. Expr(constExpr&t)
  47. {
  48. t.p->use++;
  49. //构造与赋值不一样
  50. //if(--p->use==0)
  51. //deletep;
  52. p=t.p;
  53. //cout<<"Expr"<<this<<"的use="<<p->use<<endl;
  54. }
  55. Expr&operator=(constExpr&);
  56. ~Expr()
  57. {
  58. if(--p->use==0)
  59. {
  60. //cout<<"~Expr()"<<this<<endl;
  61. deletep;
  62. }
  63. }
  64. intcompute()const
  65. {
  66. returnp->docompute();
  67. }
  68. };
  69. classInt_node:publicExpr_node
  70. {
  71. friendclassExpr;
  72. intn;
  73. Int_node(intk):n(k){}
  74. voidprint(ostream&o)const{o<<n;}
  75. intdocompute()const{returnn;}
  76. };
  77. classUnary_node:publicExpr_node
  78. {
  79. friendclassExpr;
  80. stringop;
  81. Expropnd;
  82. Unary_node(conststring&a,Exprb):
  83. op(a),opnd(b)
  84. {
  85. }
  86. voidprint(ostream&o)const
  87. {
  88. o<<"("<<op<<opnd<<")";
  89. }
  90. intdocompute()const
  91. {
  92. //暂定只能是负号
  93. if(op=="-")
  94. return-opnd.compute();
  95. elseif(op=="+")
  96. return+opnd.compute();
  97. else
  98. cout<<"error"<<endl;
  99. return-1;
  100. }
  101. };
  102. classBinary_node:publicExpr_node
  103. {
  104. friendclassExpr;
  105. stringop;
  106. Exprleft;
  107. Exprright;
  108. Binary_node(conststring&a,Exprb,Exprc):
  109. op(a),left(b),right(c)
  110. {
  111. }
  112. voidprint(ostream&o)const
  113. {
  114. o<<"("<<left<<op<<right<<")";
  115. }
  116. intdocompute()const
  117. {
  118. intleftval=left.compute();
  119. intrightval=right.compute();
  120. if(op=="-")returnleftval-rightval;
  121. if(op=="+")returnleftval+rightval;
  122. if(op=="*")returnleftval*rightval;
  123. if(op=="/"&&rightval!=0)returnleftval/rightval;
  124. return-1;
  125. }
  126. };
  127. Expr::Expr(intn)
  128. {
  129. //cout<<"Expr("<<n<<")"<<endl;
  130. p=newInt_node(n);
  131. //cout<<"Expr的地址"<<this<<endl;
  132. //cout<<"expr.expr_node的地址:"<<p<<endl;
  133. }
  134. Expr::Expr(conststring&op,Exprt)
  135. {
  136. //cout<<"Expr("<<op<<""<<t<<")"<<endl;
  137. p=newUnary_node(op,t);
  138. //cout<<"Expr的地址"<<this<<endl;
  139. //cout<<"expr.expr_node的地址:"<<p<<endl;
  140. }
  141. Expr::Expr(conststring&op,Exprleft,Exprright)
  142. {
  143. //cout<<"Expr("<<left<<""<<op<<""<<right<<")"<<endl;
  144. p=newBinary_node(op,left,right);
  145. //cout<<"Expr的地址"<<this<<endl;
  146. //cout<<"expr.expr_node的地址:"<<p<<endl;
  147. }
  148. Expr&
  149. Expr::operator=(constExpr&rhs)
  150. {
  151. rhs.p->use++;
  152. if(--p->use==0)
  153. deletep;
  154. p=rhs.p;
  155. return*this;
  156. }
  157. ostream&operator<<(ostream&o,constExpr&e)
  158. {
  159. e.p->print(o);
  160. returno;
  161. }
  162. intmain()
  163. {
  164. Expre=Expr("*",Expr("+",8,7),Expr("/",56,7));
  165. cout<<e<<"="<<e.compute()<<endl;
  166. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值