Advertisement
Aslai

Untitled

Jul 5th, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. int permute(){
  2.     cout << "This program will find all the permutations of a user defined string.\nDue to technical issues, the largest string you can process is 10 characters\nlong. I may rewrite this, but the true maximum is 11 characters as 32 bit\npointers can only access up to 2GB of memory. (11!*11 = 439MB, 12!*12 = 5.7GB)\nAnyways...\nPlease use only lower-case letters.\n";
  3.     string path=getcwd(NULL, _MAX_PATH);
  4.     string str = "";
  5.     string chr;
  6.     long int ins;
  7.     long int grab;
  8.     long int i = 0;
  9.     cout << "Please enter a string to permutate: ";
  10.     getline( cin, str );
  11.  
  12.     //open file stream
  13.     ofstream out;
  14.     out.open("Values.txt");
  15.  
  16.  
  17.     while ( str.length() == 0 || str.length() > 10 ) {
  18.         cout << "Please enter a string between 1 and 10 characters: ";
  19.         getline( cin, str );
  20.     }
  21.     bool words = decideyn( "Would you like to output ONLY actual words?" );
  22.     string a;
  23.     int megs = fact( str.length() + 1 ) / 1048576;
  24.     if ( megs >= 1 ){
  25.         cout << "\nThis operation will take approximately " << 5 * megs << "MB of memory to run,\nand " << megs << "MB of disk space. It may also take quite a bit of time. \nContinue? ";
  26.         getline( cin, a );
  27.     }
  28.     else{
  29.         a = "yes";
  30.     }
  31.     if (a.substr( 0,1 ) == "y"){
  32.         cout << "\nInitializing";
  33.         cout << "\nDeclaring variables...";
  34.         long int size (fact(str.length()));
  35.         string* holding = NULL;
  36.         string* holding2 = NULL;
  37.         holding = new string[size];
  38.         holding2 = new string[size];
  39.         holding[0] = str.substr(0,1);
  40.         string temp;
  41.         string dict = "\t";
  42.         if ( words == true ) {
  43.             cout << "\nParsing \"words.txt\"";
  44.             ifstream myReadFile;
  45.             myReadFile.open( "words.txt" );
  46.             string tmp;
  47.             if ( myReadFile.is_open() ) {
  48.                 i = 0;
  49.                 while ( !myReadFile.eof() ) {
  50.                     myReadFile >> tmp;
  51.                     dict += tmp + "\t";
  52.                     i ++;
  53.                 }
  54.             }
  55.             myReadFile.close();
  56.         }
  57.  
  58.         cout << "\nBeginning permutation";
  59.         for( long int len = 1; len < str.length(); len ++ ){
  60.             cout << "\nPass " << len;
  61.             chr = str.substr( len, 1 );
  62.             for( i = 0; i < fact( len + 1 ); i ++ ){
  63.                 ins = i % (len + 1);
  64.                 grab = i / (len + 1);
  65.                 //cout << "i = " << i << " Ins = " << ins << " Len = " << len << " Grab = " << grab << " Holding[ grab ] = " << holding[grab] << " fact( len - 1 ) = " << fact( len ) << "\n";
  66.                 temp = holding[ grab ];
  67.                 holding2[i] = temp.insert( ins, chr );
  68.             }
  69.             for( i = 0; i < size; i ++ ){
  70.                 holding[ i ] = holding2[ i ];
  71.                 //cout << "\n" << holding[i];
  72.             }
  73.             if ( words == true ) {
  74.                 for( i = 0; i < size; i ++ ){
  75.                     if ( holding[ i ] != "" ) {
  76.                         if ( dict.find( "\t" + holding[ i ] + "\t" ) < 4294967295U ){
  77.                             out << holding[ i ] << "\t";
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         cout << "\n\nSaving to file: " << path << "\\Values.txt";
  84.         if ( words == false ) {
  85.             ofstream out;
  86.             out.open("Values.txt");
  87.             for( i = 0; i < size; i ++ ){
  88.                 out << holding[ i ] << "\t";
  89.             }
  90.         }
  91.         out.close();
  92.         cout << "\n\nThere " << ((size == 1LL) ? "was " : "were ") << size << " possible combination" << ((size == 1LL) ? "." : "s.") << "\n\n";
  93.     }
  94.     else {
  95.         cout << "\n\nAborted\n\n";
  96.     }
  97.     pause();
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement