Précédent   Forum des professionnels en informatique > Dotnet > Général Dotnet > Contribuez
Contribuez Proposez vos articles, cours, tutoriels, faq, sources pour .NET
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
Vieux 07/11/2008, 17h00   #1
Responsable .NET

 
Avatar de Philippe Vialatte
 
Homme Philippe Vialatte
Architecte technique
Inscription : juillet 2004
Messages : 3 016
Détails du profil
Informations personnelles :
Nom : Homme Philippe Vialatte
Âge : 33
Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Architecte technique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : juillet 2004
Messages : 3 016
Points : 7 432
Points : 7 432
Par défaut [VB.Net/C#] Convertir un chiffre arabe en chiffre romain

Bonjour a tous,

bon, comme je me suis arrache deux-trois cheveux pour faire une conversion qui marche bien...

Code :
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
 
   private string ArabicToRoman(int arabicNumber, bool isUpperCase) {
 
            string result = string.Empty;
 
            if (arabicNumber == 0) return result;
 
            try {
                if (arabicNumber >= 889) {
                    result = ArabicToRomanHelper(arabicNumber - 1000, "M");
                } else if (arabicNumber >= 389) {
                    result = ArabicToRomanHelper(arabicNumber - 500, "D");
                } else if (arabicNumber >= 89) {
                    result = ArabicToRomanHelper(arabicNumber - 100, "C");
                } else if (arabicNumber >= 39) {
                    result = ArabicToRomanHelper(arabicNumber - 50, "L");
                } else if (arabicNumber >= 9) {
                    result = ArabicToRomanHelper(arabicNumber - 10, "X");
                } else if (arabicNumber >= 4) {
                    result = ArabicToRomanHelper(arabicNumber - 5, "V");
                } else if (arabicNumber >= 1) {
                    result = ArabicToRomanHelper(arabicNumber - 1, "I");
                } else if (arabicNumber <= -889) {
                    result = ArabicToRomanHelper(arabicNumber + 1000, "M");
                } else if (arabicNumber <= -389) {
                    result = ArabicToRomanHelper(arabicNumber + 500, "D");
                } else if (arabicNumber <= -89) {
                    result = ArabicToRomanHelper(arabicNumber + 100, "C");
                } else if (arabicNumber <= -39) {
                    result = ArabicToRomanHelper(arabicNumber + 50, "L");
                } else if (arabicNumber <= -9) {
                    result = ArabicToRomanHelper(arabicNumber + 10, "X");
                } else if (arabicNumber <= -4) {
                    result = ArabicToRomanHelper(arabicNumber + 5, "V");
                } else if (arabicNumber <= -1) {
                    result = ArabicToRomanHelper(arabicNumber + 1, "I");
                }
            } catch (Exception e) {
                result = "Error : " + e.Message;
            }
 
            return isUpperCase ? result.ToUpper() : result.ToLower();
        }
 
        private string ArabicToRomanHelper(int arabicNumber, string letter) {
 
 
            if (arabicNumber < 0) {
                return ArabicToRoman(arabicNumber, true) + letter;
            }
 
            return letter + ArabicToRoman(arabicNumber, true);
        }

et la meme en VB (tant qu'a faire...)

Code :
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
 
 Function ArabicToRoman(ByVal arabicNumber AS Integer, Optional ByVal isUpperCase As boolean = true) as string
 
            Dim result as string = string.Empty
 
            if arabicNumber = 0 Then return result
 
            try 
                if arabicNumber >= 889 then
                    result = ArabicToRomanHelper(arabicNumber - 1000, "M")
                 else if arabicNumber >= 389 then
                    result = ArabicToRomanHelper(arabicNumber - 500, "D")
                 else if arabicNumber >= 89 then
                    result = ArabicToRomanHelper(arabicNumber - 100, "C")
                 else if arabicNumber >= 39 then
                    result = ArabicToRomanHelper(arabicNumber - 50, "L")
                 else if arabicNumber >= 9 then
                    result = ArabicToRomanHelper(arabicNumber - 10, "X")
                 else if arabicNumber >= 4 then
                    result = ArabicToRomanHelper(arabicNumber - 5, "V")
                 else if arabicNumber >= 1 then
                    result = ArabicToRomanHelper(arabicNumber - 1, "I")
                 else if arabicNumber <= -889 then
                    result = ArabicToRomanHelper(arabicNumber + 1000, "M")
                 else if arabicNumber <= -389 then
                    result = ArabicToRomanHelper(arabicNumber + 500, "D")
                 else if arabicNumber <= -89 then
                    result = ArabicToRomanHelper(arabicNumber + 100, "C")
                 else if arabicNumber <= -39 then
                    result = ArabicToRomanHelper(arabicNumber + 50, "L")
                 else if arabicNumber <= -9 then
                    result = ArabicToRomanHelper(arabicNumber + 10, "X")
                 else if arabicNumber <= -4 then
                    result = ArabicToRomanHelper(arabicNumber + 5, "V")
                 else if arabicNumber <= -1 then
                    result = ArabicToRomanHelper(arabicNumber + 1, "I")
                end if
             catch e as Exception
                result = "Error : " & e.Message
             end try
 
  			if isUpperCase  then
              return result.ToUpper()
			else
				return result.ToLower()
			end if
 
        end function
 
        Function ArabicToRomanHelper(ByVal arabicNumber as Integer, Byval letter as String)  as string
 
 
            if arabicNumber < 0 then
                return ArabicToRoman(arabicNumber, true) + letter
            else
            	return letter + ArabicToRoman(arabicNumber, true)
			end if
        end function
En esperant que ca serve a qqun
__________________

Mon Blog

Présentation de Razor


The Cake is still a lie !!!

Dernière modification par Skalp ; 18/09/2009 à 07h38.
Philippe Vialatte est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 01h40   #2
Expert Confirmé Sénior
 
Avatar de smyley
 
Inscription : juin 2003
Messages : 6 270
Détails du profil
Informations forums :
Inscription : juin 2003
Messages : 6 270
Points : 6 464
Points : 6 464
Envoyer un message via MSN à smyley
Pour 47 avec ton code j'obtiens IIIL alors que c'est censé s'écrire XLVII non ?
smyley est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 11h13   #3
Responsable .NET

 
Avatar de Philippe Vialatte
 
Homme Philippe Vialatte
Architecte technique
Inscription : juillet 2004
Messages : 3 016
Détails du profil
Informations personnelles :
Nom : Homme Philippe Vialatte
Âge : 33
Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Architecte technique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : juillet 2004
Messages : 3 016
Points : 7 432
Points : 7 432
ah ?...


euh....

désolé, alors


Je remballe , je reteste, je corrige et je reviens en semaine 2
__________________

Mon Blog

Présentation de Razor


The Cake is still a lie !!!
Philippe Vialatte est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 11h28   #4
Rédacteur/Modérateur

 
Avatar de The_badger_man
 
Florian
Développeur .NET
Inscription : janvier 2005
Messages : 2 620
Détails du profil
Informations personnelles :
Nom : Florian
Âge : 28
Localisation : France, Yvelines (Île de France)

Informations professionnelles :
Activité : Développeur .NET

Informations forums :
Inscription : janvier 2005
Messages : 2 620
Points : 5 332
Points : 5 332
Sinon t'as une librairie déjà toute faite: http://www.codeproject.com/KB/cs/numberconvert.aspx
__________________
Les règles du forum
Le trio magique : FAQ + Cours + fonction rechercher
Mes articles
Pas de questions par messages privés svp

Software is never finished, only abandoned.
The_badger_man est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 20h02   #5
Responsable .NET

 
Avatar de Philippe Vialatte
 
Homme Philippe Vialatte
Architecte technique
Inscription : juillet 2004
Messages : 3 016
Détails du profil
Informations personnelles :
Nom : Homme Philippe Vialatte
Âge : 33
Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Architecte technique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : juillet 2004
Messages : 3 016
Points : 7 432
Points : 7 432
Citation:
Envoyé par The_badger_man Voir le message
Sinon t'as une librairie déjà toute faite: http://www.codeproject.com/KB/cs/numberconvert.aspx
Pinaise..

si seulement j'avais trouve ca vendredi matin


Merci bcp (n'as plus qu'a scratcher le bout de code que j'avais fait...quoique..Ca marche bien jusqu'a 39 )
__________________

Mon Blog

Présentation de Razor


The Cake is still a lie !!!
Philippe Vialatte est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 21h06   #6
Expert Confirmé Sénior
 
Avatar de smyley
 
Inscription : juin 2003
Messages : 6 270
Détails du profil
Informations forums :
Inscription : juin 2003
Messages : 6 270
Points : 6 464
Points : 6 464
Envoyer un message via MSN à smyley
Citation:
Envoyé par pvialatte Voir le message
Merci bcp (n'as plus qu'a scratcher le bout de code que j'avais fait...quoique..Ca marche bien jusqu'a 39 )
Code :
1
2
else if arabicNumber >= 39 then
                    result = ArabicToRomanHelper(arabicNumber - 50, "L")
D'ailleurs j'ai pas compris pourquoi c'était 39 et pas 50 ...
smyley est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 22h15   #7
Responsable .NET

 
Avatar de Philippe Vialatte
 
Homme Philippe Vialatte
Architecte technique
Inscription : juillet 2004
Messages : 3 016
Détails du profil
Informations personnelles :
Nom : Homme Philippe Vialatte
Âge : 33
Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Architecte technique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : juillet 2004
Messages : 3 016
Points : 7 432
Points : 7 432
parce que 40 s'ecrit XL...et j'etais persuade (encore jusqu'a 5 minutes) que 39 -> XIL...

donc, 40 doit d'abord retourner L, puis X en negatif...
__________________

Mon Blog

Présentation de Razor


The Cake is still a lie !!!
Philippe Vialatte est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 22h37   #8
Expert Confirmé Sénior
 
Avatar de smyley
 
Inscription : juin 2003
Messages : 6 270
Détails du profil
Informations forums :
Inscription : juin 2003
Messages : 6 270
Points : 6 464
Points : 6 464
Envoyer un message via MSN à smyley
Citation:
Envoyé par pvialatte Voir le message
parce que 40 s'ecrit XL...et j'etais persuade (encore jusqu'a 5 minutes) que 39 -> XIL...

donc, 40 doit d'abord retourner L, puis X en negatif...
Ah ouais c'est vrai, l'histoire des soustractions ...
Ouai bon bref les chiffres romains c'était en primaire, et c'est bien loin
smyley est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/11/2008, 23h08   #9
Responsable .NET

 
Avatar de Philippe Vialatte
 
Homme Philippe Vialatte
Architecte technique
Inscription : juillet 2004
Messages : 3 016
Détails du profil
Informations personnelles :
Nom : Homme Philippe Vialatte
Âge : 33
Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Architecte technique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : juillet 2004
Messages : 3 016
Points : 7 432
Points : 7 432
Citation:
c'est bien loin
hé, a qui tu le dis
__________________

Mon Blog

Présentation de Razor


The Cake is still a lie !!!
Philippe Vialatte est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/11/2008, 11h12   #10
Responsable .NET

 
Avatar de Philippe Vialatte
 
Homme Philippe Vialatte
Architecte technique
Inscription : juillet 2004
Messages : 3 016
Détails du profil
Informations personnelles :
Nom : Homme Philippe Vialatte
Âge : 33
Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Architecte technique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : juillet 2004
Messages : 3 016
Points : 7 432
Points : 7 432
Allez, pour pas laisser tomber l'affaire

Apres correction, ca marche jusqu'a 4000 (au-dela, faut utiliser des caracteres speciaux...)

Code C# :
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
private static string ArabicToRoman(int number, bool isUpperCase) {
 
            string result = string.Empty;
 
            if (number == 0) return result;
 
            try {	
 
                if (number > 999) {
                    result = ArabicToRomanHelper(number - 1000, "M");
                } else if (number > 899) {
                    result = ArabicToRomanHelper(number - 900, "CM");
                } else if (number > 499) {
                    result = ArabicToRomanHelper(number - 500, "D");
                } else if (number > 399) {
                    result = ArabicToRomanHelper(number - 400, "CD");
                } else if (number > 99) {
                    result = ArabicToRomanHelper(number - 100, "C");
                } else if (number > 89) {
                    result = ArabicToRomanHelper(number - 90, "XC");
                } else if (number > 49) {
                    result = ArabicToRomanHelper(number - 50, "L");
                } else if (number > 39) {
                    result = ArabicToRomanHelper(number - 40, "XL");
                } else if (number >= 9) {
                    result = ArabicToRomanHelper(number - 10, "X");
                } else if (number >= 4) {
                    result = ArabicToRomanHelper(number - 5, "V");
                } else if (number >= 1) {
                    result = ArabicToRomanHelper(number - 1, "I");
                } else if (number <= -889) {
                    result = ArabicToRomanHelper(number + 1000, "M");
                } else if (number <= -389) {
                    result = ArabicToRomanHelper(number + 500, "D");
                } else if (number <= -89) {
                    result = ArabicToRomanHelper(number + 100, "C");
                } else if (number <= -39) {
                    result = ArabicToRomanHelper(number + 50, "L");
                } else if (number <= -9) {
                    result = ArabicToRomanHelper(number + 10, "X");
                } else if (number <= -4) {
                    result = ArabicToRomanHelper(number + 5, "V");
                } else if (number <= -1) {
                    result = ArabicToRomanHelper(number + 1, "I");
                }
            } catch (Exception e) {
                result = "Error : " + e.Message;
            }
 
            return isUpperCase ? result.ToUpper() : result.ToLower();
        }
 
        private static string ArabicToRomanHelper(int number, string letter) {
 
            if (number < 0) {
                return ArabicToRoman(number, true) + letter;
            }
 
            return letter + ArabicToRoman(number, true);
        }

Et la, ca marche
__________________

Mon Blog

Présentation de Razor


The Cake is still a lie !!!

Dernière modification par tomlev ; 10/11/2008 à 13h35. Motif: QUOTE -> CODE
Philippe Vialatte est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/11/2008, 23h51   #11
Expert Confirmé Sénior
 
Avatar de smyley
 
Inscription : juin 2003
Messages : 6 270
Détails du profil
Informations forums :
Inscription : juin 2003
Messages : 6 270
Points : 6 464
Points : 6 464
Envoyer un message via MSN à smyley

En même temps les chiffres romains perdent leur intérêt dès que l'on passe les 50
smyley est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +1. Il est actuellement 11h40.


 
 
 
 
Partenaires

Hébergement Web