码迷,mamicode.com
首页 > 其他好文 > 详细

利用字符串和十进制逢10进位的特性实现大数据的算术运算。(加法案例)

时间:2015-08-16 13:38:18      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

java中普通的数据类型int,long等不支持大数据间的算术运算,会出现丢失精度的问题,甚至无法用现有数据类型表示。

例如:对这两个数做加法运算:1243543534646546546465465465464654+32423985348957348957348957348957348574=?

在java中Long类型已经无法表示了,Long类型的最大值为Long.MAX_VALUE=9223372036854775807。

但jdk类库中提供了BigInteger类型,可以解决。

这里我用字符串和十进制运算进位的特性实现下大数的加法运算,其它的运算还没来得及做。

本人的技术一般,懂得东西太少,要学的东西太多,时间实在紧迫,因此博客写的简单些,只把主要的东西将清楚,算留作自己的技术笔记!

代码:

  1 /**
  2  * Project Name:javaBaseReview
  3  * Package Name:com.xuwei.base.algorithm
  4  * author:David
  5  * Date:2015年8月16日上午11:00:53
  6  * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
  7  *
  8  */
  9 package com.xuwei.base.algorithm;
 10 
 11 /**
 12  * 利用字符串和十进制进位特性实现大数据之间的算术运算
 13  * @author David
 14  * @since 2015年8月16日 上午11:00:53
 15  * @version 
 16  * @since JDK 1.6
 17  */
 18 public class BigInt{
 19     private String valStr;
 20     private int[] val;
 21     /**
 22      * @param string
 23      */
 24     public BigInt(String val) {
 25         int[]val2=null;
 26         val2=str2IntArr(val);
 27         this.val=val2;
 28         this.valStr=val;
 29     }
 30 
 31     public String add(String val){
 32         int[]res=null;
 33         res=add(this.val, str2IntArr(val));
 34         return toString(res);
 35     }
 36     
 37     public String add(BigInt val){
 38         int[]res=null;
 39         res=add(this.val, str2IntArr(val.toString()));
 40         return toString(res);
 41     }
 42     
 43     private String toString(int[] arr){
 44         StringBuilder sb=new StringBuilder();
 45         for(int i=0;i<arr.length;i++){
 46             sb.append(arr[i]);
 47         }
 48         return sb.toString();
 49     }
 50     
 51 
 52     /**
 53      * 字符串数字转换int数组
 54      * @param val
 55      * @return
 56      */
 57     private int[] str2IntArr(String val) {
 58         char[]ch=val.toCharArray();
 59         int[]toIntArr=new int[ch.length];
 60         for(int i=0;i<ch.length;i++){
 61             toIntArr[i]=Integer.parseInt(ch[i]+"");
 62         }
 63         return toIntArr;
 64     }
 65     
 66     /* 加法 */
 67     public int[] add(int[] a, int[] b) {
 68         /* 定义进位值,初始为0 */
 69         int remainder = 0;
 70         /* 定义返回的数组 */
 71         int[] res = null;
 72         int[] c=null;
 73         if(a.length!=b.length){
 74             if(a.length<b.length){//a<b
 75                 res=new int[b.length];
 76                 c=new int[b.length];
 77                 for(int i=c.length-a.length,j=0;i<c.length;i++,j++){
 78                     c[i]=a[j];
 79                 }
 80                 add(c, b, remainder, res);
 81                 return res;
 82             }else{//a>b
 83                 res=new int[a.length];
 84                 c=new int[a.length];
 85                 for(int i=c.length-b.length,j=0;i<c.length;i++,j++){
 86                     c[i]=b[j];
 87                 }
 88                 add(c, a, remainder, res);
 89                 return res;
 90             }
 91             
 92         }else{
 93             res=new int[a.length];
 94             add(a, b, remainder, res);
 95             return res;
 96         }
 97     }
 98 
 99     //对2个位数相同的数进行加法运算
100     private void add(int[] a, int[] b, int remainder, int[] res) {
101         for (int i = a.length - 1; i >= 0; i--) {
102             res[i] = a[i] + b[i] + remainder;
103             if (res[i] < 10) {
104                 remainder = 0;
105             } else {
106                 remainder = 1;
107                 if(i!=0)
108                     res[i] = res[i]-10;
109             }
110         }
111     }
112 
113     @Override
114     public String toString() {
115         return this.valStr;
116     }
117     
118     
119 }

 

 1 /**
 2  * Project Name:javaBaseReview
 3  * Package Name:com.xuwei.base.algorithm
 4  * author:David
 5  * Date:2015年6月15日下午6:02:04
 6  * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 7  *
 8  */
 9 package com.xuwei.base.algorithm;
10 
11 import java.math.BigInteger;
12 
13 /**
14  * 用字符串实现大数据之间的算术运算
15  * 
16  * @author David
17  * @since 2015年6月15日 下午6:02:04
18  * @version
19  * @since JDK 1.6
20  */
21 public class BigCalc {
22     public static void main(String[] args) {
23         Long a=Long.MAX_VALUE;
24         long b=Long.MAX_VALUE;
25         System.out.println(a);
26         System.out.println(a+b);
27         
28         
29 
30 //        BigInt a=new BigInt("239385234454");
31 //        BigInt b=new BigInt("1223234454");
32 //        System.out.println(a.add(b));
33 //        
34 //        BigInteger c=new BigInteger("239385234454");
35 //        BigInteger d=new BigInteger("1223234454");
36 //        System.out.println(c.add(d));
37         
38     }
39 
40 }

 测试结果:

 技术分享

测试结果表明运算结果与Jdk的BigInteger算的结果相同!

利用字符串和十进制逢10进位的特性实现大数据的算术运算。(加法案例)

标签:

原文地址:http://www.cnblogs.com/davidxu/p/4733956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!