2013-10-17 5 views
1

BigInteger 클래스가없는 큰 정수 계산기에서 작업하고 있습니다. 양수와 음수를 나눌 때, 곱셈법 (작동)과 정확히 같은 else 문을 사용하더라도 음수는 반환하지 않습니다. 부울 값이 변경되지 않습니다.

내가 디버거를 통해 실행하고 I가 원하는 일을하지 않는 이유를 알아낼 수 없습니다.

감사

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를 적어도 하나 이상 비교합니다. 작동하지 않습니다. BigInt.value 또는 BigInt.toString()을 BigInt가 아닌 string과 비교해야합니다.

아마 당신은 (즉.한다)를 0으로 나누기를 제외하고 divide()의 특수 경우 생략 할 수 있습니다. 실제 분할 알고리즘을 작동시키는 데 집중하고, 대답 할 수 있어야하는 경우에는 무시해야합니다.

관련 문제