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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define TRUE 1
# define FALSE 0
void clean_stdin (void) /* Procédure pour vider le buffer */
{
int c = 0;
do
{
c = getchar();
} while (c != '\n' && c != EOF);
}
void ChiffreEnLettre(unsigned int, char*);
int main(void) /* Syntaxe correcte */
{
unsigned x = 0;
char choix = 'o', c[256];
while((choix=='o')||(choix=='O'))
{
printf ("Introduire un chiffre entier : ");
scanf ("%u", &x); /* bon format pour les unsigned */
ChiffreEnLettre(x,c);
printf ("\nEn lettre ca fait : %s", c);
printf ("\nEncore d'autre? (O/N): ");
clean_stdin ();/* on vide le buffer */
choix = getchar (); /* Il t'es possible d'utiliser getch () si tu le souhaites (avec conio.h toujours), je l'ai remplacé pour la portabilité */
printf("\n===================================================\n");
}
return 0;
}
/* Une bonne identation est correcte pour relire le code */
void ChiffreEnLettre(unsigned int chiffre, char * lettre)
{
unsigned int centaine, dizaine, unite, reste, y;
int dix = FALSE, i = 0;
strcpy(lettre, "");
reste = chiffre;
for(i=1000000000; i>=1; i/=1000)
{
y = reste/i;
if(y!=0)
{
centaine = y/100;
dizaine = (y - centaine*100)/10;
unite = y-(centaine*100)-(dizaine*10);
switch(centaine)
{
case 0: /* Oo */
break;
case 1:
strcat(lettre,"cent ");
break;
case 2:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"deux cents ");
else
strcat(lettre,"deux cent ");
break;
case 3:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"trois cents ");
else
strcat(lettre,"trois cent ");
break;
case 4:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"quatre cents ");
else
strcat(lettre,"quatre cent ");
break;
case 5:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"cinq cents ");
else
strcat(lettre,"cinq cent ");
break;
case 6:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"six cents ");
else
strcat(lettre,"six cent ");
break;
case 7:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"sept cents ");
else
strcat(lettre,"sept cent ");
break;
case 8:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"huit cents ");
else
strcat(lettre,"huit cent ");
break;
case 9:
if((dizaine == 0)&&(unite == 0))
strcat(lettre,"neuf cents ");
else
strcat(lettre,"neuf cent ");
}
switch(dizaine)
{
case 0: /* C'est inutile */
break;
case 1:
dix = TRUE;
break;
case 2:
strcat(lettre,"vingt ");
break;
case 3:
strcat(lettre,"trente ");
break;
case 4:
strcat(lettre,"quarante ");
break;
case 5:
strcat(lettre,"cinquante ");
break;
case 6:
strcat(lettre,"soixante ");
break;
case 7:
dix = TRUE;
strcat(lettre,"soixante ");
break;
case 8:
strcat(lettre,"quatre-vingt ");
break;
case 9:
dix = TRUE;
strcat(lettre,"quatre-vingt ");
}
switch(unite)
{
case 0:
if(dix == TRUE)
strcat(lettre,"dix ");
break;
case 1:
if(dix == TRUE)
strcat(lettre,"onze ");
else
strcat(lettre,"un ");
break;
case 2:
if(dix == TRUE)
strcat(lettre,"douze ");
else strcat(lettre,"deux ");
break;
case 3:
if(dix == TRUE)
strcat(lettre,"treize ");
else
strcat(lettre,"trois ");
break;
case 4:
if(dix == TRUE)
strcat(lettre,"quatorze ");
else
strcat(lettre,"quatre ");
break;
case 5:
if(dix == TRUE)
strcat(lettre,"quinze ");
else
strcat(lettre,"cinq ");
break;
case 6:
if(dix == TRUE)
strcat(lettre,"seize ");
else
strcat(lettre,"six ");
break;
case 7:
if(dix)
strcat(lettre,"dix-sept ");
else
strcat(lettre,"sept ");
break;
case 8:
if(dix == TRUE)
strcat(lettre,"dix-huit ");
else
strcat(lettre,"huit ");
break;
case 9:
if(dix == TRUE)
strcat(lettre,"dix-neuf ");
else
strcat(lettre,"neuf ");
}
switch (i)
{
case 1000000000:
if(y>1)
strcat(lettre,"milliards ");
else
strcat(lettre,"milliard ");
break;
case 1000000:
if(y>1)
strcat(lettre,"millions ");
else
strcat(lettre,"million ");
break;
case 1000:
strcat(lettre,"mille ");
break; /* à ne pas oublier */
}
}
reste -= y*i;
dix = FALSE;
}
if(strlen(lettre)==0)
strcpy(lettre,"zero");
} |
Partager