Ver Mensaje Individual
  #15 (permalink)  
Antiguo 20/08/2010, 21:06
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

mortiprogramador

Código PHP:
Ver original
  1. <?php
  2. class Roman
  3. {
  4.     var $cant;
  5.     var $roman_number;
  6.     var $array_number;
  7.     var $expression;
  8.     var $roman_ant_number = array(
  9.                                 '1'=>'I',
  10.                                 '5'=>'V',
  11.                                 '10'=>'X',
  12.                                 '50'=>'L',
  13.                                 '100'=>'C',
  14.                                 '500'=>'D',
  15.                                 '1000'=>'M',
  16.                                 '5000'=>'<span style="text-decoration: overline">V</span>',
  17.                                 '10000'=>'<span style="text-decoration: overline">X</span>',
  18.                                 '50000'=>'<span style="text-decoration: overline">L</span>',
  19.                                 '100000'=>'<span style="text-decoration: overline">C</span>',
  20.                                 '500000'=>'<span style="text-decoration: overline">D</span>',
  21.                                 '1000000'=>'<span style="text-decoration: overline">M</span>'
  22.                             );                    
  23.    
  24.     function _construct()
  25.     {
  26.         setNum();
  27.     }
  28.    
  29.     //Get a rand number
  30.     function setNum()
  31.     {
  32.         $this->num = rand(0,10000);
  33.         return $this->num;
  34.     }
  35.    
  36.     function addRoman($times,$pos)
  37.     {
  38.         $add='';
  39.         for($i = 1; $i<=$times; $i++)
  40.         {
  41.             $add.=$pos;
  42.         }
  43.         return $add;
  44.     }
  45.    
  46.     function romanNumber($num)
  47.     {
  48.         $this->expression = '//';
  49.         $this->array_number = preg_split($this->expression,trim($num),-1,PREG_SPLIT_NO_EMPTY);
  50.         switch ($num)
  51.         {
  52.             case 0:
  53.             break;
  54.             case $num > 0:
  55.                 //total of chars
  56.                 $this->cant = count($this->array_number) - 1;
  57.                 //cant to subtraction
  58.                 $min = $this->array_number[0].str_repeat(0,$this->cant);
  59.                 $num -= $min;
  60.                 $pos = '1'.str_repeat(0,$this->cant);
  61.                 if( $this->cant > 0 )
  62.                 {
  63.                     if( $this->array_number[0] > 3 )
  64.                     {
  65.                         $pos *= $this->array_number[0];
  66.                     }
  67.                    
  68.                     $pos4 = $this->array_number[0].'4';
  69.                     $pos9 = $this->array_number[0].'9';
  70.                 }
  71.                 else
  72.                 {
  73.                     $pos4 ='4';
  74.                     $pos9 = '9';
  75.                 }    
  76.                 $next = '1'.str_repeat(0,$this->cant + 1);
  77.                 if( strlen($min) > 1 && $min == '1'.str_repeat(0,$this->cant))
  78.                 {
  79.                     $this->roman_number .= $this->roman_ant_number[$pos];
  80.                 }
  81.                 if( strlen($min) > 1 && $min >= '4'.str_repeat(0,$this->cant))
  82.                 {
  83.                     if( $pos == '4'.str_repeat(0,$this->cant ))
  84.                     {
  85.                         $this->roman_number .= $this->roman_ant_number[$pos-(('1'.str_repeat(0,$this->cant))*3)].$this->roman_ant_number[$pos+10];
  86.                     }
  87.                     if( $pos == '5'.str_repeat(0,$this->cant ))
  88.                     {
  89.                         $this->roman_number .= $this->roman_ant_number[$pos];
  90.                     }
  91.                     if( $pos > '5'.str_repeat(0,$this->cant) && $pos < '9'.str_repeat(0,$this->cant))
  92.                     {
  93.                         $times = substr($pos-('5'.str_repeat(0,$this->cant)),0,1);
  94.                         $this->roman_number .= $this->roman_ant_number['5'.str_repeat(0,$this->cant)].$this->addRoman($times, $this->roman_ant_number[('1'.str_repeat(0,$this->cant))]);
  95.                     }
  96.                     if( $pos == '9'.str_repeat(0,$this->cant))
  97.                     {
  98.                         $this->roman_number .= $this->roman_ant_number['1'.str_repeat(0,$this->cant)].$this->roman_ant_number[$next];
  99.                     }                    
  100.                 }
  101.                 else
  102.                 {
  103.                     switch ($min)
  104.                     {
  105.                         case $min < $pos4:                    
  106.                             $this->roman_number .= str_repeat($this->roman_ant_number[$pos],$this->array_number[0]);
  107.                         break;
  108.                         case $pos4:
  109.                             $this->roman_number .= $this->roman_ant_number[$pos].$this->roman_ant_number[$pos4+1];
  110.                         break;
  111.                         case $min > $pos4 && $min < $pos9:
  112.                             $this->roman_number .= $this->roman_ant_number[$pos4+1];
  113.                             if( $min > $pos4+1 )
  114.                             {
  115.                                 $min -=$pos4+1;
  116.                                 $this->roman_number .= str_repeat($this->roman_ant_number[$pos],$min);
  117.                             }
  118.                         break;
  119.                         case $pos9:
  120.                             $this->roman_number .= $this->roman_ant_number[$pos].$this->roman_ant_number[$next];
  121.                         break;
  122.                     }
  123.                 }
  124.                 //add the roman value
  125.                 $this->romanNumber($num);
  126.             break;
  127.  
  128.         }
  129.         return $this->roman_number;
  130.     }
  131. }
  132.  
  133. ?>
__________________
"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