0

我正在编写一个评估 Postfix 表达式的代码。数字非常大,所以我决定使用 BigInteger。我应该将两个数字存储到链接列表中并评估它们的总和,将结果存储在第三个列表中,然后显示答案。但是,我得到以下异常:java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

public static void pfEval(String exp)
{
    Stack s2 = new Stack();
    int i=0;
    BigInteger ans = BigInteger.valueOf(0);
    BigInteger op1 = BigInteger.valueOf(0);
    BigInteger op2 = BigInteger.valueOf(0);
    while(i<exp.length()-1)
    {
        BigInteger op = BigInteger.valueOf(0);
        if(exp.charAt(i) == ' ')
            i++;
        if((exp.charAt(i) >= '0')&&(exp.charAt(i) <= '9'))
        {
            while(exp.charAt(i) != ' ')
            {
                op = op.multiply(BigInteger.valueOf(10));
                op = op.add(BigInteger.valueOf(exp.charAt(i)-48));
                i++;
            }
            s2.push(op);
        }
        else
        {
            op1 = BigInteger.valueOf((long)s2.pop());
            op2 = BigInteger.valueOf((long)s2.pop());
            switch(exp.charAt(i))
            {
            case '+': ans = addition(op2, op1);
            break;
            }
            s2.push(ans);
        }
        i++;
    }
    System.out.println("Answer is "+s2.pop());
}

这是加法功能:

public static BigInteger addition(BigInteger op2, BigInteger op1)
{
    int i, j;
    BigInteger base = BigInteger.valueOf(1000000);
    BigInteger res = BigInteger.valueOf(0);
    LinkedList l1 = new LinkedList();
    LinkedList l2 = new LinkedList();
    LinkedList l3 = new LinkedList();
    while(!op1.equals(0))
    {
        l1.add(op1.mod(base));
        op1 = op1.divide(base);
    }
    while(!op2.equals(0))
    {
        l2.add(op2.mod(base));
        op2 = op2.divide(base);
    }
    if(l1.size()<l2.size())
    {
        for(i=0, j=0; i<l1.size(); i++, j++)
        {
            l3.add(((BigInteger)l1.get(i)).add((BigInteger)l2.get(i)));
        }
        while(j<l2.size())
        {
            l3.add(l2.get(i));
        }
    }
    else if(l1.size()>l2.size())
    {
        for(i=0, j=0; i<l2.size(); i++, j++)
        {
            l3.add(((BigInteger)l1.get(i)).add((BigInteger)l2.get(i)));
        }
        while(j<l1.size())
        {
            l3.add(l1.get(i));
        }
    }
    else if(l1.size()==l2.size())
    {
        for(i=0; i<l1.size(); i++)
        {
            l3.add(((BigInteger)l1.get(i)).add((BigInteger)l2.get(i)));                 }
    }
    for(i=0; i<l3.size(); i++)
    {
        res = res.add(((BigInteger)l3.get(i)).multiply(base.pow(i)));

    }
    return res;
}

我不明白问题是什么。谁能帮我?

4

3 回答 3

2

看看这一行:

(long)s2.pop()

然后,回过头来看看你把什么放在堆栈上s2.push()

看看你是否能找出问题所在:>

于 2013-09-27T04:12:34.900 回答
1

你应该casting如下,

op1 = (BigInteger) s2.pop();
op2 = (BigInteger) s2.pop();

Stacks2已经持有 BigInteger 类型的对象,因此您需要使用 type 对其进行强制转换BigInteger

我建议您在您的情况下使用通用集合,而不是像这样声明,

Stack s2 = new Stack();

您可以定义如下,

Stack<BigInteger> s2 = new Stack<BigInteger>();

这样可以避免不必要casting的。只需使用以下pop方法,

op1 = s2.pop();
op2 = s2.pop();
于 2013-09-27T04:16:06.613 回答
0

使堆栈通用

Stack<BigInteger> s2= new Stack<>();

并且不需要铸造

op1 = s2.pop();
于 2013-09-27T04:18:20.307 回答