0.目前在项目中有一个分数计算的需求
1.首先我们需要了解一下最小公倍数和最小公约数
最大公因数:两个或者多个整数共有的约数中最大的一个。
求法:欧几里德算法gcd(a,b) = gcd(b,a mod b)首先a>b,直到取余为0,则这个数b位置就是最大公因数。
最小公约数:两个或多个整数的共有的倍数中最小的一个。
求法:[a,b] = axb/(a.b) 最小公倍数=ab乘积除以最大公因数
2.定义一个分数类
public class MyFraction {
private Integer molecular;
private Integer denominator;
public MyFraction() {
}
public MyFraction(Integer molecular, Integer denominator) {
if (molecular == 0 || denominator == 0){
throw new RuntimeException("分子或者分母不能为0");
}
this.molecular = molecular;
this.denominator = denominator;
}
public Integer getMolecular() {
return molecular;
}
public void setMolecular(Integer molecular) {
if (molecular == 0){
throw new RuntimeException("分子不能为0");
}
this.molecular = molecular;
}
public Integer getDenominator() {
return denominator;
}
public void setDenominator(Integer denominator) {
if (denominator == 0 ){
throw new RuntimeException("分母不能为0");
}
this.denominator = denominator;
}
@Override
public String toString() {
return "MyFraction{" +
"molecular=" + molecular +
", denominator=" + denominator +
'}';
}
}
3.定义一个分数工具类
public class FractionUtils {
public static int approximation(Integer x,Integer y){
return x.compareTo(y)>0 ? ((x%y) == 0 ? y : approximation(y,x%y)) : ((y%x) == 0 ? x : approximation(x,y%x));
}
public static int multiple(Integer x,Integer y){
return x*y/approximation(x,y);
}
public static MyFraction add(MyFraction a,MyFraction b){
MyFraction add = new MyFraction();
Integer denominator = multiple(a.getDenominator(), b.getDenominator());
Integer molecular = (a.getMolecular()*(denominator/a.getDenominator())) + (b.getMolecular()*(denominator/b.getDenominator()));
if (molecular==0){
throw new RuntimeException("相加分子为0");
}
Integer approximation = approximation(denominator, Math.abs(molecular));
add.setMolecular(molecular/approximation);
add.setDenominator(denominator/approximation);
return add;
}
public static MyFraction reduce(MyFraction a,MyFraction b){
MyFraction reduce = new MyFraction();
Integer denominator = multiple(a.getDenominator(), b.getDenominator());
Integer molecular = (a.getMolecular()*(denominator/a.getDenominator())) - (b.getMolecular()*(denominator/b.getDenominator()));
if (molecular==0){
throw new RuntimeException("相减分子为0");
}
Integer approximation = approximation(denominator, Math.abs(molecular));
reduce.setMolecular(molecular/approximation);
reduce.setDenominator(denominator/approximation);
return reduce;
}
public static MyFraction multiplication(MyFraction a,MyFraction b){
MyFraction multiplication = new MyFraction();
Integer denominator = a.getDenominator() * b.getDenominator();
Integer molecular = a.getMolecular() * b.getMolecular();
Integer approximation = approximation(denominator, Math.abs(molecular));
multiplication.setMolecular(molecular/approximation);
multiplication.setDenominator(denominator/approximation);
return multiplication;
}
public static MyFraction division(MyFraction a,MyFraction b){
Integer denominator = b.getDenominator();
Integer molecular = b.getMolecular();
b.setDenominator(Math.abs(b.getMolecular()));
b.setMolecular(molecular.compareTo(0)<0 ? -denominator : denominator);
return multiplication(a,b);
}
}