Skip to content

Instantly share code, notes, and snippets.

@soltys
Created May 29, 2011 18:20
Show Gist options
  • Save soltys/998005 to your computer and use it in GitHub Desktop.
Save soltys/998005 to your computer and use it in GitHub Desktop.
WordGenerator
#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