import java.lang.*;
class Rational {
private int n, d;
int divisor = gcd(n,d);
this.n = n/divisor;
this.d = d/divisor;
}
private static int gcd(int n, int d) {
return ( d == 0 ? n: gcd(d, n%d));
}
public static boolean equals(Rational x, Rational y) {
if ( x.n != y.n || x.d != y.d) return false;
else return true;
}
public static Rational add(Rational x, Rational y) {
return new Rational(x.n * y.d + y.n * x.d, x.d * y. d);
}
public static Rational sub(Rational x, Rational y) {
return new Rational(x.n * y.d - y.n * x.d, x.d * y. d);
}
public static Rational mul(Rational x, Rational y) {
return new Rational(x.n * y.n, x.d * y.d);
}
public static Rational div(Rational x, Rational y) {
return new Rational(x.n * y.d, x.d * y.n);
}
return n + "/" + d;
}
public static void cmp(Rational x, Rational y) {
if ( ! equals(x,y)) {
if ( (x.
n * y.
d ) > (y.
n * x.
d)) System.
out.
println( x.
toString() + " > " + y.
toString()); else System.
out.
println( y.
toString() + " > " + x.
toString()); }
else System.
out.
println( x.
toString() + " = " + y.
toString()); }
public static void main
(String[] args
) { try {
Rational a = new Rational(4, 5);
Rational b = new Rational(1, 2);
Rational c = new Rational(2, 5);
System.
out.
println("Equality - " + equals
(a, b
)); System.
out.
println("Equality - " + equals
(mul
(a,b
), c
)); System.
out.
println(c.
toString() + "+" + b.
toString() + "=" + add
(c, b
)); System.
out.
println(a.
toString() + "-" + b.
toString() + "=" + sub
(a, b
)); System.
out.
println(b.
toString() + "-" + a.
toString() + "=" + sub
(b, a
)); System.
out.
println(c.
toString() + "*" + b.
toString() + "=" + mul
(c, b
)); System.
out.
println(a.
toString() + "%" + b.
toString() + "=" + div
(a, b
)); cmp(a,b);
cmp(b,c);
cmp(mul(a,b), c);
}
System.
out.
println("dividing on 0"); }
}
}
aW1wb3J0IGphdmEubGFuZy4qOwoKY2xhc3MgUmF0aW9uYWwgewogICAgcHJpdmF0ZSAgaW50IG4sIGQ7CgogICAgcHVibGljIFJhdGlvbmFsIChpbnQgbiwgaW50IGQpICB0aHJvd3MgQXJpdGhtZXRpY0V4Y2V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ICAgICAgcmV0dXJuIG5ldyBSYXRpb25hbCh4Lm4gKiB5LmQsIHguZCAqIHkubik7CiAgICB9CgogICAgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICByZXR1cm4gbiArICIvIiArIGQ7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIGNtcChSYXRpb25hbCB4LCBSYXRpb25hbCB5KSB7CiAgICAgICAgaWYgKCAhIGVxdWFscyh4LHkpKSB7CiAgICAgICAgICAgIGlmICggKHgubiAqIHkuZCApID4gKHkubiAqIHguZCkpIFN5c3RlbS5vdXQucHJpbnRsbiggeC50b1N0cmluZygpICsgIiA+ICIgKyB5LnRvU3RyaW5nKCkpOwogICAgICAgICAgICBlbHNlIFN5c3RlbS5vdXQucHJpbnRsbiggeS50b1N0cmluZygpICsgIiA+ICIgKyB4LnRvU3RyaW5nKCkpOwogICAgICAgIH0KICAgICAgICBlbHNlIFN5c3RlbS5vdXQucHJpbnRsbiggeC50b1N0cmluZygpICsgIiA9ICIgKyB5LnRvU3RyaW5nKCkpOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICB0cnkgewogICAgICAgICAgICBSYXRpb25hbCBhID0gbmV3IFJhdGlvbmFsKDQsIDUpOwogICAgICAgICAgICBSYXRpb25hbCBiID0gbmV3IFJhdGlvbmFsKDEsIDIpOwogICAgICAgICAgICBSYXRpb25hbCBjID0gbmV3IFJhdGlvbmFsKDIsIDUpOwogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkVxdWFsaXR5IC0gIiArIGVxdWFscyhhLCBiKSk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRXF1YWxpdHkgLSAiICsgZXF1YWxzKG11bChhLGIpLCBjKSk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihjLnRvU3RyaW5nKCkgKyAiKyIgKyBiLnRvU3RyaW5nKCkgKyAiPSIgKyBhZGQoYywgYikpOwogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oYS50b1N0cmluZygpICsgIi0iICsgYi50b1N0cmluZygpICsgIj0iICsgc3ViKGEsIGIpKTsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGIudG9TdHJpbmcoKSArICItIiArIGEudG9TdHJpbmcoKSArICI9IiArIHN1YihiLCBhKSk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihjLnRvU3RyaW5nKCkgKyAiKiIgKyBiLnRvU3RyaW5nKCkgKyAiPSIgKyBtdWwoYywgYikpOwogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oYS50b1N0cmluZygpICsgIiUiICsgYi50b1N0cmluZygpICsgIj0iICsgZGl2KGEsIGIpKTsKICAgICAgICAgICAgY21wKGEsYik7CiAgICAgICAgICAgIGNtcChiLGMpOwogICAgICAgICAgICBjbXAobXVsKGEsYiksIGMpOwogICAgICAgIH0KICAgICAgICBjYXRjaCAoQXJpdGhtZXRpY0V4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiZGl2aWRpbmcgb24gMCIpOwogICAgICAgIH0KICAgIH0KfQ==