Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int gcd(int a, int b) {
  4.     if (b == 0) {
  5.         return a;
  6.     }
  7.     return gcd(b, a % b);
  8. }
  9. struct rational {
  10.     int m;
  11.     int n;
  12.     rational(int a, int b) {
  13.         m = a / gcd(a, b);
  14.         n = b / gcd(a, b);
  15.     }
  16.     rational(int a) {
  17.         m = a;
  18.         n = 1;
  19.     }
  20.     rational() {
  21.         m = 0;
  22.         n = 1;
  23.     }
  24.     rational(const rational &A) { //copy
  25.         m = A.m;
  26.         n = A.n;
  27.     }
  28.     rational &operator=(const rational &A) { //=
  29.         m = A.m;
  30.         n = A.n;
  31.         return *this;
  32.     }
  33.     rational &operator*=(const rational &A) { //operator *=
  34.         m *= A.m;
  35.         n *= A.n;
  36.         int d = gcd(m, n);
  37.         m /= d;
  38.         n /= d;
  39.         return *this;
  40.     }
  41.     rational &operator/=(const rational &A) { //operator /=
  42.         m *= A.n;
  43.         n *= A.m;
  44.         int d = gcd(m, n);
  45.         m /= d;
  46.         n /= d;
  47.         return *this;
  48.     }
  49.     rational &operator+=(const rational &A) { //operator +=
  50.         m = ((m * A.n) + (n * A.m));
  51.         n *= A.n;
  52.         int d = gcd(m, n);
  53.         m /= d;
  54.         n /= d;
  55.         return *this;
  56.     }
  57.     rational &operator-=(const rational &A) { //operator -=
  58.         m = ((m * A.n) - (n * A.m));
  59.         n *= A.n;
  60.         int d = gcd(m, n);
  61.         m /= d;
  62.         n /= d;
  63.         return *this;
  64.     }
  65. };
  66. rational &operator*(rational A, const rational &B) { //*
  67.     return A *= B;
  68. }
  69. rational &operator/(rational A, const rational &B) { //   /
  70.     return A /= B;
  71. }
  72. rational &operator+(rational A, const rational &B) { //  +
  73.     return A += B;
  74. }
  75. rational &operator-(rational A, const rational &B) { // -
  76.     return A -= B;
  77. }
  78. bool operator==(const rational &A, const rational &B) { // ==
  79.     return ((A.m == B.m) && (A.n == B.n));
  80. }
  81. bool operator!=(const rational &A, const rational &B) { // !=
  82.     return !(A == B);
  83. }
  84. bool operator>(const rational &A, const rational &B) { //  >
  85.     return (((A.m*B.n) - (B.m*A.n) > 0) && (A.n*A.m > 0)) || (((A.m*B.n) - (B.m*A.n) < 0) && (A.n*A.m < 0));
  86. }
  87. bool operator<(const rational &A, const rational &B) { //  <
  88.     return (((A.m*B.n) - (B.m*A.n) < 0) && (A.n*A.m > 0)) || (((A.m*B.n) - (B.m*A.n) > 0) && (A.n*A.m < 0));
  89. }
  90. bool operator>=(const rational &A, const rational &B) { //  >=
  91.     return !(A < B);
  92. }
  93. bool operator<=(const rational &A, const rational &B) { //  <=
  94.     return !(A > B);
  95. }
  96. istream& operator >>(istream &in, rational &A)
  97. {
  98.     in >> A.m >> A.n;
  99.     int d = gcd(A.m, A.n);
  100.     A.m /= d;
  101.     A.n /= d;
  102.     return in;
  103. }
  104. ostream& operator <<(ostream &out, const rational &A)
  105. {
  106.     out << A.m << "/" << A.n;
  107.     return out;
  108. }
  109. int main() {
  110.     rational A(1, 2), B(1, 6);
  111.     cout << "A = " << A << " B = " << B << endl;
  112.     cout << "A + B = " << A + B << endl;
  113.     cout << "A - B = " << A - B << endl;
  114.     cout << "A * B = " << A * B << endl;
  115.     cout << "A / B = " << A / B << endl;
  116.     rational(X) = A *= B;
  117.     cout << "A *= B =" << X << endl;
  118.     rational(Y) = A /= B;
  119.     cout << "A /= B =" << Y << endl;
  120.     rational(Z) = A += B;
  121.     cout << "A += B =" << Z << endl;
  122.     rational(O) = A -= B;
  123.     cout << "A -= B =" << O << endl;
  124.     rational C, E(5, 7);
  125.     cout << "E = " << E  << endl;
  126.     cin >> C;
  127.     cout << "C = " << C << endl;
  128.     if (C==E) {
  129.         cout << C << " == " << E << endl;
  130.     }
  131.     if (C!=E) {
  132.         cout << C << " != " << E << endl;
  133.     }
  134.     if (C > E) {
  135.         cout << C << " > " << E << endl;
  136.     }
  137.     if (C >= E) {
  138.         cout << C << " >= " << E << endl;
  139.     }
  140.     if (C < E) {
  141.         cout << C << " < " << E << endl;
  142.     }
  143.     if (C <= E) {
  144.         cout << C << " <= " << E << endl;
  145.     }
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement