Use the C and Java to simulate the BigDecimal calculation

void  add( int   * a,  int   * b,  int   * c) 
    
int i, carry = 0

    
for(i = N - 1; i >= 0; i--
        c[i] 
= a[i] + b[i] + carry; 
        
if(c[i] < 10000
            carry 
= 0
        
else // 進位 
            c[i] = c[i] - 10000
            carry 
= 1
        }
 
    }
 
}
 

void  sub( int   * a,  int   * b,  int   * c) 
    
int i, borrow = 0

    
for(i = N - 1; i >= 0; i--
        c[i] 
= a[i] - b[i] - borrow; 
        
if(c[i] >= 0
            borrow 
= 0
        
else // 借位 
            c[i] = c[i] + 10000
            borrow 
= 1
        }
 
    }
 
}
 

void  mul( int   * a,  int  b,  int   * c)  // b 為乘數 
    int i, tmp, carry = 0

    
for(i = N - 1; i >=0; i--
        tmp 
= a[i] * b + carry; 
        c[i] 
= tmp % 10000;    
        carry 
= tmp / 10000
    }
 
}
 

void  div( int   * a,  int  b,  int   * c)  {  // b 為除數 
    int i, tmp, remain = 0

    
for(i = 0; i < N; i++
        tmp 
= a[i] + remain; 
        c[i] 
= tmp / b; 
        remain 
= (tmp % b) * 10000
    }
 
}
 


    
*  Java

public   class  BigNumber  {
    
public static int[] add(int[] a, int[] b) 
        
int carry = 0;
        
int[] c = new int[a.length];

        
for(int i = a.length - 1; i >= 0; i--
            c[i] 
= a[i] + b[i] + carry; 
            
if(c[i] < 10000
                carry 
= 0
            
else // 進位 
                c[i] = c[i] - 10000
                carry 
= 1
            }
 
        }

        
        
return c;
    }
 

    
public static int[] sub(int[] a, int[] b) 
        
int borrow = 0
        
int[] c = new int[a.length];
        
        
for(int i = a.length - 1; i >= 0; i--
            c[i] 
= a[i] - b[i] - borrow; 
            
if(c[i] >= 0
                borrow 
= 0
            
else // 借位 
                c[i] = c[i] + 10000
                borrow 
= 1
            }
 
        }

        
        
return c;
    }
 

    
public static int[] mul(int[] a, int b) // b 為乘數 
        int carry = 0
        
int[] c = new int[a.length];
        
        
for(int i = a.length - 1; i >=0; i--
            
int tmp = a[i] * b + carry; 
            c[i] 
= tmp % 10000;    
            carry 
= tmp / 10000
        }
 
        
        
return c;
    }
 

    
public static int[] div(int[] a, int b) {  // b 為除數 
        int remain = 0
        
int[] c = new int[a.length];

        
for(int i = 0; i < a.length; i++
            
int tmp = a[i] + remain; 
            c[i] 
= tmp / b; 
            remain 
= (tmp % b) * 10000
        }
 
        
        
return c;
    }

    
    
public static void main(String[] args) {
        
int[] a = {12345678991019231124};
        
int[] b = {12345678991019231124};
        
int[] c = BigNumber.add(a, b);
        
        
for(int i = 0; i < c.length; i++{
            System.
out.print(c[i]);
        }

        System.
out.println();
    }

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值