2013-10-17 76 views
1

我正在研究没有BigInteger类的大整数计算器。当我将正数和负数分开时,即使我使用EXACT same else语句和我的乘法方法(其工作原理),也不会返回负数。布尔值不变

我运行它通过调试器,似乎无法弄清楚为什么它没有做我想做的。这里是我的代码的一部分(在鸿沟的方法else语句就是应该将一正一负后返回一个负数):

感谢

public BigInt multiply(BigInt B2) { 
    BigInt result = new BigInt(); 
    BigInt zero = new BigInt("0"); 
    BigInt b; 

    for (int i = 0; i < B2.str.length(); ++i) { 
     b = singleDigitMultiply(
       B2.str.charAt(B2.str.length() - i - 1), i); 
     result = result.add(b); 
    } 

    // anything * 0 is 0 
    if (this.add(zero).toString().equals("0") || B2.add(zero).toString().equals("0") || 
      this.add(zero).toString().equals("-0") || B2.add(zero).toString().equals("-0")) 
     { 
      result.num.clear(); 
      result.num.add(0); 
     } 
    else if ((!this.isPositive && B2.isPositive) || 
      (this.isPositive && !B2.isPositive)) 
    { 
     //if not 0, assign negative when -a * b or a * -b 
     result.isPositive = false; 
    } 

    return result; 
} 

private BigInt singleDigitMultiply(char b, int baseFactor) { 
    StringBuffer tmp = new StringBuffer(""); 

    int carry = 0; 
    for (int i = 0; i < str.length(); ++i) 
    { 

     if (str.charAt(str.length() - i - 1) != '-' && str.charAt(str.length() - i - 1) 
      != '+' && b != '-' && b != '+') 
     { 
      int d = str.charAt(str.length() - i - 1) - '0'; 
      int r = d * (b - '0') + carry; 
      carry = r/10; 
      int digit = r % 10; 
      tmp.append(digit); 
     } 
    } 

    if (carry != 0) 
     tmp.append(carry); 

    String result = tmp.reverse().toString(); 
    // add enough zeros to the result 
    for (int i = 0; i < baseFactor; ++i) { 
     result += '0'; 
    } 


    return new BigInt(result); 
} 

public BigInt divide(BigInt B2) 
{ 
    BigInt result; 
    BigInt divisor = B2; 
    BigInt dividend = this; 

    divisor.isPositive = true; 
    dividend.isPositive = true; 


    if (divisor.toString().equals("0") || 
     divisor.toString().equals("+0") || 
     divisor.toString().equals("-0")) 
    { 
     System.out.println("CANNOT DIVIDE BY 0"); 
     //cannot divide by 0 
     result = new BigInt("NaN"); 
    } 
    else if (divisor.equals(dividend)) 
    { 
     //anything divided by self is 1 
     result = new BigInt("1"); 
    } 
    else if (dividend.equals("0")) 
    { 
     //0 divided by anything is 0 
     result = new BigInt("0"); 
    } 
    else 
    { 
     result = divideHelper(dividend, divisor); 
     if ((!this.isPositive && divisor.isPositive) || 
     (this.isPositive && !divisor.isPositive)) 
     { 
      //if not 0, assign negative when -a * b or a * -b 
      result.isPositive = false; 
     } 
    } 


    return result; 

} 

private BigInt divideHelper(BigInt dividend, BigInt divisor) 
{ 
    int size1 = dividend.num.size(), size2 = divisor.num.size(); 
    BigInt result = new BigInt(); 

    int first = size1 - 1, 
     second = size2 - 1, 
     three; 

    if (size1 == 1 && size2 == 1) { 
     three = dividend.num.get(first)/divisor.num.get(second); 
     result.num.add(0, three); 
    } 




    return result; 
} 
+0

如果我正确理解您的情况,您需要阅读有关溢出问题。 –

+0

我并不熟悉这一点。它与我的问题有什么关系? –

回答

0

divideHelper()似乎没有正确处理大多数情况下是分工。它仅在size1 == 1 && size2 == 1时才会实际执行任何操作,对于所有其他情况(大多数分区)似乎会返回未初始化的值。

它看起来不像一个工作部门或长期分裂算法对我来说。

此外,divide()中的“快捷方式比较”至少执行一个与字符串BigInt的比较equals() - 这将不起作用。您需要将BigInt.valueBigInt.toString()与字符串进行比较,而不是直接对BigInt进行比较。

也许你可以(即应该)免除divide()中的特殊情况,除零除外。集中精力使实际的分割算法发挥作用,并且不要试图绕过它,因为它应该能够回答。