PAT 1088.Rational Arithmetic
1088. Rational Arithmetic (20)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:
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.
Output Specification:
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.
Sample Input 1:2/3 -4/2Sample Output 1:
2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3)Sample Input 2:
5/3 0/6Sample Output 2:
1 2/3 + 0 = 1 2/3 1 2/3 - 0 = 1 2/3 1 2/3 * 0 = 0 1 2/3 / 0 = Inf
两个分数的加减乘除,输出结果必须化为真分数结果。出了注意计算规则外,没什么特别注意点
1 #include<stdio.h> 2 3 long long getFactor(long long a, long long b){ 4 if (a<0){ 5 a = -a; 6 } 7 if (b<0){ 8 b = -b; 9 } 10 if (a < b){ 11 long long temp = b; 12 b = a; 13 a = temp; 14 } 15 if (b == 0) 16 return 1; 17 if (a%b == 0){ 18 return b; 19 } 20 long long c = a%b; 21 return getFactor(b, c); 22 } 23 24 class Rational{ 25 public: 26 long long n=0, d; 27 long long v=0; 28 void simply(){ 29 long long factor = getFactor(n, d); 30 n /= factor; 31 d /= factor; 32 if (d < 0){ 33 d = -d; 34 n = -n; 35 } 36 n += v*d; 37 v = 0; 38 v += n / d; 39 n -= d * v; 40 } 41 void complicate(){ 42 long long factor = getFactor(n, d); 43 n /= factor; 44 d /= factor; 45 n += v*d; 46 v = 0; 47 } 48 void print(){ 49 if (v == 0){ 50 if (n < 0){ 51 printf("(%lld/%lld)", n, d); 52 } 53 else if (n == 0){ 54 printf("0"); 55 } 56 else{ 57 printf("%lld/%lld", n, d); 58 } 59 return; 60 } 61 if (n == 0){ 62 if (v < 0){ 63 printf("(%lld)", v); 64 } 65 else{ 66 printf("%lld", v); 67 } 68 return; 69 } 70 if (v < 0){ 71 printf("(%lld %lld/%lld)", v, -n, d); 72 } 73 else{ 74 printf("%lld %lld/%lld", v, n, d); 75 } 76 } 77 }; 78 79 void printRes(Rational rat[2], Rational res, char op){ 80 rat[0].print(); 81 printf(" %c ", op); 82 rat[1].print(); 83 printf(" = "); 84 res.print(); 85 printf("\n"); 86 } 87 88 int main(void){ 89 Rational rat[2],res; 90 int i; 91 scanf("%lld/%lld", &rat[0].n, &rat[0].d); 92 scanf("%lld/%lld", &rat[1].n, &rat[1].d); 93 rat[0].simply(); 94 rat[1].simply(); 95 long long factor = getFactor(rat[0].d, rat[1].d); 96 res.d = rat[0].d / factor * rat[1].d; 97 res.n = res.d / rat[0].d * rat[0].n + res.d / rat[1].d * rat[1].n; 98 res.v = rat[0].v + rat[1].v; 99 res.simply(); 100 printRes(rat, res, '+'); 101 102 res.d = rat[0].d / factor * rat[1].d; 103 res.n = res.d / rat[0].d * rat[0].n - res.d / rat[1].d * rat[1].n; 104 res.v = rat[0].v - rat[1].v; 105 res.simply(); 106 printRes(rat, res, '-'); 107 108 rat[0].complicate(); 109 rat[1].complicate(); 110 res.n = rat[0].n*rat[1].n; 111 res.d = rat[0].d*rat[1].d; 112 res.v = 0; 113 res.simply(); 114 rat[0].simply(); 115 rat[1].simply(); 116 printRes(rat, res, '*'); 117 118 rat[0].complicate(); 119 rat[1].complicate(); 120 res.n = rat[0].n*rat[1].d; 121 res.d = rat[0].d*rat[1].n; 122 res.v = 0; 123 rat[0].simply(); 124 rat[1].simply(); 125 if (res.d != 0){ 126 res.simply(); 127 printRes(rat, res, '/'); 128 } 129 else{ 130 rat[0].print(); 131 printf(" / "); 132 rat[1].print(); 133 printf(" = Inf\n"); 134 } 135 136 return 0; 137 }