未来编辑:请使用Apache Commons或任何其他受支持的库。这只是一个示例演示。
我实现了一个小型类,可用于此目的,也许对您有用,但请谨慎使用。
import java.util.ArrayList;
public class RationalNumber {
public ArrayList<Long> nominators = new ArrayList<Long>();
public ArrayList<Long> denominators = new ArrayList<Long>();
public RationalNumber(String rationalNumberStringValue) {
this(parseRationalNumberStringValue(rationalNumberStringValue)[0],
parseRationalNumberStringValue(rationalNumberStringValue)[1]);
}
private static Long[] parseRationalNumberStringValue(
String rationalNumberStringValue) {
boolean positive = true;
if (rationalNumberStringValue.charAt(0) == '-') {
positive = false;
rationalNumberStringValue = rationalNumberStringValue.substring(1);
}
Long[] nominatorDenominator = new Long[2];
nominatorDenominator[0] = 1l;
nominatorDenominator[1] = 1l;
String[] splittedNumberArr = rationalNumberStringValue.split("\\.");
String denominatorStr = splittedNumberArr[1];
for (int i = 0; i < denominatorStr.length(); i++) {
nominatorDenominator[1] *= 10;
}
rationalNumberStringValue = removeCharAt(rationalNumberStringValue,
rationalNumberStringValue.indexOf('.'));
nominatorDenominator[0] = Long.valueOf(rationalNumberStringValue);
if (!positive) {
nominatorDenominator[0] *= -1;
}
return nominatorDenominator;
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);
}
public RationalNumber(Integer nominator, Integer denominator) {
this((long) nominator, (long) denominator);
}
public RationalNumber(Long nominator, Long denominator) {
nominators.add(nominator);
denominators.add(denominator);
simplify();
}
public RationalNumber(ArrayList<Long> nominatorList,
ArrayList<Long> denominatorList) {
nominators.addAll(nominatorList);
denominators.addAll(denominatorList);
simplify();
}
public String getStringValue() {
return getMultipliedValue(this.nominators) + "/"
+ getMultipliedValue(this.denominators);
}
public double getDoubleValue() {
return (double) getMultipliedValue(this.nominators)
/ (double) getMultipliedValue(this.denominators);
}
public RationalNumber multiply(RationalNumber rationalNumberToMultiply) {
RationalNumber mulResult = new RationalNumber(
rationalNumberToMultiply.nominators,
rationalNumberToMultiply.denominators);
mulResult.nominators.addAll(this.nominators);
mulResult.denominators.addAll(this.denominators);
return RationalNumber.simplifyRationalNumber(mulResult);
}
public RationalNumber divide(RationalNumber rationalNumberToDivide) {
RationalNumber divideResult = new RationalNumber(
rationalNumberToDivide.nominators,
rationalNumberToDivide.denominators);
ArrayList<Long> tempLongList = divideResult.nominators;
divideResult.nominators = divideResult.denominators;
divideResult.denominators = tempLongList;
return this.multiply(divideResult);
}
public RationalNumber add(RationalNumber rationalNumberToAdd) {
rationalNumberToAdd = RationalNumber
.simplifyRationalNumber(rationalNumberToAdd);
return new RationalNumber(
(getMultipliedValue(this.nominators) * getMultipliedValue(rationalNumberToAdd.denominators))
+ (getMultipliedValue(this.denominators) * getMultipliedValue(rationalNumberToAdd.nominators)),
(getMultipliedValue(this.denominators) * getMultipliedValue(rationalNumberToAdd.denominators)));
}
public RationalNumber subtract(RationalNumber rationalNumberToSubtract) {
rationalNumberToSubtract = RationalNumber
.simplifyRationalNumber(rationalNumberToSubtract);
RationalNumber subtractTempRational = new RationalNumber(
rationalNumberToSubtract.nominators,
rationalNumberToSubtract.denominators);
subtractTempRational.nominators.set(0,
(subtractTempRational.nominators.get(0) * -1));
return this.add(subtractTempRational);
}
private long getMultipliedValue(ArrayList<Long> longList) {
Long mulResult = 1l;
for (Long tempLong : longList) {
mulResult *= tempLong;
}
return mulResult;
}
public void simplify() {
long tempGcd = 1;
long iValue = 1;
long jValue = 1;
for (int i = 0; i < this.nominators.size(); i++) {
iValue = this.nominators.get(i);
for (int j = 0; j < this.denominators.size(); j++) {
jValue = this.denominators.get(j);
tempGcd = gcd(iValue, jValue);
this.nominators.set(i, iValue / tempGcd);
this.denominators.set(j, jValue / tempGcd);
}
}
}
public static RationalNumber simplifyRationalNumber(
RationalNumber rationalNumberToSimplify) {
long tempGcd = 1;
long iValue = 1;
long jValue = 1;
for (int i = 0; i < rationalNumberToSimplify.nominators.size(); i++) {
for (int j = 0; j < rationalNumberToSimplify.denominators.size(); j++) {
iValue = rationalNumberToSimplify.nominators.get(i);
jValue = rationalNumberToSimplify.denominators.get(j);
tempGcd = gcd(iValue, jValue);
rationalNumberToSimplify.nominators.set(i, iValue / tempGcd);
rationalNumberToSimplify.denominators.set(j, jValue / tempGcd);
}
}
return rationalNumberToSimplify;
}
public static long gcd(long a, long b) {
a = Math.abs(a);
b = Math.abs(b);
if (a < b) {
long temp = a;
a = b;
b = temp;
}
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public RationalNumber add(int integerToAdd) {
RationalNumber tempRationalNumber=new RationalNumber(integerToAdd,1);
return this.add(tempRationalNumber);
}
public RationalNumber subtract(int integerToSubtract) {
RationalNumber tempRationalNumber=new RationalNumber(integerToSubtract,1);
return this.subtract(tempRationalNumber);
}
public RationalNumber multiply(int integerToMultiply) {
RationalNumber tempRationalNumber=new RationalNumber(integerToMultiply,1);
return this.multiply(tempRationalNumber);
}
public RationalNumber divide(int integerToDivide) {
RationalNumber tempRationalNumber=new RationalNumber(integerToDivide,1);
return this.divide(tempRationalNumber);
}
}
System.out.println(lcd(Arrays.asList(Rational.valueOf(1, 2), Rational.valueOf(1, 2))));
,它返回 1,尽管 2 才是最小公共分母(1/2 + 1/2 = 2/2 = 1/1)。 - Alex1357