<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      24.12.01

      實驗17:解釋器模式(選作)

      本次實驗屬于模仿型實驗,通過本次實驗學生將掌握以下內容:

      1、理解解釋器模式的動機,掌握該模式的結構;

      2、能夠利用解釋器模式解決實際問題。

      [實驗任務一]:解釋器模式

      某機器人控制程序包含一些簡單的英文指令,其文法規則如下:

      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. 源代碼:

      import java.util.ArrayList;

      import java.util.List;

      import java.util.StringTokenizer;

       

      // 抽象表達式

      interface Expression {

          String interpret();

      }

       

      // 方向表達式

      class Direction implements Expression {

          private String direction;

       

          public Direction(String direction) {

              this.direction = direction;

          }

       

          @Override

          public String interpret() {

              switch (direction.toLowerCase()) {

                  case "up":

                      return "向上";

                  case "down":

                      return "向下";

                  case "left":

                      return "向左";

                  case "right":

                      return "向右";

                  default:

                      return "未知方向";

              }

          }

      }

       

      // 動作表達式

      class Action implements Expression {

          private String action;

       

          public Action(String action) {

              this.action = action;

          }

       

          @Override

          public String interpret() {

              switch (action.toLowerCase()) {

                  case "move":

                      return "移動";

                  case "run":

                      return "跑動";

                  default:

                      return "未知動作";

              }

          }

      }

       

      // 距離表達式

      class Distance implements Expression {

          private int distance;

       

          public Distance(int distance) {

              this.distance = distance;

          }

       

          @Override

          public String interpret() {

              return distance + "個單位";

          }

      }

       

      // 組合表達式

      class CompositeExpression implements Expression {

          private Expression exp1;

          private Expression exp2;

       

          public CompositeExpression(Expression exp1, Expression exp2) {

              this.exp1 = exp1;

              this.exp2 = exp2;

          }

       

          @Override

          public String interpret() {

              return exp1.interpret() + "再" + exp2.interpret();

          }

      }

       

      // 基本表達式,包含方向、動作、距離

      class BasicExpression implements Expression {

          private Expression direction;

          private Expression action;

          private Expression distance;

       

          public BasicExpression(Expression direction, Expression action, Expression distance) {

              this.direction = direction;

              this.action = action;

              this.distance = distance;

          }

       

          @Override

          public String interpret() {

              return direction.interpret() + action.interpret() + distance.interpret();

          }

      }

       

      // 上下文類:負責解析指令

      class InstructionHandler {

          private Expression expression;

       

          public void parse(String instruction) {

              StringTokenizer tokenizer = new StringTokenizer(instruction);

              List<String> tokens = new ArrayList<>();

       

              while (tokenizer.hasMoreTokens()) {

                  tokens.add(tokenizer.nextToken());

              }

       

              // 解析第一部分指令

              Expression exp1 = new BasicExpression(new Direction(tokens.get(0)), new Action(tokens.get(1)), new Distance(Integer.parseInt(tokens.get(2))));

       

              // 檢查是否有 "and"

              if (tokens.size() > 3 && tokens.get(3).equalsIgnoreCase("and")) {

                  // 解析第二部分指令

                  Expression exp2 = new BasicExpression(new Direction(tokens.get(4)), new Action(tokens.get(5)), new Distance(Integer.parseInt(tokens.get(6))));

                  expression = new CompositeExpression(exp1, exp2);

              } else {

                  expression = exp1;

              }

          }

       

          public String output() {

              return expression.interpret();

          }

      }

       

      // 測試類

      public class Client{

          public static void main(String[] args) {

              InstructionHandler handler = new InstructionHandler();

       

              System.out.println("指令1:up move 5");

              handler.parse("up move 5");

              System.out.println("輸出:" + handler.output());

       

              System.out.println("\n指令2:down run 10 and left move 20");

              handler.parse("down run 10 and left move 20");

              System.out.println("輸出:" + handler.output());

          }

      }

      posted on 2024-12-01 15:29  Daniel350  閱讀(6)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 国产成人8X人网站视频| 真实国产老熟女无套中出| 日本午夜精品一区二区三区电影| 久久人人爽人人爽人人av| 亚洲av日韩av永久无码电影| 你懂的亚洲一区二区三区| 国产999久久高清免费观看| 亚洲精品久荜中文字幕| 国产福利精品一区二区| 深夜福利国产精品中文字幕| 在线A级毛片无码免费真人| 欧美人成精品网站播放| 国产一级老熟女自拍视频| 久热久热久热久热久热久热| 免费看欧美日韩一区二区三区| 草草浮力地址线路①屁屁影院 | 亚洲另类激情专区小说婷婷久| 国内精品久久久久影视| 影音先锋啪啪av资源网站| 久久人人97超碰国产精品| 人人澡人摸人人添| 亚洲国产精品成人av网| 无码国模国产在线观看免费| 日韩乱码人妻无码中文字幕视频| 亚洲人妻一区二区精品| 久久精品国产熟女亚洲av| 日韩av在线不卡一区二区三区| 九九热精品视频在线免费| 久久老熟女一区二区蜜臀| 97精品伊人久久久大香线蕉| 定州市| 午夜精品极品粉嫩国产尤物| 亚亚洲视频一区二区三区| 欧美区一区二区三区| 裸体美女无遮挡免费网站| 久久热这里这里只有精品| 亚洲天码中文字幕第一页| 免费国产好深啊好涨好硬视频| 日本高清一区免费中文视频| 亚洲乱妇老熟女爽到高潮的片| 推油少妇久久99久久99久久|