begin process at 2008 07 24 19:19:07
1 215 861 membres
416 nouveaux aujourd'hui
14 179 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

VERSION JAVA DE : ROMANUTILS, TOUT POUR CONVERTIR LES CHIFFRES ROMAINS VERS LES ENTIERS ET INVERSEMENT


Information sur la source

Catégorie :Maths et Algorithmes Classé sous : conversion, chiffres, nombres, romain, entier Niveau : Débutant Date de création : 30/10/2007 Date de mise à jour : 30/10/2007 16:57:09 Vu / téléchargé: 3 978 / 102

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

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

Description

Rapide portage de l'excellente source Delphi disponible à
http://www.delphifr.com/code.aspx?ID=34428

Source

  • public class RomanUtils
  • {
  • private final static String[] acceptedchar={"I","V","X","L","C","D","M"};
  • private final static String[] _RRUTR = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
  • private final static String[] _RRDTR = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
  • private final static String[] _RRCTR = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
  • private final static String[] _RRMTR = {"","M","MM","MMM","","","","","",""};
  • /**
  • * Validate if the string entered is a right Roman number
  • * @param val
  • * @return true if ok
  • */
  • static boolean IsRomanNumber(String val)
  • {
  • boolean actuval;
  • for (int i=0;i<val.length();i++)
  • {
  • actuval=false;
  • for (int j=0;j<acceptedchar.length;j++)
  • if (val.substring(i, i+1).equals(acceptedchar[j])) actuval=true;
  • if (actuval==false) return false;
  • }
  • return true;
  • }
  • /**
  • * Validate if the entered integer value is in the accepted limits
  • * @param val
  • * @return true if ok
  • */
  • static boolean IsRomanNumber(int val)
  • {
  • if ((val>0)&&(val<=39999)) return true;
  • return false;
  • }
  • /**
  • * return the value of a Roman figure
  • * @param vv = The figure
  • * @return The corresponding value
  • */
  • static int RomanCharToValue(char vv)
  • {
  • switch (vv)
  • {
  • case 'I': return 1;
  • case 'V': return 5;
  • case 'X': return 10;
  • case 'L': return 50;
  • case 'C': return 100;
  • case 'D': return 500;
  • case 'M': return 1000;
  • }
  • return 0;
  • }
  • /**
  • * Return the Roman number string corresponding to the value entered.
  • * @param value to convert
  • * @return String value
  • */
  • public static String IntToRoman(int i)
  • {
  • if (IsRomanNumber(i))
  • {
  • if (i>=0 && i<=9) return _RRUTR[i];
  • if (i>=10 && i<=99) return _RRDTR[i / 10]+ _RRUTR[i % 10];
  • if (i>=100 && i<=999) return _RRCTR[i / 100]+ _RRDTR[(i / 10) % 10]+ _RRUTR[i % 10];
  • if (i>=1000 && i<=3999) return _RRMTR[i / 1000]+_RRCTR[(i / 100) % 10]+_RRDTR[(i / 10) % 10]+_RRUTR[i % 10];
  • }//endif
  • return "";
  • }
  • /**
  • * Return the int value correspondif of a Roman String entered
  • * @param value in roman figures
  • * @return int value
  • */
  • public static int RomanToInt(String RN)
  • {
  • int oc,NewRV,OldRV=0,Result=0;
  • if (IsRomanNumber(RN))
  • {
  • for (oc=0;oc<RN.length();oc++)
  • {
  • NewRV = RomanCharToValue(RN.toCharArray()[oc]);
  • if (NewRV > OldRV)
  • {
  • Result+=NewRV-(OldRV<<1);
  • }else
  • {
  • Result+=NewRV;
  • }//endif
  • OldRV=NewRV;
  • }//endfor
  • }else
  • return -1;//endif
  • return Result;
  • }
  • }
public class RomanUtils 
{
	private final static String[] acceptedchar={"I","V","X","L","C","D","M"};
	
	private final static String[] _RRUTR = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
	private final static String[] _RRDTR = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
	private final static String[] _RRCTR = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
	private final static String[] _RRMTR = {"","M","MM","MMM","","","","","",""};

	
	/**
	 * Validate if the string entered is a right Roman number
	 * @param val
	 * @return true if ok
	 */
	static boolean IsRomanNumber(String val)
	{
		boolean actuval;
		
		for (int i=0;i<val.length();i++)
		{
			actuval=false;
			for (int j=0;j<acceptedchar.length;j++)
				if (val.substring(i, i+1).equals(acceptedchar[j])) actuval=true;
			if (actuval==false) return false;
		}
		return true;
	}
	
	/**
	 * Validate if the entered integer value is in the accepted limits
	 * @param val
	 * @return true if ok
	 */
	static boolean IsRomanNumber(int val)
	{
		if ((val>0)&&(val<=39999)) return true;
		return false;
	}
	
	/**
	 * return the value of a Roman figure
	 * @param vv = The figure
	 * @return The corresponding value
	 */
	static int RomanCharToValue(char vv)
	{
		switch (vv)
		{
			case 'I': return 1;
			case 'V': return 5;
			case 'X': return 10;
			case 'L': return 50;
			case 'C': return 100;
			case 'D': return 500;
			case 'M': return 1000;
		}
		return 0;
	}
	
	/**
	 * Return the Roman number string corresponding to the value entered.  
	 * @param value to convert
	 * @return String value
	 */
	public static String IntToRoman(int i)
	{
		if (IsRomanNumber(i))
		{
			
			if (i>=0 && i<=9) return _RRUTR[i];
			if (i>=10 && i<=99) return _RRDTR[i / 10]+ _RRUTR[i % 10];
			if (i>=100 && i<=999) return _RRCTR[i / 100]+ _RRDTR[(i / 10) % 10]+ _RRUTR[i % 10];
			if (i>=1000 && i<=3999) return _RRMTR[i / 1000]+_RRCTR[(i / 100) % 10]+_RRDTR[(i / 10) % 10]+_RRUTR[i % 10];

		}//endif
		
		return "";
	}
	
	/**
	 * Return the int value correspondif of a Roman String entered
	 * @param value in roman figures
	 * @return int value
	 */
	public static int RomanToInt(String RN)
	{
		int oc,NewRV,OldRV=0,Result=0;
		
		if (IsRomanNumber(RN))
		{
			for (oc=0;oc<RN.length();oc++)
			{
				NewRV = RomanCharToValue(RN.toCharArray()[oc]);
				if (NewRV > OldRV)
				{
					Result+=NewRV-(OldRV<<1);
				}else
				{
					Result+=NewRV;
				}//endif
				OldRV=NewRV;
			}//endfor
			
		}else
			return -1;//endif

		return Result;
	}
}

Conclusion

Je ne me suis pas amusé à faire des tests de performance...
De toutes façons, je doute qu'un jour ce soit utilisé dans des flux tendus.
Et si c'est le cas, le boulot est prémâché...
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
  •   RomanNumbers
    •   be
      •   dje
    • .classpathTélécharger ce fichier [Réservé aux membres club]226 octets
    • .projectTélécharger ce fichier [Réservé aux membres club]388 octets

Télécharger le zip

30 octobre 2007 16:57:09 :
Problèmes lors de l'enregistrement de la source... Donc, l'URL n'était pas direk!
  • signaler à un administrateur
    Commentaire de f0xi le 19/01/2008 10:43:57 administrateur CS 10/10

    Oh! translation 1:1 de ma source en Java (a 98% je peux pas verifier l'exactitude du code java)!
    Mais bon boulot, traduire d'un langage a l'autre demande pas mal de competence l'exercice semble reussi ici, felicitation!

Ajouter un commentaire

Pub



Appels d'offres

Creation portail video
Budget : 3 000€
Site de e-commerce
Budget : 5 000€
Demande de devis pour ...
Budget : 7 000€

CalendriCode

Juillet 2008
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Boutique

Boutique de goodies CodeS-SourceS