标签:11 distance 13 String AbstractNode direction action public
某机器人控制程序包含一些简单的英文指令,其文法规则如下:
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个单位”。
实验要求:
1. 提交类图;
2. 提交源代码;
3. 注意编程规范。
- AbstractNode .java
package org.example;
import java.util.Stack;
abstract class AbstractNode { public abstract String interpret();
}
|
- ActionNode r.java
package org.example;
public class ActionNode extends AbstractNode{
private String action;
public ActionNode(String action) {
super();
this.action = action;
}
@Override
public String interpret() {
// TODO Auto-generated method stub
if(action.equals("move")){
return "移动";
}else if(action.equals("run")){
return "快速移动";
}else{
return "error";
}
}
}
|
- AndNode .java
package org.example;
public class AndNode extends AbstractNode {
private AbstractNode left;
private AbstractNode right;
@Override
public String interpret() {
// TODO Auto-generated method stub
return left.interpret() + "再" +right.interpret();
}
public AndNode(AbstractNode left, AbstractNode right) {
super();
this.left = left;
this.right = right;
}
}
|
- DirectionNode .java
package org.example;
public class DirectionNode extends AbstractNode{ private String direction;
public DirectionNode(String direction) { super(); this.direction = direction; } @Override public String interpret() { // TODO Auto-generated method stub if(direction.equals("up")){ return "向上"; }else if(direction.equals("down")){ return "向下"; }else if(direction.equals("left")){ return "向左"; }else if(direction.equals("right")){ return "向右"; }else{ return "error"; } } }
|
- DistanceNode .java
package org.example;
public class DistanceNode extends AbstractNode{ private String distance;
public DistanceNode(String dis) { super(); this.distance = dis; }
@Override public String interpret() { // TODO Auto-generated method stub return this.distance; }
}
|
- InstructionHandler .java
package org.example;
import java.util.Stack;
public class InstructionHandler { private AbstractNode node; public void handle(String instruction){ AbstractNode left = null; AbstractNode right = null; AbstractNode direction = null; AbstractNode action = null; AbstractNode distance = null; Stack<AbstractNode> stack = new Stack<>(); String [] words = instruction.split(" "); for (int i = 0; i < words.length; i++) { if(words[i].equalsIgnoreCase("and")){ left = (AbstractNode)stack.pop(); String dir = words[++i]; direction = new DirectionNode(dir); String a = words[++i]; action = new ActionNode(a); String dis = words[++i]; distance = new DistanceNode(dis); right = new SentenceNode(direction, action, distance); stack.push(new AndNode(left, right)); }else{ String dir = words[i]; direction = new DirectionNode(dir); String a = words[++i]; action = new ActionNode(a); String dis = words[++i]; distance = new DistanceNode(dis); left = new SentenceNode(direction, action, distance); stack.push(left);
} } this.node = (AbstractNode)stack.pop(); } public String output(){ String result = node.interpret(); return result; }
}
|
- SentenceNode .java
package org.example;
public class SentenceNode extends AbstractNode{ private AbstractNode direction; private AbstractNode action; private AbstractNode distance; public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance) { super(); this.direction = direction; this.action = action; this.distance = distance; } @Override public String interpret() { // TODO Auto-generated method stub return direction.interpret()+action.interpret()+distance.interpret(); }
}
|
标签:11,
distance,
13,
String,
AbstractNode,
direction,
action,
public
From: https://www.cnblogs.com/gyg1222/p/18543269