Click here to Skip to main content
15,890,186 members
Articles / Desktop Programming / MFC
Tip/Trick

Combinations without repetition algorithm C++/MFC

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Dec 2010CPOL 12.6K   2  
Algorithm C++/MFC

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;//global variable
CString temp("");//global variable
int COMBI;//global variable

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);
}}}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --