分数的有关操作

2017-04-04  本文已影响0人  _Raye

分数类:

package org.mobilephone;
 /**
  * 分数(有理数)
  * @author apple
  *
  */
public class Fraction {

    private int num;//分子
    private int den;//分母
    
    /**
     * 构造器:指定分子和分母,创建分数对象
     * @param num 分子
     * @param den 分母
     * @throws FractionException 如果分母为0,就会引发异常
     */
    public Fraction(int num,int den) {
        if (den == 0) {
            throw new FractionException("分母不能为零");
        }
        this.num = num;
        this.den = den;
        this.normalize();
        this.simplify();
    } 
    
    /**
     * 构造器:将小数化成分数
     * @param val 一个小数
     */
    public Fraction(double val){
//      int x = (int) (val * 10000);
//      int y = 10000;
//      this.num = x;
//      this.den = y;
        this((int)(val * 10000), 10000);//在构造器里通过this去调用已有的构造器(必须写在构造器里的第一句)
    }
    
    /**
     * 分数的加法
     * @param other 另一个分数
     * @return 相加的结果(新的分数)
     */
    public Fraction add(Fraction other){//加法
        return new Fraction(num * other.den + other.num * den, den * other.den);
    }
    
    /**
     * 分数的减法
     * @param other 另一个分数
     * @return 相减的结果(新的分数)
     */
    public Fraction sub(Fraction other){//减法
        return new Fraction(num * other.den - other.num * den, den * other.den);
    }
    
    /**
     * 分数的乘法
     * @param other 另一个分数
     * @return 相乘后的结果(新的分数)
     */
    public Fraction mul(Fraction other){//乘法
        return new Fraction(num * other.num, den * other.den);
    }
    
    /**
     * 分数的除法
     * @param other 另一个分数
     * @return 相除的结果(新的分数)
     */
    public Fraction div(Fraction other){//除法
        return new Fraction(num * other.den, den * other.num);
    }
    
    /**
     * 分数的正规化:让负号在最前面
     */
    public void normalize(){//分数正规化
        if (den < 0) {
            num = -num;
            den = -den;
        }
    }
    
    /**
     * 化简:分子分母同时除以最大公约数
     */
    public void simplify(){//化简
        if (num != 0) {//取绝对值
            int x = Math.abs(num);
            int y = Math.abs(den);
            int factor = gcd(x, y);
            if (factor > 1) {
                num /= factor;
                den /= factor;//分子分母同时除以最大公约数
            }
        }
    }
    
    @Override
    public String toString() {//tostring方法,让输出结果不是哈希码
        if (num == 0) {
            return "0";
        }
        else if (den == 1) {
            return "" + num;
        }
        else{
            return num + "/" + den;
        }
    }
    
    private int gcd(int x,int y){//找最大公约数
        if (x > y) {
            return gcd(y, x);
        }
        else if (y % x != 0) {
            return gcd(y % x,x);
        }
        else {
            return x;
        }
    }
}

分数操作异常类:

package org.mobilephone;

/**
 * 分数操作异常(运行时异常/非受检异常)
 * @author apple
 *
 */
@SuppressWarnings("serial")
public class FractionException extends RuntimeException {

    /**
     * 构造器
     * @param message 异常相关消息
     */
    public FractionException(String message){
        super(message);
    }
}

测试类:

package org.mobilephone;

public class FractionTest {

    public static void main(String[] args) {
        Fraction f1 = new Fraction(2, 3);
        System.out.println(f1);
        Fraction f2 = new Fraction(3, 4);
        System.out.println(f2);
        System.out.println(f1.add(f2));
        System.out.println(f1.sub(f2));
        System.out.println(f1.mul(f2));
        System.out.println(f1.div(f2));
    }
}
上一篇 下一篇

猜你喜欢

热点阅读