当乘以无限整数java时,整数0的问题

时间:2015-03-01 20:45:18

标签: java integer multiplication infinite

我的程序有一个非常奇怪的问题,每当我将负数乘以0时,答案显示为“-0”。无论0是操作中的第一个还是第二个数,都会发生这种情况。例如,如果我要乘以-500和0,那么-0就是出现的答案。我不确定为什么会这样做。

public InfiniteIntegerInterface multiply(final InfiniteIntegerInterface anInfiniteInteger)
    {
            // TO DO
        int place1 = 0;
        int place2 = 0;
        LInfiniteInteger tempFirstOp = new LInfiniteInteger(this.toString());
        LInfiniteInteger tempSecondOp = new LInfiniteInteger(anInfiniteInteger.toString());
        LInfiniteInteger firstOperand = new LInfiniteInteger("");
        LInfiniteInteger secondOperand = new LInfiniteInteger("");
        LInfiniteInteger result = new LInfiniteInteger("");
        LInfiniteInteger tempResult = new LInfiniteInteger("");
        String tempResultString = "";
        Integer tempResultInt = new Integer(0);
        if((tempFirstOp.compareTo(tempSecondOp) == 1) || (tempFirstOp.compareTo(tempSecondOp) == 0))
        {
            firstOperand = tempFirstOp;
            secondOperand = tempSecondOp;
        }
        else
        {
            firstOperand = tempSecondOp;
            secondOperand = tempFirstOp;
        }

        Node currentTwo = secondOperand.lastNode;
    if ((firstOperand.isNegative == true) && (secondOperand.isNegative == false)) {
        while(currentTwo != null)
        {
            place1 = 0;
            Node currentOne = firstOperand.lastNode;
            while(currentOne != null)
            {
                tempResultInt = currentOne.data * currentTwo.data;
                tempResultString = tempResultInt.toString();
                for(int i = 0; i < (place1 + place2); i++)
                {
                    tempResultString = tempResultString + "0";
                }
                tempResult = new LInfiniteInteger(tempResultString);
                result = new LInfiniteInteger(result.plus(tempResult).toString());
                place1++;
                currentOne = currentOne.previous;
            }
            place2++;
            currentTwo = currentTwo.previous;
        }
     result.isNegative = true;
        return result; 
    }  
    if ((firstOperand.isNegative == false) && (secondOperand.isNegative == true)) {
        while(currentTwo != null)
        {
            place1 = 0;
            Node currentOne = firstOperand.lastNode;
            while(currentOne != null)
            {
                tempResultInt = currentOne.data * currentTwo.data;
                tempResultString = tempResultInt.toString();
                for(int i = 0; i < (place1 + place2); i++)
                {
                    tempResultString = tempResultString + "0";
                }
                tempResult = new LInfiniteInteger(tempResultString);
                result = new LInfiniteInteger(result.plus(tempResult).toString());
                place1++;
                currentOne = currentOne.previous;
            }
            place2++;
            currentTwo = currentTwo.previous;
        }
     result.isNegative = true;
        return result; 
    }   

    else {
        while(currentTwo != null)
        {
            place1 = 0;
            Node currentOne = firstOperand.lastNode;
            while(currentOne != null)
            {
                tempResultInt = currentOne.data * currentTwo.data;
                tempResultString = tempResultInt.toString();
                for(int i = 0; i < (place1 + place2); i++)
                {
                    tempResultString = tempResultString + "0";
                }
                tempResult = new LInfiniteInteger(tempResultString);
                result = new LInfiniteInteger(result.plus(tempResult).toString());
                place1++;
                currentOne = currentOne.previous;
            }
            place2++;
            currentTwo = currentTwo.previous;
        }

        return result; 
    }   


  }

0 个答案:

没有答案