Java/C++实现解释器模式---机器人控制程序
某机器人控制程序包含一些简单的英文指令,其文法规则如下:
expression ::= direction action distance | composite
composite ::= expression and expression
direction ::= ‘up’ | ‘down’ | ‘left’ | ‘right’
action ::= ‘move’ | ‘run’
distance ::= an integer //一个整数值
如输入:up move 5,则输出“向上移动5个单位”;输入:down run 10 and left move 20,则输出“向下移动10个单位再向左移动20个单位”。
类图:

Java代码:
import java.util.Stack;
public abstract class AbstractNode {
public abstract String interpret();
} public class ActionNode extends AbstractNode{
private String action; public ActionNode(String action) {
this.action = action;
} //动作(移动方式)表达式的解释操作
public String interpret() {
if (action.equalsIgnoreCase("move")) {
return "移动";
}
else if (action.equalsIgnoreCase("run")) {
return "快速移动";
}
else {
return "无效指令";
}
}
} public class AndNode extends AbstractNode{
private AbstractNode left; //And的左表达式
private AbstractNode right; //And的右表达式 public AndNode(AbstractNode left, AbstractNode right) {
this.left = left;
this.right = right;
} //And表达式解释操作
public String interpret() {
return left.interpret() + "再" + right.interpret();
}
} public class DirectionNode extends AbstractNode{
private String direction; public DirectionNode(String direction) {
this.direction = direction;
} //方向表达式的解释操作
public String interpret() {
if (direction.equalsIgnoreCase("up")) {
return "向上";
}
else if (direction.equalsIgnoreCase("down")) {
return "向下";
}
else if (direction.equalsIgnoreCase("left")) {
return "向左";
}
else if (direction.equalsIgnoreCase("right")) {
return "向右";
}
else {
return "无效指令";
}
}
} public class DistanceNode extends AbstractNode{
private String distance; public DistanceNode(String distance) {
this.distance = distance;
} //距离表达式的解释操作
public String interpret() {
return this.distance;
}
} public class InstructionHandler {
private String instruction;
private AbstractNode node;
public void handle(String instruction) {
AbstractNode left = null, right = null;
AbstractNode direction = null, action = null, distance = null;
Stack stack = new Stack(); //声明一个栈对象用于存储抽象语法树
String[] words = instruction.split(" "); //以空格分隔指令字符串
for (int i = 0; i < words.length; i++) {
//本实例采用栈的方式来处理指令,如果遇到“and”,则将其后的三个单词作为三个终结符表达式连成一个简单句子SentenceNode作为“and”的右表达式,而将从栈顶弹出的表达式作为“and”的左表达式,最后将新的“and”表达式压入栈中。
if (words[i].equalsIgnoreCase("and")) {
left = (AbstractNode)stack.pop(); //弹出栈顶表达式作为左表达式
String word1= words[++i];
direction = new DirectionNode(word1);
String word2 = words[++i];
action = new ActionNode(word2);
String word3 = words[++i];
distance = new DistanceNode(word3);
right = new SentenceNode(direction,action,distance); //右表达式
stack.push(new AndNode(left,right)); //将新表达式压入栈中
}
//如果是从头开始进行解释,则将前三个单词组成一个简单句子SentenceNode并将该句子压入栈中
else {
String word1 = words[i];
direction = new DirectionNode(word1);
String word2 = words[++i];
action = new ActionNode(word2);
String word3 = words[++i];
distance = new DistanceNode(word3);
left = new SentenceNode(direction,action,distance);
stack.push(left); //将新表达式压入栈中
}
}
this.node = (AbstractNode)stack.pop(); //将全部表达式从栈中弹出
} public String output() {
String result = node.interpret(); //解释表达式
return result;
}
} public class SentenceNode extends AbstractNode{
private AbstractNode direction;
private AbstractNode action;
private AbstractNode distance; public SentenceNode(AbstractNode direction,AbstractNode action,AbstractNode distance) {
this.direction = direction;
this.action = action;
this.distance = distance;
} //简单句子的解释操作
public String interpret() {
return direction.interpret() + action.interpret() + distance.interpret();
}
} public class Client { public static void main(String[] args) {
// TODO Auto-generated method stub
String instruction1 = "up move 5 and down run 10 and left move 5";
String instruction2="down run 10 and left move 20";
InstructionHandler handler = new InstructionHandler();
handler.handle(instruction1);
String outString;
outString = handler.output();
System.out.println(outString);
handler.handle(instruction2);
outString = handler.output();
System.out.println(outString);
} }
C++代码:
#include<iostream>
#include<stack>
#include <sstream>
#include<string>
using namespace std;
class AbstractNode {
public:
virtual string interpret()=0;
};
class ActionNode:public AbstractNode{
private:
string action;
public:
ActionNode(string action) {
this->action = action;
}
string interpret() {
if (action=="move") {
return "移动";
}
else if (action=="run") {
return "快速移动";
}
else {
return "无效指令";
}
}
};
class AndNode:public AbstractNode{
private:
AbstractNode *left; //And的左表达式
AbstractNode *right; //And的右表达式
public:
AndNode(AbstractNode *left, AbstractNode *right) {
this->left = left;
this->right = right;
}
//And表达式解释操作
string interpret() {
return left->interpret() + "再" + right->interpret();
}
};
class DirectionNode :public AbstractNode{
private:
string direction;
public:
DirectionNode(string direction) {
this->direction = direction;
}
//方向表达式的解释操作
string interpret() {
if (direction=="up") {
return "向上";
}
else if (direction=="down") {
return "向下";
}
else if (direction=="left") {
return "向左";
}
else if (direction=="right") {
return "向右";
}
else {
return "无效指令";
}
}
};
class DistanceNode:public AbstractNode{
private:
string distance;
public:
DistanceNode(string distance) {
this->distance = distance;
}
//距离表达式的解释操作
string interpret() {
return this->distance;
}
};
class SentenceNode:public AbstractNode{
private:
AbstractNode *direction;
AbstractNode *action;
AbstractNode *distance; public:
SentenceNode(AbstractNode *direction,AbstractNode *action,AbstractNode *distance) {
this->direction = direction;
this->action = action;
this->distance = distance;
}
//简单句子的解释操作
string interpret() {
return direction->interpret() + action->interpret() + distance->interpret();
}
};
class InstructionHandler {
private:
string instruction;
AbstractNode *node;
public:
void handle(string instruction) {
AbstractNode *left = NULL, *right = NULL;
AbstractNode *direction = NULL, *action = NULL, *distance = NULL;
stack<AbstractNode*> stack; //声明一个栈对象用于存储抽象语法树
istringstream str1(instruction);
istringstream str2(instruction);
string out;
int j=0,k=0;
int n;
//以空格分隔指令字符串
while (str1 >> out) {
j++;
}
n=j;
string words[n];
string out2;
while (str2 >> out2) {
words[k]=out2;
k++;
}
for (int i = 0; i <n; i++) {
//本实例采用栈的方式来处理指令,如果遇到“and”,则将其后的三个单词作为三个终结符表达式连成一个简单句子SentenceNode作为“and”的右表达式,而将从栈顶弹出的表达式作为“and”的左表达式,最后将新的“and”表达式压入栈中。
if (words[i]=="and") {
left = stack.top(); //弹出栈顶表达式作为左表达式
stack.pop();
string word1= words[++i];
direction = new DirectionNode(word1);
string word2 = words[++i];
action = new ActionNode(word2);
string word3 = words[++i];
distance = new DistanceNode(word3);
right = new SentenceNode(direction,action,distance); //右表达式
stack.push(new AndNode(left,right)); //将新表达式压入栈中
}
//如果是从头开始进行解释,则将前三个单词组成一个简单句子SentenceNode并将该句子压入栈中
else {
string word1 = words[i];
direction = new DirectionNode(word1);
string word2 = words[++i];
action = new ActionNode(word2);
string word3 = words[++i];
distance = new DistanceNode(word3);
left = new SentenceNode(direction,action,distance);
stack.push(left); //将新表达式压入栈中
}
}
this->node = stack.top(); //将全部表达式从栈中弹出
stack.pop();
} string output() {
string result = node->interpret(); //解释表达式
return result;
}
};
int main(){
string instruction1 = "up move 5 and down run 10 and left move 5";
string instruction2="down run 10 and left move 20";
InstructionHandler *handler = new InstructionHandler();
handler->handle(instruction1);
string outString;
outString = handler->output();
cout<<outString<<endl;
handler->handle(instruction2);
outString = handler->output();
cout<<outString<<endl;
}
运行结果:


Java/C++实现解释器模式---机器人控制程序的更多相关文章
- JAVA设计模式之解释器模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述解释器(Interpreter)模式的: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个 ...
- 折腾Java设计模式之解释器模式
解释器模式 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 意图 给定一个语言,定义它的文法表 ...
- 20.java设计模式之解释器模式
基本需求 实现四则运算,如计算a+b-c+d的值 先输入表达式的形式,如a+b-c+d,要求表达式正确 再分别输出a,b,c,d的值 最后求出结果 传统方案 编写一个方法,接收表达式的形式,根据用户输 ...
- 简单的介绍一下Java设计模式:解释器模式
目录 定义 意图 主要解决问题 优缺点 结构 示例 适用情况 定义 解释器模式是类的行为型模式,给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器,客户端可以使用这个解释器来 ...
- JAVA 设计模式 解释器模式
用途 解释器模式 (Interpreter) 定义一个语言,定义它的文法的一种表示. 并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 解释器模式是一种行为型模式. 结构
- Java进阶篇设计模式之九----- 解释器模式和迭代器模式
前言 在上一篇中我们学习了行为型模式的责任链模式(Chain of Responsibility Pattern)和命令模式(Command Pattern).本篇则来学习下行为型模式的两个模式, 解 ...
- 设计模式之解释器模式——Java语言描述
解释器模式提供了评估语言的语法或表达式的方式,它属于行为型模式.这种模式实现了一个表达式接口,该接口解释一个特定的上下文.这种模式被用在SQL解析.符号处理引擎等 介绍 意图 给定一个语言,定义它的文 ...
- Java设计模式学习记录-解释器模式
前言 这次介绍另一个行为模式,解释器模式,都说解释器模式用的少,其实只是我们在日常的开发中用的少,但是一些开源框架中还是能见到它的影子,例如:spring的spEL表达式在解析时就用到了解释器模式,以 ...
- Java设计模式之九 ----- 解释器模式和迭代器模式
前言 在上一篇中我们学习了行为型模式的责任链模式(Chain of Responsibility Pattern)和命令模式(Command Pattern).本篇则来学习下行为型模式的两个模式, 解 ...
随机推荐
- 联邦学习:按混合分布划分Non-IID样本
我们在博文<联邦学习:按病态独立同分布划分Non-IID样本>中学习了联邦学习开山论文[1]中按照病态独立同分布(Pathological Non-IID)划分样本. 在上一篇博文< ...
- tp5 商品模型的添加展示
路由 //商品模型展示的路由 Route::get('type','/pyg/good/listType'); //将type_id传送至/pyg/good/addType的路由 Route::get ...
- 从零开始,开发一个 Web Office 套件(11):支持中文输入法(or 其它使用输入法的语言)
这是一个系列博客,最终目的是要做一个基于 HTML Canvas 的.类似于微软 Office 的 Web Office 套件(包括:文档.表格.幻灯片--等等). 博客园:<从零开始, 开发一 ...
- 安全学习笔记-web安全之XSS攻击
web安全之XSS攻击 XSS 即跨站脚本攻击,是 OWASP TOP10 之一.它的全称为 Cross-site scripting,因为 CSS 这个简称已经被占用表示为前端三剑客之一的CSS,所 ...
- vue js计算精度问题处理,兼容XP系统
js计算精度问题(浮点数误差,大数计算出错) JavaScript 运算时经常遇到会 0.000000001 和 0.999999999 这样奇怪的结果. 网上教程一大篇,原理请百度,抄作业往下看!! ...
- java 知识点梳理
1.ArrayList与linkedList 区别 ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦; 优点是查找比较快. Lin ...
- 使用数据库、Redis、ZK分别实现分布式锁!
分布式锁三种实现方式: 基于数据库实现分布式锁: 基于缓存(Redis等)实现分布式锁: 基于Zookeeper实现分布式锁: 基于数据库实现分布式锁 悲观锁 利用select - where - f ...
- MySQL二进制binlog日志说明以及利用binlog日志恢复数据
MySQL的binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全量备份+binlog日志恢复增量数据部分). 一.关于 ...
- vue学习过程总结(06) - vue的数据存储store
这个不知道能怎么叫不?现在对这块很迷.以下为个人理解 store是状态管理,是一个对象,有其属性和方法. 常见的值有:state/mutations/actions/getters, 这几个值的意思: ...
- SpringCloudAlibaba 微服务讲解(四)Sentinel--服务容错(一)
4.1 高并发带来的问题 在微服务中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用,但是由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现网 ...