Ver Mensaje Individual
  #10 (permalink)  
Antiguo 20/08/2010, 20:53
Avatar de mortiprogramador
mortiprogramador
Colaborador
 
Fecha de Ingreso: septiembre-2009
Ubicación: mortuoria
Mensajes: 3.805
Antigüedad: 14 años, 7 meses
Puntos: 214
Respuesta: Códigos - Reto Romanos

portalmana

Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * Clase NumerosRomanos.
  4.  * Convierte un numero en base decimal a Romano.
  5.  *
  6.  * @package     matematicas creado para forosdelweb.
  7.  * @copyright   2010 - ObjetivoPHP
  8.  * @license     Gratuito (Free) http://www.opensource.org/licenses/gpl-license.html
  9.  * @author      Marcelo Castro (ObjetivoPHP)
  10.  * @link        [email protected]
  11.  * @version     0.1.0 (16/08/2010 - 16/08/2010)
  12.  */
  13. abstract class NumerosRomanos
  14. {
  15.     /**
  16.      * Contiene las equivalencias de numeros romanos para unidades, decimales,
  17.      * centenas y miles.
  18.      * @var array
  19.      */
  20.     private static  $_romanos =     array(0   =>    array(1 => 'I',
  21.                                                           2 => 'II',
  22.                                                           3 => 'III',
  23.                                                           4 => 'IV',
  24.                                                           5 => 'V',
  25.                                                           6 => 'VI',
  26.                                                           7 => 'VII',
  27.                                                           8 => 'VIII',
  28.                                                           9 => 'IX'),
  29.                                           1    =>   array(1 => 'X',
  30.                                                           2 => 'XX',
  31.                                                           3 => 'XXX',
  32.                                                           4 => 'XL',
  33.                                                           5 => 'L',
  34.                                                           6 => 'LX',
  35.                                                           7 => 'LXX',
  36.                                                           8 => 'LXXX',
  37.                                                           9 => 'XC'),
  38.                                           2   =>    array(1 => 'C',
  39.                                                           2 => 'CC',
  40.                                                           3 => 'CCC',
  41.                                                           4 => 'CD',
  42.                                                           5 => 'D',
  43.                                                           6 => 'DC',
  44.                                                           7 => 'DCC',
  45.                                                           8 => 'DCCC',
  46.                                                           9 => 'CM'),
  47.                                           3 =>      array(1 => 'M',
  48.                                                           2 => 'MM',
  49.                                                           3 => 'MMM'));
  50.  
  51.     /**
  52.      * Contiene los divisores para identificar por donde comenzar la conversion.
  53.      * @var array
  54.      */
  55.     private static $_divisores   = array(1, 10, 100, 1000);
  56.  
  57.     /**
  58.      * Convierte un numero expresado en decimal a notacion Romana.
  59.      * @param   integer $numero Numero que se desea convertir en romano.
  60.      *                  desde 0 a 3999.-
  61.      * @return  string
  62.      */
  63.     public static function romanNumber($numero)
  64.     {
  65.         $retorno = '';
  66.         $max = (int)log10($numero);
  67.         for ($div = $max; $div > -1; $div--) {
  68.             $aux     =  (int)($numero/self::$_divisores[$div]);
  69.             $retorno.= self::$_romanos[$div][$aux];
  70.             $numero -=self::$_divisores[$div]*$aux;
  71.         }
  72.         return $retorno;
  73.     }
  74. }
__________________
"Si consigues ser algo más que un hombre, si te entregas a un ideal, si nadie puede detenerte, te conviertes en algo muy diferente."
Visita piggypon.com