public class TestFractory {
/**
* @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);//分数相除
}
static void fracAdd(int first_numerator,int first_denominator,int second_numrator,int second_denominator){
int nume, deno;
int smlg = gcd(first_denominator, second_denominator);
nume = first_numerator * (smlg / first_denominator) + second_numrator * (smlg / second_denominator);
deno = smlg;
int bigg = lcm(nume, deno);
nume = nume / bigg;
deno = deno / bigg;
System.out.println("您要求的结果是: "+nume+ "/"+deno);
//以下代码能够在控制台上显示结果
//需要调用求最大公约数的函数
//需要调用求最小公倍数的函数
}
//最小公倍数函数
static int gcd(int m,int n){
return m*n/lcm(m,n);
}
//最大公约数函数
static int lcm(int m,int n){
int temp;
while(n!=0)
{
temp = m % n;
m = n;
n =temp;
}
return m;
}
}
JAVA 程序设计 3-1 分数相加
最新推荐文章于 2022-05-17 16:31:42 发布