Java自定义分数类,可以实现分数的自由运算

本文介绍了Fraction类,用于处理分数对象,包括分数的构造、格式验证、基本运算(加、减、乘、除)以及化简,以整数表示分数并提供转换为浮点数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/**
 * 分数对象的类,有分数相关计算
 * 以String为值,(String)value= (int)up+"/"+(int)down
 * @author Zhan
 */
public class Fraction {

    //分数标准
    static final String standard1 = "-?\\d+/-?\\d+";//有分母
    static final String standard2 = "-?\\d+";//无分母

    //值String,分子up,分母down
    private String value;
    private int up;
    private int down;

    /**
     * 无参构造器,值为0
     */
    public Fraction() {
        this.value = "0";
        this.up = 0;
        this.down = 1;
    }

    /**
     * String为参的构造器
     */
    public Fraction(String value) {
        if (value == null || value.equals("")) {
            this.value = "0";
        } else if (value.matches(standard2)) {
            this.value = value;
            this.up = Integer.parseInt(value);
            this.down = 1;
        }else if (value.matches(standard1)){
            int mid = value.indexOf("/");
            this.value = value;
            this.up = Integer.parseInt(value.substring(0,mid));
            this.down = Integer.parseInt(value.substring(mid+1));
        } else {
            System.out.println("分式格式错误");
        }
    }

    /**
     * 以int类型的分子分母的构造器
     */
    public Fraction(int up, int down) {
        if (down==0){
            System.out.println("分子不可为零");
        }else {
            this.up = up;
            this.down = down;
            this.value = up + "/" + down;
        }
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        value = value.trim();
        if (value.matches(standard1) || value.matches(standard2)) {
            this.value = value;
        } else {
            System.out.println("分式格式错误");
        }
    }

    //分数相加
    public Fraction add(Fraction s2) {
        this.up = this.up*s2.down+this.down*s2.up;
        this.down = this.down*s2.down;
        this.reduction();
        return this;
    }

    //分数相减
    public Fraction reduce(Fraction s2) {
        this.up = this.up*s2.down-this.down*s2.up;
        this.down = this.down*s2.down;
        this.reduction();
        return this;
    }

    //分数相乘
    public Fraction multiply(Fraction s2) {
        this.up = this.up*s2.up;
        this.down = this.down*s2.down;
        this.reduction();
        return this;
    }

    //分数相除
    public Fraction device(Fraction s2) {
        int valueUp = this.up*s2.down;
        this.down = this.down*s2.up;
        this.up = valueUp;
        this.reduction();
        return this;
    }

    //分数化简
    private void reduction() {
        if (this.up==0){
            this.value="0";
            this.down =1;
        }else if (this.down==1) {
            this.value = this.up + "";
        }else if (this.down==-1){
            this.down = 1;
            this.up = -up;
            this.value = this.up+"";
        }else {
            int a = this.up, b = this.down;
            int r ;//最大公因数
            do{
                r = a % b ;
                a = b;
                b = r;
            }while(b != 0);
            this.up /= a;
            this.down /= a;
            if (this.down==1){
                this.value = up+"";
            }else if (this.down==-1){
                this.down = 1;
                this.up = -up;
                this.value = up+"";
            }else if (this.down<0){
                this.down = -this.down;
                this.up = -this.up;
                this.value = up+"/"+down;
            }else {
                this.value = up+"/"+down;
            }
        }
    }

    //获取当前分数的float值
    public float fractionToFloat() {
        return (0f+up)/down;
    }

    @Override
    public String toString() {
        return value;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值