lemsca 2023-04-10 19:43 采纳率: 62.5%
浏览 29
已结题

关于#java#的问题:我该怎么改使得将分数以最简结果形式输出(所有的负分数输出时需用小括号括起来,分母为1时则以整数形式输出,分子为0时无论分母为何值均输出整数0


package sy06.sy01;

import java.util.Scanner;

public class TestFraction {
    public static void main(String[] args) {
        Fraction f2 = null;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter fraction f1:");
        int n1 = sc.nextInt();
        int d1 = sc.nextInt();
        Fraction f1 = new Fraction(n1, d1);
        System.out.println("Please enter fraction f2:");
        int n2 = sc.nextInt();
        int d2 = sc.nextInt();
        f2 = new Fraction(n2, d2);
        sc.close();
        Fraction addRet = null, subRet = null, mulRet = null, divRet = null;
        addRet = f1.add(f2);
        subRet = f1.sub(f2);
        mulRet = f1.mul(f2);
        divRet = f1.div(f2);
        System.out.println(f1 + " + " + f2 + " = " + addRet);
        System.out.println(f1 + " - " + f2 + " = " + subRet);
        System.out.println(f1 + " * " + f2 + " = " + mulRet);
        System.out.println(f1 + " / " + f2 + " = " + divRet);
        System.out.println(f1 + " equals " + f2 + " ? " + f1.equals(f2));
    }
}

class Fraction {
    int nume; // numerator
    int deno; // denominator

    Fraction() {
        this(0, 1);
    }

    Fraction(int nume, int denu) {
        this.nume = nume;
        this.deno = denu;
        reduce();
    }

    Fraction add(Fraction fo) {
        int x;
        int y = this.deno;
        if (this.deno == fo.deno) {
            x = this.nume + fo.nume;
        } else {
            x = this.nume * fo.deno + fo.nume * this.deno;
            y = this.deno * fo.deno;
        }
        return new Fraction(x, y);
    }

    Fraction sub(Fraction fo) {
        int x;
        int y = this.deno;
        if (this.deno == fo.deno) {
            x = this.nume - fo.nume;
        } else {
            x = this.nume * fo.deno - fo.nume * this.deno;
            y = this.deno * fo.deno;
        }
        return new Fraction(x, y);
    }

    Fraction mul(Fraction fo) {
        int x = this.nume * fo.nume;
        int y = this.deno * fo.deno;
        return new Fraction(x, y);
    }

    Fraction div(Fraction fo) {
        if (fo.nume == 0) {
            System.out.println("Cannot be divided by zero!");
            System.exit(-1);
        }
        int x = this.nume * fo.deno;
        int y = this.deno * fo.nume;
        return new Fraction(x, y);
    }

    int getMaxComFactor(int m, int n) {
        if (nume == 0)
            return 1;
        if (m < n) {
            m = m + n;
            n = m - n;
            m = m - n;
        }
        int r = m % n;
        while (r != 0) {
            m = n;
            n = r;
            r = m % n;
        }
        return n;
    }

    void reduce() {
        if (deno == 0) {
            System.out.println("The denominator cannot be zero!");
            System.exit(-1);
        }
        int sign = 1;
        if (nume * deno < 0) {
            sign = -1;
        }
        int absNume = Math.abs(nume);
        int absDenu = Math.abs(deno);
        int maxFac = getMaxComFactor(absNume, absDenu);
        nume = absNume / maxFac * sign;
        deno = absDenu / maxFac;
    }

    @Override
    public String toString() {
        if (nume == 0)
            return "0";
        else if (nume == deno)
            return "1";
        else
            return nume + "/" + deno;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        else if (obj.getClass() == Fraction.class) {
            Fraction ft = (Fraction) obj;
            if (ft.nume == this.nume && ft.deno == this.deno)
                return true;
            else
                return false;
        } else {
            return false;
        }
    }
}

我该怎么改使得将分数以最简结果形式输出(所有的负分数输出时需用小括号括起来,分母为1时则以整数形式输出,分子为0时无论分母为何值均输出整数0

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-04-10 20:40
    关注

    如果你的代码是正确的,那么调用下 reduce
    这就是约分

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月27日
  • 已采纳回答 4月19日
  • 创建了问题 4月10日

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错