PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Michele Andreoli   PHP Coins Change   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example of use
Class: PHP Coins Change
Solve the coin change problem for a given amount
Author: By
Last change: update comment
Date: 9 years ago
Size: 2,306 bytes
 

Contents

Class file image Download
<?php
   
/**
     * @author Michele Andreoli <michi.andreoli[at]gmail.com>
     * @name index.php
     * @version 0.1 updated 30-08-2014
     * @license http://opensource.org/licenses/gpl-license-php GNU Public License
     * @package CoinsChanger
     */

   
require_once 'CoinsManager.class.php';
      
   
$amount = 30;
   
$coins = [50, 10, 5, 2, 1, 0.5];
   
$limits = [1, 2, 1, 5, 3, 10];
   
   
//Coin change problem with FINITE coins supply
   
$changerMngr = new CoinsManager($coins, $limits);
   
   
//Get an array with coins
   
$exchange = $changerMngr->getChange($amount);
   
//Get an array with the amount for each denominations
   
$groups = $changerMngr->groupBy($exchange);
   
   
//Coin change problem with INFINITE coins supply
   
$changerMngrInfinite = new CoinsManager($coins);
   
   
//Get an array with coins
   
$exchangeInfinite = $changerMngrInfinite->getChange($amount);
   
//Get an array with the amount for each denominations
   
$groupsInfinite = $changerMngrInfinite->groupBy($exchangeInfinite);
?>
<html>
    <head>
        <title>Coins Changer Machine</title>
    </head>
    <body>
        <h2>Coins changer with <b>finite</b> coins supply</h2>
        <p>Change <b><?= $amount ?>&euro;</b> with:</p>
        <?php
           
if ($exchange < 0 || $groups == FALSE) {
                echo
"<p><b>No solutions</b></p>";
            }
            else {
       
?>
<ul>
            <?php
               
foreach ($groups as $key => $group) {
                    echo
"<li><b>" . $key . "&euro;</b> => " . $group . "</li>";
                }
           
?>
</ul>
            <?php } ?>
<h2>Coins changer with <b>infinite</b> coins supply</h2>
        <p>Change <b><?= $amount ?>&euro;</b> with:</p>
        <?php
           
if ($exchangeInfinite < 0 || $groupsInfinite == FALSE) {
                echo
"<p><b>No solutions</b></p>";
            }
            else {
       
?>
<ul>
            <?php
               
foreach ($groupsInfinite as $key => $group) {
                    echo
"<li><b>" . $key . "&euro;</b> => " . $group . "</li>";
                }
           
?>
</ul>
            <?php } ?>
</body>
</html>