classFraction
{
BigInteger up, down;publicFraction (Fraction f)
{this.up=f.up;this.down=f.down;
}publicFraction(String s)
{this.up=newBigInteger(s);this.down=newBigInteger("1");
}publicFraction(BigInteger a, BigInteger b)
{this.up=a;this.down=b;
}publicBigInteger getUp()
{returnthis.up;
}publicBigInteger getDown()
{returnthis.down;
}publicFraction subtract(Fraction f)
{
BigInteger save1=this.up.multiply (f.down);
BigInteger save2=f.up.multiply(this.down);
Fraction tmp=newFraction(save1.subtract (save2), f.down .multiply (this.down));returnsimplex(tmp);
}publicFraction add(Fraction f)
{
Fraction tmp=newFraction ("0");
tmp=tmp.subtract(f);
Fraction ans=newFraction (this.subtract(tmp));returnans;
}publicFraction multiply(Fraction f)
{
Fraction tmp;
tmp=newFraction(this.up.multiply (f.up),this.down.multiply (f.down));if(tmp.down.compareTo(newBigInteger("0"))==-1)
{
tmp.down=tmp.down.multiply (newBigInteger("-1"));
tmp.up=tmp.up.multiply (newBigInteger("-1"));
}returnsimplex(tmp);
}publicFraction divide(Fraction f)
{
Fraction tmp=null;
tmp=newFraction(this.up.multiply (f.down),this.down.multiply (f.up));if(tmp.down.compareTo(newBigInteger("0"))==-1)
{
tmp.down=tmp.down.multiply (newBigInteger("-1"));
tmp.up=tmp.up.multiply (newBigInteger("-1"));
}returnsimplex(tmp);
}
BigInteger gcd(BigInteger a, BigInteger b)
{if(b.compareTo(newBigInteger("0"))==0)returna;returngcd(b, a.remainder (b));
}
Fraction simplex(Fraction f)
{
BigInteger tmp=gcd(f.up.abs(), f.down.abs ());
f.up=f.up.divide (tmp);
f.down=f.down.divide (tmp);returnf;
}voidprint()
{
BigInteger a, b, c;
a=this.getUp ();
b=this.getDown();
c=gcd(a.abs(), b.abs());
a=a.divide (c);
b=b.divide (c);if(b.compareTo (newBigInteger("1"))==0)
System.out.println(a);elseSystem.out.println (a+"/"+b);
}
}