简易分数计算器

/*题目7 分数计算器设计
1、定义一个整数类。
定义一个分数类,由整数类派生。能对分数进行各种计算和输入/输出。
2、功能要求:
(1)定义整数类和分数类。其中,包括构造函数、析构函数、显示函数等。
(2)输入/输出:对流提取和流插入运算符进行重载。
(3)计算功能:可进行分数的加、减、乘和除法运算。
(4)化简功能:将分数化简为最简分数。
(5)异常处理功能:分数中分母不能为零。
(6)菜单功能:每种功能的操作都是在菜单中进行相应选择。*/

import java.util.*;

class Integer     //整数类
{
    //定义了一个整数
    int above;

    Integer(int above)   //构造函数
    {
        this.above = above;
    }

    void ShowInt()     //显示函数
    {
        System.out.println("整数为:" + above);
    }
}

class BelowException extends Exception     //自定义异常类
{
    BelowException()     //无参构造方法
    {

    }

    public String toString() {
        return " 输入的分母为0";
    }
}

class Fraction extends Integer//分数类
{
    int below;   //分母

    Fraction(int above, int below) throws BelowException  //构造函数
    {
        super(above);     //above作为分子,调用父类构造方法
        this.below = below;
        if (below == 0)
            throw new BelowException();

    }

    void ShowFra()    //显示函数
    {
        System.out.println("计算得: " + above + "/" + below);
    }

    public void add(Fraction r)     //分数相加函数
    {
        int i;
        int j;
        i = below * r.below;
        j = above * r.below + r.above * below;
        above = j;
        below = i;
        reduction();
        ShowFra();
    }

    public void sub(Fraction r)     //分数相减函数
    {
        int i;
        int j;
        i = below * r.below;
        j = above * r.below - r.above * below;
        above = j;
        below = i;
        reduction();
        ShowFra();
    }

    public void multi(Fraction r)     //乘法函数
    {
        below = below * r.below;
        above = above * r.above;
        reduction();
        ShowFra();
    }

    public void div(Fraction r)     //除法函数,(第一个分数乘以第二个分数的倒数)
    {
        int i;
        i = r.below;
        r.below = r.above;
        r.above = i;
        below = below * r.below;
        above = above * r.above;
        reduction();
        ShowFra();
    }

    void reduction()       //约分函数,化为最简分数。
    {
        int i, j;
        // 判断分子是否为0,返回
        if (above == 0) {
            below = 0;
            return;
        }
        if (Math.abs(above) > Math.abs(below))   //将分子、分母中小的那个数的绝对值赋值给j,
            j = below;
        else
            j = above;
        if (j < 0)
            j = Math.abs(j);
        // 利用for循环寻找最大公约数,分子分母化简
        for (i = j; i > 1; i--)
            if (below % i == 0 && above % i == 0) {
                below /= i;
                above /= i;
                return;
            }

    }

}

public class Design {
    public static void main(String[] args) {
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key;
        do {
            System.out.println("================================");
            System.out.println("=======欢迎进入 :分数计算器========");
            System.out.println("1.加法运算");
            System.out.println("2.减法运算");
            System.out.println("3.乘法运算");
            System.out.println("4.除法运算");
            System.out.println("5.化简运算");
            System.out.println("6.退出");
            System.out.println("================================");


            System.out.println("请输入你所需要的操作:");
            key = scanner.next();
            //使用switch分支

            switch (key) {
                case "1" -> {
                    System.out.println("加法");
                    System.out.println("请输入第一个分数的分子: 分母:");
                    Fraction Fra1;
                    try {
                        Fra1 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;
                    }
                    System.out.println("请输入第二个分数的分子: 分母:");
                    Fraction Fra2;
                    try {
                        Fra2 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;
                    }
                    Fra1.add(Fra2);
                }
                case "2" -> {
                    System.out.println("减法");
                    System.out.println("请输入第一个分数的分子: 分母:");
                    Fraction Fra3;
                    try {
                        Fra3 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;

                    }
                    System.out.println("请输入第二个分数的分子: 分母:");
                    Fraction Fra4;
                    try {
                        Fra4 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;
                    }
                    Fra3.sub(Fra4);
                }
                case "3" -> {
                    System.out.println("乘法");
                    System.out.println("请输入第一个分数的分子: 分母:");
                    Fraction Fra5;
                    try {
                        Fra5 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println("返回菜单,请重新选择:");
                        continue;

                    }
                    System.out.println("请输入第二个分数的分子: 分母:");
                    Fraction Fra6;
                    try {
                        Fra6 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;

                    }
                    Fra5.multi(Fra6);
                }
                case "4" -> {
                    System.out.println("除法");
                    System.out.println("请输入第一个分数的分子: 分母:");
                    Fraction Fra7;
                    try {
                        Fra7 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;

                    }
                    System.out.println("请输入第二个分数的分子: 分母:");
                    Fraction Fra8;
                    try {
                        Fra8 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println("返回菜单,请重新选择:");
                        continue;

                    }
                    Fra7.div(Fra8);
                }
                case "5" -> {
                    System.out.println("化简");
                    System.out.println("请输入分数的分子: 分母:");
                    Fraction Fra9;
                    try {
                        Fra9 = new Fraction(scanner.nextInt(), scanner.nextInt());
                    } catch (BelowException e) {
                        System.out.println(e);
                        System.out.println(" 返回菜单,请重新选择:");
                        continue;

                    }
                    Fra9.reduction();
                    Fra9.ShowFra();
                }
                case "6" -> {
                    System.out.println("退出");
                    loop = false;
                }
                default -> System.out.println("输入有误,请重新选择");
            }
        } while (loop);
        System.out.println("==========退出了分数计算器==========");
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值