/*
* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:分数的四则运算
* 作 者:薛广晨
* 完成日期:2011 年 09 月 13 日
* 版 本号:x1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述: 实现分数的四则运算,即两个分数相减,相乘和相除
* 程序输出:
* 程序头部的注释结束
*/
package xue.com;
public class TestFractory1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
fracAdd(1,5,7,20);//结果为:11/20
fracSub(1,5,7,20);//分数相减
fracMul(1,5,7,20);//分数相乘
fractDiv(1,5,7,20);//分数相除
}
private static void fractDiv(int first_numrator,int first_denominator,int second_numrator,int second_denominator) {
// TODO Auto-generated method stub
int third_numrator = first_numrator * second_denominator;
int third_denominator = first_denominator * second_numrator;
int j = gcd(third_numrator, third_denominator);
third_numrator = third_numrator / j;
third_denominator = third_denominator /j;
System.out.println(first_numrator + "/" + first_denominator + " ÷ " + second_numrator + "/" + second_denominator + " = " + third_numrator + "/" + third_denominator);
}
private static void fracMul(int first_numrator,int first_denominator,int second_numrator,int second_denominator) {
// TODO Auto-generated method stub
int third_numrator = first_numrator * second_numrator;
int third_denominator = first_denominator * second_denominator;
int j = gcd(third_numrator, third_denominator);
third_numrator = third_numrator / j;
third_denominator = third_denominator /j;
System.out.println(first_numrator + "/" + first_denominator + " X " + second_numrator + "/" + second_denominator + " = " + third_numrator + "/" + third_denominator);
}
private static void fracSub(int first_numrator,int first_denominator,int second_numrator,int second_denominator) {
// TODO Auto-generated method stub
int i = lcm(first_denominator, second_denominator);
int third_numrator = (i / first_denominator) * first_numrator - (i / second_denominator) * second_numrator;
int j = gcd(third_numrator, i);
third_numrator = third_numrator / j;
int third_denominator = i /j;
System.out.println(first_numrator + "/" + first_denominator + " - " + second_numrator + "/" + second_denominator + " = " + third_numrator + "/" + third_denominator);
}
static void fracAdd(int first_numrator,int first_denominator,int second_numrator,int second_denominator){
//以下代码能够在控制台上显示结果
//需要调用求最大公约数的函数
//需要调用求最小公倍数的函数
int i = lcm(first_denominator, second_denominator);
int third_numrator = (i / first_denominator) * first_numrator + (i / second_denominator) * second_numrator;
int j = gcd(third_numrator, i);
third_numrator = third_numrator / j;
int third_denominator = i /j;
System.out.println(first_numrator + "/" + first_denominator + " + " + second_numrator + "/" + second_denominator + " = " + third_numrator + "/" + third_denominator);
}
static int gcd(int m,int n){
int i = 2;//定义循环控制变量
int gc = 1;//求最大公约数
int min = m < n ? m : n;
while (i <= min)
{
while (m % i == 0 && n % i == 0)//求分子分母共同的公约数
{
m = m / i;
n = n / i;
min = m < n ? m : n;
gc = gc * i;
}
++i;
}
return gc;
}
static int lcm(int m,int n){
int gc = gcd(m, n);//求最大公约数
int lc = (m / gc) * (n / gc) * gc;//最小公倍数与最大公约数有一定关系
return lc;
}
}
运行结果:
1/5 + 7/20 = 11/20
1/5 - 7/20 = -3/20
1/5 X 7/20 = 7/100
1/5 ÷ 7/20 = 4/7