Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
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
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é...
Historique
- 30 octobre 2007 16:57:09 :
- Problèmes lors de l'enregistrement de la source... Donc, l'URL n'était pas direk!
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 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 | | | |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|