12,950,027 members (76,629 online)

#### Stats

70.5K views
25 bookmarked
Posted 12 Jul 2004

# Implementing Permutation Variations

, 14 Jul 2004 CPOL
Several enhanced permutation algorithms created in iterative or recursive solution.
 ```// ============================================================================ // File: Permutate.cpp - Main test of three character permutations // Author: Zuoliu Ding, May 2004 // Example: Permutation of characters in the string abc: // abc acb bac bca cab cba // ============================================================================ #include #include #include #include #include using namespace std; // Three Permutation method prototypes vector StlPermutation(const char*, bool bOrdered=true); vector RecPermutation(const char*, bool bRepeated=true); vector example_02(const char* a); //vector StlPermutation(const char*);//=true); //vector RecPermutation(const char*);//=true); // ============================================================================ // Function: main // Description: Test permutation functions. // ============================================================================ void main() { char szSel[8]; char sz[32]; // "abcdefghijk"; // while (true) { cout << "Enter the first Letter to Select a Permutation Method:\n"; cout << "-----------------------------------------------------------------------\n"; cout << "S)TL "; cout << "STL-N)o-Ordered "; cout << "R)ecursion "; cout << "Re-U)nrepeated "; // cout << "I)teration,Enh "; cout << "P)hil's "; cout << "E)xit\n->"; cin.getline(szSel, 8); *szSel = toupper(*szSel); if (*szSel =='E') return; if (*szSel !='R' && *szSel !='U' && *szSel !='S' && *szSel !='P' && *szSel !='N') { cout << "Unrecognized Selection - Try again...\n\n"; continue; } cout << "Enter a String for Permutation: "; cin.getline(sz, 32); cout << "Waiting ... "; vector vStr; time_t t1, t2; time(&t1); vStr =(*szSel =='R')? RecPermutation(sz): (*szSel =='U')? RecPermutation(sz, false): (*szSel =='S')? StlPermutation(sz): (*szSel =='N')? StlPermutation(sz, false): example_02(sz); // (*szSel =='I')? EnhPermutationIteration(sz): StlPermutation(sz); time(&t2); cout << "\nTotal " << vStr.size() <<" permutations generated in result.txt.\n"; if (strlen(sz)<5) { for (int i=0; i< (int)vStr.size(); i++) { if (i) cout <<", "; cout << vStr[i]; } cout << endl; } cout << "The time: " << long(t2-t1) <<" seconds.\n"; if (strlen(sz)<11) { cout << "File saving ... "; ofstream fout("result.txt"); // Output file for all solutions fout <<((*szSel =='R')? "Recursive Permutation, Allow Repeated:": (*szSel =='U')? "Recursive Permutation, Not Repeated:": (*szSel =='S')? "STL, next_permutation, Ordered:": (*szSel =='N')? "STL, next_permutation, Not Ordered:": "Iteration, Philip Paul's Example:") <

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

## Share

 Software Developer United States
An Adjunct Faculty and Software Developer in Los Angeles and Orange County, CA

* Typical articles published in Dr. Dobb’s Journal and Windows Developer Magazine:

- A Silent Component Update for Internet Explorer
- Silent Application Update
- An MDI-Style Web Browser and Load Spy Monitor
- Implementing Wireless Print for WinNT/Win2K
- Multi-State Checkbox Tree Views
- A Generic Tool Tip Class
- An Easy Way to Add Tool Tips to Any MFC Control