home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Roman.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  4.9 KB  |  170 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19.  
  20. require_once("PEAR.php");
  21.  
  22. // {{{ Numbers_Roman
  23.  
  24. /**
  25.  * The Numbers_Roman class provides utilities to convert roman numerals to
  26.  * arabic numbers and convert arabic numbers to roman numerals
  27.  *
  28.  * <?php
  29.  * require_once 'Numbers/Roman.php';
  30.  * 
  31.  * $arabic = Numbers_Roman::toNumber('vi');
  32.  * echo "The arabic equivalent of vi is $arabic\n";
  33.  * $roman = Numbers_Roman::toNumeral($arabic);
  34.  * echo "The reverse conversion resulted in $roman\n";
  35.  * ?>
  36.  *
  37.  * @access public
  38.  * @author Sterling Hughes <sterling@php.net>
  39.  * @since  PHP 4.0.5
  40.  */
  41. class Numbers_Roman extends PEAR
  42. {
  43.     // {{{ toNumber()
  44.  
  45.     /**
  46.      * Converts a roman numeral to a number
  47.      *
  48.      * @param  string  $roman The roman numeral to convert
  49.      *
  50.      * @return integer $num   The number corresponding to the
  51.      *                        given roman numeral
  52.      *
  53.      * @access public
  54.      * @author Sterling Hughes <sterling@php.net>
  55.      * @since  PHP 4.0.5
  56.      */
  57.     function toNumber($roman)
  58.     {
  59.         $conv = array(
  60.             array("letter" => 'I', "number" => 1),
  61.             array("letter" => 'V', "number" => 5),
  62.             array("letter" => 'X', "number" => 10),
  63.             array("letter" => 'L', "number" => 50),
  64.             array("letter" => 'C', "number" => 100),
  65.             array("letter" => 'D', "number" => 500),
  66.             array("letter" => 'M', "number" => 1000),
  67.             array("letter" => 0,   "number" => 0)
  68.         );
  69.         $arabic = 0;
  70.         $state  = 0;
  71.         $sidx   = 0;
  72.         $len    = strlen($roman) - 1;
  73.  
  74.         while ($len >= 0) {
  75.             $i = 0;
  76.             $sidx = $len;
  77.  
  78.             while ($conv[$i]['number'] > 0) {
  79.                 if (strtoupper($roman[$sidx]) == $conv[$i]['letter']) {
  80.                     if ($state > $conv[$i]['number']) {
  81.                         $arabic -= $conv[$i]['number'];
  82.                     } else {
  83.                         $arabic += $conv[$i]['number'];
  84.                         $state   = $conv[$i]['number'];
  85.                     }
  86.                 }
  87.                 $i++;
  88.             }
  89.  
  90.             $len--;
  91.         }
  92.  
  93.         return($arabic);
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ toRoman()
  98.     /**
  99.      * A backwards compatibility alias for toNumeral()
  100.      *
  101.      * @access private
  102.      */
  103.     function toRoman($num)
  104.     {
  105.         return $this->toNumeral($num);
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ toNumeral()
  110.  
  111.     /**
  112.      * Converts a number to its roman numeral representation
  113.      *
  114.      * @param  integer $num   An integer between 0 and 3999 inclusive
  115.      *                        that should be converted to a roman numeral
  116.      *
  117.      * @return string  $roman The corresponding roman numeral
  118.      *
  119.      * @access public
  120.      * @author Sterling Hughes <sterling@php.net>
  121.      * @since  PHP 4.0.5
  122.      */
  123.     function toNumeral($num) {
  124.         $conv = array(10 => array('X', 'C', 'M'),
  125.                       5  => array('V', 'L', 'D'),
  126.                       1  => array('I', 'X', 'C'));
  127.         $roman = '';
  128.  
  129.         if ($num < 0) {
  130.             return '';
  131.         }
  132.  
  133.         $num = (int) $num;
  134.  
  135.         $digit  = (int) ($num / 1000);
  136.         $num   -= $digit * 1000;
  137.         while ($digit > 0) {
  138.             $roman .= 'M';
  139.             $digit--;
  140.         }
  141.  
  142.         for ($i = 2; $i >= 0; $i--) {
  143.             $power = pow(10, $i);
  144.             $digit = (int) ($num / $power);
  145.             $num -= $digit * $power;
  146.  
  147.             if (($digit == 9) || ($digit == 4)) {
  148.                 $roman .= $conv[1][$i] . $conv[$digit+1][$i];
  149.             } else {
  150.                 if ($digit >= 5) {
  151.                     $roman .= $conv[5][$i];
  152.                     $digit -= 5;
  153.                 }
  154.  
  155.                 while ($digit > 0) {
  156.                     $roman .= $conv[1][$i];
  157.                     $digit--;
  158.                 }
  159.             }
  160.         }
  161.  
  162.         return $roman;
  163.     }
  164.  
  165.     // }}}
  166. }
  167.  
  168. // }}}
  169. ?>
  170.