一个实现长整型数相加减的小程序

//联合国科教文组织要统计人口,显然一般的int不能满足需要,因此要求定义一个BigInteger的新类,
//多写孔乙己提醒,那是我初学Java时写的,实在不该帖出来,现在重写了代码.

//时间关系,只实现了加法.

BigIntegerTest.java

/**
 * 
@author Rey
 * @date 2006-10-2
 
*/

public class BigIntegerTest extends TestCase {
    
public void test1() {
        BigInteger b1 
= new BigInteger("11");
        BigInteger b2 
= new BigInteger("11");
        BigInteger b3 
= new BigInteger("2");
        assertEquals(b1, b2);
        assertFalse(b1.equals(b3));
    }


    
public void test2() {
        BigInteger b1 
= new BigInteger("12");
        BigInteger b2 
= new BigInteger("8");
        BigInteger b3 
= b1.add(b2);
        assertEquals(
new BigInteger("20"), b3);
    }


    
public void test3() {
        BigInteger b1 
= new BigInteger("99");
        BigInteger b2 
= new BigInteger("99");
        assertEquals(
new BigInteger("198"), b1.add(b2));
    }


    
public void test4() {
        BigInteger b1 
= new BigInteger("9999999999999999");
        BigInteger b2 
= new BigInteger("1");
        assertEquals(
new BigInteger("10000000000000000"), b1.add(b2));
    }


    
public void test5() {
        BigInteger b1 
= new BigInteger("1");
        BigInteger b2 
= new BigInteger("9999999999999999");
        assertEquals(
new BigInteger("10000000000000000"), b1.add(b2));
    }

}

BigInteger.java

/**
 * 
@author Rey
 * @date 2006-10-2
 *
 
*/

public class BigInteger {

    
private String value = null;

    
public BigInteger(String value) {
        
this.value = value;
    }


    
public BigInteger add(BigInteger b2) {

        
char[] a = value.toCharArray();
        
char[] b = b2.value.toCharArray();
        
        
/** 把最长的放到a里面 */
        
if (a.length < b.length) {
            
char[] temp = null;
            temp 
= a;
            a 
= b;
            b 
= temp;
        }


        
int size = a.length + 1;
        
char[] result = new char[size];
        
int j = result.length - 1;
        
/** 把b array的值copy到result中, 前面空位补48 */
        
for (int i = b.length - 1; i >= 0; i--, j--{
            result[j] 
= b[i];
        }

        
for (; j >= 0; j--{
            result[j] 
= 48;
        }

        
        
        
/** result = a + result */
        
for (int i = a.length - 1, sum = 0; i >= 0; i--{
            sum 
= a[i] + result[i + 1];
            
if (sum >= 106{
                result[i 
+ 1= (char) (sum - 10 - 48);
                result[i]
++;
            }
 else {
                result[i 
+ 1= (char) (sum - 48);
            }

        }

        
        
/** char[]转化为String value */
        StringBuffer sb 
= new StringBuffer();
        
for (int i = 0; i < result.length; i++{
            
if (i == 0 && (int) result[i] == 48)
                
continue;
            sb.append((
char) result[i]);
        }

        
return new BigInteger(sb.toString());
    }


    @Override
    
public int hashCode() {
        
final int PRIME = 31;
        
int result = 1;
        result 
= PRIME * result + ((value == null? 0 : value.hashCode());
        
return result;
    }


    @Override
    
public boolean equals(Object obj) {
        
if (this == obj)
            
return true;
        
if (obj == null)
            
return false;
        
if (getClass() != obj.getClass())
            
return false;
        
final BigInteger other = (BigInteger) obj;
        
if (value == null{
            
if (other.value != null)
                
return false;
        }
 else if (!value.equals(other.value))
            
return false;
        
return true;
    }


    @Override
    
public String toString() {
        
return value;
    }


}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值