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/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

题目大意:给两个分数,计算两个数的加减乘除;负数用括号括起来,如果除数为0,结果为Inf, 所有的数都要求是最简形式;
思路:gcd()求分子分母最大公因子, tostring()把结果转换为字符串, itoa()把数字转换为字符串
比起java来说,c++对string的操作太不友好了,太不友好了!!!

 1 #include<iostream>
 2 #include<cmath>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6 long long GCD(long long a, long long b){
 7   a=abs(a); b=abs(b);
 8   if(b==0) return -1;
 9   while(b!=0){
10     long long temp=a%b;
11     a=b;
12     b=temp;
13   }
14   return a;
15 }
16 string itoa(int a){
17   string ans="";
18   if(a==0) return "0";
19   while(a!=0){
20     ans+=(char)(a%10+'0');
21     a/=10;
22   }
23   reverse(ans.begin(), ans.end());
24   return ans;
25 }
26 string tostring(long long a, long long b){
27     if(b==0) return "Inf";
28     if(a==0) return "0";
29     bool flag = a*b<0? true : false;
30     a=abs(a); b=abs(b);
31     string ans="", s;
32     if(a/b>0){
33         ans += itoa(a/b);
34         if(a%b!=0) ans += ' ';
35     }
36     if(a%b!=0){
37         ans += itoa(a%b);
38         ans += '/';
39         ans += itoa(b);
40     }
41     if(flag) ans = "(-" + ans + ')';
42     return ans;
43 }
44 int main(){
45   long long a, b, c, d;
46   scanf("%ld/%ld %ld/%ld", &a, &b, &c, &d);
47   long long fz=0, fm=0;
48   long long gcd=GCD(a, b);
49   a/=gcd; b/=gcd;
50   gcd=GCD(c, d);
51   c/=gcd; d/=gcd;
52   string s1 = tostring(a, b), s2 = tostring(c, d), s;
53   fz = a*d+c*b; fm=b*d;
54   gcd = GCD(fz, fm);
55   fz /= gcd; fm /= gcd;
56   s = tostring(fz, fm);
57   cout<<s1<<" + "<<s2<<" = "<<s<<endl;
58   fz = a*d-b*c; fm = b*d; gcd = GCD(fz, fm); fz /= gcd; fm /= gcd;
59   s = tostring(fz, fm);
60   cout<<s1<<" - "<<s2<<" = "<<s<<endl;
61   fz = a*c; fm = b*d; gcd = GCD(fz, fm); fz /= gcd; fm /= gcd;
62   s = tostring(fz, fm);
63   cout<<s1<<" * "<<s2<<" = "<<s<<endl;
64   fz = a*d; fm = c*b; gcd = GCD(fz, fm);  fz /= gcd; fm = gcd==-1? 0 : fm/gcd;
65   s = tostring(fz, fm);
66   cout<<s1<<" / "<<s2<<" = "<<s<<endl;
67   return 0;
68 }
 

猜你喜欢

转载自www.cnblogs.com/mr-stn/p/9160216.html