有理数类 Java BigInteger实现
2015-12-06 17:37
567 查看
import java.math.BigInteger;
public class Rational extends Number implements Comparable {
private BigInteger numerator;// 分子
private BigInteger denominator;// 分母
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rational rational1 = new Rational(new BigInteger(1 + ""),
new BigInteger(10 + ""));
Rational rational2 = new Rational(new BigInteger(1 + ""),
new BigInteger(-10 + ""));
System.out.println(rational1.add(rational2));
System.out.println(rational1.subtract(rational2));
System.out.println(rational1.multiple(rational2));
System.out.println(rational1.divide(rational2));
}
public Rational() {
// TODO Auto-generated constructor stub
this(BigInteger.ZERO, BigInteger.ONE);
}
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd(numerator, denominator);
this.numerator = ((denominator.compareTo(BigInteger.ZERO)) > 0 ? BigInteger.ONE
: new BigInteger(-1 + "")).multiply(numerator).divide(gcd);
this.denominator = denominator.abs().divide(gcd);
}
public static BigInteger gcd(BigInteger a, BigInteger b) {
BigInteger n1 = a.abs();
BigInteger n2 = b.abs();
BigInteger remainder = n1.remainder(n2);
while (remainder.compareTo(BigInteger.ZERO) > 0) {
n1 = n2;
n2 = remainder;
remainder = n1.remainder(n2);
}
return n2;
}
public BigInteger getNumerator() {
return numerator;
}
public BigInteger getDenominator() {
return denominator;
}
public Rational add(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator())
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational multiple(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.getNumerator());
return new Rational(n, d);
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this.getNumerator().compareTo(((Rational) obj).getNumerator()) == 0) {
return true;
}
else {
return false;
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
if (denominator.compareTo(BigInteger.ONE) == 0) {
return numerator.toString();
}
else {
return numerator.toString() + "/" + denominator.toString();
}
}
@Override
public int intValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).intValue();
}
@Override
public long longValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).longValue();
}
@Override
public float floatValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).floatValue();
}
@Override
public double doubleValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).doubleValue();
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if (this.getNumerator().compareTo(((Rational) o).getNumerator()) > 0) {
return 1;
}
else if (this.getNumerator().compareTo(((Rational) o).getNumerator()) < 0) {
return -1;
}
else {
return 0;
}
}
}
public class Rational extends Number implements Comparable {
private BigInteger numerator;// 分子
private BigInteger denominator;// 分母
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rational rational1 = new Rational(new BigInteger(1 + ""),
new BigInteger(10 + ""));
Rational rational2 = new Rational(new BigInteger(1 + ""),
new BigInteger(-10 + ""));
System.out.println(rational1.add(rational2));
System.out.println(rational1.subtract(rational2));
System.out.println(rational1.multiple(rational2));
System.out.println(rational1.divide(rational2));
}
public Rational() {
// TODO Auto-generated constructor stub
this(BigInteger.ZERO, BigInteger.ONE);
}
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd(numerator, denominator);
this.numerator = ((denominator.compareTo(BigInteger.ZERO)) > 0 ? BigInteger.ONE
: new BigInteger(-1 + "")).multiply(numerator).divide(gcd);
this.denominator = denominator.abs().divide(gcd);
}
public static BigInteger gcd(BigInteger a, BigInteger b) {
BigInteger n1 = a.abs();
BigInteger n2 = b.abs();
BigInteger remainder = n1.remainder(n2);
while (remainder.compareTo(BigInteger.ZERO) > 0) {
n1 = n2;
n2 = remainder;
remainder = n1.remainder(n2);
}
return n2;
}
public BigInteger getNumerator() {
return numerator;
}
public BigInteger getDenominator() {
return denominator;
}
public Rational add(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator())
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational multiple(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.getNumerator());
return new Rational(n, d);
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this.getNumerator().compareTo(((Rational) obj).getNumerator()) == 0) {
return true;
}
else {
return false;
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
if (denominator.compareTo(BigInteger.ONE) == 0) {
return numerator.toString();
}
else {
return numerator.toString() + "/" + denominator.toString();
}
}
@Override
public int intValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).intValue();
}
@Override
public long longValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).longValue();
}
@Override
public float floatValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).floatValue();
}
@Override
public double doubleValue() {
// TODO Auto-generated method stub
return numerator.divide(denominator).doubleValue();
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if (this.getNumerator().compareTo(((Rational) o).getNumerator()) > 0) {
return 1;
}
else if (this.getNumerator().compareTo(((Rational) o).getNumerator()) < 0) {
return -1;
}
else {
return 0;
}
}
}
相关文章推荐
- 有理数类 Java
- Spring 自动装配 Bean
- 【java】多线程
- nyoj 517 最小公倍数 【java睑板】
- Spring 主配置文件详解
- javac - Java programming language compiler
- Java环境变量的作用
- Java 学习笔记(一)
- Struts2学习——(六)值栈与OGNL表达式[结合struts标签]
- 动态规划算法(后附常见动态规划为题及Java代码实现)
- 关于java1.7集合源码阅读
- 第一章 开发简单java应用程序
- Eclipse OS X Yosemite 提示– To open“Eclipse” you need to install the legacy Java SE 6 runtime的解决办法
- java 数据类型
- 【java】java内存模型(2)--volatile内存语义详解
- Java之——实现与JS相同的Des加解密算法
- JAVA 输入输出流 IO 总结
- springmvc 使用 @RequestBody接收参数
- MyEclipse突然关闭
- JAVA多态的实现方式