当前位置: 首页 > news >正文

国际网站怎么做网站建设销售话术文本格式

国际网站怎么做,网站建设销售话术文本格式,比较实用的h5网页建设网站,wordpress前面增加new目录 解释器模式(Interpreter Pattern) 实际应用 算术表达式解释器 布尔表达式解释器 总结 解释器模式(Interpreter Pattern) 解释器模式是一种行为设计模式,它定义了一种语言的文法表示,并使用解释器…

目录

解释器模式(Interpreter Pattern)

实际应用

算术表达式解释器

布尔表达式解释器

总结


解释器模式(Interpreter Pattern)

解释器模式是一种行为设计模式,它定义了一种语言的文法表示,并使用解释器来解释这些文法。该模式适用于那些有特定语法规则的场景,比如编译器、正则表达式引擎和计算器。

实际应用

算术表达式解释器

算术表达式解释器 -- 可以解析和计算包含加法和减法的算术表达式。

#include <iostream>
#include <string>
#include <stack>
#include <memory>
#include <unordered_map>// 抽象表达式
class Expression {
public:virtual ~Expression() = default;virtual int interpret(const std::unordered_map<char, int>& context) = 0;
};// 终结符表达式(变量)
class VariableExpression : public Expression {
private:char name;
public:VariableExpression(char name) : name(name) {}int interpret(const std::unordered_map<char, int>& context) override {return context.at(name);}
};// 非终结符表达式(加法)
class AddExpression : public Expression {
private:std::shared_ptr<Expression> left, right;
public:AddExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) : left(left), right(right) {}int interpret(const std::unordered_map<char, int>& context) override {return left->interpret(context) + right->interpret(context);}
};// 非终结符表达式(减法)
class SubtractExpression : public Expression {
private:std::shared_ptr<Expression> left, right;
public:SubtractExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) : left(left), right(right) {}int interpret(const std::unordered_map<char, int>& context) override {return left->interpret(context) - right->interpret(context);}
};// 客户端代码:解析并计算表达式
int main() {std::string expr = "a+b-c";std::unordered_map<char, int> context = {{'a', 5}, {'b', 3}, {'c', 2}};std::stack<std::shared_ptr<Expression>> stack;for (char token : expr) {if (isalpha(token)) {stack.push(std::make_shared<VariableExpression>(token));} else if (token == '+') {auto right = stack.top(); stack.pop();auto left = stack.top(); stack.pop();stack.push(std::make_shared<AddExpression>(left, right));} else if (token == '-') {auto right = stack.top(); stack.pop();auto left = stack.top(); stack.pop();stack.push(std::make_shared<SubtractExpression>(left, right));}}auto expression = stack.top();int result = expression->interpret(context);std::cout << "Result: " << result << std::endl;return 0;
}

布尔表达式解释器

布尔表达式解释器 -- 可以解析和计算包含与(AND)和或(OR)的布尔表达式。

#include <iostream>
#include <string>
#include <stack>
#include <memory>
#include <unordered_map>// 抽象表达式
class Expression {
public:virtual ~Expression() = default;virtual bool interpret(const std::unordered_map<std::string, bool>& context) = 0;
};// 终结符表达式(变量)
class VariableExpression : public Expression {
private:std::string name;
public:VariableExpression(const std::string& name) : name(name) {}bool interpret(const std::unordered_map<std::string, bool>& context) override {return context.at(name);}
};// 非终结符表达式(与操作)
class AndExpression : public Expression {
private:std::shared_ptr<Expression> left, right;
public:AndExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) : left(left), right(right) {}bool interpret(const std::unordered_map<std::string, bool>& context) override {return left->interpret(context) && right->interpret(context);}
};// 非终结符表达式(或操作)
class OrExpression : public Expression {
private:std::shared_ptr<Expression> left, right;
public:OrExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) : left(left), right(right) {}bool interpret(const std::unordered_map<std::string, bool>& context) override {return left->interpret(context) || right->interpret(context);}
};// 客户端代码:解析并计算布尔表达式
int main() {std::string expr = "a AND b OR c";std::unordered_map<std::string, bool> context = {{"a", true}, {"b", false}, {"c", true}};std::stack<std::shared_ptr<Expression>> stack;std::istringstream iss(expr);std::string token;while (iss >> token) {if (token == "a" || token == "b" || token == "c") {stack.push(std::make_shared<VariableExpression>(token));} else if (token == "AND") {auto right = stack.top(); stack.pop();auto left = stack.top(); stack.pop();stack.push(std::make_shared<AndExpression>(left, right));} else if (token == "OR") {auto right = stack.top(); stack.pop();auto left = stack.top(); stack.pop();stack.push(std::make_shared<OrExpression>(left, right));}}auto expression = stack.top();bool result = expression->interpret(context);std::cout << "Result: " << std::boolalpha << result << std::endl;return 0;
}

总结

解释器模式可以帮助我们定义和解释特定语言的语法规则,并将这些规则应用于不同的上下文。

http://www.yayakq.cn/news/124475/

相关文章:

  • 海洋cms做电影网站好做吗厚街找人做网站
  • 重庆网站建设mlfart个人工作室网页设计模板
  • 嘉定网站建设公司厚街外贸网站建设公司
  • 软件开发过程包括哪些阶段seo优化收费
  • 找人做一个小网站需要多少钱做网站栏目都包括什么
  • 官方网站下载微博卧龙区微网站开发
  • 无锡做网站小程序怎么上架商品
  • dw网站大学生代做百度seo课程
  • 深圳网页设计网站制作oa系统登录网址
  • 网站优化教程新手学做网站的书
  • 网站开发设计师的工作网站做优化一开始怎么做
  • vs做的网站如何使用淘宝客怎么做自己的网站
  • 跨境电商网站开发上海高端网站建设定制
  • 哪方面的网站成都园林设计公司推荐
  • 推广型网站建设公司刚刚突发1惊天大事
  • 南通网站制作设计wordpress 主题制作软件
  • 博罗营销网站制作上传 wordpress
  • 企业官网门户网站管理系统成都网站免费制作
  • 苏州建设企业网站微信关联网站
  • 响应式布局代码怎么写网站排名在哪里优化
  • wordpress多站点 用户同步广州网站外贸推广
  • 360网站点评crm客户管理系统排名
  • 书店网站开发男女做啊免费视频网站
  • 山东食品行业网站模板泰州seo公司
  • 公司网站建设管理意见刚做的网站怎么才能搜索到
  • 长治网站制作报价网页设计师简介
  • 双鸭山市建设局网站上海专业商城建设
  • 如何查看网站蜘蛛图片库网站建设
  • 苏州建网站公司选苏州聚尚网络中关村电脑报价官网
  • 百度做营销网站多少钱论坛网站有哪些