2012-03-25 48 views
0

我正在研究需要計算2個大整數的總和而不使用java中的biginteger類的程序。我卡在我的for循環計算總和。我得到一個額外的0,所以30 + 30 = 600.嘗試添加2個數組時額外的0位

我很確定這是因爲我錯誤地循環訪問數組。我需要去相反的方式(從右側開始,就像你添加數字時一樣),但我似乎無法修復它,沒有得到數組索引錯誤。

這裏是我的代碼:

main: 

import java.util.Scanner; 

public class testLargeInteger 
{ 



public static void main(String[] args) 
    { 
    Scanner input = new Scanner(System.in); 
     String string1; 
     String string2; 
     int exp =0; 


     System.out.print("Enter the first integer: "); 
     //Store up the input string 「string1」 entered by the user from the keyboard. 
     string1 = input.next(); 

     LargeInteger firstInt = new LargeInteger(string1); 

     System.out.print("Enter the second integer: "); 
     string2 = input.next(); 
     //Store up the input string 「string2」 entered by the user from the keyboard. 
     LargeInteger secondInt = new LargeInteger(string2); 

     System.out.print("Enter the exponential integer: "); 
     //Store up the input integer 「exp」 entered by the user from the keyboard. 
     exp = input.nextInt(); 


     LargeInteger sum = firstInt.add(secondInt); 

     System.out.printf ("First integer: %s \n", firstInt.display()); 
     System.out.println("Second integer: " + secondInt.display()); 
     System.out.println(" Exponent: " + exp); 

     System.out.printf (" Sum = %s \n", sum.display()); 

    } 
} 

大整數:

public class LargeInteger { 


    private int[] intArray; 


    //convert the strings to array 
    public LargeInteger(String s) { 
     intArray = new int[s.length()]; 
     for (int i = 0; i < s.length(); i++) { 
      intArray[i] = Character.digit(s.charAt(i), 10); // in base 10 
     } 
    } 

    public LargeInteger(int[] array) { 
     intArray = array; 
    } 

    //display the strings 
    public String display() {   
      String result=""; 

      for (int i = 0; i < intArray.length; i++) {  
      result += intArray[i]; 
      } 
      return result.toString(); 
     } 

    //get first array 
    public int[] getIntArray() { 
      return intArray; 
     } 

    //ADD method to add 2 arrays together 
    public LargeInteger add(LargeInteger secondInt){ 

     int[] otherValues = secondInt.getIntArray(); 

     int maxIterations = Math.min(intArray.length, otherValues.length); 
     int currentResult; //to store result 
     int[] resultArray = new int[Math.max(intArray.length, otherValues.length) +1 ]; 

     int needToAdd = 0; //to store result should be added next step 

     for(int i = 0; i < maxIterations; i++) { 
      currentResult = intArray[i] + otherValues[i]; 
      resultArray[i] = currentResult % 10 + needToAdd; //if more than 9 its correct answer 
      needToAdd = currentResult/10; //this is what you need to add on next step 
     } 

     resultArray[Math.max(intArray.length, otherValues.length) ] = needToAdd; 

     return new LargeInteger(resultArray); 

    } 

} 

我試圖改變的for循環總和是這樣的:

for(int i = maxIterations; i >= 0; i--) 
+2

投票結束:要求陌生人通過檢查發現代碼中的錯誤不是生產性的。您應該使用調試器或打印語句來識別(或至少隔離)問題,然後回來一個更具體的問題(一旦您將其縮小到10行[測試案例](http:///sscce.org))。 – 2012-03-25 20:20:24

+1

Java中的數組基於0。數組的有效索引是[0,array.length - 1]。 – Jeffrey 2012-03-25 20:22:44

+0

我沒有看到它是真的要發現一個錯誤..雖然也許是..我只是不知道我是否在正確的軌道,如果它肯定我需要循環相反的方式.. – Sackling 2012-03-25 20:22:51

回答

1

那個for循環只是你的一個問題。

1]您沒有正確添加進位。

2]堆棧比陣列更合適。

有一個堆棧(您的方法中的地方代碼): 注意:您正在使用number.add(num2)調用該函數;

public class LargeInt{ 
    private String number; 
    public LargeInt(String num){ 
     this.number = num; 
    } 

    public String add(String num2){ 
    Stack<Integer> adder = toIntegerStack(this.number);//UPDATE 
    Stack<Integer> addend = toIntegerStack(num2);//UPDATE 
    Stack<Integer> result = new Stack<Integer>(); 

    int carry =0; 
    int tmp = 0; 

    while(!.adder.isEmpty && !addend.isEmpty()){ 
    tmp = adder.pop()+addend.pop()+carry; 
    if(tmp > 10){ 
    carry = tmp/10; 
    tmp%=10; 
    }else{ 
    carry=0; 
    } 
    result.push(tmp); 
    }//while 

    while(!adder.isEmpty){ 
    tmp = adder.pop()+carry; 
    if(tmp > 10){ 
    carry = tmp/10; 
    tmp%=10; 
    }else{ 
    carry=0; 
    } 
    result.push(tmp); 
    }//while 

    while(!addend.isEmpty){ 
    tmp = addend.pop()+carry; 
    if(tmp > 10){ 
    carry = tmp/10; 
    tmp%=10; 
    }else{ 
    carry=0; 
    } 
    result.push(tmp); 
}//while 

//beyond this point the result is your answer 
//here convert your stack to string before returning 
} 
} 

UPDATE來回答評論: 我也編輯上面調用這個函數來填充堆棧。

private Stack<Integer> toIntegerStack(String n){ 
    Stack<Integer> stack = new Stack<Integer>(); 
    for(char c: n.toCharArray()) 
     stack.push(c-48);//ASCII 
    return stack; 
}//toStack(String) 

如果你堅持使用數組,你必須按照你的陣列相同的模式。

int indexA=0; 
int indexB=0; 
int[] result = new int[1+A.length>B.length?A.length:B.length]; 
int indexResult=result.length-1; 

while(indexA < A.length && indexB <B.length){//inside is same idea 
    tmp = A[indexA++] + B[indexB++] + carry; 
    //... do here as for stacks for tmp and carry 
    result[indexResult--]; 
} 

while(indexA < A.length){ 
    //do as in stack version 
} 

    while(indexB < B.length){ 
    //do as in stack version 
} 
+0

是的,只是試圖修復for循環已變得清晰我有其他問題。我正在看你的籌碼。我不需要使用數組,我只是更舒適(想象一下..)我喜歡使用堆棧的外觀。我想我應該知道,但加法器和加法器應該如何填充? – Sackling 2012-03-25 21:42:41

+0

爲清楚起見,請參閱新更新。我創建了一個由add方法調用兩次的私有函數。 – kasavbere 2012-03-25 21:56:16

+0

幾個問題,我不明白這個私人函數是如何工作的?什麼是C-48?我從來沒有見過這樣的事情,並擔心它超出了我們課程的範圍。我也看到你創建了一個公共類LargeInt,它應該是LargeInteger類的一部分,而我已經有了嗎? – Sackling 2012-03-25 22:18:02

1

你添加代碼假設最低有效數字位於array[0],但您的閱讀代碼將最多那裏有數字。閱讀後應該顛倒陣列。

+0

通過閱讀代碼你的意思是我的顯示方法? – Sackling 2012-03-25 20:33:35

+0

不,我的意思是帶'String'的構造函數。您將字符串中的數字從索引0開始放入數組中。並且在添加代碼中,您將進位從索引0傳播到更高索引。由於數字首先被寫入最重要的數字,所以不合適。 – 2012-03-25 20:36:47

+0

啊有道理。謝謝! – Sackling 2012-03-25 20:38:21