码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1088. Rational Arithmetic

时间:2015-02-20 20:51:10      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:注意不仅结果需要化简,等号前面的公式也需要化简。
技术分享
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm> 
  4 using namespace std;
  5 char oper[4]={
  6     +,-,*,/
  7 };
  8 struct element
  9 {
 10     long long up;
 11     long long down;
 12 };
 13 element A,B;
 14 element ans;
 15 long long GCD(long long a,long long b)
 16 {
 17     return !b?a:GCD(b,a%b);
 18 }
 19 
 20 
 21 
 22 element Add()
 23 {
 24     long long up,down;
 25     element res;
 26     down=A.down*B.down;
 27     up=A.up*B.down+A.down*B.up;
 28     long long d=GCD(abs(up),abs(down));
 29     if(down<0)
 30     {
 31         down=-down;
 32         up=-up;
 33     }
 34     res.up=up/d;
 35     res.down=down/d;
 36     return res;
 37 }
 38 element Sub()
 39 {
 40     long long up,down;
 41     element res;
 42     down=A.down*B.down;
 43     up=A.up*B.down-A.down*B.up;
 44     long long d=GCD(abs(up),abs(down));
 45     if(down<0)
 46     {
 47         down=-down;
 48         up=-up;
 49     }
 50     res.up=up/d;
 51     res.down=down/d;
 52     return res;    
 53 }
 54 element Pro()
 55 {
 56     long long up=A.up*B.up;
 57     long long down=A.down*B.down;
 58     long long d=GCD(abs(up),abs(down));
 59     element res;
 60     if(down<0)
 61     {
 62         down=-down;
 63         up=-up;
 64     }
 65     up/=d;
 66     down/=d;
 67     res.up=up;
 68     res.down=down;
 69     return res;    
 70 }
 71 element Div()
 72 {
 73     element res;
 74     if(B.up==0)
 75     {
 76         res.down=0;  //有问题?
 77         return res;
 78     }
 79     long long up=A.up*B.down;
 80     long long down=A.down*B.up;
 81     long long d=GCD(abs(up),abs(down));
 82     if(down<0)
 83     {
 84         down=-down;
 85         up=-up;
 86     }
 87     up/=d;
 88     down/=d;
 89     res.up=up;
 90     res.down=down;
 91     return res;
 92 }
 93 void Print(element C)
 94 {
 95     //进行化简
 96     if(C.down<0)
 97     {
 98        C.down=-C.down;
 99        C.up=-C.up;
100     }
101     if(C.down==0)
102     {
103         printf("Inf");
104         return ;
105     }
106     if(C.up==0)
107     {
108         printf("0");
109         return ;
110     }
111     if(C.up<0)
112     {
113         printf("(");
114     }
115     long long d=GCD(abs(C.up),abs(C.down));
116     C.up/=d;
117     C.down/=d;
118     if(abs(C.up)>abs(C.down))
119     {
120         long long Ji=C.up/C.down;
121         long long tem=abs(C.up)%C.down;
122         if(tem!=0)
123           printf("%lld %lld/%lld",Ji,tem,C.down);
124           else
125             printf("%lld",Ji);
126     }
127     else if(abs(C.up)==abs(C.down))
128     {
129         printf("%lld",C.up/C.down);
130     }
131     else
132     {
133         printf("%lld/%lld",C.up,C.down);
134     }
135     if(C.up<0)
136       printf(")");
137      
138 }
139 void Pri(int i)
140 {
141     Print(A);
142     printf(" %c ",oper[i]);
143     Print(B);
144     printf(" = ");
145     Print(ans);
146     putchar(\n);
147 }
148 int main(int argc, char *argv[])
149 {
150     scanf("%lld/%lld",&A.up,&A.down);
151     scanf("%lld/%lld",&B.up,&B.down);
152     ans=Add();
153     Pri(0);
154     ans=Sub();
155     Pri(1);
156     ans=Pro();
157     Pri(2);
158     ans=Div();
159     Pri(3);
160     return 0;
161 }
View Code

 

PAT1088. Rational Arithmetic

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4296683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!