Skip to content

Instantly share code, notes, and snippets.

@r0mdau
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r0mdau/38886af40c68a64bf3da to your computer and use it in GitHub Desktop.
Save r0mdau/38886af40c68a64bf3da to your computer and use it in GitHub Desktop.
Calculateur d'anagramme en c++. Charge un dictionnaire en mémoire et calcule autant d'anagrammes que l'on souhaite
#include <iostream>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <list>
using namespace std;
bool sontAnagrammes( const char * c_saisie , const char * c_mot )
{
string caracters( c_saisie );
string mot( c_mot );
sort( caracters.begin() , caracters.end() );
sort( mot.begin() , mot.end() );
return caracters == mot;
}
int main()
{
ifstream fichier("french.dic", ios::in);
list<string> dico;
if(fichier)
{
string mot;
while(getline(fichier, mot)){
dico.push_back(mot);
}
fichier.close();
} else {
cerr << "Impossible d'ouvrir le fichier !" << endl;
}
while(true){
string caracters;
cout << endl << "Votre mot : ";
cin >> caracters;
if(caracters == "exit") break;
for(list<string>::const_iterator iterator = dico.begin(), end = dico.end(); iterator != end; ++iterator){
string mot = *iterator;
if(sontAnagrammes(caracters.c_str(), mot.c_str())){
cout << endl << mot.c_str();
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment