有理数

喜夏-厌秋 提交于 2019-11-30 02:21:56
public class Rational{
    private long numerator=0;//分子
    private long denominator=1;//分母

    public Rational() {
        super();
    }

    public Rational(long numerator, long denominator) {
        // TODO Auto-generated constructor stub
        long gcd=gcd(numerator,denominator);
        this.numerator=((denominator>0)?1:-1)*numerator/gcd;
        this.denominator=Math.abs(denominator)/gcd;
    }
    
    //求两个数最大公约数以便约分
    private static long gcd(long n, long d) {
        // TODO Auto-generated method stub
        long n1=Math.abs(n);
        long n2=Math.abs(d);
        int gcd=1;
        
        for(int k=1;k<=n1&&k<=n2;k++){
            if(n1%k==0&&n2%k==0)
                gcd=k;
        }
        return gcd;
    }
    
    //获得有理数的分子或者分母
    public long getNumerator(){
        return numerator;
    }
    public long getDenominator(){
        return denominator;
    }
    

    //有理数相加
    public Rational add(Rational secondRational){
        long n=numerator*secondRational.getDenominator()+
                denominator*secondRational.getNumerator();
        long d=denominator*secondRational.getDenominator();
        return new Rational(n,d);
    }
    //有理数相减
    public Rational subtract(Rational secondRational){
        long n=numerator*secondRational.getDenominator()-
                denominator*secondRational.getNumerator();
        long d=denominator*secondRational.getDenominator();
        return new Rational(n,d);
    }
    //有理数相乘
    public Rational multiply(Rational secondRational){
        long n=numerator*secondRational.getNumerator();
        long d=denominator*secondRational.getDenominator();
        return new Rational(n,d);
    }
    //有理数相除
    public Rational divide(Rational secondRational){
        long n=numerator*secondRational.denominator;
        long d=denominator*secondRational.numerator;
        return new Rational(n,d);
    }
    
    public String toString(){
        if(denominator==1)
            return numerator+"";
        if(numerator==0)
            return "0";
        else
            return numerator+"/"+denominator;
    }
    
    public boolean equals(Object parm1){
        if(  this.denominator!=0 )
            return true;
        else 
            return false;
    }
         
}

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Rational R=new Rational(sc.nextInt(),sc.nextInt());
/*        long numerator=sc.nextInt();
        long denominator=sc.nextInt();*/
        Rational secondRational=new Rational(sc.nextInt(),sc.nextInt());
/*
        System.out.println("分子:"+R.getNumerator());
        System.out.println("分母:"+R.getDenominator());
        System.out.println(R.toString());
*/      if(R.equals(R)&secondRational.equals(secondRational)) {  
            System.out.println("相加后:"+R.add(secondRational).toString());
            System.out.println("相减后:"+R.subtract(secondRational).toString());    
            if(secondRational.getDenominator()==0||secondRational.getNumerator()==0)
                System.out.println("被除数不能为0");
            else
                System.out.println("相除后:"+R.divide(secondRational).toString());             
            System.out.println("相乘后:"+R.multiply(secondRational).toString());        
        }
        else {
            System.out.println("分母不能为0");
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!