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 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/array.hpp>
struct cesar
{
/*foncteur qui va modifier la LUT*/
struct func:
public std::unary_function<const boost::tuple<const double&, const int&>&, void>
{
func(boost::array<char,256> &caract)
:m_caract(caract)
{
}
void operator()(const boost::tuple<const char&, const char&>& p) const
{
/*cractere p.get<0>() correspond au caractere p.get<1>()*/
m_caract[p.get<0>()] =p.get<1>();
}
boost::array<char,256> &m_caract;
};
/*fonction sans table de caractere*/
cesar
(
int decalage
)
{
static const char zae[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/*-1 pour ne pas copier le caractere de fin de chaine*/
std::vector<char> caracteres(zae,zae+sizeof (zae) -1);
init(decalage,caracteres);
}
/*fonction avec table de caractere a utiliser*/
cesar
(
int decalage,
const std::vector<char> &caracteres
)
{
init(decalage,caracteres);
}
/*initialise le foncteur*/
void init
(
int decalage,
const std::vector<char> &caracteres
)
{
boost::circular_buffer<char> buff(caracteres.begin(),caracteres.end());
/*chaque caractere correspond a lui meme*/
for (int i =0;i<256;++i)
{
m_caracteres[i] =i;
}
/*modification de la LUT*/
if (decalage>0)
{
decalage %=caracteres.size();
}
else
{
decalage = -(-decalage % caracteres.size());
}
std::vector<char> tmp(caracteres.size());
std::rotate_copy(caracteres.begin(),(decalage>0 ? caracteres.begin() :caracteres.end()) +decalage,caracteres.end(),tmp.begin());
std::for_each(
boost::make_zip_iterator(
boost::make_tuple(
caracteres.begin(),
tmp.begin())
),
boost::make_zip_iterator(
boost::make_tuple(
caracteres.end(),
tmp.end())
),
func(m_caracteres));
}
char operator() (const char & c) const
{
return m_caracteres[c];
}
private :
boost::array<char,256> m_caracteres;
};
int main(int argc, char* argv[])
{
std::string s ="Salut tu va bien?\t STL + BOOST c'est vraiment la class!";
/*encodage*/
std::transform(s.begin(),s.end(),s.begin(),cesar(200));
std::cout<<s<<std::endl;
/*decodage*/
std::transform(s.begin(),s.end(),s.begin(),cesar(-200));
std::cout<<s<<std::endl;
std::cout<<std::endl;
const char zae[] = "abcd*efghijABCklmnNOPQR \top!qrstuvw?-xyzDE/FGHIJKLM+STUVWXYZ";
std::vector<char> autreTabCaracteres(zae,zae+sizeof (zae)-1);
/*encodage*/
std::transform(s.begin(),s.end(),s.begin(),cesar(200,autreTabCaracteres));
std::cout<<s<<std::endl;
/*decodage*/
std::transform(s.begin(),s.end(),s.begin(),cesar(-200,autreTabCaracteres));
std::cout<<s<<std::endl;
return 0;
} |
Partager