mortiprogramador
Código PHP:
Ver original<?php
class Roman
{
var $cant;
var $roman_number;
var $array_number;
var $expression;
var $roman_ant_number = array( '1'=>'I',
'5'=>'V',
'10'=>'X',
'50'=>'L',
'100'=>'C',
'500'=>'D',
'1000'=>'M',
'5000'=>'<span style="text-decoration: overline">V</span>',
'10000'=>'<span style="text-decoration: overline">X</span>',
'50000'=>'<span style="text-decoration: overline">L</span>',
'100000'=>'<span style="text-decoration: overline">C</span>',
'500000'=>'<span style="text-decoration: overline">D</span>',
'1000000'=>'<span style="text-decoration: overline">M</span>'
);
function _construct()
{
setNum();
}
//Get a rand number
function setNum()
{
$this->num = rand(0,10000); return $this->num;
}
function addRoman($times,$pos)
{
$add='';
for($i = 1; $i<=$times; $i++)
{
$add.=$pos;
}
return $add;
}
function romanNumber($num)
{
$this->expression = '//';
$this->array_number = preg_split($this->expression,trim($num),-1,PREG_SPLIT_NO_EMPTY
); switch ($num)
{
case 0:
break;
case $num > 0:
//total of chars
$this->cant = count($this->array_number) - 1; //cant to subtraction
$min = $this->array_number[0].str_repeat(0,$this->cant); $num -= $min;
if( $this->cant > 0 )
{
if( $this->array_number[0] > 3 )
{
$pos *= $this->array_number[0];
}
$pos4 = $this->array_number[0].'4';
$pos9 = $this->array_number[0].'9';
}
else
{
$pos4 ='4';
$pos9 = '9';
}
{
$this->roman_number .= $this->roman_ant_number[$pos];
}
{
{
$this->roman_number .= $this->roman_ant_number[$pos-(('1'.str_repeat(0,$this->cant))*3)].$this->roman_ant_number[$pos+10]; }
{
$this->roman_number .= $this->roman_ant_number[$pos];
}
{
$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))]); }
{
$this->roman_number .= $this->roman_ant_number['1'.str_repeat(0,$this->cant)].$this->roman_ant_number[$next]; }
}
else
{
switch ($min)
{
case $min < $pos4:
$this->roman_number .= str_repeat($this->roman_ant_number[$pos],$this->array_number[0]); break;
case $pos4:
$this->roman_number .= $this->roman_ant_number[$pos].$this->roman_ant_number[$pos4+1];
break;
case $min > $pos4 && $min < $pos9:
$this->roman_number .= $this->roman_ant_number[$pos4+1];
if( $min > $pos4+1 )
{
$min -=$pos4+1;
$this->roman_number .= str_repeat($this->roman_ant_number[$pos],$min); }
break;
case $pos9:
$this->roman_number .= $this->roman_ant_number[$pos].$this->roman_ant_number[$next];
break;
}
}
//add the roman value
$this->romanNumber($num);
break;
}
return $this->roman_number;
}
}
?>