有理数的四则运算

Posted 南笙一梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有理数的四则运算相关的知识,希望对你有一定的参考价值。

程序源码:

package RationalCalculate;


public class Test 

	/**
	 * Copyright (c) 2016,烟台大学计算机与控制工程学院 
	 * All rights reserved. 
	 * 文件名称: Test.java 
	 * @param: 赵志君 
	 * 完成时间: 2016年6月18日 
	 */
	public static void main(String[] args) 
		Rational r1 = new Rational();
		Rational r2 = new Rational();
		Rational r3 = new Rational();
		r1.setRational(3, 5);
		r2.setRational(2, 3);
		测试加法
		r3 = r1.add(r2);
		System.out.print(r1.getNumerator() + "/" + r1.getDenominator() + " + "
				+ r2.getNumerator() + "/" + r2.getDenominator() + " = ");
		System.out.println(r3.getNumerator() + "/" + r3.getDenominator());
		测试减法
		r3 = r1.sub(r2);
		System.out.print(r1.getNumerator() + "/" + r1.getDenominator() + " - "
				+ r2.getNumerator() + "/" + r2.getDenominator() + " = ");
		System.out.println(r3.getNumerator() + "/" + r3.getDenominator());
		测试乘法
		r3 = r1.mul(r2);
		System.out.print(r1.getNumerator() + "/" + r1.getDenominator() + " * "
				+ r2.getNumerator() + "/" + r2.getDenominator() + " = ");
		System.out.println(r3.getNumerator() + "/" + r3.getDenominator());
		测试除法
		r3 = r1.div(r2);
		System.out.print(r1.getNumerator() + "/" + r1.getDenominator() + " / "
				+ r2.getNumerator() + "/" + r2.getDenominator() + " = ");
		System.out.println(r3.getNumerator() + "/" + r3.getDenominator());
	


package RationalCalculate;

public class Rational 
	int numerator;    //分子
	int denominator;    //分母
	void setRational(int n, int d)
		numerator = n;
		denominator = d;
		if(denominator < 0)
			denominator = -denominator;
			numerator = -numerator;
		
		
		if(n < 0)
			n = -n;
		
		if(d < 0)
			d = -d;
		
		int commonDivisor = CommonDivisor(n,d);
		numerator = numerator / commonDivisor;
		denominator = denominator / commonDivisor;
	
	
	public int getNumerator() 
		return numerator;
	

	public int getDenominator() 
		return denominator;
	

	int CommonDivisor(int m, int n) //求最大公约数
		if(m < n)
			int t = m;
			m = n;
			n = t;
		
		int remainder = m % n;    //余数
		while(remainder != 0)
			m = n;
			n = remainder;
			remainder = m % n;
		
		return n;
	
	
	Rational add(Rational r)  //实现有理数的加法
		Rational result = new Rational();
		int fenZi = numerator*r.denominator + r.numerator*denominator;
		int fenMu = denominator * r.denominator;
		result.setRational(fenZi, fenMu);
		return result;
	
	
	Rational sub(Rational r)  //实现有理数的减法
		Rational result = new Rational();
		int fenZi = numerator*r.denominator - r.numerator*denominator;
		int fenMu = denominator * r.denominator;
		result.setRational(fenZi, fenMu);
		return result;
	
	
	Rational mul(Rational r)  //实现有理数的乘法
		Rational result = new Rational();
		int fenZi = numerator * r.numerator;
		int fenMu = denominator * r.denominator;
		result.setRational(fenZi, fenMu);
		return result;
	
	
	Rational div(Rational r)  //实现有理数的除法
		Rational result = new Rational();
		int fenZi = numerator * r.denominator;
		int fenMu = denominator * r.numerator;
		result.setRational(fenZi, fenMu);
		return result;
	


运行结果:





总结:可以考虑将四则运算的过程放到一个新的类文件中,并且将分子分母设置为private型,应该会更加符合面向对象的规律。


以上是关于有理数的四则运算的主要内容,如果未能解决你的问题,请参考以下文章

1034. 有理数四则运算(20)

1034. 有理数四则运算(20)

PAT 1034. 有理数四则运算

1034 有理数四则运算 (20分)

1034 有理数四则运算

PAT1034 有理数四则运算 (20分)