Source:
Description:
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 formk a/b
, wherek
is the integer part, anda/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, outputInf
as the result. It is guaranteed that all the output integers are in the range of long int.
Sample Input 1:
2/3 -4/2
Sample 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/6
Sample 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
Keys:
Code:
1 /* 2 Data: 2019-07-05 20:10:35 3 Problem: PAT_A1088#Rational Arithmetic 4 AC: 21:04 5 6 题目大意: 7 分数的加减乘除 8 */ 9 #include<cstdio> 10 #include<algorithm> 11 using namespace std; 12 struct fr 13 { 14 long long up,down; 15 }s1,s2; 16 17 long long GCD(long long a, long long b) 18 { 19 return !b ? a : GCD(b,a%b); 20 } 21 22 fr Reduct(fr r) 23 { 24 if(r.down < 0) 25 { 26 r.up = -r.up; 27 r.down = -r.down; 28 } 29 if(r.up==0) 30 r.down=1; 31 else 32 { 33 int gcd = GCD(abs(r.up),abs(r.down)); 34 r.up /= gcd; 35 r.down /= gcd; 36 } 37 return r; 38 } 39 40 fr Add(fr f1, fr f2) 41 { 42 fr r; 43 r.up = f1.up*f2.down+f1.down*f2.up; 44 r.down = f1.down*f2.down; 45 return Reduct(r); 46 } 47 48 fr Dif(fr f1, fr f2) 49 { 50 fr r; 51 r.up = f1.up*f2.down-f1.down*f2.up; 52 r.down = f1.down*f2.down; 53 return Reduct(r); 54 } 55 56 fr Pro(fr f1, fr f2) 57 { 58 fr r; 59 r.up = f1.up*f2.up; 60 r.down = f1.down*f2.down; 61 return Reduct(r); 62 } 63 64 fr Quo(fr f1, fr f2) 65 { 66 fr r; 67 r.up = f1.up*f2.down; 68 r.down = f1.down*f2.up; 69 return Reduct(r); 70 } 71 72 void Show(fr r) 73 { 74 r = Reduct(r); 75 if(r.up < 0) 76 printf("("); 77 if(r.down == 1) 78 printf("%lld", r.up); 79 else if(abs(r.up) > r.down) 80 printf("%lld %lld/%lld", r.up/r.down,abs(r.up)%r.down,r.down); 81 else 82 printf("%lld/%lld", r.up,r.down); 83 if(r.up < 0) 84 printf(")"); 85 } 86 87 int main() 88 { 89 #ifdef ONLINE_JUDGE 90 #else 91 freopen("Test.txt", "r", stdin); 92 #endif // ONLINE_JUDGE 93 94 scanf("%lld/%lld %lld/%lld", &s1.up,&s1.down,&s2.up,&s2.down); 95 char op[4] = {'+','-','*','/'}; 96 for(int i=0; i<4; i++) 97 { 98 Show(s1);printf(" %c ", op[i]);Show(s2);printf(" = "); 99 if(i==0) Show(Add(s1,s2)); 100 else if(i==1) Show(Dif(s1,s2)); 101 else if(i==2) Show(Pro(s1,s2)); 102 else 103 { 104 if(s2.up==0) printf("Inf"); 105 else Show(Quo(s1,s2)); 106 } 107 printf("\n"); 108 } 109 110 111 return 0; 112 }