有理数类 Java BigInteger实现
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;
}
}
}
有理数类 Java BigInteger实现的更多相关文章
- 蓝桥杯-有理数类-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- 有理数类 Java
public class Rational extends Number implements Comparable { private long numerator;// 分子 private lo ...
- 【经验】 Java BigInteger类以及其在算法题中的应用
[经验] Java BigInteger类以及其在算法题中的应用 标签(空格分隔): 经验 本来在刷九度的数学类型题,有进制转换和大数运算,故而用到了java BigInteger类,使用了之后才发现 ...
- java实现第四届蓝桥杯有理数类
有理数类 题目描述 有理数就是可以表示为两个整数的比值的数字.一般情况下,我们用近似的小数表示.但有些时候,不允许出现误差,必须用两个整数来表示一个有理数. 这时,我们可以建立一个"有理数类 ...
- 大数运算之 Java BigInteger 的基本用法
大数运算之 Java BigInteger 的基本用法 在程序设计竞赛中会遇到高精度运算的问题,C++没有高精度运算,只能手动模拟人工运算,手动实现高精度,而 java.math 包中的 BigInt ...
- 处理数字的类 —— Math类 、 Random类 、 BigDecimal类 与 BigInteger类
在我们学习C语言时,我们处理数据时要调用很多函数,那么,Java也有很多的方法可以来处理数值的类. 那么,在本篇博文中,本人就来讲解三个用于处理数值的类 -- Math类 . Random类 与 Bi ...
- 类 java.util.Scannar方法
类 java.util.Scannar方法 ·Scannar (InputStream ln):用给定的输人流创建一个Scanner对象. ·String nextLlne():读取输入的下一行内容. ...
- JavaSE-基础语法(二)-系统类(java.lang.*)和工具类(java.util.*)
系统类(java.lang.*)和工具类(java.util.*) 一.系统类(java.lang.*) 这个包下包含java语言的核心类,如String.Math.System和Thread类等,使 ...
- Java基础-类加载机制与自定义类Java类加载器(ClassLoader)
Java基础-类加载机制与自定义类Java类加载器(ClassLoader) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于类加载器的概念和分类我就不再废话了,因为我在之前的笔 ...
随机推荐
- 关于Lua 5.1中的debug.hook和coroutine
1.debug.hook只hook主线程,在coroutine中不起作用. 2.coroutine.resume之后主线程会挂起,直到协程coroutine.yield返回 3. 对于如下代码 deb ...
- vs2010常见错误
安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 用VS2012调试时发现在调用数据集时提示“尝试读取或写入受保护的内存. ...
- str*函数和大小端判断
#include <stdio.h> #include <assert.h> size_t mstrlen(const char *s) { assert(s != NULL) ...
- Solution for When browse http://xxx/ReportServer Show Error (rsAccessDenied)
Issue: Reporting Services Error The permissions granted to user 'IDEAAM\William' are insufficient fo ...
- NULL指针 Void* 和野指针
在C和C++的语言中常常有这几个概念: NULL指针.空指针.Void *指针.野指针(Wild Pointer)甚至垂悬指针(Dangling Pointer). 1.NULL指针,一般用于指向一个 ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- pm 2.5
定陵</a></div><div class="staaqi"><span class="label pmsmall" ...
- 无法识别的属性“targetFramework”。请注意属性名称区分大小写。错误分析以及解决方案
我的配置文件中是这样写的,<compilation debug="true" targetFramework="4.0"> 发布在iis上出现了 “ ...
- 史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~
继续延续坑爹标题系列.其实只是把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了,当做自己总结吧.内容看看就好,排版就不要吐槽了,反正我知道你也 ...
- C#基础知识-对象初始化顺序
本文章转载:http://blog.csdn.net/forever_wind/article/details/7442503 不错的文章:http://www.cnblogs.com/McJerem ...