For two rational numbers, your task is to implement the basic
arithmetics, that is, to calculate their sum, difference,
product and quotient.
Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2".
The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in
front of the numerator. The denominators are guaranteed to be non-zero numbers.
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each
line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is
the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the
denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.
5/3 0/6
1 2/3 + 0 = 1 2/3<br/>1 2/3 - 0 = 1 2/3<br/>1 2/3 * 0 = 0<br/>1 2/3 / 0 = Inf
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String str1 = in.next(); String str2 = in.next(); String[] s1 = str1.split("/"); String[] s2 = str2.split("/"); int num1 = Integer.parseInt(s1[0]) * Integer.parseInt(s2[1]); int demo1 = Integer.parseInt(s1[1]) * Integer.parseInt(s2[1]); int num2 = Integer.parseInt(s2[0]) * Integer.parseInt(s1[1]); int demo2 = Integer.parseInt(s2[1]) * Integer.parseInt(s1[1]); String befor = func(num1, demo1); String after = func(num2, demo2); System.out.println(befor + " + " + after + " = " + func(num1 + num2, demo1)); System.out.println(befor + " - " + after + " = " + func(num1 - num2, demo1)); System.out.println(befor + " * " + after + " = " + func(num1 * num2, demo1 * demo2)); System.out.println(befor + " / " + after + " = " + func(num1 * demo2, demo1 * num2)); } } public static String func(int num, int demo) { if (num == 0) { return "0"; } if (demo == 0) { return "Inf"; } int coe = maxCoe(num, demo); num /= coe; demo /= coe; String ret = fake(num, demo); return ret; } public static int maxCoe(int num, int demo) { int i = num % demo; while (i != 0) { num = demo; demo = i; i = num % demo; } return demo; } public static String fake(int num, int demo) { boolean flag = true; if (demo == 0) { return "Inf"; } if (num < 0) { flag = !flag; num *= (-1); } if (demo < 0) { flag = !flag; demo *= (-1); } String frac = ""; if (num < demo) { frac = num + "/" + demo; } else if (num == demo) { frac = "1"; } else { int inte = num / demo; num = num - inte * demo; if (num != 0) { frac = inte + " " + num + "/" + demo; } else { frac = inte + ""; } } if (flag) { return frac; } else { return "(-" + frac + ")"; } } }面向测试用例编程
import java.util.Scanner; public class Main { static void pr(long a, long b) { long x = gcd(a, b); a /= x; b /= x; if (b < 0) { a *= -1; b *= -1; } if (a < 0) { if (-a % b == 0) { System.out.print("(" + a / b + ")"); } else if (-a < b) { System.out.print("("+a + "/" + b+")"); } else { System.out.print("(" + a / b + " " + (-a + (b * (a / b))) + "/" + b + ")"); } } else { if (a % b == 0) { System.out.print(a / b); } else if (a < b) { System.out.print(a + "/" + b); } else { System.out.print(a / b + " " + (a - (b * (a / b))) + "/" + b ); } } } static long gcd(long a, long b) { //辗转相除法求最大公约数 if (b == 0) { return a; } long r = a % b; return gcd(b, r); } static void add(long a, long b, long c, long d) { pr(a, b); System.out.print(" + "); pr(c, d); System.out.print(" = "); pr(a * d + b * c, b * d); } static void minus(long a, long b, long c, long d) { pr(a, b); System.out.print(" - "); pr(c, d); System.out.print(" = "); pr(a * d - b * c, b * d); } static void multiply(long a, long b, long c, long d) { pr(a, b); System.out.print(" * "); pr(c, d); System.out.print(" = "); pr(a * c, b * d); } static void division(long a, long b, long c, long d) { long m = a * d; long n = b * c; pr(a, b); System.out.print(" / "); pr(c, d); System.out.print(" = "); if (c == 0) { System.out.print("Inf"); } else { if (n < 0) { m = m * -1; //把负号调整到分子上 n = n * -1; } pr(m, n); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); String[] split = s.split(" "); String[] s1 = split[0].split("/"); String[] s2 = split[1].split("/"); long a = Long.parseLong(s1[0]); long b = Long.parseLong(s1[1]); long c = Long.parseLong(s2[0]); long d = Long.parseLong(s2[1]); add(a, b, c, d); System.out.println(); minus(a, b, c, d); System.out.println(); multiply(a, b, c, d); System.out.println(); division(a, b, c, d); } } }