首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有BigInteger的BigInteger.pow()

带有BigInteger的BigInteger.pow()
EN

Stack Overflow用户
提问于 2010-07-28 11:44:10
回答 2查看 533关注 0票数 0

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

提前谢谢。

下面是toPostfix方法:

代码语言:javascript
复制
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();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-28 11:47:59

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

票数 0
EN

Stack Overflow用户

发布于 2010-07-28 12:48:57

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3349766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档