用java实现分数各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值)

本文主要是介绍用java实现分数各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

用java实现分数各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值)


1,要求


     用户以分数形式输入,以分数形式输出,用java语言实现分数中的各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值)


2,java代码(+详细注释)


      2.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;}static Calculator qiuyu(double left,double right){ // 求余运算double point_result = Math.IEEEremainder(left, right);int r1 = (int)point_result;double r2 = point_result - r1;int newNumerator = r1*10000 + (int)(r2*10000);int newDenominator = 10000;Calculator result = new Calculator(newNumerator,newDenominator);return result;}static Calculator pow(double left,double right){ // 求幂运算left^rightdouble point_result = Math.pow(left, right);int r1 = (int)point_result;double r2 = point_result - r1;int newNumerator = r1*10000 + (int)(r2*10000);int newDenominator = 10000;Calculator result = new Calculator(newNumerator,newDenominator);return result;}static Calculator max(double left,double right){ // 求两数中的较大值double point_result = Math.min(left, right);int r1 = (int)point_result;double r2 = point_result - r1;int newNumerator = r1*10000 + (int)(r2*10000);int newDenominator = 10000;Calculator result = new Calculator(newNumerator,newDenominator);return result;}static Calculator min(double left,double right){ // 求两数中的较小值double point_result = Math.min(left, right);int r1 = (int)point_result;double r2 = point_result - r1;int newNumerator = r1*10000 + (int)(r2*10000);int newDenominator = 10000;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);}if(operation.equals("%")){ // 两数求余double left = (double)data1_1/(double)data1_2;double right = (double)data2_1/(double)data2_2;result = qiuyu(left,right);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);}if(operation.equals("^")){ // 两数求幂double left = (double)data1_1/(double)data1_2;double right = (double)data2_1/(double)data2_2;result = pow(left,right);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);			}if(operation.equals("max")){ // 两数中的较大值double left = (double)data1_1/(double)data1_2;double right = (double)data2_1/(double)data2_2;result = max(left,right);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);				}if(operation.equals("min")){ // 两数中的较小值double left = (double)data1_1/(double)data1_2;double right = (double)data2_1/(double)data2_2;result = min(left,right);a = result.getNumerator();b = result.getDenominator();System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);				}}
}



package Rational;import java.util.Scanner;
/*** @author SunKun* 分数各种运算的主函数* */
public class TestCal {public static void main(String[] args) {// TODO Auto-generated method stub		while(true){// 用户输入两分数和运算符System.out.println("---------------------------------");Scanner input = new Scanner(System.in);System.out.println("请用户输入第一个分数(格式a/b)");String data1 = input.next();System.out.println("加减乘除:+-*/,求余求幂:%,^,求两数中较大值较小值max,min");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);		}	}
}


2.2 测试代码


public class Test1{/*** @author SunKun* 测试所进行的分数各种运算*/public static void main(String[] args) {// 测试所用数据double data1 = 0.25;double data2 = 0.2;// java自带类库double result1 = data1 + data2;  // 加double result2 = data1 - data2;  // 减double result3 = data1 * data2;  // 乘double result4 = data1 / data2;  // 除double result5 = Math.IEEEremainder(data1, data2);  // 求余double result6 = Math.pow(data1,data2);  // 求幂double result7 = Math.max(data1, data2);  // 求两数中的较大值double result8 = Math.min(data1, data2);  // 求两数中的较小值// 输出运算结果System.out.println(data1 + " + " + data2 + " = " + result1);System.out.println(data1 + " - " + data2 + " = " + result2);System.out.println(data1 + " * " + data2 + " = " + result3);System.out.println(data1 + " / " + data2 + " = " + result4);System.out.println(data1 + " % " + data2 + " = " + result5);System.out.println(data1 + " ^ " + data2 + " = " + result6);System.out.println(data1 + " max " + data2 + " = " + result7);System.out.println(data1 + " min " + data2 + " = " + result8);}
}


3.运算结果截图


3.1 代码


(1)


(2)




(3)


(4)



(5)


(6)




(7)


(8)



3.2测试


(9)


4.备注


    扩展自自己的以前的一篇博客,链接为:http://blog.csdn.net/u010043538/article/details/12175705
如果您有好的想法或建议,欢迎留言,大家共同学习进步

这篇关于用java实现分数各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/959310

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法