Java中一个分数类的简单设计
时间:2011-03-23 51cto博客 zhangjunhd
这个类对于分数提供化简和加减乘除四种操作,基于"不变"的设计原则,因此是线程安全的.
其中使用了几个算法:
[1]Fraction simpler(Fraction f);//分数化简
[2]Fraction[] RCD(Fraction f1, Fraction f2);//通分
[3]int GCD(int s, int b);//最大公约数
[4]int LCM(int a, int b);//最小公倍数
使用的形式:
[1]在控制台输入[分数][回车] //化简
[2]在控制台输入[分数][空格][运算符][空格][分数][回车] //计算
其中[运算符]为+ - * / 之一.
Fraction.java
package net.zj.fraction;
import java.io.IOException;
public class Fraction {
private int numerator;
private int denominator;
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Fraction(int numerator) {
this(numerator, 1);
}
public static Fraction add(Fraction f1, Fraction f2) {
Fraction[] fs = RCD(f1, f2);
Fraction add = new Fraction(fs[0].numerator + fs[1].numerator,
fs[0].denominator);
return simpler(add);
}
public static Fraction minus(Fraction f1, Fraction f2) {
Fraction[] fs = RCD(f1, f2);
Fraction minus = new Fraction(fs[0].numerator - fs[1].numerator,
fs[0].denominator);
return simpler(minus);
}
public static Fraction multi(Fraction f1, Fraction f2) {
Fraction multi = new Fraction(f1.numerator * f2.numerator,
f1.denominator * f2.denominator);
return simpler(multi);
}
public static Fraction div(Fraction f1, Fraction f2) {
return multi(f1, new Fraction(f2.denominator, f2.numerator));
}
public static void input(String s) {
String[] ss = s.split(" ");
if (ss.length == 1) {
Fraction f = StringToFraction(ss[0]);
if (f == null)
output("Usage: Should input a numeric");
else
output(f);
} else if (ss.length == 3) {
Fraction f1 = StringToFraction(ss[0]);
Fraction f2 = StringToFraction(ss[2]);
if (f1 == null) {
output("Usage: The first input should be numeric/numeric");
return;
}
if (f2 == null) {
output("Usage: The third input should be numeric/numeric");
return;
}
switch (ss[1].charAt(0)) {
case ''+'':
output(add(f1, f2));
break;
case ''-'':
output(minus(f1, f2));
break;
case ''*'':
output(multi(f1, f2));
break;
case ''/'':
output(div(f1, f2));
break;
default:
output("Usage: The second input should be one of +-*/");
break;
}
} else
output("Usage: Should input one fraction or two fractions and a operator with the style ''f1 + f2''");
}
public static void output(Fraction f) {
if (f.denom
|