This algorithm is written in C++ (with MFC classes), calculates combinations without repetition of characters contained in a string
. The algorithm allows to define, during the execution,the length of the string
and length combinations, all using a recursive function. For simplicity and to keep the code compact, I used class CStringArray
to store the combinations, and class CString
to enter the string
to process.I also used three global variables needed for the proper functioning of the algorithm. The initialization function is necessary to delete items in CStringArray
before a new execution, and define globally the length of the combinations. This function must be called before the function containing the combinatorial algorithm.
CStringArray dani;
CString temp("");
int COMBI;
void inizializzazione(int lungcombinazioni){
COMBI=lungcombinazioni;
dani.RemoveAll();
}
void combinazioni(CString str,int combi){
int tot=str.GetLength();
int rr;
for(int y=0;y<tot;y++){
temp=temp+str[y];
if(temp.GetLength()==COMBI){
dani.Add(temp);
rr=temp.GetLength();
temp=temp.Left(rr-1);
}
else{
if(combi==0)
return;
CString str1;
str1=str.Right(tot-1-y);
combinazioni(str1,combi-1);
rr=temp.GetLength();
temp=temp.Left(rr-1);
}}}