Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int permute(){
- 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";
- string path=getcwd(NULL, _MAX_PATH);
- string str = "";
- string chr;
- long int ins;
- long int grab;
- long int i = 0;
- cout << "Please enter a string to permutate: ";
- getline( cin, str );
- //open file stream
- ofstream out;
- out.open("Values.txt");
- while ( str.length() == 0 || str.length() > 10 ) {
- cout << "Please enter a string between 1 and 10 characters: ";
- getline( cin, str );
- }
- bool words = decideyn( "Would you like to output ONLY actual words?" );
- string a;
- int megs = fact( str.length() + 1 ) / 1048576;
- if ( megs >= 1 ){
- 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? ";
- getline( cin, a );
- }
- else{
- a = "yes";
- }
- if (a.substr( 0,1 ) == "y"){
- cout << "\nInitializing";
- cout << "\nDeclaring variables...";
- long int size (fact(str.length()));
- string* holding = NULL;
- string* holding2 = NULL;
- holding = new string[size];
- holding2 = new string[size];
- holding[0] = str.substr(0,1);
- string temp;
- string dict = "\t";
- if ( words == true ) {
- cout << "\nParsing \"words.txt\"";
- ifstream myReadFile;
- myReadFile.open( "words.txt" );
- string tmp;
- if ( myReadFile.is_open() ) {
- i = 0;
- while ( !myReadFile.eof() ) {
- myReadFile >> tmp;
- dict += tmp + "\t";
- i ++;
- }
- }
- myReadFile.close();
- }
- cout << "\nBeginning permutation";
- for( long int len = 1; len < str.length(); len ++ ){
- cout << "\nPass " << len;
- chr = str.substr( len, 1 );
- for( i = 0; i < fact( len + 1 ); i ++ ){
- ins = i % (len + 1);
- grab = i / (len + 1);
- //cout << "i = " << i << " Ins = " << ins << " Len = " << len << " Grab = " << grab << " Holding[ grab ] = " << holding[grab] << " fact( len - 1 ) = " << fact( len ) << "\n";
- temp = holding[ grab ];
- holding2[i] = temp.insert( ins, chr );
- }
- for( i = 0; i < size; i ++ ){
- holding[ i ] = holding2[ i ];
- //cout << "\n" << holding[i];
- }
- if ( words == true ) {
- for( i = 0; i < size; i ++ ){
- if ( holding[ i ] != "" ) {
- if ( dict.find( "\t" + holding[ i ] + "\t" ) < 4294967295U ){
- out << holding[ i ] << "\t";
- }
- }
- }
- }
- }
- cout << "\n\nSaving to file: " << path << "\\Values.txt";
- if ( words == false ) {
- ofstream out;
- out.open("Values.txt");
- for( i = 0; i < size; i ++ ){
- out << holding[ i ] << "\t";
- }
- }
- out.close();
- cout << "\n\nThere " << ((size == 1LL) ? "was " : "were ") << size << " possible combination" << ((size == 1LL) ? "." : "s.") << "\n\n";
- }
- else {
- cout << "\n\nAborted\n\n";
- }
- pause();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement