Java分数的加减乘除


 

import java.util.Scanner;

public class demo {

    public static class Person{

        public int molecularone; //分子
        public int denominatorone; //分母
        public String symbol;
        public int moleculartwo;
        public int denominatortwo;
        public int resultone=0;
        public int resulttwo=0;
        public Person(int nextInt, int nextInt1, String next, int nextInt2, int nextInt3) {
            molecularone=nextInt ; //分子
            denominatorone=nextInt1 ; //分母
            symbol=next ;
            moleculartwo=nextInt2 ;
            denominatortwo=nextInt3 ;
        }
        
//求最小公倍数        
        static int gcd(int x,int y){
            int r;
            while( y!= 0) {
                r = x%y;
                x = y;
                y = r;
            }
            return x;
        }
        //加法
        public  void addItion() {

            if (denominatorone == denominatortwo) {
                resulttwo = denominatortwo;
                resultone = molecularone + moleculartwo;
            } else {
                resulttwo = denominatorone * denominatortwo;
                resultone = molecularone * denominatortwo + moleculartwo * denominatorone;
            }
            int gcd=gcd(resultone ,resulttwo );
            if(gcd>0||(resultone>0&&resulttwo >0 )||(resultone<0&&resulttwo <0)) {//防止有的数被去符号化
                resultone = resultone / gcd;
                resulttwo = resulttwo / gcd;
            }else {
                resultone = resultone / -gcd;
                resulttwo = resulttwo / -gcd;
            }
            if (resultone == 0) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + "0");

            } else if (resulttwo == 1) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone );
            } else {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone + "/" + resulttwo);
            }
        }

        //减法
        public  void subtraction(){

            if(denominatorone ==denominatortwo ){
                resulttwo =denominatortwo ;
                resultone =molecularone -moleculartwo ;
            }else{
                resulttwo =denominatorone *denominatortwo ;
                resultone = (molecularone * denominatortwo) - (moleculartwo * denominatorone);
            }
            int gcd=gcd(resultone ,resulttwo );
            if(gcd>0||(resultone>0&&resulttwo >0 )||(resultone<0&&resulttwo <0)) {
                resultone = resultone / gcd;
                resulttwo = resulttwo / gcd;
            }else {
                resultone = resultone / -gcd;
                resulttwo = resulttwo / -gcd;
            }
            if (resultone == 0) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + "0");

            } else if (resulttwo == 1) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone );
            } else {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone + "/" +resulttwo  );
            }
        }

        //乘法
        public void multiplication (){

            resultone =molecularone *moleculartwo ;
            resulttwo =denominatorone *denominatortwo ;
            int gcd=gcd(resultone ,resulttwo );
            if(gcd>0||(resultone>0&&resulttwo >0 )||(resultone<0&&resulttwo <0)) {
                resultone = resultone / gcd;
                resulttwo = resulttwo / gcd;
            }else {
                resultone = resultone / -gcd;
                resulttwo = resulttwo / -gcd;
            }
            if (resultone == 0) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + "0");

            } else if (resulttwo == 1) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone );
            } else {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone + "/" + resulttwo);
            }
        }

        //除法
        public void division ( ){

            resultone =molecularone *denominatortwo ;
            resulttwo =denominatorone *moleculartwo ;
            int gcd=gcd(resultone ,resulttwo );
            if(gcd>0||(resultone>0&&resulttwo >0 )||(resultone<0&&resulttwo <0)) {
                resultone = resultone / gcd;
                resulttwo = resulttwo / gcd;
            }else {
                resultone = resultone / -gcd;
                resulttwo = resulttwo / -gcd;
            }
            if (resultone == 0) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + "0");

            } else if (resulttwo == 1) {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone );
            } else {
                System.out.println("(" + molecularone + "/" + denominatorone + ")" + symbol + "(" + moleculartwo + "/" + denominatortwo + ")" + "=" + resultone + "/" + resulttwo);
            }
        }
    }




    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
             int molecularone= cin.nextInt(); //分子
             int denominatorone=cin .nextInt() ; //分母
             String symbol=cin .next() ;
             int moleculartwo=cin .nextInt() ;
             int denominatortwo=cin .nextInt() ;
            Person person = new Person(molecularone ,denominatorone ,symbol ,moleculartwo,denominatortwo  );
            if (symbol  .equals("+") ) {
                person.addItion();
            } else if (symbol  .equals("-") ) {
                person.subtraction();
            } else if (symbol .equals("*") ) {
                person.multiplication();
            }else if(symbol .equals("/") ) {
                person.division();
            }
        }

    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值