Java 分数处理工具类

/**
 * @Package_name: com.zdhu.testmybatiesplus.entity
 * @Auther: huzhidong
 * @Date: 2018/12/25 16:43
 * @Description: 分数,注意分母不为0
 */
public class Fraction {
    //分子
    private long numerator;
    //分母
    private long denominator;

    public Fraction(long numerator, long denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    /**
     * @Author huzhidong
     * @Description //最大公约数
     * @Date 17:49 2018/12/25
     * @Param []
     * @return long
     **/
    public long getGcd() {
        long gcd = 0L;
        long tempNum = numerator > 0 ? numerator : -numerator;
        long tempDen = denominator > 0 ? denominator : -denominator;
        if (tempNum < tempDen) {// 交换n1、n2的值
            tempNum = tempNum + tempDen;
            tempDen = tempNum - tempDen;
            tempNum = tempNum - tempDen;
        }
        if (tempNum % tempDen == 0) {
            gcd = tempDen;
        }
        while (tempNum % tempDen > 0) {
            tempNum = tempNum % tempDen;

            if (tempNum < tempDen) {
                tempNum = tempNum + tempDen;
                tempDen = tempNum - tempDen;
                tempNum = tempNum - tempDen;
            }
            if (tempNum % tempDen == 0) {
                gcd = tempDen;
            }
        }
        return gcd;
    }
    /**
     * @Author huzhidong
     * @Description //最小公倍数
     * @Date 17:49 2018/12/25
     * @Param []
     * @return long
     **/
    public long getLcm() {
        long tempNum = numerator > 0 ? numerator : -numerator;
        long tempDen = denominator > 0 ? denominator : -denominator;
        return tempNum * tempDen / getGcd();
    }

    @Override
    public boolean equals(Object obj) {
        // 如果为同一对象的不同引用,则相同
        if (this == obj) {
            return true;
        }
        // 如果传入的对象为空,则返回false
        if (obj == null) {
            return false;
        }

        // 如果两者属于不同的类型,不能相等
        if (getClass() != obj.getClass()) {
            return false;
        }

        // 类型相同, 比较内容是否相同
        Fraction other = (Fraction) obj;
        Fraction thisBast = this.getBast();
        Fraction otherBast = other.getBast();

        return thisBast.getBastString().equals(otherBast.getBastString());
    }

    /***
     * @Author huzhidong
     * @Description //获取最简单表达
     * @Date 17:41 2018/12/25
     * @Param []
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public Fraction getBast(){
        long gcd = this.getGcd();
        long numerator = this.getNumerator() / gcd;
        long denominator = this.getDenominator() / gcd;
        if (numerator < 0 && denominator < 0) {
            return new Fraction(-numerator,-denominator);
        }
        return new Fraction(numerator,denominator);
    }

    /***
     * @Author huzhidong
     * @Description //获取最简单表达
     * @Date 17:41 2018/12/25
     * @Param []
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public String getBastString(){
        Fraction bast = this.getBast();
        long numerator = bast.getNumerator();
        long denominator = bast.getDenominator();
        if (numerator < 0 || denominator < 0) {
            Fraction absolute = bast.absolute();
            return "-"+absolute.getNumerator()+"/"+absolute.getDenominator();
        }
        return numerator+"/"+denominator;
    }

    /***
     * @Author huzhidong
     * @Description //比较当前与other ,小于返回-1 等于返回0 大于返回1
     * @Date 17:32 2018/12/25
     * @Param [fraction]
     * @return int
     **/
    public int compare(Fraction other) {
        if (other == null) {
            new Exception("error:fration is not allow null");
        }
        boolean equals = this.equals(other);
        if (equals) {
            return 0;
        }
        Fraction thisBast = this.getBast();
        Fraction otherBast = other.getBast();
        long thisBastNumerator = thisBast.getNumerator();
        long thisBastDenominator = thisBast.getDenominator();
        long otherBastNumerator = otherBast.getNumerator();
        long otherBastDenominator = otherBast.getDenominator();
        //是否含有负数
        boolean thisHaveRev = false;
        boolean otherHaveRev = false;
        if (thisBastNumerator < 0 || thisBastDenominator < 0) {
            thisHaveRev = true;
        }
        if (otherBastNumerator < 0 || otherBastDenominator < 0) {
            otherHaveRev = true;
        }
        if (thisHaveRev && !otherHaveRev) {
            return -1;
        }
        if (!thisHaveRev && otherHaveRev) {
            return 1;
        }

        return thisBastNumerator * otherBastDenominator > otherBastNumerator * thisBastDenominator ? 1:-1;
    }

    /**
     * @Author huzhidong
     * @Description //判断是否在范围 在true 不在false
     * @Date 18:30 2018/12/25
     * @Param [fraction1, fraction2, type] type -1:左包含 1:右包含 0:左右都包含 2:左右都不包含
     * @return boolean
     **/
    public boolean inRange(Fraction fraction1, Fraction fraction2,Fraction.ContainType type){
        if (fraction1 == null || fraction2 == null) {
            return false;
        }
        int compare = fraction1.compare(fraction2);
        //fraction1 大于 fraction2
        if (compare == 1) {
            Fraction temp = fraction1;
            fraction1 = fraction2;
            fraction2 = temp;
        }
        int compare1 = this.compare(fraction1);
        int compare2 = this.compare(fraction2);
        switch (type){
            case left:
                if (compare1 >= 0 && compare2 == -1) {
                    return true;
                }
                break;
            case both:
                if (compare1 >= 0 && compare2 <= 0) {
                    return true;
                }
                break;
            case right:
                if (compare1 == 1 && compare2 <= 0) {
                    return true;
                }
                break;
            case unboth:
                if (compare1 == 1 && compare2 == -1) {
                    return true;
                }
                break;
            default:

                break;
        }
        return false;
    }

    /**
     * @Author huzhidong
     * @Description //加法
     * @Date 11:37 2018/12/26
     * @Param [fraction]
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public Fraction add(Fraction fraction) {
        if (fraction == null) {
            return null;
        }
        Fraction otherBast = fraction.getBast();
        Fraction thisBast = this.getBast();
        long otherNumerator = otherBast.getNumerator();
        long otherDenominator = otherBast.getDenominator();
        long thisNumerator = thisBast.getNumerator();
        long thisDenominator = thisBast.getDenominator();
        long tempNumerator = otherNumerator * thisDenominator + thisNumerator * otherDenominator;
        long tempDenominator = thisDenominator * otherDenominator;
        return new Fraction(tempNumerator,tempDenominator).getBast();
    }

    /**
     * @Author huzhidong
     * @Description //减法
     * @Date 11:43 2018/12/26
     * @Param [sub]
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public Fraction subtract(Fraction sub) {
        if (sub == null) {
            return null;
        }
        Fraction otherBast = sub.getBast();
        Fraction thisBast = this.getBast();
        long otherNumerator = otherBast.getNumerator();
        long otherDenominator = otherBast.getDenominator();
        long thisNumerator = thisBast.getNumerator();
        long thisDenominator = thisBast.getDenominator();
        long tempNumerator = thisNumerator * otherDenominator - otherNumerator * thisDenominator;
        long tempDenominator = thisDenominator * otherDenominator;
        return new Fraction(tempNumerator,tempDenominator).getBast();
    }

    /**
     * @Author huzhidong
     * @Description //乘法
     * @Date 11:48 2018/12/26
     * @Param [mul]
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public Fraction multiply(Fraction mul) {
        if (mul == null) {
            return null;
        }
        Fraction otherBast = mul.getBast();
        Fraction thisBast = this.getBast();
        long otherNumerator = otherBast.getNumerator();
        long otherDenominator = otherBast.getDenominator();
        long thisNumerator = thisBast.getNumerator();
        long thisDenominator = thisBast.getDenominator();
        long tempNumerator = thisNumerator * otherNumerator;
        long tempDenominator = thisDenominator * otherDenominator;
        return new Fraction(tempNumerator,tempDenominator).getBast();
    }

    /**
     * @Author huzhidong
     * @Description //除法
     * @Date 11:49 2018/12/26
     * @Param [div]
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public Fraction divide(Fraction div) {
        if (div == null) {
            return null;
        }
        Fraction otherBast = div.getBast();
        Fraction thisBast = this.getBast();
        long otherNumerator = otherBast.getNumerator();
        long otherDenominator = otherBast.getDenominator();
        long thisNumerator = thisBast.getNumerator();
        long thisDenominator = thisBast.getDenominator();
        long tempNumerator = thisNumerator * otherDenominator;
        long tempDenominator = thisDenominator * otherNumerator;
        return new Fraction(tempNumerator,tempDenominator).getBast();
    }

    /**
     * @Author huzhidong
     * @Description //绝对值获取
     * @Date 11:57 2018/12/26
     * @Param []
     * @return com.zdhu.testmybatiesplus.entity.Fraction
     **/
    public Fraction absolute() {
        long numerator = this.getNumerator() < 0 ? -this.getNumerator() : this.getNumerator();
        long denominator = this.getDenominator() < 0 ? -this.getDenominator() : this.getDenominator();
        return new Fraction(numerator,denominator).getBast();
    }

    public long getNumerator() {
        return numerator;
    }

    public long getDenominator() {
        return denominator;
    }

    @Override
    public String toString() {
        return "Fraction{" +
                "numerator=" + numerator +
                ", denominator=" + denominator +
                '}';
    }

    public static enum ContainType{
        left(-1,"左包含"),
        right(1,"右包含"),
        both(0,"左右包含"),
        unboth(2,"左右不包含");
        private int type;
        private String desc;

        ContainType(int type, String desc) {
            this.type = type;
            this.desc = desc;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

    public static void main(String[] args) {
        long a = -6;
        long b = 21;
        Fraction fraction1 = new Fraction(a, b);
        long c = 18;
        long d = 42;
        Fraction fraction2 = new Fraction(c, d);
        Fraction add = fraction1.add(fraction2);
        System.out.println("add = " + add.getBastString());
        Fraction subtract = fraction1.subtract(fraction2);
        System.out.println("subtract = " + subtract.getBastString());
        Fraction multiply = fraction1.multiply(fraction2);
        System.out.println("multiply = " + multiply.getBastString());
        Fraction divide = fraction1.divide(fraction2);
        System.out.println("divide = " + divide.getBastString());
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值