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

C++ Discussion :

les chifres romains en c++


Sujet :

C++

  1. #1
    Candidat au Club
    Inscrit en
    Février 2005
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 2
    Points : 2
    Points
    2
    Par défaut les chifres romains en c++
    salut j aimerai ecrire un programme qui transcrit n importe quel nombre siasis en chifre romains

  2. #2
    Membre averti Avatar de xxiemeciel
    Inscrit en
    Juin 2005
    Messages
    371
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 371
    Points : 352
    Points
    352
    Par défaut
    Salut,

    Une chose est sur il n'existe pas une solution unique mais je suppose que tu devrais definir les principales valeur puis ensuite etablir des regles

    I = 1
    V = 5
    X = 10

    puis tu analyses lettre par lettre :

    par exemple XVII = 10 + 5 + 1 + 1
    et IV = 5 - 1 (moins parceque le I est avant)

    Maintenant il y a surement des milliers d'autres facons de faire

    XXiemeciel
    XXiemeciel

  3. #3
    Invité
    Invité(e)
    Par défaut
    J'ai un algorithme, mais il est en DarkBasic
    Ca doit tout de même te permettre de réaliser ta fonction :

    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
    function mod(a, b)
      if b=0 then exitfunction a
      a=a-a/b*b
    endfunction a
     
    function nombre_romain$(nombre)
     
      if nombre<1 or nombre>9999 then exitfunction ""
     
      dim rom$(3, 3)
      rom$(3, 0)="M"
      rom$(2, 0)="C" : rom$(2, 1)="CD" : rom$(2, 2)="D" : rom$(2, 3)="CM"
      rom$(1, 0)="X" : rom$(1, 1)="XL" : rom$(1, 2)="L" : rom$(1, 3)="XC"
      rom$(0, 0)="I" : rom$(0, 1)="IV" : rom$(0, 2)="V" : rom$(0, 3)="IX"
     
      n=1
      romain$ = ""
     
      for i=0 to 3
     
        chiffre = mod(nombre, n*10)/n
     
        if i<3
          if chiffre<4
            while chiffre>0
              romain$ = rom$(i, 0) + romain$
              dec chiffre
            endwhile
          endif
          if chiffre=4
            romain$ = rom$(i, 1) + romain$
          endif
          if chiffre>4 and chiffre<9
            while chiffre>5
              romain$ = rom$(i, 0) + romain$
              dec chiffre
            endwhile
            romain$ = rom$(i, 2) + romain$
          endif
          if chiffre=9
            romain$ = rom$(i, 3) + romain$
          endif
        else
          while chiffre>0
            romain$ = rom$(i, 0) + romain$
            dec chiffre
          endwhile
        endif
     
        n = n*10
     
      next i
     
      `undim rom$
     
    endfunction romain$
    Je précise qu'en DarkBasic, les variables ne sont pas déclarées avant emploi, et qu'une variable se terminant par '$' est une chaine et une variable sans suffixe un nombre entier. Je ferai un petit code si j'ai le temps.

  4. #4
    Invité
    Invité(e)
    Par défaut
    Suite à ta demande, voici un code en C/C++. J'ai du réécrire un semblant de strcat() car la fonction "officielle" du C me sortait une erreur de segmentation (la mémoire n'est pas libérée, je sais) :

    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
    #include <stdio.h>
    #include <string.h>
     
    #define strcat _my_cat
     
    char* _my_cat(char* ch1,char* ch2)
    {
        char* retour;
        int ln=0;
        char* pos = ch1;
        while(*pos!='\0')
        {
            pos+=1;
            ln+=1;
        }
        pos = ch2;
        while(*pos!='\0')
        {
            pos+=1;
            ln+=1;
        }
     
        retour = new char[ln];
        char* cible = retour;
     
        pos = ch1;
        while(*pos!='\0')
        {
            *cible = *pos;
            pos+=1;
            cible+=1;
        }
        pos = ch2;
        while(*pos!='\0')
        {
            *cible = *pos;
            pos+=1;
            cible+=1;
        }
     
        return retour;
    }
     
    char* romain(int nombre)
    {
        if(nombre<1 || nombre>9999)
            return NULL;
     
        char* rom[4][4];
        rom[3][0]="M";
        rom[2][0]="C"; rom[2][1]="CD"; rom[2][2]="D"; rom[2][3]="CM";
        rom[1][0]="X"; rom[1][1]="XL"; rom[1][2]="L"; rom[1][3]="XC";
        rom[0][0]="I"; rom[0][1]="IV"; rom[0][2]="V"; rom[0][3]="IX";
     
        int n=1,i,chiffre;
        char* romain="";
     
        for(i=0;i<=3;i+=1)
        {
            chiffre = (nombre%(n*10))/n;
     
            if(i<3)
            {
                if(chiffre<4)
                {
                    while(chiffre>0)
                    {
                        printf("romain = \"%s\"\n",romain);
                        romain = strcat(rom[i][0],romain);
                        chiffre-=1;
                    }
                }
     
                else if(chiffre==4)
                    romain = strcat(rom[i][1],romain);
     
                else if(chiffre>4 && chiffre<9)
                {
                    while(chiffre>5)
                    {
                        romain = strcat(rom[i][0],romain);
                        chiffre-=1;
                    }
     
                    romain = strcat(rom[i][2],romain);
                }
     
                else if(chiffre==9)
                    romain = strcat(rom[i][3],romain);
            }
            else
            {
                while(chiffre>0)
                {
                    romain = strcat(rom[i][0],romain);
                    chiffre-=1;
                }
            }
     
            n = n*10;
        }
     
        return romain;
    }

Discussions similaires

  1. Les meilleurs cours et tutoriels C++
    Par Community Management dans le forum C++
    Réponses: 1
    Dernier message: 13/05/2015, 13h50
  2. Afficher les chiffres en romain
    Par aminao dans le forum SAS Base
    Réponses: 2
    Dernier message: 10/01/2012, 11h25
  3. Obligatoire : lisez les règles du forum : MAJ 06/08/2010
    Par Anomaly dans le forum Mode d'emploi & aide aux nouveaux
    Réponses: 0
    Dernier message: 03/07/2008, 13h46
  4. xpath-->test sur les chifres
    Par yos dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 30/01/2006, 15h41

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