当数字超过long int范围时,如何运用数组自己编写一个工具类来专门计算大数字求和运算(禁止用BigDecimal)?我自己编了一个,不满意,请问各位是否有更好的方法?
//大数字运算
public class AddBigNumHe {
private String s1 = new String();
private String s2 = new String();
public AddBigNumHe(String s1,String s2){
this.s1 = s1;
this.s2 = s2;
String s3 = AddBigNumHe.add(s1, s2);
System.out.println(s3);
}
public static String add(String s1,String s2){
String ans = ""; //答案
String slong,sshort; //长数和短数
int cy=0; //进位
slong = s1;
sshort = s2;
if(s1.length()<=s2.length()){
slong = s2;
sshort = s1;
}//判断长短数
for(int i=0;i<sshort.length();i++){ //先加和短数一样长的部分
char c1 = sshort.charAt(sshort.length()-1-i);//从后往前依次获得短数的的字符
System.out.println(c1+"");
char c2 = slong.charAt(slong.length()-1-i);//从后往前依次获得长数的字符
System.out.println(c2+"");
int i1 = Integer.parseInt(c1+"");//将获得字符转换成int型
int i2 = Integer.parseInt(c2+"");//将获得字符转换成int型
System.out.println(i1);
System.out.println(i2);
int sum=i1+i2+cy; //相加
System.out.println(sum);
if(sum>=10){
cy = 1;//大于进位数置1
System.out.println(cy);
//将获得个位数插入前所得数的前面
ans = sum%10+ans;//(此写法很好)
System.out.println(ans);
}else {
cy = 0;//小于进位数置0
System.out.println(cy);
//将所得值插入前所得值的前面
ans = sum+ans;//(此思路很好)
System.out.println(ans);
}
}
for(int i=slong.length()-sshort.length()-1;i>=0;i--){ //长数加剩下的
char c3 = slong.charAt(i);
System.out.println(c3+"");
int i3 = Integer.parseInt(c3+"");
System.out.println(i3);
int sum = i3+cy;
System.out.println(sum);
if(sum>=10){
cy = 1;
System.out.println(cy);
ans = 0+ans;
System.out.println(ans);
}else {
cy = 0;
System.out.println(cy);
ans = sum+ans;
System.out.println(ans);
}
}
if(cy==1){ //判断最后一位是否有进位
//!!此写法和好
ans = 1+ans;
}
return ans;
}
public static void main(String[] args) {
new AddBigNumHe( "1299876","9876");
}}
//大数字运算
public class AddBigNumHe {
private String s1 = new String();
private String s2 = new String();
public AddBigNumHe(String s1,String s2){
this.s1 = s1;
this.s2 = s2;
String s3 = AddBigNumHe.add(s1, s2);
System.out.println(s3);
}
public static String add(String s1,String s2){
String ans = ""; //答案
String slong,sshort; //长数和短数
int cy=0; //进位
slong = s1;
sshort = s2;
if(s1.length()<=s2.length()){
slong = s2;
sshort = s1;
}//判断长短数
for(int i=0;i<sshort.length();i++){ //先加和短数一样长的部分
char c1 = sshort.charAt(sshort.length()-1-i);//从后往前依次获得短数的的字符
System.out.println(c1+"");
char c2 = slong.charAt(slong.length()-1-i);//从后往前依次获得长数的字符
System.out.println(c2+"");
int i1 = Integer.parseInt(c1+"");//将获得字符转换成int型
int i2 = Integer.parseInt(c2+"");//将获得字符转换成int型
System.out.println(i1);
System.out.println(i2);
int sum=i1+i2+cy; //相加
System.out.println(sum);
if(sum>=10){
cy = 1;//大于进位数置1
System.out.println(cy);
//将获得个位数插入前所得数的前面
ans = sum%10+ans;//(此写法很好)
System.out.println(ans);
}else {
cy = 0;//小于进位数置0
System.out.println(cy);
//将所得值插入前所得值的前面
ans = sum+ans;//(此思路很好)
System.out.println(ans);
}
}
for(int i=slong.length()-sshort.length()-1;i>=0;i--){ //长数加剩下的
char c3 = slong.charAt(i);
System.out.println(c3+"");
int i3 = Integer.parseInt(c3+"");
System.out.println(i3);
int sum = i3+cy;
System.out.println(sum);
if(sum>=10){
cy = 1;
System.out.println(cy);
ans = 0+ans;
System.out.println(ans);
}else {
cy = 0;
System.out.println(cy);
ans = sum+ans;
System.out.println(ans);
}
}
if(cy==1){ //判断最后一位是否有进位
//!!此写法和好
ans = 1+ans;
}
return ans;
}
public static void main(String[] args) {
new AddBigNumHe( "1299876","9876");
}}
* 求 1000 阶乘(用自定义的 BigInteger)
*/
public class TestBigInteger { public static void main(String[] args) {
BigInteger i = new BigInteger(1); for (int n = 2; n <= 1000; n++) {
i.multiply(n);
} System.out.println(i);
System.out.println(i.toString().length());
}
}// 自定义的 BigInteger
class BigInteger { // 初始化数组。每个元素可以包含 9 位数字,所以初始化有 90 位
private int ints[] = new int[10]; // 达到或超过这个值就进位
private static final int UPPER_BOUND = 1000000000; public BigInteger(int value) {
ints[0] = value;
} public BigInteger(BigInteger value) {
ints = Arrays.copyOf(value.ints, value.ints.length);
} public void multiply(int n) {
BigInteger that = new BigInteger(this);
for (int i = 0; i < n - 1; i++) {
add(that);
}
} private void add(BigInteger bigInteger) {
int[] ints2 = bigInteger.ints; for (int i = 0; i < ints2.length; i++) {
ints[i] += ints2[i];
checkAndCarry(i);
} checkAndIncrease();
} // 检查指定位置的值。有必要的话就进位。
private void checkAndCarry(int i) {
if (ints[i] >= UPPER_BOUND && i < ints.length - 1) {
ints[i] -= UPPER_BOUND;
ints[i + 1]++;
}
} // 检查最后一个元素的值。达到或超过进位大小则增加数组长度。
private void checkAndIncrease() {
if (ints[ints.length - 1] > UPPER_BOUND) {
int[] newints = Arrays.copyOf(ints, ints.length + 1);
newints[ints.length - 1] -= UPPER_BOUND;
newints[ints.length]++;
ints = newints;
}
} @Override
public String toString() {
DecimalFormat f = new DecimalFormat("000000000");
StringBuffer sb = new StringBuffer(); for (int each : ints) {
sb.insert(0, f.format(each));
} while (sb.charAt(0) == '0') {
sb.delete(0, 1);
} return sb.toString();
}
}
public static String add(String s1,String s2){
int len1 = s1.length();
int len2 = s2.length();
int len = Math.max(len1, len2);
char[] temp = new char[Math.abs(len1 - len2)];
Arrays.fill(temp, '0');
String prefix = String.valueOf(temp);
if (len1 <= len2) {
s1 = prefix + s1;
} else {
s2 = prefix+s2;
}
StringBuffer rs = new StringBuffer();
int jw = 0;//进位
for (int i = len - 1; i >= 0; i--) {
int a = Integer.parseInt("0" + s1.charAt(i));
int b = Integer.parseInt("0" + s2.charAt(i));
int c = a + b + jw;
jw = c >= 10 ? 1 : 0;
c = c >= 10 ? c - 10 : c;
rs.append(c);
}
if (jw != 0) {
rs.append(jw);
}
return rs.reverse().toString();
}