begin process at 2012 05 27 22:06:27
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > CHIFFRE ROMAIN (ENTRE 1 ET 9999 COMPRIS)

CHIFFRE ROMAIN (ENTRE 1 ET 9999 COMPRIS)


 Information sur la source

Note :
8 / 10 - par 1 personne
8,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Maths & Algorithmes Niveau :Débutant Date de création :25/05/2004 Date de mise à jour :28/05/2004 14:08:41 Vu :49 782

Auteur : Dri

Ecrire un message privé
Commentaire sur cette source (3)
Ajouter un commentaire et/ou une note

 Description

Le script se limite à une fonction qui convertit un nombre entier décimal en nombre composé exclusivement de chiffres romains... Tout est dans le titre...

Source

  • <?php
  • function entier_romain($entier)
  • {
  • $entier = intval($entier);
  • if($entier<1 || $entier>9999)return "";
  • $rom[3][4];
  • $rom[3] = array("M", "", "", "");
  • $rom[2] = array("C", "CD", "D", "CM");
  • $rom[1] = array("X", "XL", "L", "XC");
  • $rom[0] = array("I", "IV", "V", "IX");
  • $i = 1;
  • $n = 0;
  • $romain = "";
  • for($n=0; $n<4; $n++)
  • {
  • $chiffre = intval(($entier % ($i*10))/$i);
  • if($n<3)
  • {
  • if($chiffre<4)
  • {
  • while($chiffre>0)
  • {
  • $romain = $rom[$n][0].$romain;
  • $chiffre--;
  • }
  • }
  • elseif($chiffre==4)
  • $romain = $rom[$n][1].$romain;
  • elseif($chiffre<9)
  • {
  • while($chiffre>5)
  • {
  • $romain = $rom[$n][0].$romain;
  • $chiffre--;
  • }
  • $romain = $rom[$n][2].$romain;
  • }
  • else
  • $romain = $rom[$n][3].$romain;
  • }
  • else
  • {
  • while($chiffre>0)
  • {
  • $romain = $rom[$n][0].$romain;
  • $chiffre--;
  • }
  • }
  • $i = $i*10;
  • }
  • return $romain;
  • }
  • function romain_entier($romain)
  • {
  • if(!est_romain($romain))return 0;
  • $rom[2][7];
  • $rom[0] = array("I", "V", "X", "L", "C", "D", "M");
  • $rom[1] = array( 1, 5, 10, 50, 100, 500,1000);
  • $nombre = 0;
  • $precedent = 1000;
  • for($i=0; $i<strlen($romain); $i++)
  • {
  • for($j=0; $j<7; $j++)
  • if($romain[$i]==$rom[0][$j])break;
  • $valeur = $rom[1][$j];
  • if($valeur>$precedent)$nombre = $nombre - 2*($precedent);
  • $nombre = $nombre + $valeur;
  • $precedent = $valeur;
  • }
  • return $nombre;
  • }
  • function est_romain($romain)
  • {
  • $rom = array("I", "V", "X", "L", "C", "D", "M");
  • $precedent = 6;
  • for($i=0; $i<strlen($romain); $i++)
  • {
  • for($j=0; $j<7; $j++)
  • if($romain[$i]==$rom[$j])break;
  • if($j==7) return 0;
  • if($j>$precedent)
  • if($j&1==1)
  • if($j-$precedent!=1)return 0;
  • else
  • if($j-$precedent!=1 && $j-$precedent!=2)return 0;
  • $precedent = $j;
  • }
  • return 1;
  • }
  • $x = 490;
  • echo entier_romain($x)."<br>";
  • echo romain_entier(entier_romain($x))."<br>";
  • echo "XD : ".est_romain("XD")."<br>";
  • echo "CDCX : ".est_romain("CDXC")."<br>";
  • ?>
<?php

function entier_romain($entier)
{
    $entier = intval($entier);
    if($entier<1 || $entier>9999)return "";
    
    $rom[3][4];
    $rom[3] = array("M", "", "", "");
    $rom[2] = array("C", "CD", "D", "CM");
    $rom[1] = array("X", "XL", "L", "XC");
    $rom[0] = array("I", "IV", "V", "IX");

    $i = 1;
    $n = 0;
    $romain = "";
    
    for($n=0; $n<4; $n++)
    {
    $chiffre = intval(($entier % ($i*10))/$i);
    if($n<3)
    {
        if($chiffre<4)
        {
        while($chiffre>0)
        {
            $romain = $rom[$n][0].$romain;
            $chiffre--;
        }
        }
        elseif($chiffre==4)
        $romain = $rom[$n][1].$romain;
        elseif($chiffre<9)
        {
        while($chiffre>5)
        {
            $romain = $rom[$n][0].$romain;
            $chiffre--;
        }
        $romain = $rom[$n][2].$romain;
        }
        else
        $romain = $rom[$n][3].$romain;
    }
    else
    {
        while($chiffre>0)
        {
        $romain = $rom[$n][0].$romain;
        $chiffre--;
        }
    }
    $i = $i*10;
    }
    return $romain;
}

function romain_entier($romain)
{

  if(!est_romain($romain))return 0;

  $rom[2][7];
  $rom[0] = array("I", "V", "X", "L", "C", "D", "M");
  $rom[1] = array(  1,   5,  10,  50, 100, 500,1000);
  
  $nombre = 0;
  $precedent = 1000;
  
  for($i=0; $i<strlen($romain); $i++)
  {
    for($j=0; $j<7; $j++)
      if($romain[$i]==$rom[0][$j])break;
	$valeur = $rom[1][$j];
	if($valeur>$precedent)$nombre = $nombre - 2*($precedent);
	$nombre = $nombre + $valeur;
	$precedent = $valeur;
  }
  return $nombre;
}

function est_romain($romain)
{
  $rom = array("I", "V", "X", "L", "C", "D", "M");
  
  $precedent = 6;
  
  for($i=0; $i<strlen($romain); $i++)
  {
    for($j=0; $j<7; $j++)
      if($romain[$i]==$rom[$j])break;
	if($j==7) return 0;
	if($j>$precedent)
	  if($j&1==1)
	    if($j-$precedent!=1)return 0;
	  else
	    if($j-$precedent!=1 && $j-$precedent!=2)return 0;
	$precedent = $j;
  }
  return 1;
}

$x = 490;

echo entier_romain($x)."<br>";
echo romain_entier(entier_romain($x))."<br>";

echo "XD : ".est_romain("XD")."<br>";
echo "CDCX : ".est_romain("CDXC")."<br>";

?>



 Sources de la même categorie

EXEMPLE D'APPLICATION DE L'ALGORITHME DE DIJKSTRA EN PHP par philtr8
CLEF POUR EAN 13 ET 14 par RaftY
FONCTION DE CALCUL DU NOMBRE DE DUEL UNIQUE POUR UN NOMBRE N... par mtrix000
Source avec Zip Source avec une capture TRIANGLE DE PASCAL ET SON ÉQUATION par vendeeHdLR89
Source avec Zip CONVERTISSEUR LAMBERT2 ÉTENDU EN COORDONNÉE GÉOGRAPHIQUE (LO... par varfendell

Commentaires et avis

Commentaire de willinfeo le 26/05/2004 11:47:01

C'est quoi un nombre entier décimal. C'est nouveau ?
Remarque, ca fait longtemps que je suis pas allé en cours de math.

Commentaire de Dri le 27/05/2004 14:36:10

Par décimal j'entend base 10 ^^
Parce que les types en php c'est en gros tous des chaines de caracteres... Donc les variables numériques... Qu'elles soient décimale binaire hexa etc... Ca reste toujours du texte... Donc je précise base 10... C'est vrai que c'est un peu con en se relisant...

Dri ^^

Commentaire de Dri le 28/05/2004 14:12:06

J'ai ajouté deux autres fonctions, une pour savoir si une chaine est un nombre romain et une autre qui prend en parametre un nombre romain et retourne un entier...

Dri

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,452 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales