Click here to Skip to main content
Click here to Skip to main content

Combinations without repetition algorithm C++/MFC

, 28 Dec 2010
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

danielexx

Italy Italy
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Dec 2010
Article Copyright 2010 by danielexx
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid