11月11日

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

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

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. 类图:

 

 

  1. 源代码:

package org.example;

import java.util.ArrayList;
import java.util.List;

// 表达式接口
interface Expression {
    void interpret();
}

// 上下文环境类
class Context {
    private String input;
    private int position;

    public Context(String input) {
        this.input = input;
        this.position = 0;
    }

    public String getNextToken() {
        StringBuilder token = new StringBuilder();
        while (position < input.length() && Character.isWhitespace(input.charAt(position))) {
            position++;
        }
        while (position < input.length() && !Character.isWhitespace(input.charAt(position))) {
            token.append(input.charAt(position++));
        }
        if( token.toString().equals("and")){
            token= new StringBuilder();
            while (position < input.length() && Character.isWhitespace(input.charAt(position)))
                position++;
            while (position < input.length() && !Character.isWhitespace(input.charAt(position))) {
                token.append(input.charAt(position++));
            }
        }
        return token.toString().trim();
    }

    public boolean hasMoreInput() {
        return position < input.length();
    }

    public boolean isNextAnd() {
        return position + 3 < input.length() && input.substring(position, position + 3).equals("and");
    }

    public void skipAnd() {
        if (isNextAnd()) {
            position += 3; // 跳过 "and"
            while (position < input.length() && Character.isWhitespace(input.charAt(position))) {
                position++;
            }
        }
    }
}

// 方向表达式类
class DirectionExpression implements Expression {
    private String direction;
    private String action;
    private String distance;

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

    @Override
    public void interpret() {
        switch (direction) {
            case "up":
                System.out.print("向上");
                break;
            case "down":
                System.out.print("向下");
                break;
            case "left":
                System.out.print("向左");
                break;
            case "right":
                System.out.print("向右");
                break;
        }
        switch (action) {
            case "move":
                System.out.print("移动");
                break;
            case "run":
                System.out.print("跑动");
                break;
        }
        System.out.print(distance + "个单位");
    }
}

// 复合表达式类
class CompositeExpression implements Expression {
    private List<Expression> expressions = new ArrayList<>();

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

    @Override
    public void interpret() {
        for (int i = 0; i < expressions.size(); i++) {
            expressions.get(i).interpret();
            if (i < expressions.size() - 1) {
                System.out.print(" 和 ");
            }
        }
    }
}

// 解释器类
class Interpreter {
    public void interpret(String command) {
        Context context = new Context(command);
        CompositeExpression composite = new CompositeExpression();

        while (context.hasMoreInput()) {
            String direction = context.getNextToken();
            if (isDirection(direction)) {
                String action = context.getNextToken();
                String distance = context.getNextToken();
                if (action.isEmpty() || distance.isEmpty()) {
                    System.out.print("无效的命令");
                    return;
                }
                Expression directionExpression = new DirectionExpression(direction, action, distance);
                composite.addExpression(directionExpression);
            } else {
                System.out.print("无效的命令");
                return;
            }
            if (context.hasMoreInput() && context.isNextAnd()) {
                context.skipAnd();
            }
        }

        composite.interpret();
    }

    private boolean isDirection(String direction) {
        return direction.equals("up") || direction.equals("down") || direction.equals("left") || direction.equals("right");
    }
}

// 客户端测试代码
public class InterpreterPatternDemo {
    public static void main(String[] args) {
        Interpreter interpreter = new Interpreter();
Scanner scanner = new Scanner(System.in);

System.out.println("请输入命令(输入'exit'退出):");
while (true) {
    String command = scanner.nextLine();
    if ("exit".equalsIgnoreCase(command)) {
        break;
    }
    interpreter.interpret(command);
    System.out.println();
}

scanner.close();


    }
}

 

  1. 运行截图:

 

 

 

posted @ 2024-11-11 22:30  序章0  阅读(9)  评论(0编辑  收藏  举报