Created
May 29, 2011 18:20
-
-
Save soltys/998005 to your computer and use it in GitHub Desktop.
WordGenerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WordGenerator.h" | |
#include <algorithm> | |
void WordGenerator::createAlphabet(char* range) | |
{ | |
char start,end; | |
while(*range != '\0') //Koniec stringu | |
{ | |
if(*(range+1) == '-'){ | |
alphabet.push_back(*range); //Wpisywanie danych które sa swobodne | |
start=*range++; //przechwytanie ostatniego swobodnego | |
//jako poczatek zakresu | |
} | |
else{ | |
alphabet.push_back(*range++); | |
} | |
if(*range=='-') { | |
end = *(range+1); | |
if(start > end) | |
while(--start >= end) | |
alphabet.push_back(start); | |
else | |
while(++start <= end) | |
alphabet.push_back(start); | |
start=end; | |
range +=2; //Przeskok przez - i znak konca zakresu | |
} | |
} | |
} | |
std::string WordGenerator::getCurrent() | |
{ | |
return currentWord; | |
} | |
char WordGenerator::getNextLetterAlphabet(char after) | |
{ | |
std::vector<char>::iterator it; | |
it = std::find(alphabet.begin(),alphabet.end(),after); | |
++it; | |
return *it; | |
} | |
void WordGenerator::insertFrontCharacter(std::string& word) | |
{ | |
word.insert(word.begin(),firstLetter); | |
} | |
void WordGenerator::changeWord(std::string& word) | |
{ | |
std::string::reverse_iterator rbegin = word.rbegin(); | |
std::string::reverse_iterator rend = word.rend(); | |
bool carry = true; | |
while(carry) | |
{ | |
if(rbegin < rend) | |
{ | |
if(*rbegin == lastLetter) | |
{ | |
*rbegin = firstLetter; | |
carry = true; | |
} | |
else | |
{ | |
*rbegin = getNextLetterAlphabet(*rbegin); | |
carry = false; | |
} | |
} | |
else | |
{ | |
insertFrontCharacter(word); | |
return; | |
} | |
rbegin++; | |
} | |
} | |
std::string WordGenerator::generateNext() | |
{ | |
std::string newWord = currentWord; | |
changeWord(newWord); | |
currentWord = newWord; | |
return currentWord; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment