2015-04-06 20 views
0

我无法将两个“大整数”字节数组一起添加,它们以相反的顺序存储以帮助数学。这是我的构造函数。将两个“Bigint”字节数组加在一起

public class Intzilla { 
public byte[] digits; 
//private byte negative = -1; 
private byte zero = 0; 
private byte positive = 1; 
private byte posNegZero = 0; 
private boolean negative; 
private String inputString; 

public Intzilla() { 
    this("0"); 
} 

public Intzilla(String s) { 
    String tempString = s; 
    inputString = s; 
    tempString = tempString.trim(); 
    if(tempString.substring(0,1).equals("-")){ 
     negative = true; 
     tempString = tempString.substring(1); 
    } else if(tempString.substring(0,1).equals("+")){ 
     negative = false; 
     tempString = tempString.substring(1); 
    }else { 
     negative = false; 
    } 

    while((tempString.substring(0,1).equals("0")) && (tempString.length() > 1)){ 
     tempString = tempString.substring(1); 
    } 

    digits = new byte[tempString.length()]; 

    for(int i = 0; i < tempString.length(); i++){ 
     String currentChar = tempString.substring(i, i+1); 
     byte tempDigits = Byte.parseByte(currentChar); 
     digits[(digits.length - 1) -i] = tempDigits; 
    } 
} 

这是我迄今为止尝试的加法。获得“可能的有损转换到字节”。

public Intzilla plus(Intzilla addend) { 
    byte carry = 0; 
    byte mod = 10; 
    Intzilla result = new Intzilla(); 

    for(int i = 0; i <= addend.digits.length-1; i++) { 
     result.digits[i] = (byte)(this.digits[i] + addend.digits[i] + carry)% mod; 
     carry = (byte)(this.digits[i] + addend.digits[i] + carry)/10; 
    } 
    return result; 
} 
+1

嗨大卫,今天你haev什么问题吗? – 2015-04-06 04:43:46

+0

感谢您的回复。我很困惑,为什么我应该在this.digits [i]和addend.digits [i]中存储一个“可能的有损转换形式int到byte”。 – 2015-04-06 04:57:21

+0

尝试'carry =(this.digits [i] + addend.digits [i] + carry)/(byte)10;' – 2015-04-06 05:02:23

回答

0

这种格式的工作

byte [] newbytes = new byte [20]; 
    byte [] oldbytes = new byte [20]; 

    for (int x = 0; x < oldbytes.length; x++) { 
     newbytes [x] = (byte) ((newbytes[x] + oldbytes [x])/100); 
    }