用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,備注


    如果您有更好的想法或建議,歡迎留言,大家共同學習,相互提高



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM