实现大数字加减。



public class Biginter2 {


boolean sign = true;  //纪录整数或者负数
int  arr[] ; //默认存储的数组

public Biginter2(){
arr = new int[1];
arr[0] = 0;
}
public Biginter2(int num){
this(String.valueOf(num));
}
public Biginter2(int[] arr){
this.arr = arr;
}
public Biginter2(String str){   //构造方法的规范。
StringBuilder sb = new StringBuilder(str);
if(str.charAt(0) == '-'){
sb.deleteCharAt(0);
this.sign = false;
}else{
this.sign = true;
}

arr = new int[sb.length()];
for(int i=0;i<sb.length();i++){
switch(sb.charAt(i)){
case '0':arr[i] = 0;break;
case '1':arr[i] = 1;break;
case '2':arr[i] = 2;break;
case '3':arr[i] = 3;break;
case '4':arr[i] = 4;break;
case '5':arr[i] = 5;break;
case '6':arr[i] = 6;break;
case '7':arr[i] = 7;break;
case '8':arr[i] = 8;break;
case '9':arr[i] = 9;break;
}
}

}



public Biginter2 add(int num){
return this.add(String.valueOf(num));
}



public Biginter2 add(String another){
return this.add(new Biginter2(another));
}
 
public Biginter2 add(Biginter2 big){
Biginter2 bigint = new Biginter2();  //新建一个对象
if(this.sign == big.sign){

 bigint = this.addnum(this.arr, big.arr);   //计算同号相加减。
 
 bigint.sign = this.sign; 
}else{
bigint = divnum(this.arr,big.arr);
}


return bigint;
}



public  Biginter2 divnum(int[] temp1,int[] temp2){
Biginter2 big = new Biginter2();
int length1  = temp1.length; 
int length2  = temp2.length;
int biglength = length1>length2?length1:length2;  //获取较长的那个数组长度
int temp[]  = new int[biglength];

//数组A>B  或者  数组相等 , 但 A的最高位>B的最高位
if((length1 == length2 && temp1[0]>=temp2[0]) || length1 > length2 ){
for(int i=1;i<=biglength;i++){
int i1 = (length1 - i < 0)?0:temp1[length1 - i];
int i2 = (length2 - i < 0)?0:temp2[length2 - i];

temp[biglength - i] = i1 - i2;  
}


big.sign = true;
}else{
for(int i=1;i<=biglength;i++){
int i1 = (length2 - i < 0)?0:temp2[length2 - i];
int i2 = (length1 - i < 0)?0:temp1[length1 - i];

temp[biglength - i] = i1 - i2;  
}
big.sign = false;
}

//执行借位
for(int i=biglength-1;i>=0;i--){
if(temp[i] < 0 ){
temp[i] +=10; 
if(i!=0){
temp[i - 1] -= 1 ;
}
}
}
//删除0操作
temp = del(temp);


big.arr = temp;


return big;
}

//2个正数,或者2个负数
public  Biginter2 addnum(int[] temp1,int[] temp2){
Biginter2 big = new Biginter2();
int length1  = temp1.length; 
int length2  = temp2.length;
int biglength = length1>length2?length1:length2;  //获取较长的那个数组长度
int temp[]  = new int[biglength];

for(int i=1;i<=biglength;i++){
int i1 = (length1 - i < 0 )?0:temp1[length1 - i ];
int i2 = (length2 - i < 0 )?0:temp2[length2 - i ];

int s =  i1 + i2;

temp[biglength - i] = s;
}
//计算完成后才进行进位操作。
for(int i=biglength-1;i>=0;i--){
if(temp[i]>=10){
if(temp[0]>=10)break;  //最高位如果要进位,则数组长度不够。
temp[i] -= 10;
temp[i-1] +=1;
}
}
//如果最高位权需要进位,则新建一个数组重新传递。
if(temp[0]>=10){
int newtemp[] = new int[biglength + 1];
newtemp[0] = 1;
for(int i=1;i<biglength;i++){
newtemp[i] = temp[i];
}

}
// temp = this.del(temp);
big.arr = temp;
return big;
// big.toString();
}



private static int[] del(int [] a){
int[] temp = null ;
int index = 0;
// StringBuilder strb = new StringBuilder();
for(int i=0;i<a.length;i++){
// strb.append(a[i]);
if(a[i] == 0){
index++;
}else{
break;
}

}

//1,前面有0
//2,前面一段距离有0
//3,前面没0
if(index == a.length){
temp = new int[1];
temp[0] = 0;
}else{
int array[] = new int[a.length - index];
for(int i=index,j = 0;i<a.length;i++,j++){
array[j] = a[i];
}
temp = array;
}


return temp;
// return show(temp);
}
 
public String toString(){
StringBuilder sb = new StringBuilder();
//若为负数,加上负号
if( (sb.length()==1&&sb.charAt(0)=='0')){
 
}
else if(this.sign == false ){
sb.append("-");
}
 
for(int i=0;i<this.arr.length;i++){
sb.append(this.arr[i]);
}
String str = new String(sb);
return str;
}

public static void main(String args[]){
Biginter2 b1 = new Biginter2("123456");
Biginter2 b2 = new Biginter2("1000000");

//测试加法。
System.out.println(b1.add(b2));

System.out.println("==================分割线======================");

Biginter2 b3= new Biginter2("123456");
Biginter2 b4 = new Biginter2("-123456");

//测试减法
System.out.println(b3.add(b4));



//测试减法
System.out.println(b3.add("-100000"));
}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值