仿照BigDecimal类以面向对象的方式设计有理数类。
package RationalNumber; public class RationalNumber { private int numerator; private int denominator; private boolean isInt(String str) { int i = 0; if (str.charAt(0) == '-') i++; for (; i < str.length(); i++) { if (str.charAt(i) < '0' || str.charAt(i) > '9') return false; } return true; } public RationalNumber(String str) { if (isInt(str)) { this.numerator = Integer.valueOf(str); this.denominator = 1; } else if (str.indexOf(".") != -1) { String str1 = str.substring(0, str.indexOf(".")); String str2 = str.substring(str.indexOf(".") + 1, str.length()); if (isInt(str1) && isInt(str2)) { this.numerator = Integer.valueOf(str1); this.denominator = Integer.valueOf(str2); } this.numerator *= Math.pow(10, str2.length()); this.denominator *= Math.pow(10, str2.length()); fractionReduction(); } else if (str.indexOf("/") != -1) { String str1 = str.substring(0, str.indexOf("/")); String str2 = str.substring(str.indexOf("/") + 1, str.length()); if (isInt(str1) && isInt(str2)) { this.numerator = Integer.valueOf(str1); this.denominator = Integer.valueOf(str2); if (this.denominator == 0) { System.out.print("分母不能为零"); } else fractionReduction(); } else { System.out.print("输入错误"); } } } public int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public void fractionReduction() { int flag = 1; if (this.numerator < 0) { this.numerator *= -1; flag *= -1; } if (this.denominator < 0) { this.denominator *= -1; flag *= -1; } int mgcd = gcd(this.numerator, this.denominator); this.numerator = flag * this.numerator / mgcd; this.denominator = this.denominator / mgcd; } public RationalNumber add(RationalNumber x) { this.numerator = this.numerator * x.denominator + this.denominator * x.numerator; this.denominator = this.denominator * x.denominator; fractionReduction(); return this; } public RationalNumber subtract(RationalNumber x) { this.numerator = this.numerator * x.denominator - this.denominator * x.numerator; this.denominator = this.denominator * x.denominator; fractionReduction(); return this; } public RationalNumber multply(RationalNumber x) { this.numerator = this.numerator * x.numerator; this.denominator = this.denominator * x.denominator; fractionReduction(); return this; } public RationalNumber divide(RationalNumber x) { this.numerator = this.numerator * x.denominator; this.denominator = this.denominator * x.numerator; return this; } public String toString() { if (this.denominator == 1) { return this.numerator + ""; } else { return this.numerator + "/" + this.denominator; } } }
package RationalNumber; public class Main { public static void main(String[] args) { int n = 2; RationalNumber num[] = new RationalNumber[10]; num[0] = new RationalNumber("1"); num[1] = new RationalNumber("5/8"); System.out.println("测试数据:\n"); for (int i = 0; i < 2; i++) { System.out.println(num[i].toString() + "\n"); } System.out.println("加法\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.println(num[i].toString() + "+" + num[j].toString() + "=" + num[0].add(num[1]) + "\n"); } } System.out.println("减法\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.println(num[i].toString() + "-" + num[j].toString() + "=" + num[0].subtract(num[1]) + "\n"); } } System.out.println("乘法\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.println(num[i].toString() + "*" + num[j].toString() + "=" + num[0].multply(num[1]) + "\n"); } } System.out.println("除法\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.println(num[i].toString() + "/" + num[j].toString() + "=" + num[0].divide(num[1]) + "\n"); } } } }
说明:怎么查看BigDecimal的设计?
使用JDK文档查看BigDecimal的公共方法(入参、返回值、功能)。
进阶:在Eclipse查看BigDecimal的代码。先看其属性,再看其公共方法的声明。可以不用详细看代码实现。