2012-03-25 24 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