#include <conio.h> //au lieu de #include <conio.c> fprintf(File, "%s\n%s\n", mail , mail );// enregistre dans le fichier en désordre //l'option -u permet de gérer l'unicité system("sort -u mail.txt>>ordre.txt");//classe par ordre alpha
#include #include #include <set> #include <cstdlib> #include <fstream> /*! ** Get a random number between min and max ** ** @param min The lower limit ** @param max The upper limit ** ** @return A random number */ int randi(int min, int max) { return (min + (rand() % (max - min + 1))); } /*! ** Generate mail with a given size ** ** @param longueur The size of the mail ** ** @return A random mail */ std::string generateMail(int longueur) { char mail[32] = {0}; for (int j = 0; j < longueur; ++j) switch (randi(1, 2)) { case 1: mail[j] = randi('-', '.'); break; case 2: mail[j] = randi('a', 'z'); break; } return mail; } /*! ** Fill a set with random mails ** ** @param mail The mail list ** @param nb Number of mail to add ** @param longueur Size of each mail */ void genMails(std::set<std::string>& mail, int nb, int longueur) { for (int i = 1; i <= nb; ++i) mail.insert(generateMail(longueur)); } /*! ** Display mails ** ** @param mail The mail list */ void displayMail(const std::set<std::string>& mail) { typedef std::set<std::string>::const_iterator iter; for (iter it = mail.begin(); it != mail.end(); ++it) std::cout << *it << std::endl; } /*! ** Copy each mail into a file ** ** @param filename The file where to copy mail ** @param mail The mail list */ void copyMailIntoFile(const std::string& filename, const std::set<std::string>& mail) { typedef std::set<std::string>::const_iterator iter; std::ofstream file(filename.c_str(), std::ios::out | std::ios::trunc); if (!file) { std::cerr << "Impossible d'écrire dans le fichier" << std::endl; return; } for (iter it = mail.begin(); it != mail.end(); ++it) file<< *it << std::endl; file.close(); } /*! ** The entry point :) ** ** @return 0 */ int main() { int number = 0; std::set<std::string> mail; const std::string filename = "mail.txt"; srand(time(NULL)); std::cout << "Nombre de mail à générer" << std::endl; std::cin >> number; genMails(mail, number, 7); // displayMail(mail); copyMailIntoFile(filename, mail); return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question