用户以分数形式输入,以分数形式输出,用java语言实现分数中的各种运算(加减乘除,求余,求幂,求两个数中的较大值,较小值)
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^right double 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); } } }
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); } }