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;
}
嗨大衛,今天你haev什麼問題嗎? – 2015-04-06 04:43:46
感謝您的回覆。我很困惑,爲什麼我應該在this.digits [i]和addend.digits [i]中存儲一個「可能的有損轉換形式int到byte」。 – 2015-04-06 04:57:21
嘗試'carry =(this.digits [i] + addend.digits [i] + carry)/(byte)10;' – 2015-04-06 05:02:23