1 | <?php |
||
5 | class Fraction |
||
6 | { |
||
7 | /** @var int $whole */ |
||
8 | private $whole; |
||
9 | |||
10 | /** @var int $numerator */ |
||
11 | private $numerator; |
||
12 | |||
13 | /** @var int $denominator */ |
||
14 | private $denominator; |
||
15 | |||
16 | /** @var bool $negative */ |
||
17 | private $negative; |
||
18 | |||
19 | 5 | public function __construct($whole = 0, $numerator = 0, $denominator = 1) |
|
26 | |||
27 | /** |
||
28 | * @return int |
||
29 | */ |
||
30 | 1 | public function getWhole() |
|
34 | |||
35 | /** |
||
36 | * @param int $whole |
||
37 | * @return Fraction |
||
38 | */ |
||
39 | 4 | public function setWhole($whole) |
|
44 | |||
45 | /** |
||
46 | * @return int |
||
47 | */ |
||
48 | 1 | public function getNumerator() |
|
52 | |||
53 | /** |
||
54 | * @param int $numerator |
||
55 | * @return Fraction |
||
56 | */ |
||
57 | 4 | public function setNumerator($numerator) |
|
62 | |||
63 | /** |
||
64 | * @return int |
||
65 | */ |
||
66 | 1 | public function getDenominator() |
|
70 | |||
71 | /** |
||
72 | * @param int $denominator |
||
73 | * @return Fraction |
||
74 | */ |
||
75 | 4 | public function setDenominator($denominator) |
|
80 | |||
81 | 1 | private function refactor() |
|
82 | { |
||
83 | // 9/8 would become 1 1/8 for instance |
||
84 | 1 | if ($this->shouldRefactorWhole()) { |
|
85 | 1 | $this->refactorWhole(); |
|
86 | } |
||
87 | 1 | if ($this->shouldRefactorFraction()) { |
|
88 | 1 | $this->refactorFraction(); |
|
89 | } |
||
90 | 1 | } |
|
91 | |||
92 | /** |
||
93 | * @return bool |
||
94 | */ |
||
95 | 1 | private function shouldRefactorWhole() |
|
100 | |||
101 | /** |
||
102 | * @return bool |
||
103 | */ |
||
104 | 1 | private function shouldRefactorFraction() |
|
109 | |||
110 | 1 | private function refactorWhole() |
|
111 | { |
||
112 | // decrement $x and the numerator by the denominator each loop, and add to the whole |
||
113 | 1 | for ($x = $this->numerator; $x >= $this->denominator; $x = $x - $this->denominator) { |
|
114 | 1 | $this->whole ++; |
|
115 | 1 | $this->numerator -= $this->denominator; |
|
116 | } |
||
117 | 1 | } |
|
118 | |||
119 | 1 | private function refactorFraction() |
|
125 | |||
126 | /** |
||
127 | * @param int $x |
||
128 | * @param int $y |
||
129 | * @return int |
||
130 | */ |
||
131 | 1 | private function getGreatestCommonDenominator($x, $y) |
|
145 | |||
146 | /** |
||
147 | * @param int $num |
||
148 | * @return array The common denominators of $num |
||
149 | */ |
||
150 | 1 | private function getFactors($num) |
|
151 | { |
||
152 | 1 | $factors = []; |
|
153 | // get factors of the numerator |
||
154 | 1 | for ($x = 1; $x <= $num; $x ++) { |
|
155 | 1 | if ($num % $x == 0) { |
|
156 | 1 | $factors[] = $x; |
|
157 | } |
||
158 | } |
||
159 | 1 | return $factors; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | 1 | public function isNegative() |
|
166 | { |
||
167 | 1 | return $this->negative; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param bool $negative |
||
172 | * @return Fraction |
||
173 | */ |
||
174 | 1 | public function setNegative($negative) |
|
175 | { |
||
176 | 1 | $this->negative = $negative; |
|
177 | 1 | return $this; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1 | public function isInteger() |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | public function __toString() |
|
192 | { |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | 1 | private function getStringWhole() |
|
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | 1 | private function getStringFraction() |
|
216 | |||
217 | /** |
||
218 | * @param string $whole |
||
219 | * @param string $fraction |
||
220 | * @return string |
||
221 | */ |
||
222 | 1 | private function formatString($whole, $fraction) |
|
227 | |||
228 | /** |
||
229 | * @return float |
||
230 | */ |
||
231 | 1 | public function toDecimal() |
|
240 | } |