0

These are the current codes for my addition subtraction methods that work perfectly fine:

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;
}

So if I add two rationals like 1/3 and 4/6 I would get 18/18 (reduces to 1). However, I want to write these in a different way so that the program sees that 3 goes into 6 and would just print out 6/6.



I know I would take the LCM for the denominator, which I understand. I don't understand how to make it so that the numerator would follow suit?

Also, I think there would need to be an if statement to determine whether or not to use the LCM or just continue using the code already there.

4

3 回答 3

2

请参阅此答案:在 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获取 aRational并返回一个新的规范化。另一个将 a 打印为字符串的函数。RationalasFractionasStringRational

附带说明,除非这是您的特定要求,否则我宁愿将方法作为类的成员,Rational而不是作为静态方法。

于 2013-11-05T00:56:58.923 回答
0

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

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)));
    }
}
于 2013-11-05T01:59:11.743 回答
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 整除的数。

于 2013-11-05T01:01:47.853 回答