PAT 1088. Rational Arithmetic (20)

http://www.patest.cn/contests/pat-a-practise/1088

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cassert>
  4 
  5 using namespace std;
  6 
  7 typedef long long LL;
  8 
  9 struct Fraction {
 10     LL x;
 11     LL y;
 12     char s[100];
 13 
 14     Fraction(LL _x, LL _y) {
 15         assert(_y > 0);
 16         x = _x;
 17         y = _y;
 18         if (y < 0) {
 19             y = -y;
 20             x = -x;
 21         }
 22     }
 23 
 24     LL _gcd(LL _x, LL _y) const {
 25         return _y == 0 ? _x : _gcd(_y, _x % _y);
 26     }
 27 
 28     LL _lcm(LL _x, LL _y) const {
 29         return _x / _gcd(_x, _y) * _y;
 30     }
 31 
 32     Fraction operator + (const Fraction &other) const {
 33         LL lcm = _lcm(y, other.y);
 34         LL _ox = lcm / other.y * other.x;
 35         LL _x = lcm / y * x + _ox;
 36         LL _y = lcm;
 37         return Fraction(_x, _y);
 38     }
 39 
 40     Fraction operator - (const Fraction &other) const {
 41         LL lcm = _lcm(y, other.y);
 42         LL _ox = lcm / other.y * other.x;
 43         LL _x = lcm / y * x - _ox;
 44         LL _y = lcm;
 45         return Fraction(_x, _y);
 46     }
 47 
 48     Fraction operator * (const Fraction &other) const {
 49         LL _x = x * other.x;
 50         LL _y = y * other.y;
 51         return Fraction(_x, _y);
 52     }
 53 
 54     Fraction operator / (const Fraction &other) const {
 55         LL _x = other.x;
 56         LL _y = other.y;
 57         if (_x < 0) {
 58             _y = -_y;
 59             _x = -_x;
 60         }
 61         Fraction c(_y, _x);
 62         return *this * c;
 63     }
 64 
 65     void simply() {
 66         LL _x = x >= 0 ? x : -x;
 67         LL g = _gcd(_x, y);
 68         x /= g;
 69         y /= g;
 70     }
 71 
 72     bool test() const {
 73         return x == 0;
 74     }
 75 
 76     char* to_string() {
 77         simply();
 78         int sign = x >= 0 ? 1 : -1;
 79         LL  _x = x >= 0 ? x : -x;
 80         if (_x % y == 0) {
 81             if (sign < 0) {
 82                 sprintf(s, "(%lld)", _x / y * sign);
 83             } else {
 84                 sprintf(s, "%lld", _x / y * sign);
 85             }
 86         } else {
 87             if (_x < y) {
 88                 if (sign < 0) 
 89                     sprintf(s, "(%lld/%lld)", x, y);
 90                 else 
 91                     sprintf(s, "%lld/%lld", x, y);
 92             } else {
 93                 LL int_part = _x / y;
 94                 _x %= y;
 95                 if (sign < 0) {
 96                     sprintf(s, "(%lld %lld/%lld)", int_part * sign, _x, y);
 97                 } else {
 98                     sprintf(s, "%lld %lld/%lld", int_part, _x, y);
 99                 }
100             }
101         }
102         return s;
103     }
104 };
105 
106 int main() {
107     freopen("input", "r", stdin);
108     LL x1, y1, x2, y2;
109     scanf("%lld/%lld %lld/%lld", &x1, &y1, &x2, &y2);
110     //printf("x1 = %lld\ny1 = %lld\nx2 = %lld\ny2 = %lld\n", x1, y1, x2, y2);
111     Fraction a(x1, y1);
112     Fraction b(x2, y2);
113     printf("%s + %s = %s\n", a.to_string(), b.to_string(), (a + b).to_string());
114     printf("%s - %s = %s\n", a.to_string(), b.to_string(), (a - b).to_string());
115     printf("%s * %s = %s\n", a.to_string(), b.to_string(), (a * b).to_string());
116     printf("%s / %s = ", a.to_string(), b.to_string());
117     if (b.test()) {
118         printf("Inf\n");
119     } else {
120         printf("%s\n", (a / b).to_string());
121     }
122     return 0;
123 }

 

posted @ 2015-08-06 02:45  ACSeed  Views(223)  Comments(0)    收藏  举报