关于此特定应用程序中 BigInteger 和 Lambda 使用的问题

Question regarding BigInteger and Lambda Usage in this specific application

BigInteger_Question

Sample_Output

给出如下界面class

interface BigOperation
{
    public BigInteger operation (Biglnteger x, Biglnteger y);
}

写Lambda表达式实现BigOperation模拟三种算术运算 / 和 % 在 Biglnteger 上。您可以分别称它们为 op1、op2 和 op3。请注意,三个 Biglnteger 中的运算分别称为乘法、除法和取余。 使用这三个操作,我们希望将 Biglnteger 中的所有数字相乘。例如,如果 Biglnteger为234,乘积为2 * 3 * 4 = 24。方法有如下方法标题:

static Biglnteger product (BigInteger n, BigOperation op1, BigOperation op2,
BigOperation op3)

使用两种方法比较此方法的实现:迭代和递归。 将 Lambda 表达式放在 main 方法中。构造几个 Biglnteger objects,调用 测试我们设计的方法。下面显示了一些示例输出:

Given big integer 8584803
(Iterative) e
(Recursive) 0
Given big integer 12345
(Iterative) 120
(Recursive) 120

我不知道如何处理附件中的代码。我想尝试使用 foreach 遍历 BigInteger N 但它似乎不起作用。接下来,我想做一个 for(int i = 0) 等循环,但我不知道如何获取对 BigInteger 变量的位置引用。我不知道如何为此使用 lambda。

提前感谢您回答我的问题。

import java.math.BigInteger;
import java.util.Random;

interface BigOperation{
    public BigInteger operation(BigInteger x,BigInteger y);
}

class LearningBigInteger {
    static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
    }

    public static void main(String[] args) {
        BigOperation multiply = BigInteger::multiply;
        BigOperation divide = BigInteger::divide;
        BigOperation remainder = BigInteger::remainder;
        Random r = new Random();
        BigInteger n = BigInteger.valueOf(Math.abs(r.nextInt()));
        BigInteger m = product(n,multiply,divide,remainder);
    }
}
class LearningBigInteger {
    // Iterable version
    static BigInteger product(BigInteger n, BigOperation... ops) {
        BigInteger temp = n;

        for (BigOperation op : ops) {
            // I only have temp to work it
            // so it is being processed
            // to itself, pointless i know!
            temp = op.operation(temp, temp);
        }

        return temp;
    }

    // Discrete version
    static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
        BigInteger temp = n;

        // This layout is good
        // if you keep the same temp
        // variable as input!
        temp = op1.operation(temp, temp);
        temp = op2.operation(temp, temp);
        temp = op3.operation(temp, temp);

        return temp;
    }
}

另外我无法打开您提供的链接。修复后通知我!

首先是代码。解释出现在代码之后。

import java.math.BigInteger;

public class LearningBigInteger {
    private static BigOperation multiply;
    private static BigOperation divide;
    private static BigOperation remainder;

    private static BigInteger digitProduct(BigInteger x) {
        if (x.compareTo(BigInteger.TEN) <= 0) {
            return x;
        }
        else {
            BigInteger r = remainder.operation(x, BigInteger.TEN);
            BigInteger newX = divide.operation(x, BigInteger.TEN);
            return multiply.operation(r, digitProduct(newX));
        }
    }

    public static void main(String[] args) {
        multiply = (x, y) -> x.multiply(y);
        divide = (x, y) -> x.divide(y);
        remainder = (x, y) -> x.remainder(y);
        BigInteger x = new BigInteger("8584803");
        BigInteger product = BigInteger.ONE;
        while (x.compareTo(BigInteger.TEN) >= 0) {
            BigInteger r = remainder.operation(x, BigInteger.TEN);
            product = multiply.operation(product, r);
            x = divide.operation(x, BigInteger.TEN);
        }
        System.out.println(product);
        System.out.println(digitProduct(new BigInteger("12345")));
    }
}

interface BigOperation {
    public BigInteger operation(BigInteger x,BigInteger y);
}

接口 BigOperation 被称为 功能接口 因为它只包含一个 [抽象] 方法。

lambda 表达式是一种 shorthand 编写函数式接口中唯一方法实现的方式。

lambda表达式的写法是先写方法的参数列表,然后是箭头符号,最后是方法体。语法根据方法参数的数量和方法主体中的行数略有不同。在上面的代码中,语法是针对一个有两个参数的方法,方法体只有一行。如果只有一个方法参数或方法主体中有多行,则语法会有所不同。

你问题中的代码使用method references which, I suppose, are a special form of lambda expression.

在上面的代码中,方法 main 包含迭代方法,而方法 digitProduct 实现递归方法。

试试这个。

interface BigOperation {
    BigInteger operation(BigInteger x, BigInteger y);
}

static class Iterative {
    static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
        BigInteger product = BigInteger.ONE;
        for ( ; x.compareTo(BigInteger.ZERO) > 0; x = op2.operation(x, BigInteger.TEN))
            product = op1.operation(product, op3.operation(x, BigInteger.TEN));
        return product;
    }
}

static class Recursive {
    static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
        if (x.compareTo(BigInteger.TEN) < 0)
            return x;
        else
            return op1.operation(op3.operation(x, BigInteger.TEN),
                product(op2.operation(x, BigInteger.TEN), op1, op2, op3));
    }
}

static void test(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
    System.out.println("Given bit integer " + x);
    System.out.println("(Iterative) " + Iterative.product(x, op1, op2, op3));
    System.out.println("(Recursive) " + Recursive.product(x, op1, op2, op3));
}

public static void main(String[] args) {
    BigOperation multiply = BigInteger::multiply;
    BigOperation divide = BigInteger::divide;
    BigOperation remainder = BigInteger::remainder;
    test(new BigInteger("8584803"), multiply, divide, remainder);
    test(new BigInteger("12345"), multiply, divide, remainder);
}

输出:

Given bit integer 8584803
(Iterative) 0
(Recursive) 0
Given bit integer 12345
(Iterative) 120
(Recursive) 120