Bonjour,j'ai essayé de traduire un programme qui a ete ecrit en langage pascal en langage c++, mais,il ne marche pas.pouvez vous detecter des erreurs s'il y en a.
c est un programme qui convertit un nombre entier en toutes lettres.
code pascal:
Code pascal : 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 function NombreEnLettres(n : integer) : string; Const unite : array[1..16] of string = ('un','deux','trois','quatre','cinq','six', 'sept','huit','neuf','dix','onze','douze', 'treize','quatorze','quinze','seize'); dizaine : array[2..8] of string = ('vingt','trente','quarante','cinquante', 'soixante','','quatre-vingt'); coefs : array[0..3] of string = ('cent','mille','million','milliard'); Var temp : string; c, d, u : byte; coef : byte; i : word; neg : boolean; Begin If n = 0 then begin result := ' zero'; exit; end; result := ''; neg := n < 0; If neg then n := -n; coef:=0; Repeat u:=n mod 10; n:=n div 10; {Récupère unité et dizaine} d:=n mod 10; n:=n div 10; {Récupère dizaine} If d in [1,7,9] then begin Dec(d); Inc(u,10); end; temp := ''; if d > 1 then begin temp := ' ' + Dizaine[d]; if (d < 8) and ((u = 1) or (u = 11)) then temp := temp + ' et'; end; if u > 16 then begin temp := temp + ' ' + unite[10]; Dec(u,10); end; if u > 0 then temp := temp + ' ' + unite[u]; If (result = '') and (d = 8) and (u = 0) then result := 's'; result := temp + result; c := n mod 10; n := n div 10; {Récupère centaine} If c > 0 then begin temp := ''; if c > 1 then temp := ' ' + unite[c] + temp; temp := temp + ' ' + coefs[0]; If (result = '') and (c > 1) then result := 's'; result := temp + result; end; if n > 0 then begin Inc(coef); I := n mod 1000; If (i > 1) and (coef > 1) then result := 's' + result; If i > 0 then result := ' ' + coefs[coef] + result; If (i = 1) and (coef = 1) then Dec(n); end; until n = 0; result := Trim(result); if neg then result := 'moins ' + result; End;
code c++
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 #include<stdio.h> #include<conio.h> #include<string.h> #include<math.h> #include <stdlib.h> char NombreEnLettres( int n ){ char unite[][20] = {"un","deux","trois","quatre","cinq","six", "sept","huit","neuf","dix","onze","douze", "treize","quatorze","quinze","seize"}; char dizaine[][20] = {"vingt","trente","quarante","cinquante", "soixante","soixante-dix","quatre-vingt"}; char coefs[][20] = {"cent","mille","million","milliard"}; long unsigned int number ; char* temp; int c, d, u ; int coef ; int i ; char result ; if (n == 0) result = ' zero'; result = ''; coef=0; do { u= n % 10; n=n / 10; d=n % 10; n=n / 10; if ((d=1)&&(d=7)&&(d=9)) d--; u=u+10; temp = ''; if (d > 1){ temp = ' ' + dizaine[d]; if ((d < 8) && ((u = 1) || (u = 11))) temp = temp + ' et';} if (u > 16) temp = temp + ' ' + unite[10][20]; u=u-10; if (u > 0) temp = temp + ' ' + unite[u][10]; if ((result = '') && (d = 8) && (u = 0)) { result = 's'; result = 'temp' + result ;} c = n % 10; n = n / 10; if (c > 0){ temp = ''; if (c > 1) { temp = ' ' + unite[c][20] + temp; temp = temp + ' ' + coefs[0][20];} if ((result = '') && (c > 1)) {result = 's'; result = 'temp' + result;} } if (n > 0) { coef++; i = n % 1000; if ((i > 1)&& (coef > 1)) result = 's' + result; if (i > 0) result = ' ' + coefs[coef][20] + result; if ((i = 1) && (coef = 1)) n-- ; } }while (n = 0); } main(){ int n; printf ("entrer un nombre"); scanf("%d",&n); return NombreEnLettres( n ); getch(); }
Partager