用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

相关文章

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

Java中的密码加密方式

《Java中的密码加密方式》文章介绍了Java中使用MD5算法对密码进行加密的方法,以及如何通过加盐和多重加密来提高密码的安全性,MD5是一种不可逆的哈希算法,适合用于存储密码,因为其输出的摘要长度固... 目录Java的密码加密方式密码加密一般的应用方式是总结Java的密码加密方式密码加密【这里采用的