21xrx.com
2024-11-03 22:00:39 Sunday
登录
文章检索 我的文章 写文章
Java编写分数计算器程序:实现分数的运算与计算公式
2023-06-15 19:54:15 深夜i     --     --
Java 编程 分数计算器

Java编写计算器程序已经非常的常见了,它不仅可以计算整数和小数,还可以计算分数。分数计算器可以在学习数学的时候帮助我们更好地理解分数的计算。

下面我们就来介绍一下Java编写计算分数的程序代码:

首先,我们需要定义两个类,一个是分数类Fraction,另一个是计算类Calculator。在Fraction类中定义了相应的分数属性和方法,例如:分子,分母,约分,通分等操作;在Calculator类中,我们定义了分数的加减乘除运算方法。

实现以上功能后,我们就可以开始编写Java程序代码。具体的实现过程如下:

1. 定义Fraction类,包括相应属性和方法。

2. 定义Calculator类,其中实现分数的加减乘除运算方法。

3. 编写测试类,用于测试分数计算器程序的功能。

代码示例:

Fraction.java


public class Fraction {

  private int numerator;//分子

  private int denominator;//分母

  public Fraction(int numerator, int denominator)

    this.numerator = numerator;

    this.denominator = denominator;

  

  //约分操作

  public void reduction() {

    int gcd = gcd(numerator, denominator);

    numerator /= gcd;

    denominator /= gcd;

  }

  //辗转相除法求最大公约数

  public int gcd(int m, int n) {

    if (m < n)

      int temp = m;

      m = n;

      n = temp;

    

    if (m % n == 0)

      return n;

     else {

      return gcd(n, m % n);

    }

  }

  //通分操作

  public void lcm(Fraction n2) {

    int lcm = this.denominator * n2.getDenominator() / gcd(this.denominator, n2.getDenominator());

    this.numerator *= lcm / this.denominator;

    this.denominator = lcm;

    n2.setNumerator(n2.getNumerator() * lcm / n2.getDenominator());

    n2.setDenominator(lcm);

  }

  //加法

  public Fraction add(Fraction n2) {

    Fraction result = new Fraction(this.numerator, this.denominator);

    result.lcm(n2);

    result.setNumerator(result.getNumerator() + n2.getNumerator());

    result.reduction();

    return result;

  }

  //减法

  public Fraction sub(Fraction n2) {

    Fraction result = new Fraction(this.numerator, this.denominator);

    result.lcm(n2);

    result.setNumerator(result.getNumerator() - n2.getNumerator());

    result.reduction();

    return result;

  }

  //乘法

  public Fraction mul(Fraction n2) {

    Fraction result = new Fraction(this.numerator, this.denominator);

    result.setNumerator(result.getNumerator() * n2.getNumerator());

    result.setDenominator(result.getDenominator() * n2.getDenominator());

    result.reduction();

    return result;

  }

  //除法

  public Fraction div(Fraction n2) {

    Fraction result = new Fraction(this.numerator, this.denominator);

    result.setNumerator(result.getNumerator() * n2.getDenominator());

    result.setDenominator(result.getDenominator() * n2.getNumerator());

    result.reduction();

    return result;

  }

}

Calculator.java


public class Calculator {

  //加法

  public static Fraction add(Fraction n1,Fraction n2) {

    return n1.add(n2);

  }

  //减法

  public static Fraction sub(Fraction n1,Fraction n2) {

    return n1.sub(n2);

  }

  //乘法

  public static Fraction mul(Fraction n1,Fraction n2) {

    return n1.mul(n2);

  }

  //除法

  public static Fraction div(Fraction n1,Fraction n2) {

    return n1.div(n2);

  }

}

Test.java


public class Test {

  public static void main(String[] args) {

    Fraction n1 = new Fraction(1, 2);

    Fraction n2 = new Fraction(2, 3);

    Fraction n3 = Calculator.add(n1, n2);//计算1/2 + 2/3

    System.out.println(n1.getNumerator() + "/" + n1.getDenominator() + " + " + n2.getNumerator() + "/" + n2.getDenominator() + " = " + n3.getNumerator() + "/" + n3.getDenominator());

    Fraction n4 = Calculator.sub(n1, n2);//计算1/2 - 2/3

    System.out.println(n1.getNumerator() + "/" + n1.getDenominator() + " - " + n2.getNumerator() + "/" + n2.getDenominator() + " = " + n4.getNumerator() + "/" + n4.getDenominator());

    Fraction n5 = Calculator.mul(n1, n2);//计算1/2 * 2/3

    System.out.println(n1.getNumerator() + "/" + n1.getDenominator() + " * " + n2.getNumerator() + "/" + n2.getDenominator() + " = " + n5.getNumerator() + "/" + n5.getDenominator());

    Fraction n6 = Calculator.div(n1, n2);//计算1/2 / 2/3

    System.out.println(n1.getNumerator() + "/" + n1.getDenominator() + " / " + n2.getNumerator() + "/" + n2.getDenominator() + " = " + n6.getNumerator() + "/" + n6.getDenominator());

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复