/////////////////////////////////////////////////////////////////////////////////////// // // Rational.java // Represents rational numbers as a pair of BigIntegers // /////////////////////////////////////////////////////////////////////////////////////// import java.io.*; import java.util.StringTokenizer; import java.math.BigInteger; class Rational{ // Fields private BigInteger numerator; private BigInteger denominator; //***** Constructor **************************************************************** // Creates N/D from string Rational(String str){ BigInteger N, D, G; StringTokenizer st = new StringTokenizer(str, "/"); N = new BigInteger(st.nextToken()); if(st.countTokens()==0) D = BigInteger.valueOf(1); else D = new BigInteger(st.nextToken()); int comp = D.compareTo(BigInteger.ZERO); if(comp==0){ throw new RuntimeException( "Rational Error: Cannot construct Rational with zero denominator" ); } if(comp<0) { N = N.negate(); D = D.negate(); } G = N.gcd(D); numerator = N.divide(G); denominator = D.divide(G); } //***** Access Functions *********************************************************** // isZero() boolean isZero(){ return this.numerator.compareTo(BigInteger.ZERO) == 0;} // isPositive() boolean isPositive(){ return this.numerator.compareTo(BigInteger.ZERO) > 0;} // isNegative() boolean isNegative(){ return this.numerator.compareTo(BigInteger.ZERO) < 0;} // getNumerator() BigInteger getNumerator(){ return this.numerator;} // getDenominator BigInteger getDenominator(){ return this.denominator;} // compareTo() int compareTo(Rational Q){ BigInteger A=this.numerator; BigInteger B=this.denominator; BigInteger C=Q.numerator; BigInteger D=Q.denominator; BigInteger E=(A.multiply(D)).subtract(B.multiply(C)); int comp = E.compareTo(BigInteger.ZERO); return (comp>0)?1:(comp==0)?0:-1; } // compareTo() int compareTo(BigInteger C){ BigInteger A=this.numerator; BigInteger B=this.denominator; BigInteger D=A.subtract(B.multiply(C)); int comp = D.compareTo(BigInteger.ZERO); return (comp>0)?1:(comp==0)?0:-1; } // compareTo() int compareTo(long c){ return this.compareTo(BigInteger.valueOf(c)); } //***** Manipulation Procedures **************************************************** // negate() void negate(){ numerator = this.numerator.negate(); } // invert() void invert(){ BigInteger N = this.numerator; BigInteger D = this.denominator; int comp = N.compareTo(BigInteger.ZERO); if(comp==0) throw new RuntimeException("Rational Error: Cannot invert zero"); if(comp<0){ numerator = D.negate(); denominator = N.negate();} else{ numerator = D; denominator = N;} } // add() Rational add(Rational Q){ Rational P=this.copy(); P.addEq(Q); return P; } // addEq() void addEq(Rational Q){ BigInteger A=this.numerator; BigInteger B=this.denominator; BigInteger C=Q.numerator; BigInteger D=Q.denominator; numerator = (A.multiply(D)).add(B.multiply(C)); denominator = B.multiply(D); BigInteger G = numerator.gcd(denominator); numerator = numerator.divide(G); denominator = denominator.divide(G); } // sub() Rational sub(Rational Q){ Rational P=this.copy(); P.subEq(Q); return P; } // subEq() void subEq(Rational Q){ BigInteger A=this.numerator; BigInteger B=this.denominator; BigInteger C=Q.numerator; BigInteger D=Q.denominator; numerator = (A.multiply(D)).subtract(B.multiply(C)); denominator = B.multiply(D); BigInteger G = numerator.gcd(denominator); numerator = numerator.divide(G); denominator = denominator.divide(G); } // mult() Rational mult(Rational Q){ Rational P=this.copy(); P.multEq(Q); return P; } // multEq() void multEq(Rational Q){ BigInteger A=this.numerator; BigInteger B=this.denominator; BigInteger C=Q.numerator; BigInteger D=Q.denominator; BigInteger Gad = A.gcd(D); BigInteger Gbc = B.gcd(C); numerator = (A.divide(Gad)).multiply(C.divide(Gbc)); denominator = (B.divide(Gbc)).multiply(D.divide(Gad)); } // div() Rational div(Rational Q){ Rational P=this.copy(); P.divEq(Q); return P; } // divEq() void divEq(Rational Q){ BigInteger A=this.numerator; BigInteger B=this.denominator; BigInteger C=Q.numerator; BigInteger D=Q.denominator; int comp = C.compareTo(BigInteger.ZERO); if(comp<0){ C = C.negate(); D = D.negate();} BigInteger Gac = A.gcd(C); BigInteger Gbd = B.gcd(D); numerator = (A.divide(Gac)).multiply(D.divide(Gbd)); denominator = (B.divide(Gbd)).multiply(C.divide(Gac)); } // copy() Rational copy(){ return Rational.valueOf(this.numerator, this.denominator); } //***** Other Functions ************************************************************ // toString() public String toString(){ int comp = this.denominator.compareTo(BigInteger.ONE); if(comp==0) return this.numerator.toString(); else return this.numerator.toString() + "/" + this.denominator.toString(); } // valueOf() // Returns String s as a Rational static Rational valueOf(String s){ return new Rational(s); } // valueOf() // Returns n/1 as a Rational static Rational valueOf(long n){ Rational Q = new Rational("1"); Q.numerator = BigInteger.valueOf(n); return Q; } // valueOf() // Returns n/d as a Rational static Rational valueOf(long n, long d){ Rational Q = new Rational("1"); if(d==0) throw new RuntimeException("Rational Error: zero denominator"); if(d<0) { n = -n; d = -d;} BigInteger N = BigInteger.valueOf(n); BigInteger D = BigInteger.valueOf(d); BigInteger G = N.gcd(D); Q.numerator = N.divide(G);; Q.denominator = D.divide(G); return Q; } // valueOf() // Returns N/D as a Rational static Rational valueOf(BigInteger N, BigInteger D){ Rational Q = new Rational("1"); int comp = D.compareTo(BigInteger.ZERO); if(comp==0) throw new RuntimeException("Rational Error: zero denominator"); if(comp<0){ N = N.negate(); D = D.negate();} BigInteger G = N.gcd(D); Q.numerator = N.divide(G); Q.denominator = D.divide(G); return Q; } // main // A test driver for the Rational class // To run this test, do "java Rational" at the command line public static void main(String[] args){ Rational A = new Rational("25893/51647"); Rational B = new Rational("46008/51647"); Rational C = new Rational("62750931/51808256"); System.out.println(A); System.out.println(B); System.out.println(C); Rational D=B.mult(C); System.out.println(D.getNumerator()); System.out.println(D.getDenominator()); System.out.println(D); A.subEq(D); System.out.println(A); System.out.println(B); System.out.println(C); System.out.println(D); } }