4

この単純な電卓プログラムの switch/case ステートメントをジャンプ テーブルにするにはどうすればよいですか。

import java.lang.*;
import java.util.*;

public class Calculator
{
    private int solution;
    private static int x, y, ops;
    private char operators;

    public Calculator()
    {
        solution = 0;
    }

    public int addition(int x, int y)
    {
       return x + y;
    }
    public int subtraction(int x, int y)
    {
       return x - y;
    }
    public int multiplication(int x, int y)
    {
       return x * y;
    }
    public int division(int x, int y)
    {
       solution = x / y;
       return solution;
    }

    public void calc(int ops){
         Scanner operands = new Scanner(System.in);

         System.out.println("operand 1: ");
         x = operands.nextInt();
         System.out.println("operand 2: ");
         y = operands.nextInt();

         System.out.println("Solution: ");

         switch(ops)
         {
             case(1):
               System.out.println(addition(x, y));
               break;
             case(2):
               System.out.println(subtraction(x, y));
               break;
             case(3):
               System.out.println(multiplication(x, y));
               break;
             case(4):
               System.out.println(division(x, y));
               break;
          }
    }
    public static void main (String[] args)
    {
      System.out.println("What operation? ('+', '-', '*', '/')");
      System.out.println(" Enter 1 for Addition");
      System.out.println(" Enter 2 for Subtraction");
      System.out.println(" Enter 3 for Multiplication");
      System.out.println(" Enter 4 for Division");

      Scanner operation = new Scanner(System.in);
      ops = operation.nextInt();

      Calculator calc = new Calculator();
      calc.calc(ops);


  }
}

正直なところ、ジャンプ テーブルが何であるかは正確にはわかりません (オンラインで説明を見つけることができませんでした)。そのため、switch/case ステートメントとの違いはわかりません。

補足: このコードは整数のみを処理するため、5/3 を割ると 1 になります。浮動小数点数/倍精度数を取るように簡単に変更するにはどうすればよいですか。

4

4 に答える 4

0

ラムダをサポートする Java のバージョンを使用している場合、"ジャンプ テーブル" として実装するという要件により忠実なソリューションは、実際のジャンプ テーブルを使用します。オペランド。

これは、面倒な switch ステートメントをなくすだけでなく、より保守しやすく拡張しやすいコードを生成するための便利な方法です。Calculator の実装に変更を加えることなく、将来の新しいオペランドを後で簡単に追加できます。new 演算子とそのネーミング メソッドを実装し、それをジャンプ テーブルに追加するだけです。電卓は新しいオペランドを自動的にサポートします。

import com.google.common.collect.ImmutableMap;

import java.lang.*;
import java.util.*;

public class Calculator
{
    private static final Map<Integer,BinaryOperator<Integer>> evaluators = ImmutableMap.<Integer, BinaryOperator<Integer>>builder()
        .put(1, (Integer x, Integer y) -> new IntAddition().evaluateFor(x,y))
        .put(2, (Integer x, Integer y) -> new IntSubtraction().evaluateFor(x,y))
        .put(3, (Integer x, Integer y) -> new IntMultiplication().evaluateFor(x,y))
        .put(4, (Integer x, Integer y) -> new IntDivision().evaluateFor(x,y))
        .build();

    private static final Map<Integer,Nameable> names = ImmutableMap.<Integer, Nameable>builder()
        .put(1, () -> new IntAddition().getName())
        .put(2, () -> new IntSubtraction().getName())
        .put(3, () -> new IntMultiplication().getName())
        .put(4, () -> new IntDivision().getName())
        .build();

    private int solution;
    private static int x, y, ops;

    public Calculator()
    {
        solution = 0;
    }

    public void calc(int opcode)
    {
        Scanner operands = new Scanner(System.in);

        System.out.println("Enter operand 1: ");
        x = operands.nextInt();
        System.out.println("Enter operand 2: ");
        y = operands.nextInt();

        System.out.print("Solution: ");
        System.out.println(evaluators.get(opcode).evaluateFor(x, y));
    }

    public static void main(String[] args)
    {
        System.out.println("What operation?");
        for (Integer opcode : evaluators.keySet())
        {
            System.out.println(String.format(" Enter %d for %s", opcode, names.get(opcode).getName()));
        }

        Scanner operation = new Scanner(System.in);
        ops = operation.nextInt();

        Calculator calc = new Calculator();
        calc.calc(ops);
    }

    interface Nameable
    {
        String getName();
    }

    interface BinaryOperator<T>
    {
        T evaluateFor(T x, T y);
    }

    static class IntAddition implements BinaryOperator<Integer>, Nameable
    {
        IntAddition() { }

        public Integer evaluateFor(Integer x, Integer y)
        {
            return x + y;
        }

        public String getName()
        {
            return "Addition";
        }
    }
    static class IntSubtraction implements BinaryOperator<Integer>, Nameable
    {
        IntSubtraction() { }

        public Integer evaluateFor(Integer x, Integer y)
        {
            return x - y;
        }

        public String getName()
        {
            return "Subtraction";
        }
    }
    static class IntMultiplication implements BinaryOperator<Integer>, Nameable
    {
        IntMultiplication() { }

        public Integer evaluateFor(Integer x, Integer y)
        {
            return x * y;
        }

        public String getName()
        {
            return "Multiplication";
        }

    }
    static class IntDivision implements BinaryOperator<Integer>, Nameable
    {
        IntDivision() { }

        public Integer evaluateFor(Integer x, Integer y)
        {
            return x / y;
        }

        public String getName()
        {
            return "Division";
        }
    }
}
于 2019-02-15T22:10:09.833 に答える