话不多说,直接上代码:
main.java
public class main { public static void main(String[] args) { //四则运算测试 Rational r1 = new Rational(); r1.setNumerator(1); r1.setDenominator(5); Rational r2 = new Rational(); r2.setNumerator(-3); r2.setDenominator(2); //加法 Rational result_1 = r1.add(r2); int a_1 = result_1.getNumerator(); int b_1 = result_1.getDenominator(); if(a_1 * b_1 < 0) System.out.println("1/5 + (-3/2) = -" + Math.abs(a_1) + "/" + Math.abs(b_1)); else System.out.println("1/5 + (-3/2) = " + Math.abs(a_1) + "/" + Math.abs(b_1)); //减法 Rational result_2 = r1.sub(r2); int a_2 = result_2.getNumerator(); int b_2 = result_2.getDenominator(); if(a_2 * b_2 < 0) System.out.println("1/5 - (-3/2) = -" + Math.abs(a_2) + "/" + Math.abs(b_2)); else System.out.println("1/5 - (-3/2) = " + Math.abs(a_2) + "/" + Math.abs(b_2)); //乘法 Rational result_3 = r1.muti(r2); int a_3 = result_3.getNumerator(); int b_3 = result_3.getDenominator(); if(a_3 * b_3 < 0) System.out.println("1/5 * (-3/2) = -" + Math.abs(a_3) + "/" + Math.abs(b_3)); else System.out.println("1/5 * (-3/2) = " + Math.abs(a_3) + "/" + Math.abs(b_3)); //除法 Rational result_4 = r1.div(r2); int a_4 = result_4.getNumerator(); int b_4 = result_4.getDenominator(); if(a_4 * b_4 < 0) System.out.println("1/5 / (-3/2) = -" + Math.abs(a_4) + "/" + Math.abs(b_4)); else System.out.println("1/5 / (-3/2) = " + Math.abs(a_4) + "/" + Math.abs(b_4)); } }
Rational.java
public class Rational { int numerator = 1; //分子 int denominator = 1; //分母 void setNumerator(int a){ //设置分子 int c = f(Math.abs(a), denominator); numerator = a / c; denominator /= c; if(numerator<=0 && denominator<=0){ numerator = -numerator; denominator = -denominator; } } void setDenominator(int b){ //设置分母 int c = f(numerator, Math.abs(b)); numerator /= c; denominator = b / c; if(numerator<=0 && denominator<=0){ numerator = -numerator; denominator = -denominator; } } int getNumerator(){ //获取分子 return numerator; } int getDenominator(){ //获取分母 return denominator; } int f(int a, int b){ //辗转相除求最大公约数 if(a == 0) return 1; if(a < b){ //a, b交换 a = a^b; b = a^b; a = a^b; } int r = a % b; while(r != 0){ a = b; b = r; r = a % b; } return b; } Rational add(Rational r){ //加法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumberator = numerator*b + denominator*a; //计算新分子 int newDenominator = denominator*b; //计算新分母 Rational result = new Rational(); result.setNumerator(newNumberator); result.setDenominator(newDenominator); return result; } Rational sub(Rational r){ //减法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumberator = numerator*b - denominator*a; //计算新分子 int newDenominator = denominator*b; //计算新分母 Rational result = new Rational(); result.setNumerator(newNumberator); result.setDenominator(newDenominator); return result; } Rational muti(Rational r){ //乘法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumberator = numerator*a; //计算新分子 int newDenominator = denominator*b; //计算新分母 Rational result = new Rational(); result.setNumerator(newNumberator); result.setDenominator(newDenominator); return result; } Rational div(Rational r){ //除法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumberator = numerator*b; //计算新分子 int newDenominator = denominator*a; //计算新分母 Rational result = new Rational(); result.setNumerator(newNumberator); result.setDenominator(newDenominator); return result; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)