Java 利用数组实现超大整数加、乘

2016-04-28  本文已影响351人  Yanci516


import org.apache.commons.lang3.StringUtils;

public class BigInt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        BigInt bigInt1 = new BigInt("1231723333333131343453458938458937834234242534532");
        BigInt bigInt2 = new BigInt("234242342342387492746278462783492748926742342424");
        System.out.println(bigInt1.toString());
        System.out.println(bigInt2.toString());
        System.out.println(bigInt1.addBigInt(bigInt2));
        System.out.println(bigInt1.multiply(bigInt2));
        /*System.out.println(bigInt1.subtract(bigInt2));*/
    
    }
    public BigInt(String bigbigNum) {
        splitToArray(bigbigNum);
    }
    
    private int[] arrayInt = new int[100];
    
    //强数据拆分为数组
    public void splitToArray(String bigNum){
        StringBuffer sb = new StringBuffer();
        //先将 字符串逆序
        bigNum = new StringBuffer(bigNum).reverse().toString();
        int j = 0;
        for (int i = 0; i < bigNum.length(); i++) {
            // 用一个整形数组来储存,且长度不超过 4位
            if (i%4 ==0) {
                //再讲sb逆序之后 存储于整形数组第i和元素中
                if(sb !=null && !sb.toString().equals("")){
                    arrayInt[j] = Integer.valueOf(sb.reverse().toString());
                    sb = new StringBuffer();
                    j++;
                }
            }
            sb.append(bigNum.charAt(i));
        }
        //处理最后几个不满4位的数据
        if( sb!=null ){
            arrayInt[j] = Integer.valueOf(sb.reverse().toString());
        }
    }
    
    //从数组后开始打印输出,才能得到原序列
    public String printArrayInt(int[] array){
        String str = "";
    
        StringBuffer sb = new StringBuffer();
        boolean isFirstArrayInt = true;
        for (int i = array.length-1; i >=0; i--) {
            if (array[i] != 0) {
                System.out.println("第"+i+"个数据:"+array[i]);
                //判断每个数组元素是否都为 4位,不足4位在左边补0
                if (!isFirstArrayInt && String.valueOf(array[i]).length()<4) {
                    sb.append(StringUtils.leftPad(String.valueOf(array[i]),4,"0"));
                }else {
                    sb.append(array[i]);
                    if(isFirstArrayInt)
                        isFirstArrayInt = false;
                }
            }
        }
        return sb.toString();
    }
    
    //利用数组实现大整数的加法
    public String addBigInt(BigInt bigInt){
        int a[] = this.arrayInt;
        int b[] = bigInt.arrayInt;
        int result[] = new int[100];
        for (int i = 0; i < a.length; i++) {
            if (a[i] == 0 && b[i] != 0) {
                result[i] = b[i];
            }else if (a[i] != 0 && b[i] == 0) {
                result = a;
            }else if (a[i] != 0 && b[i] != 0) {
                result[i] = a[i]+b[i];
            }else {
                result[i] = 0;
            }
        }
        //处理result的进位
        for (int i = 0; i < result.length; i++) {
            if (result[i] != 0 && String.valueOf(result[i]).length()>4) {
                result[i] = Integer.valueOf(String.valueOf(result[i]).substring(1));
                result[i+1] = result[i+1] +1;
            }
        }
        return printArrayInt(result);
    }
    //实现大整数乘法
    public String multiply(BigInt bigInt) {
        int a[] = this.arrayInt;
        int b[] = bigInt.arrayInt;
        int result[] = new int[100];
        for (int i = 0; i < a.length; i++) {
            if (a[i] == 0 || b[i] == 0) {
                result[i] = 0;
            }else {
                result[i] = a[i] * b[i];
            }
        }
        //处理result的进位
        for (int i = 0; i < result.length; i++) {
            if (result[i] != 0 && String.valueOf(result[i]).length()>4) {
                result[i] = Integer.valueOf(String.valueOf(result[i]).substring(1));
                result[i+1] = result[i+1] +1;
            }
        }
        return printArrayInt(result);
    }
    @Override
    public String toString(){
        return printArrayInt(arrayInt);
    }
}

上一篇下一篇

猜你喜欢

热点阅读