当前位置:网站首页>运算表达式的抽象
运算表达式的抽象
2022-07-20 09:34:00 【小小bugbug】
#pragma once
#include<ostream>
#include<string>
class Expr;
class Expr_node
{
friend class Expr;
friend std::ostream& operator <<(std::ostream& os, const Expr& e);
int useCount;
protected:
Expr_node() :useCount(1) {
}
virtual void Print(std::ostream& os) const = 0;
virtual int eval() const = 0;
virtual ~Expr_node() {
}
};
class Expr
{
friend std::ostream& operator <<(std::ostream& os, const Expr& e);
Expr_node* pNode;
public:
Expr(int num);
Expr(const std::string& op, const Expr& t);
Expr(const std::string& op, const Expr& left, const Expr& right);
Expr(const std::string& op, const Expr& left, const Expr& middle, const Expr& right);
Expr(const Expr& other) :pNode(other.pNode)
{
++pNode->useCount;
}
Expr& operator=(const Expr& rhs)
{
++rhs.pNode->useCount;
if (--pNode->useCount == 0)
delete pNode;
pNode = rhs.pNode;
return *this;
}
int eval() const
{
return pNode->eval();
}
~Expr()
{
if (--pNode->useCount == 0)
delete pNode;
}
};
class inc_node :public Expr_node
{
friend Expr;
int num;
inc_node(int n) :num(n) {
}
virtual void Print(std::ostream& os)const
{
os << num;
}
virtual int eval() const
{
return num;
}
};
class Unary_node :public Expr_node
{
friend Expr;
std::string op;
Expr opnd;
Unary_node(const std::string& str, const Expr& t) :op(str), opnd(t) {
}
virtual void Print(std::ostream& os)const
{
os << "(" << op << opnd << ")";
}
virtual int eval() const
{
if (op == "-")
return -opnd.eval();
throw("unknown");
return 0;
}
};
class Binary_node :public Expr_node
{
friend Expr;
std::string op;
Expr left;
Expr right;
Binary_node(const std::string& str, const Expr& a,const Expr& b) :op(str), left(a),right(b) {
}
virtual void Print(std::ostream& os)const
{
os << "(" << left << op << right << ")";
}
virtual int eval() const
{
int lres = left.eval();
int rres = right.eval();
if (op == "+")
return lres + rres;
if (op == "-")
return lres - rres;
if (op == "*")
return lres * rres;
if (op == "/"&& rres!=0 )
return lres / rres;
throw "unknown";
return 0;
}
};
class Ternary_node :public Expr_node
{
friend Expr;
std::string op;//这个成员也没什么用可以去掉,暂时先这样
Expr left;
Expr middle;
Expr right;
Ternary_node(const std::string& str, const Expr& a, const Expr& b,const Expr& c) :op(str), left(a),middle(b), right(c) {
}
virtual void Print(std::ostream& os)const
{
os << "(" << left<<"?" << middle<<":" << right << ")";
}
virtual int eval() const
{
return left.eval() ? middle.eval() : right.eval();
}
};
inline Expr::Expr(int num) {
pNode = new inc_node(num);
}
inline Expr::Expr(const std::string& op, const Expr& t)
{
pNode = new Unary_node(op, t);
}
inline Expr::Expr(const std::string& op, const Expr& left, const Expr& right)
{
pNode = new Binary_node(op, left, right);
}
inline Expr::Expr(const std::string& op, const Expr& left, const Expr& middle, const Expr& right)
{
pNode = new Ternary_node(op, left,middle, right);
}
//main.cpp
#include"expr.h"
#include<iostream>
using namespace std;
std::ostream& operator <<(std::ostream& os, const Expr& e)
{
e.pNode->Print(os);//class Expr;前置声明可以避免这里提示错误
return os;
}
int main()
{
Expr test("*", Expr("-", 7), Expr("+", 4, 5));
cout << test<<endl;
test = Expr("*", test, test);
cout << test<<endl;
cout << test.eval() << endl;
Expr three("san", 0, 4, 5);
cout << three << endl;
cout << three.eval()<<endl;
return 0;
}
边栏推荐
- 数商云精细化工行业管理平台一体化信息化解决方案
- 探索者TSSD 2019软件安装包下载及安装教程
- File文件改名字
- Redis-Hash类型
- Explanation of new basic surveying and mapping terms
- 煤炭行业供应链集采系统:数字化推进煤炭产业转型升级
- 【码蹄集新手村 600 题】如何将一个十进制整数的所有二进制的偶数位置上的二进制数值都变为 0
- QT|QLabel显示多行文本过多后显示省略号
- 牛客-TOP101-BM33
- 【考研词汇训练营】Day 8 —— complete,traditional,extra,afford,professional,require
猜你喜欢
浅谈Calcite的Calc优化
9 Redis压力测试redis-benchmark
ts 使用第三方库,没有类型声明文件报错处理
Redis-Set类型
Stm32+dht11 reading temperature and humidity data display
[email protected] Moveexecutionstosingleactivityid parallel gateway or inclusive gateway fallback"/>
Learning records [email protected] Moveexecutionstosingleactivityid parallel gateway or inclusive gateway fallback
程序中提升几毫秒、节省几 kB 的内存有必要吗?
Application of linked list
Leetcode-24-exchange nodes in linked list in pairs
JVM堆内存解析
随机推荐
MATLAB画雷达图(四行代码)
小心!正则 test() 匹配的一个“坑”
Qt|qlabel displays multiple lines of text. If there is too much text, ellipsis will be displayed
DOM系列之创建元素
Win10账户初始化配置
平衡二叉树(AVL树)的插入
【循环数组】js循环数组的方法合集
Unity learning notes conversion between plane pixel coordinates of spherical panorama and coordinates on three-dimensional coordinate system
【HDLBits 刷题】Verilog Language -- Basics 部分
剑指 Offer 11. 旋转数组的最小数字
Change your posture to do operation and maintenance! GOPs 2022 Shenzhen station highlights first!
How to write technical documents - this is all my experience
Redis-Set类型
Kudu1.11 environmental installation
重金属行业B2B电商交易平台:打破数据孤立状态,提升重金属行业效益
Common instructions of NPM and yarn
pyspark里mapPartitions的用法
Qt | QWidget的一些总结
发电机组工作安排问题
Guys, this CDC collects Oracle data. Why does the number type become an object after it is collected?