• 自己动手写Java大整数《3》除法和十进制转换


    之前已经完毕了大整数的表示、绝对值的比較大小、取负值、加减法运算以及乘法运算。

    详细见前两篇博客(自己动手写Java * )。

    这里加入除法运算。

    另外看到作者Pauls Gedanken在blog(http://paul-ebermann.tumblr.com/post/6312290327/big-numbers-selfmade-part-2-14-conversion-from)中的转换十进制数到大整数的方法,这里一并列出。


    除法

    除法使用经典的除法法则,可是有几个须要注意的问题,以下列出。

    1,除数是0的问题;

    2。两者相等,输出1余数是被除数;

    3。除数比較大,直接输出0。余数是被除数

    4,子数组长度和除数数组长度相等或者大1。相等时候结果由子数组第一位除以除数第一位决定,

          大1时候结果通过子数组头两位除以除数第一位决定

    5,判定结果第一位是否为0.

    我们看下简单的图演示样例子。看看除法的详细操作



    	/*
    		 * 除法
    		 */
    		
    		public DecimalBig Divide(DecimalBig that)
    		{
    			if (that.Compare(Zero)==0)
    				throw new IllegalArgumentException("Divided by 0!");
    			if (this.Abscompare(that)==-1)
    				return Zero;
    			if (this.Abscompare(that)==0)
    				return One;
    			
    			int thislength=this.digits.length;
    			int thatlength=that.digits.length;
    	
    			int[] mulres=new int[thatlength];
    			System.arraycopy(this.digits, 0, mulres, 0, thatlength);
    			int digitdivide=0;
    			int[] result=new int[thislength-thatlength+1];
    			
    			for (int i=0; i<thislength-thatlength;i++)
    			{
    				if (Arraycompare(mulres,that.digits)==-1)
    				{
    					digitdivide=0;
    				} else
    				{
    					if (mulres.length==that.digits.length)
    						digitdivide=mulres[0]/that.digits[0];
    					else{
    						long firtwei=(long)mulres[0]*Radix+mulres[1];
    						digitdivide=(int)firtwei/that.digits[0];
    						
    					}
    					
    					int[] temp=Multiplyint(that.digits, digitdivide);
    					
    					if (Arraycompare(mulres,temp)==-1){
    						digitdivide-=1;
    						temp=Substract(temp,that.digits);
    					}
    					mulres=Substract(mulres, temp);	
    				}
    				
    				result[i]=digitdivide;
    				if (mulres.length==1&&mulres[0]==0){
    					mulres[0]=this.digits[i+thatlength];
    				}else{
    			    int[] temp2=new int[mulres.length+1];
    				System.arraycopy(mulres, 0, temp2, 0, mulres.length);
    				temp2[mulres.length]=this.digits[i+thatlength];
    				mulres=temp2;
    				}
    			}
    			
    			if (Arraycompare(mulres,that.digits)==-1)
    			{
    				digitdivide=0;
    			} else
    			{
    				if (mulres.length==that.digits.length)
    					digitdivide=mulres[0]/that.digits[0];
    				else{
    					long firtwei=(long)mulres[0]*Radix+mulres[1];
    					long ttt=firtwei/that.digits[0];
    					digitdivide=(int) ttt;
    				}
    
    				int[] temp=Multiplyint(that.digits, digitdivide);
    				
    				if (Arraycompare(mulres,temp)==-1){
    					digitdivide-=1;
    					temp=Substract(temp,that.digits);
    				}
    				mulres=Substract(mulres, temp);	
    			}
    			
    			/*
    			 * 最后的mulres就是余数。
    			 */
    			
    			result[thislength-thatlength]=digitdivide;			
    	        /*
    	         * 去掉首位的零		
    	         */
    			int i=0;
    			while(i<result.length&&result[i]==0)
    				i++;
    		    //截取非零项
    			int[] temp = new int[result.length-i];
    			System.arraycopy(result, i, temp, 0, result.length-i);
    			result = temp;
    			
    			return new DecimalBig(1, result);
    		}
    		
    		/*
    		 * 除法余数
    		 */
    		
    		public DecimalBig DivideReminder(DecimalBig that)
    		{
    			if (that.Compare(Zero)==0)
    				throw new IllegalArgumentException("Divided by 0!");
    			if (this.Abscompare(that)==-1)
    				return this;
    			if (this.Abscompare(that)==0)
    				return Zero;
    			
    			int thislength=this.digits.length;
    			int thatlength=that.digits.length;
    	
    			int[] mulres=new int[thatlength];
    			System.arraycopy(this.digits, 0, mulres, 0, thatlength);
    			int digitdivide=0;
    			int[] result=new int[thislength-thatlength+1];
    			
    			for (int i=0; i<thislength-thatlength;i++)
    			{
    				if (Arraycompare(mulres,that.digits)==-1)
    				{
    					digitdivide=0;
    				} else
    				{
    					if (mulres.length==that.digits.length)
    						digitdivide=mulres[0]/that.digits[0];
    					else{
    						long firtwei=(long)mulres[0]*Radix+mulres[1];
    						digitdivide=(int)firtwei/that.digits[0];
    						
    					}
    					
    					int[] temp=Multiplyint(that.digits, digitdivide);
    					
    					if (Arraycompare(mulres,temp)==-1){
    						digitdivide-=1;
    						temp=Substract(temp,that.digits);
    					}
    					mulres=Substract(mulres, temp);	
    				}
    				
    				result[i]=digitdivide;
    				if (mulres.length==1&&mulres[0]==0){
    					mulres[0]=this.digits[i+thatlength];
    				}else{
    			    int[] temp2=new int[mulres.length+1];
    				System.arraycopy(mulres, 0, temp2, 0, mulres.length);
    				temp2[mulres.length]=this.digits[i+thatlength];
    				mulres=temp2;
    				}
    			}
    			
    			if (Arraycompare(mulres,that.digits)==-1)
    			{
    				digitdivide=0;
    			} else
    			{
    				if (mulres.length==that.digits.length)
    					digitdivide=mulres[0]/that.digits[0];
    				else{
    					long firtwei=(long)mulres[0]*Radix+mulres[1];
    					long ttt=firtwei/that.digits[0];
    					digitdivide=(int) ttt;
    				}
    
    				int[] temp=Multiplyint(that.digits, digitdivide);
    				
    				if (Arraycompare(mulres,temp)==-1){
    					digitdivide-=1;
    					temp=Substract(temp,that.digits);
    				}
    				mulres=Substract(mulres, temp);	
    			}
    			
    			/*
    			 * 最后的mulres就是余数。

    */ return new DecimalBig(1, mulres); }


    十进制转换

    引自Pauls Gedanken的方法

        /**引用
         * Pauls Gedanken在bloghttp://paul-ebermann.tumblr.com/post/6312290327/big-numbers-selfmade-part-2-14-conversion-from
         * 中的方法
         * creates a DecimalBigInt from a decimal representation.
         * @param decimal a string of decimal digits.
         * @throws NumberFormatException if the number is not in
         *     correct decimal format, e.g. if it contains any characters
         *     outside of 0..9.
         */
        public static DecimalBig valueOf(int si, String decimal) {
            int decLen = decimal.length();
            int bigLen = (decLen-1) / Radix_Decimal_length + 1;
            // length of first block
            int firstSome = decLen - (bigLen-1) * Radix_Decimal_length;
            int[] digits = new int[bigLen];
            for(int i = 0; i < bigLen ; i++) {
                String block =
                    decimal.substring(Math.max(firstSome + (i-1)*Radix_Decimal_length, 0),
                                      firstSome +   i  *Radix_Decimal_length);
                digits[i] = Integer.parseInt(block);
            }
            return new DecimalBig(si, digits);
        }
    



  • 相关阅读:
    IO之同步、异步、阻塞、非阻塞 (2)
    IO之同步、异步、阻塞、非阻塞
    Syncthing源码解析
    Syncthing源码解析
    在Gogland里对GO程序进行单元测试!
    GO学习笔记
    GO学习笔记
    GO学习笔记
    GO学习笔记
    GO学习笔记
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5200019.html
Copyright © 2020-2023  润新知