-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminimum_coins.php
48 lines (41 loc) · 1.05 KB
/
minimum_coins.php
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
<?
Class cashRegister {
public $change;
public $remainder;
}
function testCurrency($money, $currency, $currency_value, $change) {
// echo "TESTING $money with $currency \n";
if ($money/$currency_value >= 1) {
$ct = floor($money/$currency_value);
$change += $ct;
$money = round($money - $ct*$currency_value,2);
}
$cash_reg = new cashRegister();
$cash_reg->remainder = $money;
$cash_reg->change = $change;
return $cash_reg;
}
function change($money) {
$change = 0;
$remaining = $money;
if ($money < 0) {
return "ERROR";
} elseif ($money == 0) {
return "ZERO";
} else {
$cash_reg = testCurrency($money, 'FIVE', 5, 0);
$cash_reg = testCurrency($cash_reg->remainder, 'THREE', 3, $cash_reg->change);
$cash_reg = testCurrency($cash_reg->remainder, 'ONE', 1, $cash_reg->change);
}
$change = $cash_reg->change;
return $change;
}
$fh = fopen($argv[1], "r");
while (!feof($fh)) {
$test = trim(fgets($fh));
if ($test != "") {
echo change($test) . "\n";
}
}
fclose($fh);
?>