【问题标题】:How to get arithmetical operators at runtime?如何在运行时获取算术运算符?
【发布时间】:2023-03-09 22:58:01
【问题描述】:

如何在 Java 运行时获取算术运算符?假设我有价值观

  • ADD应该加号
  • MUL 那么它应该乘以这个数字

举例

  public calculate(int x, String str){
   while(str.equals("some value")){
     If( str.equals("ADD"))
        // it should return me something like x+
     if( str.equals("MUL"))
        it return me something like x*
    }
    if( str.equals("FINAL"))
        it should return me x+x*x

  }

【问题讨论】:

  • 我投票决定关闭,因为 IMO 过于本地化。
  • 我不明白你的问题,你已经用伪代码提供了解决方案。只需执行return x * x; 并将您的方法签名更改为public int calculate(...)
  • 您好,我修改了我的问题..
  • 我还是不明白。你说的“x* 之类的东西”是什么确切?下面提供的 2 个答案有什么问题?
  • 我看不出有什么理由关闭这个问题。 Stackoverflow 用户,请不要滥用您的信誉积分和您的权利。

标签: java arithmetic-expressions


【解决方案1】:

您需要的不是运行时元编程,而是一流的函数。

以下代表一等函数,分别为 1 和 2。

abstract class UnaryFunction<A, B> {
  public abstract B apply(A a);
}

abstract class BinaryFunction<A, B, C> {
  public abstract C apply(A a, B b);
}

为简单起见,让我们使用上述类的特殊版本。

abstract class UnaryOperation {
  public abstract int apply(int a);
}

abstract class BinaryOperation {
  public abstract int apply(int a, int b);
}

现在构建所需算术运算的字典。

Map<String, BinaryOperation> ops = new HashMap<String, BinaryOperation>();
ops.put("ADD", new BinaryOperation() {
  public int apply(int a, int b) {
    return a + b;
  }
});
ops.put("MUL", new BinaryOperation() {
  public int apply(int a, int b) {
    return a * b;
  }
});
// etc.

添加一种在一个参数上部分应用BinaryOperation 的方法。

abstract class BinaryOperation {
  public abstract int apply(int a, int b);

  public UnaryOperation partial(final int a) {
    return new UnaryOperation() {
      public int apply(int b) {
        return BinaryOperation.this.apply(a, b);
      }
    };
  }
}

现在我们可以编写你的calculate 方法了。

public UnaryOperation calculate(int x, String opString) {
  BinaryOperation op = ops.get(opString);
  if(op == null)
    throw new RuntimeException("Operation not found.");
  else
    return op.partial(x);
}

用途:

UnaryOperation f = calculate(3, "ADD");
f.apply(5); // returns 8

UnaryOperation g = calculate(9, "MUL");
f.apply(11); // returns 99

上述解决方案中使用的抽象,即第一类函数接口和部分应用程序,均在this库中提供。

【讨论】:

  • 在检查the edit history of the question 之后,我想我可能完全错过了问题的重点。 OP 需要付出更多努力来解释他的情况。
  • 非常感谢 missingfaktor 这正是我所需要的 :)
【解决方案2】:
public class Calculator {
    public static enum Operation {ADD, MUL, SUB, DIV};
    private int x; // store value from previous operations

    public void calculate(int x, Operation operation) {
        switch(operation) {
        case ADD:
            this.x += x;
            break;
        case MUL:
            this.x *= x;
            break;
        case SUB:
            this.x -= x;
            break;
        case DIV:
            this.x /= x;
            break;
        }
    }

    public int getResult() {
        return this.x;
    }
}

要在代码的其他地方使用它:

public static void main(String[] args) {
    Calculator c = new Calculator();
    c.calculate(4, Calculator.Operation.ADD);
    // Other operations
    c.getResult(); // get final result
}

【讨论】:

    【解决方案3】:

    假设您只是尝试将 x 相加和相乘,只需执行以下操作:

    public int calculate(int x, String str) {
        // while(true) is gonna get you into some trouble
        if( str.equals("ADD")) {
            return x + x;
        }
        else if( str.equals("MUL")) {
            return x * x;
        }
        else
            return x; // not sure what you want to do in this case
    }
    

    【讨论】:

    • 嗨 TreeBranch,感谢您的回复我修改了我的问题我认为有些混乱
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2013-04-16
    相关资源
    最近更新 更多