#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;
}
总结:顾名思义就是在需要解释的时候使用这个模型
解释器模式
最新推荐文章于 2024-06-13 08:30:00 发布