BigInteger类的问题

天明29233 2016-07-28 09:44:44
import acm.util.*;
import java.math.*;

/**
* The Rational class is used to represent rational numbers, which * are defined
* to be the quotient of two integers.
*/
public class Rational {
/** * Creates a new Rational initialized to zero. */
public Rational() {
this(0);
}

/**
* Creates a new Rational from the integer argument. * @param n The initial
* value
*/
public Rational(int n) {
this(n, 1);
}

/**
* Creates a new Rational with the value x / y. * @param x The numerator of
* the rational number * @param y The denominator of the rational number
*/
public Rational(int x, int y) {
if (y == 0)
throw new ErrorException("Division by 0");
int g = gcd(Math.abs(x), Math.abs(y));
num = x / g;
den = Math.abs(y) / g;
if (y < 0)
num = -num;
}

/**
* Adds the rational number r to this one and returns the sum. * @param r
* The rational number to be added * @return The sum of the current number
* and r
*/
public Rational add(Rational r) {
return new Rational(this.num * r.den + r.num * this.den, this.den * r.den);
}

/**
* Subtracts the rational number r from this one. * @param r The rational
* number to be subtracted * @return The result of subtracting r from the
* current number
*/
public Rational subtract(Rational r) {
return new Rational(this.num * r.den - r.num * this.den, this.den * r.den);
}

/**
* Multiplies this number by the rational number r. * @param r The rational
* number used as a multiplier * @return The result of multiplying the
* current number by r
*/
public Rational multiply(Rational r) {
return new Rational(this.num * r.num, this.den * r.den);
}

/**
* Divides this number by the rational number r. * @param r The rational
* number used as a divisor * @return The result of dividing the current
* number by r
*/
public Rational divide(Rational r) {
return new Rational(this.num * r.den, this.den * r.num);
}

/**
* Creates a string representation of this rational number. * @return The
* string representation of this rational number
*/
public String toString() {
if (den == 1) {
return "" + num;
} else {
return num + "/" + den;
}
}

/**
* Calculates the greatest common divisor using Euclid's algorithm. * @param
* x First integer * @param y Second integer * @return The greatest common
* divisor of x and y
*/
private int gcd(int x, int y) {
int r = x % y;
while (r != 0) {
x = y;
y = r;
r = x % y;
}
return y;
}

/* Private instance variables */
private BigInteger num; /* The numerator of this Rational */
private BigInteger den; /* The denominator of this Rational */
}



源代码是书本上的,将私有实例变量num和den声明为BigInteger,源代码应该怎么改额,谢谢,希望不吝赐教。。。
...全文
108 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xixibudong 2016-07-29
  • 打赏
  • 举报
回复
import acm.util.*;
import java.math.*;

/**
 * The Rational class is used to represent rational numbers, which * are defined
 * to be the quotient of two integers.
 */
public class Rational {
	/** * Creates a new Rational initialized to zero. */
	public Rational() {
		this(BigInteger.ZERO);
	}

	/**
	 * Creates a new Rational from the integer argument. * @param n The initial
	 * value
	 */
	public Rational(BigInteger n) {
		this(n, BigInteger.ONE);
	}

	/**
	 * Creates a new Rational with the value x / y. * @param x The numerator of
	 * the rational number * @param y The denominator of the rational number
	 */
	public Rational(BigInteger x, BigInteger y) {
		if (y.equals(BigInteger.ZERO))
			throw new ErrorException("Division by 0");
		BigInteger g = gcd(x.abs(), y.abs());
		num = x.divide(g);
		den = y.abs().divide(g);
		if (BigInteger.ZERO.compareTo(y) > 0)
			num = num.negate();
	}

	/**
	 * Adds the rational number r to this one and returns the sum. * @param r
	 * The rational number to be added * @return The sum of the current number
	 * and r
	 */
	public Rational add(Rational r) {
		return new Rational(this.num.multiply(r.den).add(r.num.multiply(this.den)), this.den.multiply(r.den));
	}

	/**
	 * Subtracts the rational number r from this one. * @param r The rational
	 * number to be subtracted * @return The result of subtracting r from the
	 * current number
	 */
	public Rational subtract(Rational r) {
		return new Rational(this.num.multiply(r.den).subtract(r.num.multiply(this.den)), this.den.multiply(r.den));
	}

	/**
	 * Multiplies this number by the rational number r. * @param r The rational
	 * number used as a multiplier * @return The result of multiplying the
	 * current number by r
	 */
	public Rational multiply(Rational r) {
		return new Rational(this.num.multiply(r.num), this.den.multiply(r.den));
	}

	/**
	 * Divides this number by the rational number r. * @param r The rational
	 * number used as a divisor * @return The result of dividing the current
	 * number by r
	 */
	public Rational divide(Rational r) {
		return new Rational(this.num.multiply(r.den), this.den.multiply(r.num));
	}

	/**
	 * Creates a string representation of this rational number. * @return The
	 * string representation of this rational number
	 */
	public String toString() {
		if (BigInteger.ONE.compareTo(den) == 0) {
			return "" + num;
		} else {
			return num + "/" + den;
		}
	}

	/**
	 * Calculates the greatest common divisor using Euclid's algorithm. * @param
	 * x First integer * @param y Second integer * @return The greatest common
	 * divisor of x and y
	 */
	private BigInteger gcd(BigInteger x, BigInteger y) {
		BigInteger r = x.divide(y);
		while (BigInteger.ZERO.compareTo(r)!=0) {
			x = y;
			y = r;
			r = x.divide(y);
		}
		return y;
	}

	/* Private instance variables */
	private BigInteger num; /* The numerator of this Rational */
	private BigInteger den; /* The denominator of this Rational */
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧