标签:
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 <iostream> 2 #include <string> 3 #include <sstream> 4 5 using namespace std; 6 7 struct Fraction 8 { 9 long a; 10 long b; 11 }; 12 13 Fraction Add(const Fraction& lhs, const Fraction& rhs) 14 { 15 Fraction sum; 16 sum.b = lhs.b*rhs.b; 17 sum.a = lhs.a*rhs.b + rhs.a*lhs.b; 18 19 return sum; 20 } 21 22 Fraction Sub(const Fraction& lhs, const Fraction& rhs) 23 { 24 Fraction neg = rhs; 25 neg.a = -neg.a; 26 27 return Add(lhs, neg); 28 } 29 30 Fraction Mul(const Fraction& lhs, const Fraction& rhs) 31 { 32 Fraction mul; 33 mul.a = lhs.a * rhs.a; 34 mul.b = lhs.b * rhs.b; 35 36 return mul; 37 } 38 39 Fraction Div(const Fraction& lhs, const Fraction& rhs) 40 { 41 Fraction rev; 42 rev.a = rhs.b; 43 rev.b = rhs.a; 44 45 46 return Mul(lhs, rev); 47 } 48 49 string Reduction(Fraction fra) 50 { 51 long min = abs(fra.a) < abs(fra.b) ? abs(fra.a) : abs(fra.b); 52 long max = abs(fra.a) > abs(fra.b) ? abs(fra.a) : abs(fra.b); 53 54 if (fra.a == 0) 55 return "0"; 56 else if (fra.b == 0) 57 return "Inf"; 58 if (fra.b < 0) 59 { 60 fra.a = -fra.a; 61 fra.b = -fra.b; 62 } 63 while (max%min != 0) 64 { 65 long tmp = min; 66 min = max % min; 67 max = tmp; 68 } 69 fra.a /= min; 70 fra.b /= min; 71 stringstream ss; 72 if (fra.b == 1) 73 ss << fra.a; 74 else if (abs(fra.a) < abs(fra.b)) 75 ss << fra.a << "/" << fra.b; 76 else 77 ss << fra.a / fra.b << " " << abs(fra.a)%fra.b << "/" << fra.b; 78 if (fra.a < 0) 79 return "(" + ss.str() + ")"; 80 return ss.str(); 81 } 82 83 int main() 84 { 85 Fraction lhs, rhs; 86 string s; 87 getline(cin, s); 88 for (int i = 0; i < s.size(); i++) 89 if (s[i] == ‘/‘) 90 s[i] = ‘ ‘; 91 stringstream ss; 92 ss << s; 93 ss >> lhs.a >> lhs.b >> rhs.a >> rhs.b; 94 95 cout << Reduction(lhs) << " + " << Reduction(rhs) << " = " << Reduction(Add(lhs, rhs)) << endl; 96 cout << Reduction(lhs) << " - " << Reduction(rhs) << " = " << Reduction(Sub(lhs, rhs)) << endl; 97 cout << Reduction(lhs) << " * " << Reduction(rhs) << " = " << Reduction(Mul(lhs, rhs)) << endl; 98 cout << Reduction(lhs) << " / " << Reduction(rhs) << " = " << Reduction(Div(lhs, rhs)) << endl; 99 }
PAT 1088. Rational Arithmetic (20)
标签:
原文地址:http://www.cnblogs.com/jackwang822/p/4737623.html