Trad de chiffres romains en chiffres arabes

sacocheannick Messages postés 1 Date d'inscription dimanche 20 juillet 2014 Statut Membre Dernière intervention 20 juillet 2014 - 20 juil. 2014 à 11:18
mpmp93 Messages postés 6652 Date d'inscription mercredi 13 avril 2011 Statut Membre Dernière intervention 28 septembre 2015 - 30 juil. 2014 à 22:17
en lisant un missel en page de garde il y a ceci MCMCI je suppose que cet une date merci d'éclairer ma lanterne
A voir également:

2 réponses

jordane45 Messages postés 37718 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 22 septembre 2023 342
20 juil. 2014 à 12:33
BONJOUR (si si ... ça se dit toujours !!! )

Quel est le rapport avec le langage PHP.... forum dans lequel tu viens de poster ???

Ne crois tu pas que tu pourrais trouver cette réponse TOUT SEUL en t'aidant de ton moteur de recherches ???

Mais bon... si tu as du mal à trouver...; regarde ici :
http://www.chambily.com/recursivite/chap_II_2.htm



0
mpmp93 Messages postés 6652 Date d'inscription mercredi 13 avril 2011 Statut Membre Dernière intervention 28 septembre 2015 4
30 juil. 2014 à 22:11
AMENe le code:

$romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$roman = 'MMMCMXCIX';
$result = 0;

foreach ($romans as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
echo $result;



0
mpmp93 Messages postés 6652 Date d'inscription mercredi 13 avril 2011 Statut Membre Dernière intervention 28 septembre 2015 4
30 juil. 2014 à 22:17
PS, le même programme en FORTH:

\ prints a single roman digit from array
: roman_digit_print \ ( n -- )
case
1 of ." i" endof
4 of ." iv" endof
5 of ." v" endof
9 of ." ix" endof
10 of ." x" endof
40 of ." xl" endof
50 of ." l" endof
90 of ." xc" endof
100 of ." c" endof
400 of ." cd" endof
500 of ." d" endof
900 of ." cm" endof
1000 of ." m" endof
endcase ;

\ iterates on a single digit until all are printed
: do_single_digit \ (number digit -- remainder)
begin 2dup >= while
dup roman_digit_print
2dup \ number digit number digit
- \ number digit remainder
swap \ number remainder digit
rot \ remainder digit number
drop \ remainder digit
repeat
drop ;

\ prints an integer on the stack as roman numeral
: roman_numeral_print ( number -- )
1000 do_single_digit
900 do_single_digit
500 do_single_digit
400 do_single_digit
100 do_single_digit
90 do_single_digit
50 do_single_digit
40 do_single_digit
10 do_single_digit
9 do_single_digit
5 do_single_digit
4 do_single_digit
1 do_single_digit
drop ;

Clin d'oeil à mon avatar qui a 4TH sur son body (FORTH => le langage des trucages du tout premier Star Wars....)
0
Rejoignez-nous