Algs4-1.2.16有理數
阿新 • • 發佈:2018-10-25
可變 字符 字符串表 bsp stat oid hat return div
public class Rational
{
private final long myNumerator;
private final long myDenominator;
private long gcd(long p,long q)
{
if (q==0) return p;
return gcd(q,p%q);
}
public Rational(long numerator, long denominator)
{
long gcdValue=gcd(numerator,denominator);
myNumerator=numerator/gcdValue;
myDenominator=denominator/gcdValue;
}
public long Numberator()
{
return myNumerator;
}
public long Denominator()
{
return myDenominator;
}
public Rational plus(Rational b)
{
long gcdValue=gcd(this.Denominator(),b.Denominator());
long n=this.Numberator()*b.Denominator()/gcdValue+b.Numberator()*this.Denominator()/gcdValue;
long d=this.Denominator()*b.Denominator()/gcdValue;
//
gcdValue=gcd(d,n);
n=n/gcdValue;
d=d/gcdValue;
return new Rational(n,d);
}
public Rational minus(Rational b)
{
long gcdValue=gcd(this.Denominator(),b.Denominator());
long n=this.Numberator()*b.Denominator()/gcdValue-b.Numberator()*this.Denominator()/gcdValue;
long d=this.Denominator()*b.Denominator()/gcdValue;
//
gcdValue=gcd(d,n);
n=n/gcdValue;
d=d/gcdValue;
return new Rational(n,d);
}
public Rational times(Rational b)
{
long gcdValue1=gcd(this.Numberator(),b.Denominator());
long gcdValue2=gcd(this.Denominator(),b.Numberator());
//
long n=this.Numberator()/gcdValue1*b.Numberator()/gcdValue2;
long d=this.Denominator()/gcdValue2*b.Denominator()/gcdValue1;
return new Rational(n,d);
}
public Rational divides(Rational b)
{
long gcdValue1=gcd(this.Numberator(),b.Numberator());
long gcdValue2=gcd(this.Denominator(),b.Denominator());
//
long n=this.Numberator()/gcdValue1*b.Denominator()/gcdValue2;
long d=this.Denominator()/gcdValue2*b.Numberator()/gcdValue1;
return new Rational(n,d);
}
public boolean equals(Rational that)
{
if(this==that) return true;
if(that==null) return false;
if(this.Numberator()!=that.Numberator()) return false;
if(this.Denominator()!=that.Denominator()) return false;
return true;
}
public String toString()
{
return this.Numberator()+"/"+this.Denominator();
}
public static void main(String[] args)
{
long Numberator=Long.parseLong(args[0]);
long Denominator=Long.parseLong(args[1]);
Rational r1=new Rational(Numberator,Denominator);
Rational r2=new Rational(Numberator,Denominator);
//=
StdOut.printf("r1=%-7s r2=%-7s r1=rs2 is:%s\n",r1.toString(),r2.toString(),r1.equals(r2));
//+
StdOut.printf("r1=%-7s r2=%-7s r1+rs2=%-7s\n",r1.toString(),r2.toString(),r1.plus(r2));
//-
StdOut.printf("r1=%-7s r2=%-7s r1-rs2=%-7s\n",r1.toString(),r2.toString(),r1.minus(r2));
//*
StdOut.printf("r1=%-7s r2=%-7s r1*rs2=%-7s\n",r1.toString(),r2.toString(),r1.times(r2));
// /
StdOut.printf("r1=%-7s r2=%-7s r1/rs2=%-7s\n",r1.toString(),r2.toString(),r1.divides(r2));
}
}
1.2.16有理數。為有理數實現一個可變數據類型Rational,支持加減乘除操作。無需測試溢出(請見練習1.2.17),只需使用兩個long型實例變量表示分子和分母來控制溢出的可能性。使用歐幾裏得算法來保證分子和分母沒有公因子。編寫一個測試用例檢測你實現的所有方法。
public class Rational
Rational(int numerator. int denominator)
Rational plus(Rational b) 該數與b之和
Rational minus(Rational b) 該數與b之差
Rational times(Rational b) 該數與b之積
Rational divides(Rational b) 該數與b之商
boolean equals(Rational that) 該數與that相等嗎
String toString() 對象的字符串表示
答:
public class Rational
{
private final long myNumerator;
private final long myDenominator;
private long gcd(long p,long q)
{
if (q==0) return p;
return gcd(q,p%q);
}
public Rational(long numerator, long denominator)
{
long gcdValue=gcd(numerator,denominator);
myNumerator=numerator/gcdValue;
myDenominator=denominator/gcdValue;
}
public long Numberator()
{
return myNumerator;
}
public long Denominator()
{
return myDenominator;
}
public Rational plus(Rational b)
{
long gcdValue=gcd(this.Denominator(),b.Denominator());
long n=this.Numberator()*b.Denominator()/gcdValue+b.Numberator()*this.Denominator()/gcdValue;
long d=this.Denominator()*b.Denominator()/gcdValue;
//
gcdValue=gcd(d,n);
n=n/gcdValue;
d=d/gcdValue;
return new Rational(n,d);
}
public Rational minus(Rational b)
{
long gcdValue=gcd(this.Denominator(),b.Denominator());
long n=this.Numberator()*b.Denominator()/gcdValue-b.Numberator()*this.Denominator()/gcdValue;
long d=this.Denominator()*b.Denominator()/gcdValue;
//
gcdValue=gcd(d,n);
n=n/gcdValue;
d=d/gcdValue;
return new Rational(n,d);
}
public Rational times(Rational b)
{
long gcdValue1=gcd(this.Numberator(),b.Denominator());
long gcdValue2=gcd(this.Denominator(),b.Numberator());
//
long n=this.Numberator()/gcdValue1*b.Numberator()/gcdValue2;
long d=this.Denominator()/gcdValue2*b.Denominator()/gcdValue1;
return new Rational(n,d);
}
public Rational divides(Rational b)
{
long gcdValue1=gcd(this.Numberator(),b.Numberator());
long gcdValue2=gcd(this.Denominator(),b.Denominator());
//
long n=this.Numberator()/gcdValue1*b.Denominator()/gcdValue2;
long d=this.Denominator()/gcdValue2*b.Numberator()/gcdValue1;
return new Rational(n,d);
}
public boolean equals(Rational that)
{
if(this==that) return true;
if(that==null) return false;
if(this.Numberator()!=that.Numberator()) return false;
if(this.Denominator()!=that.Denominator()) return false;
return true;
}
public String toString()
{
return this.Numberator()+"/"+this.Denominator();
}
public static void main(String[] args)
{
long Numberator=Long.parseLong(args[0]);
long Denominator=Long.parseLong(args[1]);
Rational r1=new Rational(Numberator,Denominator);
Rational r2=new Rational(Numberator,Denominator);
//=
StdOut.printf("r1=%-7s r2=%-7s r1=rs2 is:%s\n",r1.toString(),r2.toString(),r1.equals(r2));
//+
StdOut.printf("r1=%-7s r2=%-7s r1+rs2=%-7s\n",r1.toString(),r2.toString(),r1.plus(r2));
//-
StdOut.printf("r1=%-7s r2=%-7s r1-rs2=%-7s\n",r1.toString(),r2.toString(),r1.minus(r2));
//*
StdOut.printf("r1=%-7s r2=%-7s r1*rs2=%-7s\n",r1.toString(),r2.toString(),r1.times(r2));
// /
StdOut.printf("r1=%-7s r2=%-7s r1/rs2=%-7s\n",r1.toString(),r2.toString(),r1.divides(r2));
}
}
Algs4-1.2.16有理數