# 一、有理数类的代码

package BIgDecinal;

public class RationalNumber {
private int numerator;//分子
private int denominator;//分母

public int getNumerator() {
return numerator;
}

public int getDenominator() {
return denominator;
}

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

private static int gcd(int numerator, int denominator) {//约分
int m = Math.abs(numerator);
int n = Math.abs(denominator);
int temp = 1;
while(n != 0) {
temp = m % n;
m = n;
n = temp;
}
return m;
}

public RationalNumber add(RationalNumber number1,RationalNumber number2) {//加法运算
int n = number1.getNumerator() * number2.getDenominator() + number2.getNumerator() * number1.getDenominator();
int d = number1.getDenominator() * number2.getDenominator();
return new RationalNumber(n,d);
}

public RationalNumber subtract(RationalNumber number1, RationalNumber number2) {//减法运算
int n = number1.getNumerator() * number2.getDenominator() - number2.getNumerator() * number1.getDenominator();
int d = number1.getDenominator() * number2.getDenominator();
return new RationalNumber(n,d);
}

public RationalNumber multiply(RationalNumber number1, RationalNumber number2) {//乘法运算
int n = number1.getNumerator() * number2.getNumerator();
int d = number1.getDenominator() * number2.getDenominator();
return new RationalNumber(n, d);
}

public RationalNumber divide(RationalNumber number1, RationalNumber number2) {//除法运算
int n = number1.getNumerator() * number2.getDenominator();
int d = number1.getDenominator() * number2.getNumerator();
return new RationalNumber(n, d);
}

public boolean equal(RationalNumber number1,RationalNumber number2) {//判断两数是否相等
if(number1.getDenominator() == number2.getDenominator() && number1.getNumerator() == number2.getNumerator())
return true;
else
return false;
}

public double doubleValue() {//转double
return this.numerator * 1.0 / this.denominator;
}

public long longValue() {//转long
return (long)doubleValue();
}

public String toString() {//转string
return this.numerator+"/"+this.denominator;
}

}

# 二、测试代码

package ss;

import java.util.Scanner;
import BIgDecinal.RationalNumber;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数的分子、分母，将以有理数形式输出：");
int a = sc.nextInt(),b = sc.nextInt();
System.out.println("输入第个数的分子、分母，将以有理数形式输出：");
int c = sc.nextInt(),d = sc.nextInt();
RationalNumber num1 = new RationalNumber(a,b);
RationalNumber num2 = new RationalNumber(c,d);
System.out.println("第一个数"+num1.getNumerator()+"/"+num1.getDenominator());
System.out.println("第二个数"+num2.getNumerator()+"/"+num2.getDenominator());//此处可展示约分功能
System.out.println("-----------------------------------------------");
System.out.print("相加得：");
System.out.print("相减得：");
System.out.println(num1.subtract(num1, num2));
System.out.print("相乘得：");
System.out.println(num1.multiply(num1, num2));
System.out.print("相除得：");
System.out.println(num1.divide(num1, num2));
System.out.print("两数是否相等：");
if(num1.equal(num1, num2))
System.out.println("相等");
else
System.out.println("不相等");
System.out.print("第一个数转换为长整型型：");
System.out.println(num1.longValue());
System.out.print("第一个数转换为双精度浮点型型：");
System.out.println(num1.doubleValue());

}
}

# 四、讨论

## 4、问：有理数类的public方法是否设置合适?为什么有的方法设置为private?

posted @ 2020-10-04 21:20  楠木。  阅读(77)  评论(0编辑  收藏  举报