po3a  

[实验任务一]:解释器模式

某机器人控制程序包含一些简单的英文指令,其文法规则如下:

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. 注意编程规范。

 

1.类图

 2.源代码

复制代码
//Expression.java
// 抽象表达式类
abstract class Expression {
    public abstract void interpret();
}

//CompositeExpression.java
// 组合表达式类
import java.util.ArrayList;
import java.util.List;

class CompositeExpression extends Expression {
    private List<Expression> expressions = new ArrayList<>();

    public void add(Expression expression) {
        expressions.add(expression);
    }

    @Override
    public void interpret() {
        for (Expression expression : expressions) {
            expression.interpret();
        }
    }
}

//Command.java
// 命令类
abstract class Command extends Expression {
    protected String direction;
    protected String action;
    protected int distance;

    public Command(String direction, String action, int distance) {
        this.direction = direction;
        this.action = action;
        this.distance = distance;
    }
}

//DirectionCommand.java
// 方向命令类
class DirectionCommand extends Command {
    public DirectionCommand(String direction, String action, int distance) {
        super(direction, action, distance);
    }

    @Override
    public void interpret() {
        System.out.println("向" + direction + " " + action + " " + distance + "个单位");
    }
}

//AndCommand.java
// 逻辑与命令类
class AndCommand extends CompositeExpression {
    private Expression left;
    private Expression right;

    public AndCommand(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public void interpret() {
        left.interpret();
        right.interpret();
    }
}

//Interpreter.java
// 解释器类
class Interpreter {
    public static void interpret(String command) {
        String[] tokens = command.split(" ");
        Expression expression = parse(tokens);
        if (expression != null) {
            expression.interpret();
        }
    }

    private static Expression parse(String[] tokens) {
        if (tokens.length == 3) {
            return new DirectionCommand(tokens[0], tokens[1], Integer.parseInt(tokens[2]));
        } else if (tokens.length > 3 && "and".equals(tokens[tokens.length - 3])) {
            Expression left = parse(Arrays.copyOfRange(tokens, 0, tokens.length - 3));
            Expression right = parse(Arrays.copyOfRange(tokens, tokens.length - 2, tokens.length));
            return new AndCommand(left, right);
        }
        return null;
    }
}

//Main.java
// 测试类
public class Main {
    public static void main(String[] args) {
        Interpreter.interpret("up move 5");
        Interpreter.interpret("down run 10 and left move 20");
    }
}
复制代码

 

posted on   po3a  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
 
点击右上角即可分享
微信分享提示