• API
• FAQ
• Tools
• Archive
daily pastebin goal
49%
SHARE
TWEET

# Untitled

dmitryokh Dec 7th, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2.
3. int gcd(int a, int b) {
4.     if (b == 0)
5.         return a;
6.     else
7.         return gcd (b, a % b);
8. }
9.
10.
11. class Rational {
12. private:
13.     int num, den;
14.
15. public:
16.     Rational(int n, int d) {
17.         num = 0;
18.         den = 1;
19.         int gcdd = gcd(n, d);
20.         n /= gcdd;
21.         d /= gcdd;
22.         if (d < 0) {
23.             n *= -1;
24.             d *= -1;
25.         }
26.         num = n;
27.         den = d;
28.     }
29.
30.     Rational(int n) {
31.         num = n;
32.         den = 1;
33.     }
34.
35.     int numerator() const {
36.         return num;
37.     }
38.
39.     int denominator() const {
40.         return den;
41.     }
42.
43.     Rational operator + (const Rational& r) {
44.         return Rational (num * r.den + den * r.num, den * r.den);
45.     }
46.
47.     Rational operator + (const int& i) {
48.         return Rational (num + den * i, den);
49.     }
50.
51.     Rational operator - (const Rational& r) {
52.         return Rational (num * r.den - den * r.num, den * r.den);
53.     }
54.
55.     Rational operator - (const int& i) {
56.         return Rational (num - den * i, den);
57.     }
58.
59.     Rational operator * (const Rational& r) {
60.         return Rational (num * r.num, den * r.den);
61.     }
62.
63.     Rational operator * (const int& i) {
64.         return Rational (num * i, den);
65.     }
66.
67.     Rational operator / (const Rational& r) {
68.         return Rational (num * r.den, den * r.num);
69.     }
70.
71.     Rational operator / (const int& i) {
72.         return Rational (num, den * i);
73.     }
74.
75.     Rational operator + () {
76.         return Rational (num, den);
77.     }
78.
79.     Rational operator - () {
80.         return Rational (-num, -den);
81.     }
82.
83.     Rational operator += (const Rational& r) {
84.         num = num * r.den + den * r.num;
85.         den = den * r.den;
86.         int gcdd = gcd(num, den);
87.         num /= gcdd;
88.         den /= gcdd;
89.         if (den < 0) {
90.             num *= -1;
91.             den *= -1;
92.         }
93.         return *this;
94.     }
95.
96.     Rational operator -= (const Rational& r) {
97.         num = num * r.den - den * r.num;
98.         den = den * r.den;
99.         int gcdd = gcd(num, den);
100.         num /= gcdd;
101.         den /= gcdd;
102.         if (den < 0) {
103.             num *= -1;
104.             den *= -1;
105.         }
106.         return *this;
107.     }
108.
109.     Rational operator *= (const Rational& r) {
110.         num = num * r.num;
111.         den = den * r.den;
112.         int gcdd = gcd(num, den);
113.         num /= gcdd;
114.         den /= gcdd;
115.         if (den < 0) {
116.             num *= -1;
117.             den *= -1;
118.         }
119.         return *this;
120.     }
121.
122.     Rational operator /= (const Rational& r) {
123.         num = num * r.den;
124.         den = den * r.num;
125.         int gcdd = gcd(num, den);
126.         num /= gcdd;
127.         den /= gcdd;
128.         if (den < 0) {
129.             num *= -1;
130.             den *= -1;
131.         }
132.         return *this;
133.     }
134.
135.     bool operator == (const Rational& r) {
136.         return (num == r.num && den == r.den);
137.     }
138.
139.     bool operator != (const Rational& r) {
140.         return (num != r.num || den != r.den);
141.     }
142.
143.     Rational operator++ () {
144.         num = num + den;
145.         int gcdd = gcd(num, den);
146.         num /= gcdd;
147.         den /= gcdd;
148.         if (den < 0) {
149.             num *= -1;
150.             den *= -1;
151.         }
152.         return *this;
153.     }
154.
155.     Rational operator-- () {
156.         num = num + den;
157.         int gcdd = gcd(num, den);
158.         num /= gcdd;
159.         den /= gcdd;
160.         if (den < 0) {
161.             num *= -1;
162.             den *= -1;
163.         }
164.         return *this;
165.     }
166.
167.     Rational operator++ (int) {
168.         Rational temp(*this);
169.         num = num + den;
170.         int gcdd = gcd(num, den);
171.         num /= gcdd;
172.         den /= gcdd;
173.         if (den < 0) {
174.             num *= -1;
175.             den *= -1;
176.         }
177.         return temp;
178.     }
179.
180.     Rational operator-- (int) {
181.         Rational temp(*this);
182.         num = num - den;
183.         int gcdd = gcd(num, den);
184.         num /= gcdd;
185.         den /= gcdd;
186.         if (den < 0) {
187.             num *= -1;
188.             den *= -1;
189.         }
190.         return temp;
191.     }
192. };
193.
194. Rational operator * (const int& i, const Rational& r) {
195.     return Rational(r.numerator() * i, r.denominator());
196. }
197.
198. Rational operator / (const int& i, const Rational& r) {
199.     return Rational(r.denominator() * i, r.numerator());
200. }
201.
202. Rational operator + (const int& i, const Rational& r) {
203.     return Rational(r.numerator() + r.denominator() * i, r.denominator());
204. }
205.
206. Rational operator - (const int& i, const Rational& r) {
207.     return Rational(r.denominator() * i - r.numerator(), r.denominator());
208. }
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