带有 BigInteger 的 BigInteger.pow()

发布于 2024-09-11 16:24:56 字数 2389 浏览 6 评论 0原文

当 BigInteger 大于 Integer.MAX_VALUE 时,我试图抛出异常。它不允许我为指数情况抛出异常。我不确定当 biginteger 值太大而无法传递到 BigInteger.pow() 方法时如何让它抛出异常。

提前致谢。

这是 toPostfix 方法:

public BigInteger evalPostfix(String postfix){
    BigInteger a, b;
    Stack stack = new Stack();

        for(int i=0; i<postfix.length(); i++){
            if(this.isOp(postfix.charAt(0)))
                throw new ArithmeticException("Malformed Postfix Expression");
            switch(postfix.charAt(i)){
                case '+':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.add(a));
                    break;
                case '-':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.subtract(a));
                    break;
                case '*':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.multiply(a));
                    break;
                case '/':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    if(a == BigInteger.valueOf(0)){
                        throw new ArithmeticException("Cannot divide by 0");
                    }else{
                        stack.push(b.divide(a));
                    }
                    break;
                case '%':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.mod(a));
                    break;
                case '^':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    if(b.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
                        throw new ArithmeticException("BigInteger value is too large");
                    stack.push(a.pow(b.intValue()));
                    break;
                default:
                    if(this.numbers.get(postfix.substring(i, i+1)) == null)
                        throw new NullPointerException(postfix.substring(i, i+1) + " is not mapped to any value");
                    stack.push(this.numbers.get(postfix.substring(i,i+1)));
            }
        }

    return (BigInteger)stack.pop();
}

I am tying to throw an exception when a BigInteger is greater than Integer.MAX_VALUE. It will not allow me to throw that exception for the exponent case. I am not sure how to get it to throw an exception when the biginteger value is too large to pass into the BigInteger.pow() method.

Thanks in advance.

here is the toPostfix method:

public BigInteger evalPostfix(String postfix){
    BigInteger a, b;
    Stack stack = new Stack();

        for(int i=0; i<postfix.length(); i++){
            if(this.isOp(postfix.charAt(0)))
                throw new ArithmeticException("Malformed Postfix Expression");
            switch(postfix.charAt(i)){
                case '+':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.add(a));
                    break;
                case '-':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.subtract(a));
                    break;
                case '*':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.multiply(a));
                    break;
                case '/':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    if(a == BigInteger.valueOf(0)){
                        throw new ArithmeticException("Cannot divide by 0");
                    }else{
                        stack.push(b.divide(a));
                    }
                    break;
                case '%':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.mod(a));
                    break;
                case '^':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    if(b.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
                        throw new ArithmeticException("BigInteger value is too large");
                    stack.push(a.pow(b.intValue()));
                    break;
                default:
                    if(this.numbers.get(postfix.substring(i, i+1)) == null)
                        throw new NullPointerException(postfix.substring(i, i+1) + " is not mapped to any value");
                    stack.push(this.numbers.get(postfix.substring(i,i+1)));
            }
        }

    return (BigInteger)stack.pop();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-09-18 16:24:56

按照其编写方式,如果指数大于 Integer.MAX_VALUE,则应抛出 ArithmeticException("Negative Exponent Error")。当你尝试时会发生什么?

The way it is written, it should throw ArithmeticException("Negative Exponent Error") if the exponent is greater than Integer.MAX_VALUE. What happens when you try it?

小ぇ时光︴ 2024-09-18 16:24:56

您以错误的顺序弹出堆栈。指数将位于堆栈顶部,而不是尾数下方。在减法、除法和模数中也有同样的问题,在加法和乘法中采用同样的方法也没有什么坏处。在任何情况下都应该是 b = stack.pop();然后 a = stack.pop()。如果将堆栈声明为 Stack stack = new Stack(),则不需要所有这些类型转换。

You're popping the stack in the wrong order. The exponent will be on top of the stack, not under the mantissa. You have the same problem in subtraction, division, and modulus, and it wouldn't hurt to do it the same way for addition and multiplication either. In every case it should be b = stack.pop(); then a = stack.pop(). And if you declare the stack as Stack stack = new Stack() you won' t need all those typecasts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文