Rational加/减方法Java

时间:2013-11-05 00:48:36

标签: java

这些是我的加法减法方法的当前代码完全正常:

public static Rational sub(Rational r1, Rational r2){
    int a = r1.getNum();
    int b = r1.getDenom();
    int c = r2.getNum();
    int d = r2.getDenom();

    int numForNow = a*d - b*c;
    int denomForNow = b*d;

    Rational ratNum = new Rational (numForNow, denomForNow);

    return ratNum;

public static Rational add(Rational r1, Rational r2){
    int numForNow = r1.getNum()*r2.getDenom() + r2.getNum() * r1.getDenom();
    int denomForNow = r1.getDenom() * r2.getDenom();

    Rational ratNum = new Rational (numForNow, denomForNow);

    return ratNum;
}

因此,如果我添加两个有理数,如1/3和4/6,我会得到18/18(减少到1)。 但是,我想以不同的方式编写这些内容,以便程序看到3进入6并且只打印出6/6。


我知道我会把LCM作为分母,我理解。我不明白怎么做才能让分子跟着呢?

另外,我认为需要有一个if语句来确定是否使用LCM或者只是继续使用已存在的代码。

3 个答案:

答案 0 :(得分:2)

请参阅此答案:simplifying fractions in Java

public static long gcm(long a, long b) {
    return b == 0 ? a : gcm(b, a % b); // Not bad for one line of code :)
}

public static String asFraction(long a, long b) {
    long gcm = gcm(a, b);
    return (a / gcm) + "/" + (b / gcm);
}

或者在您的情况下,您可能需要一个名为normalize的函数获取Rational并使用与上述Rational函数相同的逻辑返回一个新的asFraction。另一个asString函数将Rational打印为字符串。

在旁注中,除非这是您的特定要求,否则我宁愿将方法作为类Rational的成员而不是静态方法。

答案 1 :(得分:0)

int numberYouWant = 0;    
int start = Math.max(r1, r2);
for(int i = 0; i<(r1*r2); i++){
    if((i%r1==0)&&(i%r2==0)){
        numberYouWant = i;
        return;
    }
}

我觉得这样的事情就是你想要的。 它返回r1和r2可分割的第一个数字。

答案 2 :(得分:0)

如果你愿意,这里有一点Rational-class:

package snippets;

public class Rational {
    private final long num;
    private final long denom;
    private volatile int hashCode;

    public Rational(long num, long denom) {
        this.num = num;
        this.denom = denom;
    }

    public long getNum() {
        return num;
    }

    public long getDenom() {
        return denom;
    }

    public Rational add(Rational other) {
        long numForNow = num * other.denom + other.num * denom;
        long denomForNow = denom * other.denom;
        return new Rational(numForNow, denomForNow).cancel();
    }

    public Object sub(Rational other) {
        long numForNow = num * other.denom - denom * other.num;
        long denomForNow = denom * other.denom;
        return new Rational(numForNow, denomForNow).cancel();
    }

    public Rational cancel() {
        long gcd = gcd(num, denom);
        return new Rational(num / gcd, denom / gcd);
    }

    // greatest common divisor
    long gcd(long a, long b) {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Rational))
            return false;

        Rational other = (Rational) obj;
        return num == other.num && denom == other.denom;
    }

    @Override
    public int hashCode() {
        if (hashCode == 0) {
            int result = 17;
            result = 31 * Long.valueOf(num).hashCode();
            result = 31 * Long.valueOf(denom).hashCode();
            hashCode = result;
        }
        return hashCode;
    }

    @Override
    public String toString() {
        return num + "/" + denom;
    }

}

经过测试:

package snippets;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class RationalTest {

    @Test
    public void testCancel() throws Exception {
        assertEquals(new Rational(2, 1), new Rational(6, 3).cancel());
        assertEquals(new Rational(7, 8), new Rational(7, 8).cancel());
    }

    @Test
    public void testAdd() {
        assertEquals(new Rational(2, 1), new Rational(2, 3).add(new Rational(4, 3)));
        assertEquals(new Rational(51, 40), new Rational(2, 5).add(new Rational(7, 8)));
    }

    @Test
    public void testSub() throws Exception {
        assertEquals(new Rational(2, 3), new Rational(4, 3).sub(new Rational(2, 3)));
        assertEquals(new Rational(19, 40), new Rational(7, 8).sub(new Rational(2, 5)));
    }
}