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
|
#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <fstream>
#include <algorithm>
// Foncteur qui effectue un chiffrement par decalage
class ChiffrementSubst{
public:
ChiffrementSubst(const std::string& nomFichier):nom_fichier(nomFichier),tableauCopy()
{
std::ifstream fichier(nom_fichier.c_str());
while(fichier)
{
fichier>>cle;
fichier>>valeur;
tableauCopy[cle] = valeur;
}
}
char operator ()(char lettre)
{
for(std::map<char,char>::iterator it = tableauCopy.begin();it != tableauCopy.end();++it)
{
if(lettre == cle)
{
lettre = valeur;
}
return lettre;
}
}
private:
std::string nom_fichier;
char cle, valeur;
std::map<char,char>tableauCopy;
};
int main()
{
// Le message a crypter
std::string texte("BIENVENUE SUR LE MOOC C++ D'OCR !!");
std::cout << "Quel fichier contenant la cle voulez-vous utiliser ? ";
std::string nomFichier;
std::cin >> nomFichier;
ChiffrementSubst foncteur(nomFichier);
// Chaine de caracteres pour le message crypter
std::string texte_crypte;
transform(texte.begin(),texte.end(),texte_crypte.begin(),foncteur);
std::cout<<texte_crypte<<std::endl;
return 0;
} |
Partager