使用BigInteger将double转换为fraction

时间:2015-11-29 05:30:02

标签: java

我学习Java。

我正在尝试使用Rational类中的BigInteger将decimal转换为fraction。

如果我打印分子和分母,则输出正确的输出。 但是,当我加,减,乘,除时,会出现一些奇怪的输出。

例如,当我输入

3.25 -3

输出

13 4
-3 1
RationalTest@14ae5a5
RationalTest@7f31245a
RationalTest@6d6f6e28
RationalTest@135fbaa4

我的代码出了什么问题?什么是哈希码? 我无法在互联网上找到,所以我需要你的帮助:<

import java.util.Scanner;
import java.math.*;

public class TestTest
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        RationalTest r1 = RationalTest.getFraction(sc.next());
        RationalTest r2 = RationalTest.getFraction(sc.next());
        //Test if the right numerator and denominator comes out
        System.out.println(r1.getNumerator()+" "+r1.getDenominator());
        System.out.println(r2.getNumerator()+" "+r2.getDenominator());

        System.out.println(r1.add(r2));
        System.out.println(r1.subtract(r2));
        System.out.println(r1.multiply(r2));
        System.out.println(r1.divide(r2));
    }
}

class RationalTest extends Number implements Comparable<RationalTest>
{
    private static final long serialVersionUID = 1L;
    private BigInteger numerator = BigInteger.ZERO;
    private BigInteger denominator = BigInteger.ONE;

    public RationalTest()
    {
        this(BigInteger.ZERO, BigInteger.ONE);
    }
    //Find GCD of numerator and denominator
    public RationalTest(BigInteger numerator, BigInteger denominator)
    {
        BigInteger gcd = gcd(numerator, denominator);
        this.numerator = ((denominator.compareTo(BigInteger.ZERO)>0) ? new BigInteger("1"):new BigInteger("-1")).multiply(numerator.divide(gcd));
        this.denominator = denominator.abs().divide(gcd);
    }
    //Converting decimal to fraction
    public static RationalTest getFraction(String s)
    {
        int result=-1;

        for(int i=0; i<s.length(); i++)
        {
            if(s.charAt(i)=='.')
            {
                result=1;
                break;
            }
            else
                result=0;
        }
        //If result=1, String s is a decimal
        if(result==1)
        {
            double d = Double.parseDouble(s);
            long num = (long) Math.floor(d); // Only the int part
            double denom = d-num; // Only the decimal part
            int digitDec = s.length()-1-s.indexOf('.');

            long up = (int) (denom*Math.pow(10, digitDec)); // numerator of denom
            long down = (int) Math.pow(10, digitDec); // denominator of denim

            return new RationalTest(BigInteger.valueOf(down*num+up), BigInteger.valueOf(down));
        }
        //If result=0, String s is not a decimal
        else
        {
            return new RationalTest(BigInteger.valueOf(Long.parseLong(s)), BigInteger.ONE);
        }
    }

    private static BigInteger gcd(BigInteger n, BigInteger d)
    {
        BigInteger n1 = n.abs();
        BigInteger n2 = d.abs();
        BigInteger gcd = BigInteger.ONE;

        for(BigInteger k=BigInteger.ONE; k.compareTo(n1)<=0 && k.compareTo(n2)<=0; k=k.add(BigInteger.ONE))
        {
            if(n1.mod(k).equals(BigInteger.ZERO) && n2.mod(k).equals(BigInteger.ZERO))
                gcd = k;
        }

        return gcd;
    }

    public BigInteger getNumerator()
    {
        return numerator;
    }

    public BigInteger getDenominator()
    {
        return denominator;
    }

    public RationalTest add(RationalTest secondRationalTest)
    {
        BigInteger n = (numerator.multiply(secondRationalTest.getDenominator())).add(denominator.multiply(secondRationalTest.getNumerator()));
        BigInteger d = denominator.multiply(secondRationalTest.getDenominator());
        return new RationalTest(n, d);
    }

    public RationalTest subtract(RationalTest secondRationalTest)
    {
        BigInteger n = (numerator.multiply(secondRationalTest.getDenominator())).subtract(denominator.multiply(secondRationalTest.getNumerator()));
        BigInteger d = denominator.multiply(secondRationalTest.getDenominator());
        return new RationalTest(n, d);
    }

    public RationalTest multiply(RationalTest secondRationalTest)
    {
        BigInteger n = numerator.multiply(secondRationalTest.getNumerator());
        BigInteger d = denominator.multiply(secondRationalTest.getDenominator());
        return new RationalTest(n, d);
    }

    public RationalTest divide(RationalTest secondRationalTest)
    {
        BigInteger n = numerator.multiply(secondRationalTest.getDenominator());
        BigInteger d = denominator.multiply(secondRationalTest.getNumerator());
        return new RationalTest(n, d);
    }

    @Override
    public boolean equals(Object other)
    {
        if((this.subtract((RationalTest)(other))).getNumerator().equals(BigInteger.ZERO))
            return true;
        else
            return false;
    }

    @Override
    public int intValue()
    {
        return (int)doubleValue();
    }

    @Override
    public float floatValue()
    {
        return (float)doubleValue();
    }

    @Override
    public double doubleValue()
    {
        double x = this.getNumerator().doubleValue();
        double y = this.getDenominator().doubleValue();
        return x/y;
    }

    @Override
    public long longValue()
    {
        return (long)doubleValue();
    }

    @Override
    public int compareTo(RationalTest o) {
        if(this.subtract(o).getNumerator().compareTo(BigInteger.ZERO)>0)
            return 1;
        else if(this.subtract(o).getNumerator().compareTo(BigInteger.ZERO)<0)
            return -1;
        else
            return 0;
    }
}

1 个答案:

答案 0 :(得分:0)

哦我发现我必须使用toString()返回字符串..

感谢您的帮助!