• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Feb 19th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <cmath>
3.
4.
5. class Rational{
6.     int a, b;
7.
8.     int nod(int t, int s) const {
9.             while (s) {
10.                 t %= s;
11.                 std::swap(t, s);
12.             }
13.             return abs(t);
14.     }
15.
16.     void rev_com_div() {
17.         if (b < 0) {
18.             a *= -1;
19.             b *= -1;
20.         }
21.         int s = nod(a, b);
22.         a /= s;
23.         b /= s;
24.     }
25.
26. public:
27.     Rational(int a = 0, int b = 1) : a(a), b(b)
28.     {
29.         rev_com_div();
30.     }
31.
32.     Rational(Rational const& s) : a(s.a), b(s.b)
33.     {
34.         rev_com_div();
35.     }
36.
37.     Rational & Add(Rational const& s) {
38.         a = a * s.b + b * s.a;
39.         b = b * s.b;
40.         rev_com_div();
41.         return *this;
42.     }
43.
44.     Rational & Substract(Rational const& s) {
45.         a = a * s.b - b * s.a;
46.         b = b * s.b;
47.         rev_com_div();
48.         return *this;
49.     }
50.
51.     Rational & Multiply(Rational const& s) {
52.         a *= s.a;
53.         b *= s.b;
54.         rev_com_div();
55.         return *this;
56.     }
57.
58.     Rational & Divide(Rational const& s) {
59.         a *= s.b;
60.         b *= s.a;
61.         rev_com_div();
62.         return *this;
63.     }
64.
65.     bool EqualTo(Rational const& s) const {
66.         return a == s.a && b == s.b;
67.     }
68.
69.     int CompareTo(Rational const& s) const {
70.         int sum = a * s.b - s.a * b;
71.         return sum;
72.     }
73.
74.     bool IsInteger() const {
75.         return b == 1;
76.     }
77.
78.     std::string ToString() const {
79.         return std::to_string(a) + ":" + std::to_string(b);
80.     }
81.
82. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top