首页 > 代码库 > 关于大整数的加减乘除求余运算 java

关于大整数的加减乘除求余运算 java

自己用java 实现了大字符串整数的加减乘除和求余运算,

加减法的算法是模拟手工笔算的计算过程,

除法就是从最高位不停的减操作,

乘法的算法 :遍历一个数的各个位数以及他所在位置,另一个数根据这个数的位置末位添n个0,然后累加次数为这个位置数的数值

原创代码如下:

public class Demo6 {    public static void main(String[]args){        System.out.println(add("-989","989"));        System.out.println(sub("890","-235"));        System.out.println(divide("-21","22"));        System.out.println(mtp("-123","4567"));        System.out.println(remainder("22","21"));    }        public static String add(String s1,String s2){        StringBuffer res=new StringBuffer();        if(s1.contains("-")&&s2.contains("-")){            res.append("-");            s1=s1.replace("-", "");            s2=s2.replace("-", "");        }else if((!s1.contains("-")&&s2.contains("-"))){            return sub(s1,s2.replace("-", ""));        }else if((s1.contains("-")&&!s2.contains("-"))){            return sub(s2,s1.replace("-", ""));        }                int len =Math.abs(s1.length()-s2.length());        if(s1.length()>s2.length()){            s2=castSame(s2,len);        }else{            s1=castSame(s1,len);        }        int n=0;        for(int i=s1.length()-1;i>=0;i--){            int temp = (s1.charAt(i)-‘0‘+s2.charAt(i)-‘0‘+n);            if(i==0){                res.append(temp%10).append(temp/10==0?"":temp/10);            }else{                res.append(temp%10);                n=temp/10;            }            }        return kickZero(String.valueOf(res.reverse()));            }        public static String sub(String s1,String s2){        boolean flag = false;        StringBuffer res=new StringBuffer();        if(s1.contains("-")&&s2.contains("-")){            return sub(s2.replace("-", ""),s1.replace("-", ""));                }else if((!s1.contains("-")&&s2.contains("-"))){            return add(s1,s2.replace("-", ""));        }else if((s1.contains("-")&&!s2.contains("-"))){            return "-"+add(s2,s1.replace("-", ""));        }                if(!isGreater(s1,s2)){            flag = true;            String temp =s1;            s1=s2;            s2=temp;        }        s2 = castSame( s2,s1.length()-s2.length());        int n=0;        for(int i=s1.length()-1;i>=0;i--){            Integer temp = s1.charAt(i)-‘0‘-s2.charAt(i)+‘0‘-n;                        temp = i==0?(temp==0?null:temp):temp;            res.append(temp==null?"":temp>=0?temp:temp+10);                        n=temp==null?0:temp<0?1:0;                    }        return kickZero(flag?String.valueOf(res.append("-").reverse()):String.valueOf(res.reverse()));        }        public static String divide(String s1,String s2){        StringBuffer res = new StringBuffer();        int c=0;        if(s1.contains("-")){            s1=s1.replace("-", "");            c++;        }        if(s2.contains("-")){            s2=s2.replace("-", "");            c++;        }        if(c==1)            res.append("-");        if("0".equals(s2)){            return "ERROR";        }        if(!isGreater(s1,s2)){            return "0";        }                int n=s2.length();        String temp=s1.substring(0,n);        while(true){            int count=0;            while(sub(temp,s2).contains("-")){                temp +=s1.charAt(n);                n++;            }            while(true){                count++;                temp=sub(temp,s2);                if(sub(temp,s2).contains("-")){                    break;                }            }            res.append(count);            if(n>=s1.length()){                return String.valueOf(res);                }            }        }        public static String mtp(String s1,String s2){        String res = "";        int c=0;        if(s1.contains("-")){            s1=s1.replace("-", "");            c++;        }        if(s2.contains("-")){            s2=s2.replace("-", "");            c++;        }        if(!isGreater(s1,s2)){            String t =s1;            s1=s2;            s2=t;        }        String temp = s1;        for(int i=s2.length()-1;i>=0;i--){                        for(int j=0;j<s2.charAt(i)-‘0‘;j++){                res= add(res,temp);            }            temp=String.valueOf(new StringBuffer(temp).append("0"));        }        return c==1?"-"+res:res;    }         public static String  remainder(String s1,String s2){        if("0".equals(s2)){            return "ERROR";        }        if(!isGreater(s1,s2)){            return s1;        }        StringBuffer res = new StringBuffer();        int n=s2.length();        String temp=s1.substring(0,n);        while(true){            int count=0;            System.out.println("n:"+n);            while(sub(temp,s2).contains("-")){                temp +=s1.charAt(n);                n++;            }            while(true){                count++;                temp=sub(temp,s2);                if(sub(temp,s2).contains("-")){                    break;                }            }            res.append(count);            if(n>=s1.length()){                return temp;                }            }        }            public static boolean isGreater(String s1,String s2){//890  235                if(s1.length()>s2.length()){            return true;        }else if(s1.length()<s2.length()){            return false;        }else if(s1.length()==s2.length()){            int i=0;            while(i<s1.length()){                if(s1.charAt(i)<s2.charAt(i)){                    return false;                }else if(s1.charAt(i)>s2.charAt(i)){                    return true;                }                i++;            }        }        return true;            }        public static String castSame(String s,int len){        StringBuffer sb =new StringBuffer();        for(int i=0;i<len;i++){            sb.append("0");        }        s=String.valueOf(sb.append(s));        return s;        }        public static String kickZero(String s){        int i=0;        while(s.length()>1){            if(‘0‘==s.charAt(0)){                s=s.substring(1);            }else{                break;            }        }        return s;            }}

我比较懒,没加注释,如有不明白欢迎提问,方法比较笨,见笑了!