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
|
#include <ctime>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
// Global Gen Var
boost::mt19937 gen;
// Prototype
int rand (int, int);
void get_words (std::vector<std::string> &);
void jumble (std::vector<std::string> &);
void jumble (std::string &);
void swap (char &, char &);
void print (const std::vector<std::string> &);
int main ()
{
gen.seed( (unsigned)std::time(0) );
std::vector<std::string> sentence;
get_words(sentence);
jumble(sentence);
print(sentence);
}
int rand(int min, int max)
{
boost::uniform_int<> dist(min, max);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> > rnd(gen, dist);
return rnd();
}
void get_words (std::vector<std::string> &words)
{
std::ifstream fin("jumble.txt");
if (!fin)
{
std::cerr << "Cannot find file \'jumble.txt\'. Please check directory.";
//throw std::invalid_argument;
}
std::string temp;
while ( fin >> temp )
{
words.push_back(temp);
}
fin.close();
return;
}
void jumble (std::vector<std::string> &words)
{
for (std::vector<std::string>::iterator it = words.begin();
it != words.end(); ++it)
{
if (it->length() > 2)
jumble(*it);
}
return;
}
void jumble (std::string &str)
{
for (size_t i = 0; i < str.length()-1; ++i)
{
size_t k = rand(1, str.length()-2), j = rand(1, str.length()-2);
if ( k != j )
swap(str[k], str[j]);
}
return;
}
void swap(char &a, char &b)
{
char temp = a;
a = b;
b = temp;
return;
}
void print (const std::vector<std::string> &words)
{
std::ostream_iterator<std::string> out_it (std::cout, " ");
std::copy ( words.begin(), words.end(), out_it );
return;
}
|