解释器模式

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Context
{
public:
	void setContext(const string& strContext)
	{
		size_t start = 0;
		size_t end = 0;
		m_strContext = strContext;
		while (start != string::npos)
		{
			start = m_strContext.find_first_of(" \t");
			end = m_strContext.find_first_not_of(" \t", start + 1);
			if (start == string::npos)
			{
				break;
			}
			else if (end == string::npos)
			{
				m_strContext.erase(start);
			}
			else
			{
				m_strContext.erase(start, end - start);
			}
		}
		m_strContext.erase(0, m_strContext.find_first_not_of(" \t"));
		size_t t = m_strContext.find_last_not_of(" \t");
		m_strContext.erase(m_strContext.find_last_not_of(" \t") + 1);
	}
	const string& getContext()
	{
		return m_strContext;
	}
private:
	string m_strContext;
};

class Expression
{
public:
	const bool interpret(Context& objContext)
	{
		if (!this->operateNumber(objContext, "*/"))
		{
			if (this->operateNumber(objContext, "+-"))
			{
				return true;
			}
			return false;
		}
		else
		{
			return true;
		}
	}
	virtual void execute(const char& operation) = 0;
private:
	const bool operateNumber(Context& objContext, const string& strOperation)
	{
		string strContext = objContext.getContext();
		size_t operation = strContext.find_first_of(strOperation);
		size_t start = 0;
		size_t end = 0;
		bool isIn = false;
		if (operation != string::npos)
		{
			start = strContext.find_last_of("+-*/", operation - 1);
			string strNumber;
			if (start == string::npos)
			{
				strNumber = strContext.substr(0, operation);
			}
			else
			{
				strNumber = strContext.substr(start + 1, operation - start - 1);
			}
			m_dNumberOne = atof(strNumber.c_str());
			end = strContext.find_first_of("+-*/", operation + 1);
			if (end == string::npos)
			{
				strNumber = strContext.substr(operation + 1);
			}
			else
			{
				strNumber = strContext.substr(operation + 1, end - operation - 1);
			}
			m_dNumberTwo = atof(strNumber.c_str());
			isIn = true;
		}
		if (isIn)
		{
			this->execute(strContext.at(operation));
			ostringstream oss;
			if (start == string::npos && end == string::npos)
			{
				strContext.erase(0);
				start = 0;
			}
			else if (start == string::npos)
			{
				strContext.erase(0, end - start - 1);
				start = 0;
			}
			else if (end == string::npos)
			{
				strContext.erase(++start);
			}
			else
			{
				strContext.erase(++start, end - start);
			}
			oss << m_dResult << endl;
			strContext.insert(start, oss.str());
			strContext.erase(strContext.find("\n"), 1);
			objContext.setContext(strContext);
			cout << objContext.getContext() << endl;
		}
		return isIn;
	}
protected:
	double m_dNumberOne = 0;
	double m_dNumberTwo = 0;
	double m_dResult = 0;
};

class NonTerminalExpression : public Expression
{
public:
	virtual void execute(const char& operation) override
	{
		switch (operation)
		{
		case '*':
			m_dResult = m_dNumberOne * m_dNumberTwo;
			break;
		case '-':
			m_dResult = m_dNumberOne - m_dNumberTwo;
			break;
		case '+':
			m_dResult = m_dNumberOne + m_dNumberTwo;
			break;
		case '/':
			m_dResult = m_dNumberOne / m_dNumberTwo;
			break;
		default:
			break;
		}
	}
};

int main()
{
	Context objContext;
	objContext.setContext("2+2*3+2/4");
	Expression* pobjExpression = new NonTerminalExpression();
	while (pobjExpression->interpret(objContext));
	return 0;
}
总结:顾名思义就是在需要解释的时候使用这个模型
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值