大数相加
public String bigNumberAdd(String f, String s) {
char[] a = new StringBuffer(f).reverse().toString().toCharArray();
char[] b = new StringBuffer(s).reverse().toString().toCharArray();
int lenA = a.length;
int lenB = b.length;
int len = lenA > lenB ? lenA : lenB;
int[] result = new int[len + 1];
for (int i = 0; i < len + 1; i++) {
int aint = i < lenA ? (a[i] - '0') : 0;
int bint = i < lenB ? (b[i] - '0') : 0;
result[i] = aint + bint;
}
for (int i = 0; i < result.length; i++) {
if (result[i] >= 10) {
result[i + 1] += result[i] / 10;
result[i] %= 10;
}
}
StringBuffer sb = new StringBuffer();
boolean flag = true;
for (int i = len; i >= 0; i
if (result[i] == 0 && flag) {
continue;
} else {
flag = false;
}
sb.append(result[i]);
}
return sb.reverse().toString();
}
大数相减
public static String bigNumberSub(String a, String b) {
if(a == null || b == null || a.equals("") || b.equals(""))
return null;
if(a.equals(b))
return "0";
char[] aChars = new StringBuilder(a).reverse().toString().toCharArray();
char[] bChars = new StringBuilder(b).reverse().toString().toCharArray();
int lenA = aChars.length;
int lenB = bChars.length;
int maxLen = lenA > lenB ? lenA : lenB;
char sign = '+';
if(lenA < lenB) {
sign = '-';
}else {
if(lenA == lenB) {
int i = maxLen - 1;
while(i >= 0 && aChars[i] == bChars[i]) {
i--;
}
if(i >= 0 && aChars[i] < bChars[i]) {
sign = '-';
}
}
}
int[] result = new int[maxLen];
for(int i = 0; i < maxLen; i++) {
int aInt = i < lenA ? (aChars[i] - '0') : 0;
int bInt = i < lenB ? (bChars[i] - '0') : 0;
if(sign == '+') {
result[i] = aInt - bInt;
}else {
result[i] = bInt - aInt;
}
}
for(int i = 0; i < maxLen-1; i++) {
if(result[i] < 0) {
result[i+1] -= 1;
result[i] += 10;
}
}
StringBuilder retBuilder = new StringBuilder();
boolean flag = true;
for(int i = maxLen - 1; i >= 0; i--) {
if(flag && result[i] == 0){
continue;
}else {
flag = false;
}
retBuilder.append(result[i]);
}
if(sign == '-') {
retBuilder.insert(0, '-');
}
return retBuilder.toString();
}
大数相乘
public static String bigNumberMultiply(String a, String b) { if(a == null || b == null || a.equals("") || b.equals("")) return null; char signA = a.charAt(0); char signB = b.charAt(0); char sign = '+'; if(signA == '+' || signA == '-') { sign = signA; a = a.substring(1); } if(signB == '+' || signB == '-') { if(sign == signB) { sign = '+'; }else { sign = '-'; } b = b.substring(1); } if(a.equals("0") && b.equals("0")) return "0"; char[] aChars = new StringBuilder(a).reverse().toString().toCharArray(); char[] bChars = new StringBuilder(b).reverse().toString().toCharArray(); int lenA = aChars.length; int lenB = bChars.length; int len = lenA + lenB; int[] result = new int[len]; for(int i = 0; i < lenA; i++) { for(int j = 0; j < lenB; j++) { result[i + j] += (aChars[i] - '0')*(bChars[j] - '0'); } } for(int i = 0; i < len; i++) { if(result[i] >= 10) { result[i+1] += result[i]/10; result[i] %= 10; } } StringBuilder retStrBuilder = new StringBuilder(); boolean flag = true; for(int i = len - 1; i >= 0; i--) { if(flag && result[i] == 0) { continue; }else { flag = false; } retStrBuilder.append(result[i]); } if(sign == '-') retStrBuilder.insert(0, "-"); return retStrBuilder.toString(); }