1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
void swap(char *fir, char *sec)
{
char temp = *fir;
*fir = *sec;
*sec = temp;
}
ofstream *pFilelist[27];
void createFileStreams(char* basename)
{
int i;
char fullname[255];
for(i = 0; i < 26;i++)
{
sprintf(fullname,"%s%s%c%s", basename, "_", (i + 'a'), ".txt");
//ofstream *pStream = new ofstream();
pFilelist[i] = new ofstream();
pFilelist[i]->open(fullname);
}
sprintf(fullname, "%s%s%s", basename, "_misc", ".txt"); //filename starting with numbers
pFilelist[i] = new ofstream();
pFilelist[i]->open(fullname);
}
ofstream* getFileStream(const char *pString)
{
if(isalpha(*pString))
{
return pFilelist[*pString - 'a'];
}
else
{
return pFilelist[26]; // filename starting with numbers
}
}
void deleteFileStreams()
{
//close the streams and delete the memory when the program closes
}
void permutation(char * arr, int size, char* result, int depth, int limit)
{
//ofstream myfile ("permutation.txt", fstream::app);
if(depth == limit)
{
std::string str;
for(int a=0; a<limit; a++){
//myfile << result[a] << "";
//cout << result[a] << "";
str += result[a];
}
ofstream* stream = getFileStream(str.c_str());
*stream << str;
//myfile << str;
cout << str << "";
*stream << "\n";
cout << endl;
}
else
{
for(int i=0; i<size; i++)
{
result[depth] = arr[i];
permutation(arr, size, result, depth + 1, limit);
}
}
//myfile.close();
}
int main()
{
//ofstream myfile ("permutation.txt");
//myfile << "";
//myfile.close();
createFileStreams("permutation");
string answer;
char *rArray;
string startProcess = "N";
std::cout << "Welcome to permutation v1" << endl;
std::cout << "-------------------------" << endl;
std::cout << "Please enter how long the string should be: ";
std::getline (std::cin,answer);
int result = atoi(answer.c_str());
rArray = new char[result];
std::cout << "\n\nThank You!\n" << endl;
std::cout << "Please wait, generating possible character array for length of " << result << "." << endl;
std::cout << "Would you like to proceed? Y = yes & N = no: ";
std::getline (std::cin,startProcess);
char str[] = "abcdefghijklmnopqrstuvwxyz1234567890";
if(startProcess == "Y")
{
permutation(str, sizeof(str)-1, rArray, 0, result);
}
else
{
std::cout << "\n\nOperation Terminated. No permutations being generated..." << endl;
}
cin.get();
return EXIT_SUCCESS;
}
|