zoukankan      html  css  js  c++  java
  • 有理数类 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;
    }
    }
    }

  • 相关阅读:
    Kubernetes基础:Pod的详细介绍
    十分钟带你理解Kubernetes核心概念
    kubectl命令行工具用法详解
    GDPR给安全的影响
    开源软件会被云杀死吗 ?
    VMware前路难测,多个厂家群雄逐鹿
    如何实现linux+windows双系统启动
    IT行业——Linux
    i3 窗口管理器使 Linux 更美好
    在 Linux 中使用超级用户权限
  • 原文地址:https://www.cnblogs.com/diyishijian/p/5023954.html
Copyright © 2011-2022 走看看