-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractionCalculator.php
More file actions
256 lines (223 loc) · 6.28 KB
/
fractionCalculator.php
File metadata and controls
256 lines (223 loc) · 6.28 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
require_once 'fraction.php';
/**
* Fraction Calculation Class
*
* @autor oreX
*/
class fractionCalculator {
/**
* Holds the chosen mathmatical operator
*
* @var string
*/
protected $_operator = 'add';
/**
* Holds an array of fractions to use
*
* @var array of fraction
* @see fraction
*/
protected $_fractions = array();
/**
* Holds the result
*
* @var fraction
*/
public $result = null;
/*
* Holds any error messages
*
* @var array
*/
public $errors = array();
/**
* Method to add a fraction
*
* @param fraction $fraction
*/
public function addFraction(fraction $fraction) {
$this->_fractions[] = $fraction;
}
/**
* Sets the operator
*
* @param string $operator
*/
public function setOperator($operator) {
$validOperators = array(
'add',
'subtract',
'multiply',
'divide',
);
if (!in_array($operator, $validOperators)) {
throw new Exception("Invalid operator of '$operator' specified.");
}
$this->_operator = $operator;
}
/**
* Performs the calculation
*
* @return void
*/
public function calculate() {
try {
$method = $this->_operator;
$this->$method();
} catch (Exception $e){
}
$this->_displayResults();
}
/**
* Method to add fractions together
*
* @return bool
*/
public function add() {
$lcd = $this->getLcd();
$newNumerator = 0;
foreach($this->_fractions as $fraction) {
//Determine how much to multiply the numerator by
$multiplier = ($lcd / $fraction->denominator);
//Equalize the numerator with the new lcd and add it to the new numerator.
$newNumerator += ($fraction->numerator * $multiplier);
}
//Create the result
$result = new fraction($newNumerator, $lcd);
//Reduce the fraction
$result->reduce($result);
$this->result = $result;
return true;
}
/**
* Method to add fractions together
*
* @return bool
*/
public function subtract() {
//Ensure we have atleast two fractions to work with
if (count($this->_fractions) < 2) {
throw new Exception("You need two or more fractions in order to subtract them.");
}
//Get the lcd and fraction array
$lcd = $this->getLcd();
$fractions = $this->_fractions;
//Get the initial fraction to subtract from
$leftFraction = array_shift($fractions);
//Equalize the fraction using the lcd
$multiplier = ($lcd / $leftFraction->denominator);
$newNumerator = ($multiplier * $leftFraction->numerator);
//Subtract the remaining fractions
foreach($fractions as $fraction) {
//Determine how much to multiply the numerator by
$multiplier = ($lcd / $fraction->denominator);
//Equalize the numerator with the new lcd and subtract it from the new numerator.
$newNumerator -= ($multiplier * $fraction->numerator);
}
//Create the result
$result = new fraction($newNumerator, $lcd);
//Reduce the fraction
$result->reduce($result);
$this->result = $result;
return true;
}
/**
* Method to multiply fractions
*
* @return bool
*/
public function multiply() {
//Initiate the first fraction
$fractions = $this->_fractions;
$firstFraction = array_shift($fractions);
$numerator = $firstFraction->numerator;
$denominator = $firstFraction->denominator;
foreach($fractions as $fraction) {
$numerator = $fraction->numerator * $numerator;
$denominator = $fraction->denominator * $denominator;
}
$fraction = new fraction($numerator, $denominator);
$fraction->reduce($fraction);
$this->result = $fraction;
return true;
}
/**
* Method to divide fractions
*
* @return bool
*/
public function divide() {
//Initiate the left fraction
$fractions = $this->_fractions;
$firstFraction = array_shift($fractions);
$leftFraction = clone $firstFraction;
foreach($fractions as $fraction) {
//Flip the right fraction then multiply the numerator and denominator
$numerator = $fraction->denominator;
$denominator = $fraction->numerator;
$leftFraction->numerator = $leftFraction->numerator * $numerator;
$leftFraction->denominator = $leftFraction->denominator * $denominator;
}
$leftFraction->reduce();
$this->result = $leftFraction;
return true;
}
/**
* Method to get the least commong denominator of the fractions
*
* @return integer $lcm
*/
public function getLcd() {
//Ensure we have atleast two fractions to work with
if (count($this->_fractions) < 2) {
throw new Exception("You need two or more fractions in order to calculate the lease common multiple.");
}
/* Visit this link for an explination of the forumla used, secion 3.3 explains it
* http://en.wikipedia.org/wiki/Least_common_multiple#A_simple_algorithm
*/
//Get the first two fractions
$fractions = $this->_fractions;
$leftFraction = array_shift($fractions);
$rightFraction = array_shift($fractions);
//Need to find the least common multiple of the denominators
$lcm = $this->getLcm($leftFraction->denominator, $rightFraction->denominator);
foreach ($fractions as $fraction) {
$lcm = $this->getLcm($lcm, $fraction->denominator);
}
return $lcm;
}
/**
* Calculates the least common multiple of two numbers
*
* @param integer $left
* @param integer $right
* @return integer $lcm
*/
public function getLcm($left, $right) {
//Create a new fraction in order to get the gcd
$gcdFraction = new fraction(0, 0);
$gcd = $gcdFraction->getGcd((int) $left, (int) $right);
//Get the greated common multiple, lookup lcm on wiki for an explination of the formula
$lcm = ($left / $gcd) * $right;
return $lcm;
}
/**
* Displays the results of the calculation
*
* @return void
*/
protected function _displayResults() {
if (null !== $this->result) {
//Determine if the fraction is negative and add the prefix to the begginging.
$prefix = (($this->result->numerator < 0) || ($this->result->denominator < 0)) ? "-" : null;
echo '<h3 id="result">'
. $prefix
. abs($this->result->numerator)
. '/'
. abs($this->result->denominator)
. '</h3>';
}
}
}
?>