IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Contribuez Python Discussion :

convertisseur de Chiffres Arabes en Chiffres Romain sens unique python 2.7


Sujet :

Contribuez Python

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Novembre 2011
    Messages
    44
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2011
    Messages : 44
    Par défaut
    Bonjour je met mon 2ème module, une fonction qui cette fois convertie les Chiffre Arabes en Chiffres Romain à sens unique cette fois, toujours pour python 2.7
    Attention vous devez impérativement utiliser idle ou autres programme supportant les caractère unicode pour les nombre supérieur à 3.999, car la fonction utilise le caractère unicode 304 en hexadécimal sois 772 en décimal pour le macron des milliers le code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    # -*- coding: cp1252 -*-
    def chiffres_romain(n, sep=u"."):
        s=str(int(n))
        l=len(s)
        def nombre_romain(list_chiffre, res_chiffre, romain, nb_macron, rang, sep):
            for chiffre_var in list_chiffre:
                if (4<=res_chiffre<=999 and nb_macron>=1) or (0<=res_chiffre<=999 and nb_macron==0):
                    if rang==0:
                        if 1<=int(chiffre_var)<=3:
                            romain=unicode(("I" + (unichr(772) * nb_macron)) * int(chiffre_var) + romain)
                            rang+=1
                        elif 4<=int(chiffre_var)<=8:
                            nb=int(chiffre_var)-5
                            if nb==-1:
                                romain=unicode(("I" + (unichr(772) * nb_macron)) + ("V" + (unichr(772) * nb_macron)) + romain)
                                rang+=1
                            elif 0<=nb<=3:
                                romain=unicode(("V" + (unichr(772) * nb_macron)) + ("I" + (unichr(772) * nb_macron)) * nb + romain)
                                rang+=1
                        elif int(chiffre_var)==9:
                            romain=unicode(("I" + (unichr(772) * nb_macron)) + ("X" + (unichr(772) * nb_macron)) + romain)
                            rang+=1
                        elif int(chiffre_var)==0:
                            romain=unicode(romain)
                            rang+=1
                    elif rang==1:
                        if 1<=int(chiffre_var)<=3:
                            romain=unicode(("X" + (unichr(772) * nb_macron)) * int(chiffre_var) + romain)
                            rang+=1
                        elif 4<=int(chiffre_var)<=8:
                            nb=int(chiffre_var)-5
                            if nb==-1:
                                romain=unicode(("X" + (unichr(772) * nb_macron)) + ((unichr(772) * nb_macron) + "L") + romain)
                                rang+=1
                            elif 0<=nb<=3:
                                romain=unicode(("L" + (unichr(772) * nb_macron)) + ("X" + (unichr(772) * nb_macron)) * nb + romain)
                                rang+=1
                        elif int(chiffre_var)==9:
                            romain=unicode(("X" + (unichr(772) * nb_macron)) + ("C" + (unichr(772) * nb_macron)) + romain)
                            rang+=1
                        elif int(chiffre_var)==0:
                            romain=unicode(romain)
                            rang+=1
                    elif rang==2:
                        if 1<=int(chiffre_var)<=3:
                            romain=unicode(("C" + (unichr(772) * nb_macron)) * int(chiffre_var) + romain)
                            rang+=1
                        elif 4<=int(chiffre_var)<=8:
                            nb=int(chiffre_var)-5
                            if nb==-1:
                                romain=unicode(("C" + (unichr(772) * nb_macron)) + ("D" + (unichr(772) * nb_macron)) + romain)
                                rang+=1
                            elif 0<=nb<=3:
                                romain=unicode(("D" + (unichr(772) * nb_macron)) + ("C" + (unichr(772) * nb_macron)) * nb + romain)
                                rang+=1
                        elif int(chiffre_var)==9:
                            romain=unicode(("C" + (unichr(772) * nb_macron)) + ("M" + (unichr(772) * nb_macron)) + romain)
                            rang+=1
                        elif int(chiffre_var)==0:
                            romain=unicode(romain)
                            rang+=1
                elif 1<=res_chiffre<=3 and nb_macron>=1:
                    romain=unicode(sep + ("M" + (unichr(772) * (nb_macron-1))) * res_chiffre + romain)
                    rang=0
            return romain
        nb_macron=0
        nc=0
        res=""
        romain=u""
        rang=0
        for i in range(l-1, -1 ,-1):
            res=s[i] + res
            nc+=1
            if nc==3:
                list_chiffre=list(res)
                list_chiffre.reverse()
                res_chiffre=int(res)
                romain=nombre_romain(list_chiffre, res_chiffre, romain, nb_macron, rang, sep)
                if (4<=res_chiffre<=999 and nb_macron>=1) or (1<=res_chiffre<=999 and nb_macron==0):
                    romain=unicode(sep + romain)
                elif 1<=res_chiffre<=3 and nb_macron>=1:
                    None
                nb_macron+=1
                nc=0
                del res_chiffre
                del list_chiffre
                del res
                res=""
        try:
            if 1<=len(res)<=2:
                list_chiffre=list(res)
                list_chiffre.reverse()
                res_chiffre=int(res)
                romain=nombre_romain(list_chiffre, res_chiffre, romain, nb_macron, rang, sep)
                del res_chiffre
                del list_chiffre
                del res
                del nc
        except:
            None
        if romain.startswith(sep):
            romain=romain[1:]
        if n < 0 and romain[1]==sep:
            romain=list(romain)
            del romain[1]
            romain="".join(romain)
        return romain
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. [JavaScript] Conversion de chiffres arabes en chiffres romains et inversement
    Par danielhagnoul dans le forum Contribuez
    Réponses: 1
    Dernier message: 05/11/2012, 07h48
  2. Chiffres arabes en chiffres romains
    Par khayyam90 dans le forum Contribuez
    Réponses: 0
    Dernier message: 09/02/2011, 22h26
  3. [C++0x] Convertisseur de chiffre arabe en romain
    Par Xanto dans le forum Langage
    Réponses: 5
    Dernier message: 30/07/2010, 14h41
  4. [Dvp.NET|Intégré] [VB.Net/C#] Convertir un chiffre arabe en chiffre romain
    Par Philippe Vialatte dans le forum Contribuez
    Réponses: 10
    Dernier message: 10/11/2008, 23h51
  5. Inverseur chiffres arabes <-> romains
    Par arraroussama dans le forum Assembleur
    Réponses: 1
    Dernier message: 26/02/2006, 21h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo