JAVA--超长数据高精度计算(仅支持整数)

JAVA–超长数据高精度计算(仅支持整数)

/**
 * Created by AndyJuseKing on 2020/1/2.
 * 超长数据高精度计算
 * 仅支持整数
 */
public class CYAccuracy {
    private static String cyNum;
    private static String nowNum;

    public CYAccuracy(String a){
        cyNum = a;
    }

    public static void add(String n){
        cyNum = makeAdd(cyNum,n);
    }
    public static String getAdd(String n){
        nowNum = n;
        return makeAdd(cyNum,nowNum);
    }

    public static void subtract(String n){
        nowNum = n;
        cyNum = makeSubtract(cyNum,nowNum);
    }
    public static String getSubtract(String n){
        nowNum = n;
        return makeSubtract(cyNum,nowNum);
    }

    public static void multiply(String n){
        nowNum = n;
        cyNum = makeMultiply(cyNum,nowNum);
    }
    public static String getMultiply(String n){
        nowNum = n;
        return makeMultiply(cyNum,nowNum);
    }

    public static String[] divideAndRemainder(String n){
        nowNum = n;
        String h = cyNum;
        h = removeZero(h);
        String i = h;
        String divNum = "";
        String remNum = "";
        String a = "0";
        int c = h.length();
        int d = nowNum.length();
        int e = c;
        while (d<=e){
            String f = h;
            if(e==c){ f = h.substring(0, d); }
            String g = f;
            if(d<=c) {
                while (!f.contains("-")) {
                    g = f;
                    f = makeSubtract(f, n);
                    a = makeAdd(a, "1");
                    f = removeZero(f);
                }
                a = makeSubtract(a, "1");
                if(i.length()>=(d+divNum.length()+1)) {
                    h = addZero(g, 1);
                    h = makeAdd(h, i.substring(d + divNum.length(), d + 1 + divNum.length()));
                } else {
                    remNum = g;
                    e = 0;
                }
                c = h.length();
                divNum = divNum + a;
                a = "0";
            } else if(i.length()<(d+divNum.length()+1)){
                remNum = g;
                e = 0;
            } else {
                h = addZero(g, 1);
                h = makeAdd(h, i.substring(d+divNum.length(), d+1+divNum.length()));
                c = h.length();
                divNum = divNum + "0";
            }
        }

//        while (!newNum.contains("-")) {
//            newNum = makeSubtract(newNum,n);
//            a = makeAdd(a,"1");
//            newNum = removeZero(newNum);
//            System.out.print(newNum + "\n");
//        }
//        a = makeSubtract(a,"1");
//        b = newNum.substring(1);
        return (divNum+","+remNum).split(",");
    }

    public static Double getDouble(){
        return Double.parseDouble(cyNum);
    }

    public static Integer getInt(){
        return Integer.parseInt(cyNum);
    }

    public static String getString(){
        return cyNum;
    }

    private static String makeAdd(String x,String y){
        String newNum = "";
        int i = 1;if(x.substring(0,1).equals("-")){i = -1;}
        int j = 1;if(y.substring(0,1).equals("-")){j = -1;}
        int m = x.length();
        int n = y.length();
        if (m < n) {
            int c = n - m;
            for (int d = 0; d < c; d++) {
                x = "0" + x;
            }
        } else if (m > n) {
            int c = m - n;
            for (int d = 0; d < c; d++) {
                y = "0" + y;
            }
        }
        String[] a = x.split("");
        String[] b = y.split("");
        int g = 0;
        for(int c = a.length;c>0;c--){
            int d = c-1;
            int f = (Integer.parseInt(a[d])*i) + (Integer.parseInt(b[d])*j) + g;
            int e = f%10;
            newNum = e + newNum;
            g = f/10;
            if(d==0&&g!=0){
                newNum = g + newNum;
            }
        }
        return newNum;
    }

    private static String makeSubtract(String x,String y){
        String newNum = "";
        int m = x.length();
        int n = y.length();
        if (m < n) {
            int c = n - m;
            for (int d = 0; d < c; d++) {
                x = "0" + x;
            }
        } else if (m > n) {
            int c = m - n;
            for (int d = 0; d < c; d++) {
                y = "0" + y;
            }
        }
        String[] a = x.split("");
        String[] b = y.split("");
        int g = 0;
        for(int c = a.length;c>0;c--){
            int d = c-1;
            int h = Integer.parseInt(a[d]);
            int i = Integer.parseInt(b[d]);
            int f = (h - i) + g;
            int e = f%10;
            if(e==-1){ e = 9; }
            g = f/10;
            if(e<0){
                g = g-1;
                e = e * -1;
            }
            newNum = e + newNum;
            if(d==0&&g<0){
                newNum = "-" + newNum;
            }
        }
        return newNum;
    }

    private static String makeMultiply(String x,String y){
        String newNum = "0";
        String[] a = x.split("");
        String[] b = y.split("");
        String k = "";
        for(int h = b.length;h>0;h--) {
            int i = h - 1;
            k = k + "0";
            int g = 0;
            String j = "";
            for (int c = a.length; c > 0; c--) {
                int d = c - 1;
                int f = (Integer.parseInt(a[d])+g) * Integer.parseInt(b[i]);
                int e = f % 10;
                j = e + j;
                g = f / 10;
                if (d == 0 && g != 0) {
                    j = g + j;
                }
            }
            String l = j+k;
            newNum = makeAdd(newNum,l);
        }
        return newNum;
    }

    private static String removeZero(String x){
        String y = x;
        String[] a = x.split("");
        for(int b = 0;b<a.length;b++){
            if(y.substring(0,1).equals("0")){
                y = y.substring(1);
            }
        }
        if("".equals(y)){
            y = "0";
        }
        return y;
    }

    private static String addZero(String x,int length){
        while(length>0){
            x = x + "0";
            length--;
        }
        return x;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值