Fonction récursive créant un probleme.
Bonjour.
J'ai récupéré une fonction VB générant toutes les combinaisons possible avec un nombre, et des lettres déterminées.
Code:
1 2 3 4 5 6 7 8 9 10
| Sub Main
Dim chaine As String
Dim PosLettre(1 To 3) As Integer 'initiale un tableau de n case pour un mot à n lettre
chaine = ""
PosLettre(1) = 1
PosLettre(2) = 1
PosLettre(3) = 1
GeneMot "abcd", PosLettre, chaine, 3, 3 |
'le premier chiffre correspond au nombre lettre de la chaine ex abc = 3
' le deuxieme correspond au nombre de lettre voulue en sortie ex 3= aaa,aab,aac 2=aa,ab,ac
MsgBox chaine
End Sub
Public Function GeneMot(chaine As String, PosLettre As Variant, ch As String, LongeurChaine As Integer, nbLettreVoulue As Integer)
While PosLettre(1) < LongeurChaine + 1
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| For l = 1 To nbLettreVoulue
a = a & Mid(chaine, PosLettre(l), 1)
Next l
ch = ch & a & Chr(13)
For m = nbLettreVoulue To 1 Step -1
If PosLettre(m) = LongeurChaine Then
If m <> 1 Then
PosLettre(m) = 1
Else
PosLettre(m) = PosLettre(m) + 1
End If
Else
PosLettre(m) = PosLettre(m) + 1
Exit For
End If
Next m
GeneMot chaine, PosLettre, ch, LongeurChaine, nbLettreVoulue
Wend
end function |
J'ai transformé cette fonction en C++, mais elle crée un problème, si on ne l'appelle pas, la compilation ne pose pas de problème.
Par contre, si on appelle la fonction, le compilateur se ferme.
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 59 60 61 62
| #include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
string GeneMot(string chaine, int PosLettre[], string ch, int LongeurChaine, int nbLettreVoulue);
int main(int argc, char *argv[])
{ string chaine ;
int PosLettre [3];
PosLettre[1] = 1;
PosLettre[2] = 1;
PosLettre[3] = 1 ;
string s= "abc";
GeneMot(s,PosLettre,chaine,2,2); }
string GeneMot(string chaine, int PosLettre[], string ch, int LongeurChaine, int nbLettreVoulue)
{
int l;
int m;
string a ;
while (PosLettre[1] < LongeurChaine + 1)
for (l = 1; l <= nbLettreVoulue; l++)
{ a = chaine.substr (PosLettre[l],1) ; }
ch = ch + a + "\r";
for (m = nbLettreVoulue; m >= 1; m--)
{
if (PosLettre[m] == LongeurChaine)
{
if (m != 1)
{ PosLettre[m] = 1; }
else
{ PosLettre[m] = PosLettre[m] + 1;}
}
else
{ PosLettre[m] = PosLettre[m] + 1;
break;
}
}
GeneMot(chaine, PosLettre, ch, LongeurChaine, nbLettreVoulue);
}
} |