java 对String 加法计算

// 对两个字符串,不使用long,int等类型计算
 @Test
    public void jia(){
        String ax = "9234562343465434567";
        String bx = "1234567234563246754324567";

        char [] axArrs = ax.toCharArray();

        char [] bxArrs = bx.toCharArray();

        char [] tMax = axArrs.length> bxArrs.length?axArrs:bxArrs;//max
        char [] tMin = axArrs.length> bxArrs.length?bxArrs:axArrs;//min

        int len  = tMax.length - tMin.length;
        char [] tArrAppend = new char[len];
        char [] tMin2 = new char[tMin.length];

        System.arraycopy(tMax, 0, tArrAppend, 0, len);
        System.arraycopy(tMax, len, tMin2, 0, tMin2.length);


        int [] tMin3 = new int[tMin.length];
        boolean flag = false;
        for (int i=tMin.length-1;i>-1;i--){
            int count = Character.getNumericValue(tMin2[i])+Character.getNumericValue(tMin[i]) + (flag?1:0);
            int b1 = count;
            flag = count>9;
            if (flag){
                b1 = count % 10;
            }
            tMin3[i]= b1;
        }

        int [] t = new int[tArrAppend.length];
        for (int i=tArrAppend.length-1;i>-1;i--){
            int count = Character.getNumericValue(tArrAppend[i])+ (flag?1:0);
            int b1 = count;
            flag = count>9;
            if (flag){
                b1 = count % 10;
            }
            t[i] = b1;
        }

        int [] result = new int[tMax.length+ (flag?1:0)];
        if (flag){
            result[0] = 1;
        }
        System.arraycopy(t,0,result,(flag?1:0),t.length);
        System.arraycopy(tMin3,0,result,t.length+(flag?1:0),tMin3.length);

        System.out.println(ax+"\t+\t" +bx+ "\t=\t"+ Arrays.toString(result).replaceAll("[\\[,\\]\\s*]",""));
    }

 

你可能感兴趣的:(java,JDK基础)