This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrational.cpp
More file actions
150 lines (122 loc) · 3 KB
/
Copy pathrational.cpp
File metadata and controls
150 lines (122 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// rational.cpp
// Mandelbrot
//
// Created by Benjamin Mayes on 10/19/11.
// Copyright 2011 RIT. All rights reserved.
//
#include "rational.h"
rational::rational() : _n(1), _d(1) {
}
rational::rational(const long& num, const long& den) : _n(num), _d(den) {
simplify();
}
rational::rational(const long& num ) : _n(num), _d(1) {
}
rational::~rational() {}
long rational::numerator() const {
return _n;
}
long rational::denominator() const {
return _d;
}
void rational::set_numerator( const long& num ) {
_n = num;
simplify();
}
void rational::set_denominator( const long& den ) {
_d = den;
simplify();
}
int rational::is_negative() const {
return _n < 0 ^ _d < 0;
}
void rational::simplify() {
long div = gcd(_n,_d);
_n /= div;
_d /= div;
}
rational rational::operator+( const rational& rhs ) const {
long n = _n * rhs._d + rhs._n * _d;
long d = _d * rhs._d;
return rational(n,d);
}
rational rational::operator-( const rational& rhs ) const {
long n = _n * rhs._d - rhs._n * _d;
long d = _d * rhs._d;
return rational(n,d);
}
rational rational::operator*( const rational& rhs ) const {
long n = _n * rhs._n;
long d = _d * rhs._d;
return rational(n,d);
}
rational rational::operator/( const rational& rhs ) const {
long n = _n * rhs._d;
long d = _d * rhs._n;
return rational(n,d);
}
rational& rational::operator+=( const rational& rhs ) {
_n = _n * rhs._d + rhs._n * _d;
_d = _d * rhs._d;
simplify();
return *this;
}
rational& rational::operator-=( const rational& rhs ) {
_n = _n * rhs._d - rhs._n * _d;
_d = _d * rhs._d;
simplify();
return *this;
}
rational& rational::operator*=( const rational& rhs ) {
_n = _n * rhs._n;
_d = _d * rhs._d;
simplify();
return *this;
}
rational& rational::operator/=( const rational& rhs ) {
long n = _n * rhs._d;
_d = _d * rhs._n;
_n = n;
simplify();
return *this;
}
rational& rational::operator=( const rational& rhs ) {
_n = rhs._n;
_d = rhs._d;
}
int rational::operator<( const rational& rhs ) const {
return (*this - rhs).is_negative();
}
int rational::operator<=( const rational& rhs ) const {
return *this < rhs || *this == rhs;
}
int rational::operator>( const rational& rhs ) const {
return (rhs - *this).is_negative();
}
int rational::operator>=( const rational& rhs ) const {
return *this > rhs || *this == rhs;
}
int rational::operator==( const rational& rhs ) const {
return (_n == rhs._n && _d == rhs._d);
}
int rational::operator!=( const rational& rhs ) const {
return (_n != rhs._n || _d != rhs._d);
}
long gcd( long a, long b ) {
while( a && b ) {
if( a > b ) {
a -= b;
} else {
b -= a;
}
}
return a > b ? a : b;
}
std::ostream& operator<<( std::ostream& out, const rational& r ) {
out << r._n;
if( r._d > 1 ) {
out << '/' << r._d;
}
return out;
}