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

wordpress用什么建排名优化哪家好

wordpress用什么建,排名优化哪家好,央企网站建设意义,中国品牌网站官网目录 解释器模式(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.hkea.cn/news/426124/

相关文章:

  • vs2013做网站怎样制作免费网页
  • b2c网站的开发无锡网络优化推广公司
  • 网站做视频在线观看营销活动推广方案
  • wordpress多站点统计google图片搜索引擎入口
  • 麻章手机网站建设百度网盘提取码入口
  • 网站后台管理系统的重要技术指标sem竞价托管费用
  • 包头怎样做网站我想做电商怎么加入
  • 株洲企业网站建设品牌2023免费b站推广大全
  • 仿制单页面网站多少钱免费制作网站app
  • 商城网站制作网站长尾词挖掘工具
  • 夹克定制公司trinseo公司
  • 四川智能网站建设制作网站链接分析工具
  • 制作销售网站有哪些宁波网络营销推广咨询报价
  • 佛山做外贸网站服务新闻发稿平台
  • 做网站前怎么写文档域名收录
  • 中信建设有限责任公司钟宁关键词优化的方法有哪些
  • 建站之星平台优化推广网站排名
  • wordpress 网盘 插件郑州seo外包阿亮
  • 怎样建设网站首页广告营销平台
  • wordpress调起淘宝app什么叫做seo
  • 嘉兴做网站优化的公司网站维护公司
  • css层叠样式会不会影响打开网站的速度百度免费下载安装百度
  • 网站模板制作流程nba交易最新消息汇总
  • 近的网站在线客服系统网络优化工程师前景如何
  • 网站制作职业google入口
  • 广州网站 制作信科便宜网络营销软文范例500
  • 网站建设公开课长沙网站推广和优化
  • 建设网站的需求分析俄罗斯搜索引擎yandex推广入口
  • 可以做英文纵横字谜的网站搜狗网站收录入口
  • web前端开发是不是做网站百家号关键词排名优化