
Convertir números romanos a arábigos con PHP
Publicado por Pablo (17 intervenciones) el 10/01/2023 23:44:51
Hola he estado buscando en google varios días a esta solución y al final encontré esta class que me arroja un error y no se porque es: PHP Parse error: syntax error, unexpected 'abstract' (T_ABSTRACT) in /home/kwxnrefv/public_html/Numeros.php on line 14
Numeros.php
Prueba.php
¿Alguien me puede ayudar?
Numeros.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* Clase NumerosRomanos.
* Realiza conversiones entre numeros romanos y decimales.
* Compatible con error_reporting(E_ALL | E_STRICT).
*
* @package matematicas creado para forosdelweb.
* @copyright 2010 - ObjetivoPHP
* @license Gratuito (Free) http://www.opensource.org/licenses/gpl-license.html
* @author Marcelo Castro (ObjetivoPHP)
* @link objetivophp@******.***.**
* @version 0.2.1 (16/08/2010 - 18/08/2010)
*/
abstract class NumerosRomanos
{
/**
* Contiene las equivalencias de numeros romanos para unidades, decimales,
* centenas y miles.
* @var array
*/
private static $_romanos = array(0 => array(0 => '',
1 => 'I',
2 => 'II',
3 => 'III',
4 => 'IV',
5 => 'V',
6 => 'VI',
7 => 'VII',
8 => 'VIII',
9 => 'IX'),
1 => array(0 => '',
1 => 'X',
2 => 'XX',
3 => 'XXX',
4 => 'XL',
5 => 'L',
6 => 'LX',
7 => 'LXX',
8 => 'LXXX',
9 => 'XC'),
2 => array(0 => '',
1 => 'C',
2 => 'CC',
3 => 'CCC',
4 => 'CD',
5 => 'D',
6 => 'DC',
7 => 'DCC',
8 => 'DCCC',
9 => 'CM'),
3 => array(0 => '',
1 => 'M',
2 => 'MM',
3 => 'MMM'));
/**
* Contiene los divisores para identificar por donde comenzar la conversion.
* @var array
*/
private static $_divisores = array(1, 10, 100, 1000);
/**
* Contiene las equivalencias entre romano y decimal.
* @var array
*/
private static $_decimal = array('.' => 0,
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000);
/**
* Convierte un numero expresado en decimal a notacion Romana.
* @param integer $numero Numero que se desea convertir en romano.
* desde 0 a 3999.-
* @return string
*/
public static function decimalRomano($numero)
{
$retorno = '';
$max = (int)log10($numero);
for ($div = $max; $div > -1; $div--) {
$aux = (int)($numero/self::$_divisores[$div]);
$retorno.= self::$_romanos[$div][$aux];
$numero -=self::$_divisores[$div]*$aux;
}
return $retorno;
}
/**
* Convierte un numero expresado en romanos a notacion decimal.
* @param string $romano Numero de tipo romano Ej.DCCCLXXXVIII.
* @return integer
*/
public static function romanoDecimal($romano)
{
$decimal = 0;
$letras = strlen($romano);
$romano .= '.';
for ($r = 0; $r < $letras; $r++) {
$valorI = self::$_decimal[$romano[$r]];
$valorII = self::$_decimal[$romano[$r+1]];
$decimal += ($valorI < $valorII)? - $valorI : $valorI;
}
return $decimal;
}
}
?>
Prueba.php
1
2
3
4
5
6
7
<?php
require_once('Numeros.php');
echo NumerosRomanos::romanoDecimal('MXXX');
?>
¿Alguien me puede ayudar?
Valora esta pregunta


0