Created
October 27, 2021 02:41
-
-
Save yanfeng42/a653f819c54a72bb07cb03907f6f8590 to your computer and use it in GitHub Desktop.
alg4 1.2.16 实现一个简易的 分数 计算工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.StdOut; | |
// for 1.2.16 | |
public class Rational { | |
// 分子. | |
private long numerator; | |
// 计算最大公约数. ref: https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 | |
private static long gcd(long a, long b) { | |
if (b == 0) { | |
return a; | |
} else { | |
return gcd(b, a % b); | |
} | |
} | |
// 分母. | |
private long denominator; | |
public long numerator() { | |
return this.numerator; | |
} | |
public long denominator() { | |
return this.denominator; | |
} | |
Rational(long numerator, long denominator) { | |
assert numerator > Integer.MIN_VALUE && numerator < Integer.MAX_VALUE && | |
denominator > Integer.MIN_VALUE && denominator < Integer.MAX_VALUE && | |
numerator * denominator > Integer.MIN_VALUE && numerator * denominator < Integer.MAX_VALUE | |
: "numerator or denominator is overflow"; | |
long gcdNumber = gcd(Math.abs(numerator), Math.abs(denominator)); | |
if (gcdNumber > 1) { // 保证没有公因子. | |
numerator /= gcdNumber; | |
denominator /= gcdNumber; | |
} | |
if (numerator * denominator < 0) { | |
numerator = -Math.abs(numerator); | |
} else { | |
numerator = Math.abs(numerator); | |
} | |
denominator = Math.abs(denominator); | |
this.numerator = numerator; | |
this.denominator = denominator; | |
} | |
public Rational plus(Rational b) { | |
return new Rational(this.numerator() * b.denominator() + b.numerator() * this.denominator(), | |
this.denominator() * b.denominator()); | |
} | |
public Rational minus(Rational b) { | |
return plus(new Rational(-b.numerator(), b.denominator())); | |
} | |
public Rational times(Rational b) { | |
return new Rational(this.numerator() * b.numerator(), | |
this.denominator() * b.denominator()); | |
} | |
public Rational divides(Rational b) { | |
return times(new Rational(b.denominator(), b.numerator())); | |
} | |
public boolean equals(Rational b) { | |
return this.numerator() == b.numerator() && this.denominator == b.denominator(); | |
} | |
public String toString() { | |
return this.numerator() + "/" + this.denominator(); | |
} | |
public static void main(String[] args) { | |
Rational a = new Rational(1, 3); | |
Rational b = new Rational(2, 3); | |
StdOut.println(a.plus(b).equals(new Rational(1, 1))); | |
StdOut.println(a.minus(b).equals(new Rational(-1, 3))); | |
StdOut.println(a.times(b).equals(new Rational(2, 9))); | |
StdOut.println(a.divides(b).equals(new Rational(1, 2))); | |
StdOut.println(a.times(b).toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
所以说, 不用羡慕 直接支持 分数计算的 编程语言.
在特定业务场景下, 完全可以自己实现 基于 分数的 较精确的 数学计算.