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,源代码应该怎么改额,谢谢,希望不吝赐教。。。
...全文
154 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 */
}
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 用户账户控制(UAC)白名单的配置 Windows7环境中 UAC(User Account Control,用户帐户控制)是由微软在Windows Vista版本中推出的一项旨在增强系统安全性的创新技术,该技术强制要求用户在执行可能干扰计算机正常运作的操作或进行更改会波及其他用户设置的变动前,必须提供相应的权限或管理员密码进行验证。通过对这些操作启动前进行授权确认,UAC能够有效阻止恶意软件及间谍软件在未获授权的状态下于计算机内进行安装或实施修改。 自从Vista版本问世以来,微软便开始推行这一全新的安全机制,可视为对系统安全防护的显著提升。尽管UAC确实能够在一定程度上对某些非法程序起到防御作用,但与此同时,这一功能也给众多用户带来了诸多不便。 因此,许多用户开始探寻是否存在似于白名单的功能,以便将那些值得信赖的程序直接赋予运行权限。事实上,这功能确实存在,不过微软并未将其作为标准配置提供。 网络上关于此问题的绝大多数建议都是建议禁用UAC,这种说法显然缺乏针对性,因为若用户希望禁用此功能,本就不会提出相关疑问。 通过运用微软官方发布的Microsoft Application Compatibility Toolkit 5.6版本,可以将信任的程序纳入系统白名单范畴。 获取Application Compatibility Toolkit 安装程序成功后会出现三个可执行文件 以管理员身份启动Compatibility Administrator 在Custom DataBases部分创建新的数据库,并添加一个Application Fix(在下方空白处点击右键,选择...
下载代码方式:https://pan.quark.cn/s/a4b39357ea24 DELL服务器的操作系统部署流程包含一系列细致的环节,其适用范围涵盖多种操作系统型,例如Windows Server与Red Hat Linux等。在启动部署之前,必须确认服务器的光驱设备为DVD驱动器,并且需准备对应的系统安装媒介。下面将详细列出完整的部署步骤: 1. **启动准备**:将随服务器提供的Systems Management Tools and Documentation version 6.0光盘置入服务器光驱,随后设定服务器以光驱作为启动设备。此环节旨在确保服务器在启动阶段能够读取安装光盘内容。 2. **语言设定**:服务器启动后,选定简体中文作为部署语言,并确认接受许可协议条款。 3. **时区选择**:在部署期间,需设定时区为北京、香港、重庆或乌鲁木齐,依据实际地理位置进行适配选择。 4. **系统型选择**:随后,需选定计划部署的操作系统,支持的版本包括Server 2003 SP2、Server 2003 SP2 64位版本、Windows 2003 SBS SP2、Server 2008、Windows 2008 SBS/EBS x64版本等,以及多种Red Hat和SUSE Linux版本。 5. **RAID设定**:若服务器出厂时已预设RAID配置,则可选择跳过此步骤。若需重新设定RAID,操作时需格外小心,因为这一过程可能引发硬盘数据遗失。 6. **引导分区规划**:设定引导分区的大小,通常C盘建议预留至少20GB的空间,具体容量需根据系统需求进行调整。 7. **网络设定**:网络设定可在系统部署完成后执行,部署期间建议暂时拔除...

62,620

社区成员

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

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