Java简单实现大数相加
public class BigNumberAdd {
/**
* 假设两个都是正数
* @param num1
* @param num2
* @return num1 + num2
*/
public static String add(String num1, String num2) {
int m = num1.length();
int n = num2.length();
StringBuilder sb = new StringBuilder();
int carry = 0;
int index1 = m - 1;
int index2 = n - 1;
while (carry != 0 || index1 >= 0 || index2 >= 0) {
int a = 0;
int b = 0;
if (index1 >= 0) {
a = num1.charAt(index1) - '0';
index1--;
}
if (index2 >= 0) {
b = num2.charAt(index2) - '0';
index2--;
}
int tmp = (a + b + carry) % 10;
carry = (a + b + carry) / 10;
sb.append(tmp);
}
sb.reverse();
return sb.toString();
}
/**
* 比较num1 和 num2 两个谁的绝对值比较大
* @param num1
* @param num2
* @return -1 : num2绝对值较大,
* 0 : 两个绝对值一样大,
* 1 : num1绝对值较大
*/
public static int compareAbsoluteValue(String num1, String num2) {
// TODO::先不考虑异常的输入
String s1 = num1.charAt(0) == '-' ? num1.substring(1) : num1;
String s2 = num2.charAt(0) == '-' ? num1.substring(1) : num2;
if (s1.length() > s2.length()) {
return 1;
}
else if (s1.length() < s2.length()) {
return -1;
}
else {
int n = s1.length();
int index = 0;
while (index < n) {
if (s1.charAt(index) > s2.charAt(index)) {
return 1;
}
else if (s1.charAt(index) < s2.charAt(index)) {
return -1;
}
index++;
}
}
return 0;
}
/**
* 判断两个数是否同号
* @param s1
* @param s2
* @return 如果同号,返回true;否则返回false
*/
public static boolean isSameNumber(String s1, String s2) {
// return (s1.charAt(0) <= '9' && s1.charAt(0) >= '0' && s2.charAt(0) <= '9' && s2.charAt(0) >= '0') ||
// (s1.charAt(0) == '-' && s2.charAt(0) == '-');
return isPositive(s1) == isPositive(s2);
}
/**
* 默认两个都是正数,且num1 > num2
* @param num1
* @param num2
* @return num1 - num2
*/
public static String minus(String num1, String num2) {
int m = num1.length();
int n = num2.length();
int carry = 0;
int index1 = m - 1;
int index2 = n - 1;
StringBuilder sb = new StringBuilder();
while (carry != 0 || index1 >= 0 || index2 >= 0) {
int a = 0;
int b = 0;
if (index1 >= 0) {
a = num1.charAt(index1) - '0';
index1--;
}
if (index2 >= 0) {
b = num2.charAt(index2) - '0';
index2--;
}
int tmp = (a - b - carry);
if (tmp < 0) {
tmp += 10;
carry = 1;
}
else {
carry = 0;
}
if (tmp == 0 && index1 == -1) {
break;
}
sb.append(tmp);
}
sb.reverse();
return sb.toString();
}
private static boolean isPositive(String num1) {
return !(num1.charAt(0) == '-');
}
public static String addTwoBigNumber(String num1, String num2) {
boolean isSame = isSameNumber(num1, num2);
if (isSame) {
boolean isPositive = isPositive(num1);
if (isPositive) {
return add(num1, num2);
}
else {
return "-" + (add(num1.substring(1), num2.substring(1)));
}
}
else {
int compareResult = compareAbsoluteValue(num1, num2);
if (compareResult == 0) {
return "";
}
boolean isPositive = isPositive(num1);
// num2绝对值比较大
if (compareResult == -1) {
// num1是正数
if (isPositive) {
return "-" + minus(num2.substring(1), num1);
}
else {
return minus(num2, num1.substring(1));
}
}
else {
if (isPositive) {
return minus(num1, num2.substring(1));
}
else {
return "-" + minus(num1.substring(1), num2);
}
}
}
}
}
标签:return,num1,num2,大数,int,相加,charAt,Java,String
From: https://www.cnblogs.com/ZJHqs/p/17652003.html