• 有理数类 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;
    }
    }
    }

  • 相关阅读:
    PHP使用Memcache来存储session 其他【转载】
    Linux 学习记录 20170218
    php 数组去重
    关于php的array_diff和array_diff_assoc的使用总结
    使用谷歌浏览器调试WEB前端的一些必备调试技巧
    MySql 赋值操作符"="与":="
    移动设备检测类Mobile_Detect.php
    PHP Filter 函数 日常可用
    以符合人类阅读的方式打印php数组【转载】
    JavaWeb:HttpSession(一)
  • 原文地址:https://www.cnblogs.com/diyishijian/p/5023954.html
Copyright © 2020-2023  润新知