java 实现任意位数相加!

package com.ajax;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



/**
 * @author xn14168
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Test {
    public static void main(String[] args) {
        while(true) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String a = null;
            String b = null;
            try {
                System.out.println("Number1:");
                a = br.readLine();
                System.out.println("Number2:");
                b = br.readLine();
                
            
            Object[] result = add(a, b);
            System.out.println("Result : ");
            for(int i=0; i < result.length; i++) {
                System.out.print(result[i]);
            }
            System.out.println();
            System.out.println("Continue?(Y/N)");
            String exit = br.readLine();
            if("N".equalsIgnoreCase(exit)) {
                break;
            }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   
   
    private static Object[] add(String a, String b) {
        String m = a;
        String n = b;
        if(a.length()>b.length()) {
            n = padZero(b, a.length()-b.length());
        } else {
            m = padZero(a, b.length()-a.length());
        }
        System.out.println(m);
        System.out.println(n);
        long[] x = getNumberArray(m);
        long[] y = getNumberArray(n);
        return doAdd(x,y);
    }
   
    private static long[] getNumberArray(String number) {
        int time = getTime(number);
       
        if(time == 1 ) {
            return new long[]{Long.parseLong(number)};
        }
       
        long[] result = new long[time];
        String temp = number;
        for(int i=time-1; i >=0; i-- ) {
            if(temp.length() <=18) {
                result[i] = Long.parseLong(temp);
            } else {
                String subNum = temp.substring(temp.length()-18, temp.length());
                result[i] = Long.parseLong(subNum);
                temp = temp.substring(0, temp.length()-18);
            }
           
        }
       
        return result;
    }
   
    private static String padZero(String a, int zeroNum) {
        StringBuffer sb = new StringBuffer();
       
        for(int i = 0; i             sb.append("0");
        }
        return sb.append(a).toString();
    }
   
    private static int getTime(String number) {
        int length = number.length();
        int time = (int)Math.ceil(length/18f);
        return time;
    }
   
    private static Object[] doAdd(long[] a, long[] b) {
        Object[] result=new Object[a.length];
        int jinwei = 0;
        for(int i = b.length-1; i>=0; i--) {
//            System.out.print(a[i] + " + " + b[i] + " + " + jinwei);
            result[i] = new Long(a[i] + b[i] + jinwei);
            long longValue = ((Long)result[i]).longValue();
            if( longValue >= 1000000000000000000l) {
                jinwei = 1;
                String temp = String.valueOf(longValue - 1000000000000000000l);
                int needPad = 18 - temp.length();
                result[i] = new String(padZero(temp, needPad));
            } else if(i!=0) {
                String temp = String.valueOf(longValue);
                int needPad = 18 - temp.length();
                result[i] = new String(padZero(temp, needPad));
                jinwei = 0;
            }
//            System.out.println("= " + result[i]);
        }
//        if(jinwei == 1) {
//            long longValue = ((Long)result[0]).longValue();
//            result[0] =  new Long(longValue + 1l);
//        }
        return result;
    }
   
}

你可能感兴趣的:(Java)