题解链接
class RationalNum {
//分子
private long numerator;
//分母
private long denominator;
//整数部分
private long integer;
//判断符号
private boolean isNegative = false;
//判断分母为零
private boolean isZero = false;
//参与运算的分子
private long totalNumerator;
//构造方法 对这些值初始化
public RationalNum(long n, long d) {
if(d == 0) {
isZero = true;
return;
}
if(n < 0) {
isNegative = true;
}
//经过除法计算可能将分母变为负数
if(d < 0) {
isNegative = !isNegative;
}
//如果输入是假分数转换为真分数
integer = n / d;
numerator = n - integer * d;
denominator = Math.abs(d);
//化简 分子分母同时除以最大公约数
if(numerator > 1 || numerator < -1) {
long gcd = isGcd(Math.abs(numerator), denominator);
if(gcd > 0) {
numerator /= gcd;
denominator /= gcd;
}
}
//进行计算的分子,一定是简化后
totalNumerator = integer * denominator + numerator;
}
//辗转相除法,求最大公约数
private long isGcd(long a, long b) {
if(b == 0) {
return a;
}
return isGcd(b,a % b);
}
//根据字符串得到分子
public static long parseNumerator(String s) {
return Long.parseLong(s.substring(0, s.indexOf("/")));
}
//根据字符串得到分母
public static long paresDenominator(String s) {
return Long.parseLong(s.substring(s.indexOf('/')+1));
}
//加法操作
public RationalNum add(RationalNum r) {
long n = totalNumerator * r.denominator + denominator * r.totalNumerator;
long d = denominator * r.denominator;
return new RationalNum(n, d);
}
//减法操作
public RationalNum sub(RationalNum r) {
long n = totalNumerator * r.denominator - denominator * r.totalNumerator;
long d = denominator * r.denominator;
return new RationalNum(n, d);
}
//乘法操作
public RationalNum mul(RationalNum r) {
long n = totalNumerator * r.totalNumerator;
long d = denominator * r.denominator;
return new RationalNum(n , d);
}
//除法操作
public RationalNum div(RationalNum r) {
long n = totalNumerator * r.denominator;
long d = denominator * r.totalNumerator;
return new RationalNum(n , d);
}
//对于输出进行重写
@Override
public String toString() {
StringBuffer s = new StringBuffer();
if(isZero) {
s.append("Inf");
return new String(s);
}
if(integer == 0 && numerator == 0) {
s.append("0");
return new String(s);
}
if(isNegative) {
s.append("(-");
}
//有整数,注意与分子空格
if(integer != 0) {
s.append(Math.abs(integer));
if(numerator != 0) {
s.append(' ');
}
}
//可能没有整数部分
if(numerator != 0) {
s.append(Math.abs(numerator));
s.append('/');
s.append(denominator);
}
if(isNegative) {
s.append(')');
}
return new String(s);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
//接收一个分数,next遇到换行空格等空白符停止,且不包含上述空白符
String s = in.next();
RationalNum r1 = new RationalNum(RationalNum.parseNumerator(s),RationalNum.paresDenominator(s));
//在接收第二个分数
s = in.next();
RationalNum r2 = new RationalNum(RationalNum.parseNumerator(s),RationalNum.paresDenominator(s));
System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.sub(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.mul(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.div(r2));
}
}
}