有理数类 Java BigInteger实现

import java.math.BigInteger;

public class Rational extends Number implements Comparable {

private BigInteger numerator;// 分子
private BigInteger denominator;// 分母

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
Rational rational1 = new Rational(new BigInteger(1 + ""),
new BigInteger(10 + ""));
Rational rational2 = new Rational(new BigInteger(1 + ""),
new BigInteger(-10 + ""));
System.out.println(rational1.add(rational2));
System.out.println(rational1.subtract(rational2));
System.out.println(rational1.multiple(rational2));
System.out.println(rational1.divide(rational2));
}

public Rational() {

// TODO Auto-generated constructor stub
this(BigInteger.ZERO, BigInteger.ONE);
}

public Rational(BigInteger numerator, BigInteger denominator) {

BigInteger gcd = gcd(numerator, denominator);
this.numerator = ((denominator.compareTo(BigInteger.ZERO)) > 0 ? BigInteger.ONE
: new BigInteger(-1 + "")).multiply(numerator).divide(gcd);
this.denominator = denominator.abs().divide(gcd);
}

public static BigInteger gcd(BigInteger a, BigInteger b) {

BigInteger n1 = a.abs();
BigInteger n2 = b.abs();
BigInteger remainder = n1.remainder(n2);
while (remainder.compareTo(BigInteger.ZERO) > 0) {
n1 = n2;
n2 = remainder;
remainder = n1.remainder(n2);
}
return n2;
}

public BigInteger getNumerator() {

return numerator;
}

public BigInteger getDenominator() {

return denominator;
}

public Rational add(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}

public Rational subtract(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getDenominator())
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}

public Rational multiple(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}

public Rational divide(Rational secondRational) {

BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.getNumerator());
return new Rational(n, d);
}

@Override
public boolean equals(Object obj) {

// TODO Auto-generated method stub
if (this.getNumerator().compareTo(((Rational) obj).getNumerator()) == 0) {
return true;
}
else {
return false;
}
}

@Override
public String toString() {

// TODO Auto-generated method stub
if (denominator.compareTo(BigInteger.ONE) == 0) {
return numerator.toString();
}
else {
return numerator.toString() + "/" + denominator.toString();
}
}

@Override
public int intValue() {

// TODO Auto-generated method stub
return numerator.divide(denominator).intValue();
}

@Override
public long longValue() {

// TODO Auto-generated method stub
return numerator.divide(denominator).longValue();
}

@Override
public float floatValue() {

// TODO Auto-generated method stub
return numerator.divide(denominator).floatValue();
}

@Override
public double doubleValue() {

// TODO Auto-generated method stub
return numerator.divide(denominator).doubleValue();
}

@Override
public int compareTo(Object o) {

// TODO Auto-generated method stub
if (this.getNumerator().compareTo(((Rational) o).getNumerator()) > 0) {
return 1;
}
else if (this.getNumerator().compareTo(((Rational) o).getNumerator()) < 0) {
return -1;
}
else {
return 0;
}
}
}

转载于:https://www.cnblogs.com/diyishijian/p/5023954.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值