码迷,mamicode.com
首页 > 编程语言 > 详细

有理数类 Java

时间:2015-12-06 19:07:26      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

public class Rational extends Number implements Comparable {

private long numerator;// 分子
private long denominator;// 分母

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
Rational rational1 = new Rational(1, 10);
Rational rational2 = new Rational(1, -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(0, 1);

}

public Rational(long numerator, long denominator) {

long gcd = gcd(numerator, denominator);
this.numerator = (denominator > 0 ? 1 : -1) * numerator / gcd;
this.denominator = Math.abs(denominator) / gcd;
}

public static long gcd(long a, long b) {

long n1 = Math.abs(a);
long n2 = Math.abs(b);
long remainder = n1 % n2;
while (remainder > 0) {
n1 = n2;
n2 = remainder;
remainder = n1 % n2;
}
return n2;
}

public long getNumerator() {

return numerator;
}

public long getDenominator() {

return denominator;
}

public Rational add(Rational secondRational) {

long n = numerator * secondRational.getDenominator() + denominator
* secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational subtract(Rational secondRational) {

long n = numerator * secondRational.getDenominator() - denominator
* secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational multiple(Rational secondRational) {

long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Rational(n, d);
}

public Rational divide(Rational secondRational) {

long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.getNumerator();
return new Rational(n, d);
}

@Override
public boolean equals(Object obj) {

// TODO Auto-generated method stub
if (this.subtract((Rational) obj).getNumerator() == 0) {
return true;
}
else {
return false;
}
}

@Override
public String toString() {

// TODO Auto-generated method stub
if (denominator == 1) {
return String.valueOf(numerator);
}
else {
return numerator + "/" + denominator;
}
}

@Override
public int intValue() {

// TODO Auto-generated method stub
return (int) doubleValue();
}

@Override
public long longValue() {

// TODO Auto-generated method stub
return (long) doubleValue();
}

@Override
public float floatValue() {

// TODO Auto-generated method stub
return (float) doubleValue();
}

@Override
public double doubleValue() {

// TODO Auto-generated method stub
return numerator * 1.0 / denominator;
}

@Override
public int compareTo(Object o) {

// TODO Auto-generated method stub
if (this.subtract((Rational) o).getNumerator() > 0) {
return 1;
}
else if (this.subtract((Rational) o).getNumerator() < 0) {
return -1;
}
else {
return 0;
}
}
}

有理数类 Java

标签:

原文地址:http://www.cnblogs.com/diyishijian/p/5023950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!