Jul 13th, 2020
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2.
3. struct Rational {
4.     Rational(int numerator = 0, int denominator = 1) : numerator_(numerator), denominator_(denominator) {};
5.
7.         numerator_ = numerator_ * rational.denominator_ + rational.numerator_ * denominator_;
8.         denominator_ *= rational.denominator_;
9.     }
10.
11.     void sub(Rational rational) {
12.         numerator_ = numerator_ * rational.denominator_ - rational.numerator_ * denominator_;
13.         denominator_ *= rational.denominator_;
14.     }
15.
16.     void mul(Rational rational) {
17.         numerator_ *= rational.numerator_;
18.         denominator_ *= rational.denominator_;
19.     }
20.
21.     void div(Rational rational) {
22.         numerator_ *= rational.denominator_;
23.         denominator_ *= rational.numerator_;
24.     }
25.
26.     void neg() {
27.         numerator_ = -numerator_;
28.     }
29.
30.     void inv() {}
31.
32.     double to_double() const {
33.         return numerator_ / (double)denominator_;
34.     }
35.
36.     explicit operator bool() const
37.     {
38.         return numerator_;
39.     }
40.
41.     Rational & operator += (Rational r)
42.     {
44.         return *this;
45.     }
46.
47.     Rational & operator -= (Rational r)
48.     {
49.         this->sub(r);
50.         return *this;
51.     }
52.
53.     Rational & operator *= (Rational r)
54.     {
55.         this->mul(r);
56.         return *this;
57.     }
58.
59.     Rational & operator /= (Rational r)
60.     {
61.         this->div(r);
62.         return *this;
63.     }
64.
65.     Rational operator - () const
66.     {
67.         return Rational(-this->numerator_, this->denominator_);
68.     }
69.
70.     Rational operator + () const
71.     {
72.         return *this;
73.     }
74.
75.     int & getNum() { return numerator_; }
76.     int & getDenom() { return denominator_; }
77.
78.     int getNum() const { return numerator_; }
79.     int getDenom() const { return denominator_; }
80.
81. private:
82.     int numerator_;
83.     int denominator_;
84. };
85.
86. Rational operator + (Rational r1, Rational const & r2)
87. {
88.     return r1 += r2;
89. }
90.
91. Rational operator - (Rational r1, Rational const & r2)
92. {
93.     return r1 -= r2;
94. }
95.
96. Rational operator * (Rational r1, Rational const & r2)
97. {
98.     return r1 *= r2;
99. }
100.
101. Rational operator / (Rational r1, Rational const & r2)
102. {
103.     return r1 /= r2;
104. }
105.
106. bool operator == (Rational const & r1, Rational const & r2)
107. {
108.     return !(r1 - r2);
109. }
110.
111. bool operator < (Rational const & r1, Rational const & r2)
112. {
113.     return ((r1.getNum() * r2.getDenom() - r2.getNum() * r1.getDenom()) < 0);
114. }
115.
116. bool operator > (Rational const & r1, Rational const & r2)
117. {
118.     return r2 < r1;
119. }
120.
121. bool operator <= (Rational const & r1, Rational const & r2)
122. {
123.     return !(r2 < r1);
124. }
125.
126. bool operator >= (Rational const & r1, Rational const & r2)
127. {
128.     return !(r1 < r2);
129. }
130.
131. bool operator != (Rational const & r1, Rational const & r2)
132. {
133.     return !(r1 == r2);
134. }
135.
136. using namespace std;
137.
138. int main() {
139.     Rational r1(1, 1);
140.     Rational r2(2, 3);
141.
142.     cout << (r1 < 1.5) << endl;
143.
144.     //cout << (+r1).to_double() << endl;
145.     //cout << (-r1).to_double() << endl;
146.     //Rational r2(1, 3);
147.     //Rational r3(5);
148.
150.     //std::cout << r1.to_double() << std::endl;
151.     //r1.sub(r2);
152.     //std::cout << r1.to_double() << std::endl;
153.     //r1.neg();
154.     //std::cout << r1.to_double() << std::endl;
155.     //r3.mul(r1);
156.     //std::cout << r3.to_double() << std::endl;
157.     //r3.div(r2);
158.     //std::cout << r3.to_double() << std::endl;
159.
160.     system("pause>nul");
161.     return 0;
162. }
RAW Paste Data