您的位置:首页 > 编程语言 > Java开发

有理数类 Java BigInteger实现

2015-12-06 17:37 567 查看
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: