赞
踩
作者:翟天保Steven
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处
解释器模式是一种行为型的软件设计模式,定义了一个解释器,来解释给定语言和文法的句子。也可以理解为翻译吧,比如1+1,翻译为一加上一,等于二,这样就做成了一个简单的加法计算器。
解释器模式的优点:
解释器模式的缺点:
客户端即Main主函数,客户端通过解释器来解析表达式内容,表达式又分为终结型和非终结型。就拿计算器举例,1+1,1就是终结符类型,表达式可以用它结尾;而+就是非终结符类型,出现了+,就意味着它前后还有别的表达式字符,自然不能作终结。
场景描述:实现简单的加减法计算器。
- //Interpreter.h
- /****************************************************/
- #pragma once
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <stack>
-
- using namespace std;
-
- // 抽象表达式类
- class Expression
- {
- public:
- // 解释
- virtual int interpret() = 0;
-
- };
-
- // 数字表达式类
- class NumberExpression : public Expression
- {
- public:
- // 构造函数
- NumberExpression(int num) : number(num) {}
-
- // 解释
- virtual int interpret() { return number; }
-
- private:
- int number;
- };
-
- // 加法表达式类
- class AddExpression : public Expression
- {
- public:
- // 构造函数
- AddExpression(Expression* left, Expression* right) : left(left), right(right) {}
-
- // 解释
- virtual int interpret() { return left->interpret() + right->interpret(); }
-
- private:
- Expression* left;
- Expression* right;
- };
-
- // 减法表达式类
- class SubExpression : public Expression
- {
- public:
- // 构造函数
- SubExpression(Expression* left, Expression* right) : left(left), right(right) {}
-
- // 解释
- virtual int interpret() { return left->interpret() - right->interpret(); }
-
- private:
- Expression* left;
- Expression* right;
- };
-
- // 解释器类
- class Interpreter
- {
- public:
- // 构造函数
- Interpreter(string exp) : expression(exp) {}
-
- // 解释
- int interpret() {
- stack<Expression*> s;
- // 遍历表达式字符
- for (int i = 0; i < expression.length(); i++) {
- if (isdigit(expression[i])) {
- // 识别数字
- int j = i;
- while (j < expression.length() && isdigit(expression[j])) {
- j++;
- }
- int num = stoi(expression.substr(i, j - i));
- s.push(new NumberExpression(num));
- i = j - 1;
- }
- else if (expression[i] == '+') {
- // 把左数提取出来
- Expression* left = s.top();
- s.pop();
- // 识别右数
- int j = i + 1;
- while (j < expression.length() && isdigit(expression[j])) {
- j++;
- }
- Expression* right = new NumberExpression(stoi(expression.substr(i + 1, j - (i + 1))));
- // 左数+右数的表达式放入栈中
- s.push(new AddExpression(left, right));
- i = j - 1;
- }
- else if (expression[i] == '-') {
- // 把左数提取出来
- Expression* left = s.top();
- s.pop();
- // 识别右数
- int j = i + 1;
- while (j < expression.length() && isdigit(expression[j])) {
- j++;
- }
- Expression* right = new NumberExpression(stoi(expression.substr(i + 1, j - (i + 1))));
- // 左数-右数的表达式放入栈中
- s.push(new SubExpression(left, right));
- i = j - 1;
- }
- }
- return s.top()->interpret();
- }
-
- private:
- string expression;
- };
- //main.cpp
- /****************************************************/
- #include <iostream>
- #include <string>
- #include <unordered_map>
- #include "Interpreter.h"
-
- using namespace std;
-
- int main()
- {
- unordered_map<string, int> variables;
- string input;
- while (getline(cin, input)) {
- cout << "input:" << input << endl;
- Interpreter interpreter(input);
- variables[input] = interpreter.interpret();
- cout << "result:" << variables[input] << endl;
- }
-
- return 0;
- }
程序结果如下。
上述实现的简易计算器,也是许多大学C/C++课程中的大作业,记得以前用MFC实现了一款计算器,开心了老半天哈哈,梦回大学。感兴趣的同学也可以试试复杂计算器,比如引入了括号还有各类运算符。
我尽可能用较通俗的话语和直观的代码例程,来表述我对解释器模式的理解,或许有考虑不周到的地方,如果你有不同看法欢迎评论区交流!希望我举的例子能帮助你更好地理解解释器模式。
如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。