本文主要是介绍用java具体代码实现分数(即有理数)四则运算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用java具体代码实现分数(即有理数)四则运算
1,背景
Java老师布置了一个关于有理数运算的题目,因为参考书上有基本代码,所以自己主要是对书上代码做了一点优化,使其用户交互性更加友好以及代码封装性更强
2,说明:
分数也称作有理数,是我们很熟悉的一种数。有时希望程序能对分数进行四则运算,而且两个分数四则运算的结果仍然是分数。下面是具体实现
3,java代码具体实现
3.1 运算类封装
package Rational;import java.util.StringTokenizer;
/*** @author sunkun* 分数四则运算*/
public class Calculator {int numerator; // 分子int denominator; // 分母Calculator(){}Calculator(int a,int b){if(a == 0){numerator = 0;denominator = 1;}else{setNumeratorAndDenominator(a,b);}}void setNumeratorAndDenominator(int a, int b){ // 设置分子和分母int c = f(Math.abs(a),Math.abs(b)); // 计算最大公约数numerator = a / 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){ // 求a和b的最大公约数if(a < b){int c = a;a = b;b = c;}int r = a % b;while(r != 0){a = b;b = r;;r = a % b;}return b;}Calculator add(Calculator r){ // 加法运算int a = r.getNumerator();int b = r.getDenominator();int newNumerator = numerator * b + denominator * a;int newDenominator = denominator * b;Calculator result = new Calculator(newNumerator,newDenominator);return result;}Calculator sub(Calculator r){ // 减法运算int a = r.getNumerator();int b = r.getDenominator();int newNumerator = numerator * b - denominator * a;int newDenominator = denominator * b;Calculator result = new Calculator(newNumerator,newDenominator);return result;} Calculator muti(Calculator r){ // 乘法运算int a = r.getNumerator();int b = r.getDenominator();int newNumerator = numerator * a;int newDenominator = denominator * b;Calculator result = new Calculator(newNumerator,newDenominator);return result;}Calculator div(Calculator r){ // 除法运算int a = r.getNumerator();int b = r.getDenominator();int newNumerator = numerator * b;int newDenominator = denominator * a;Calculator result = new Calculator(newNumerator,newDenominator);return result;}// 封装了具体运算,主要为对输入进行转换,对输出封装public static void compute(String data1,String operation,String data2){StringTokenizer fenxi = new StringTokenizer(data1,"/");int data1_1 = Integer.parseInt(fenxi.nextToken());int data1_2 = Integer.parseInt(fenxi.nextToken());fenxi = new StringTokenizer(data2,"/");int data2_1 = Integer.parseInt(fenxi.nextToken());int data2_2 = Integer.parseInt(fenxi.nextToken());Calculator r1 = new Calculator(data1_1,data1_2);Calculator r2 = new Calculator(data2_1,data2_2);Calculator result;int a,b;if(operation.equals("+")){result = r1.add(r2);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);}if(operation.equals("-")){result = r1.sub(r2);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);}if(operation.equals("*")){result = r1.muti(r2);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);}if(operation.equals("/")){result = r1.div(r2);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);}}
}
3.2 测试类
package Rational;import java.util.Scanner;
/*** @author sunkun* 为了测试分数四则运算* */
public class TestCal {public static void main(String[] args) {// TODO Auto-generated method stub// 用户输入两分数和运算符Scanner input = new Scanner(System.in);System.out.println("请用户输入第一个分数(格式a/b)");String data1 = input.next();System.out.println("请用户输入要进行运算的运算符(+-*/)");String operation = input.next();System.out.println("请用户输入第二个分数(格式c/d)");String data2 = input.next();// 根据用户输入进行具体运算Calculator cal = new Calculator();System.out.println("运算结果为:");cal.compute(data1, operation, data2);
// } }
}
4,实验结果
5,备注
如果您有更好的想法或建议,欢迎留言,大家共同学习,相互提高
有理数进一步运算,包括运算功能扩展,分数与小数之间的互转,会写在另一篇博客中。
这篇关于用java具体代码实现分数(即有理数)四则运算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!