Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <fstream>
- #include <cassert>
- #include <cctype>
- #include <sstream>
- template<typename T>
- void entree_securisee(T & variable)
- {
- while (!(std::cin >> variable))
- {
- std::cout << "Entrée invalide. Recommence." << std::endl;
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- }
- template <typename T, typename Predicat>
- void entree_securisee(T & variable, Predicat predicat)
- {
- while (!(std::cin >> variable) || !predicat(variable))
- {
- std::cout << "Entrée invalide. Recommence." << std::endl;
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- }
- enum Commande {afficher_, enregister_, charger_, quitter_, ajouter_};
- typedef struct Album
- {
- std::string nom;
- }Album;
- typedef struct Chanteur
- {
- std::string nom;
- }Chanteur;
- typedef struct Musique
- {
- Chanteur artiste;
- Album album;
- std::string nom;
- }Musique;
- void delSpaces(std::string & text)
- {
- auto firstNonSpace { std::find_if_not(std::begin(text), std::end(text), isspace) };
- text.erase(std::begin(text), firstNonSpace);
- std::reverse(std::begin(text), std::end(text));
- firstNonSpace = std::find_if_not(std::begin(text), std::end(text), isspace) ;
- text.erase(std::begin(text), firstNonSpace);
- std::reverse(std::begin(text), std::end(text));
- }
- std::ostream & operator<<(std::ostream & sortie, Chanteur const & artiste)
- {
- sortie << artiste.nom;
- return sortie;
- }
- std::ostream & operator<<(std::ostream & sortie, Album const & album)
- {
- sortie << album.nom;
- return sortie;
- }
- std::ostream & operator<<(std::ostream & sortie, Musique const & morceau)
- {
- sortie << morceau.nom << " | " << morceau.album << " | " << morceau.artiste;
- return sortie;
- }
- std::istream & operator>>(std::istream & entree, Musique & morceau)
- {
- std::string mot{};
- std::ostringstream flux{};
- // Récupération du nom du morceau.
- while (entree >> mot && mot != "|")
- {
- flux << mot << " ";
- }
- std::string nom_morceau{ flux.str() };
- if (nom_morceau == "")
- {
- nom_morceau = "Morceau inconnu";
- }
- delSpaces(nom_morceau);
- morceau.nom = nom_morceau;
- flux.str(std::string{});
- // Récupération du nom de l'album.
- while (entree >> mot && mot != "|")
- {
- flux << mot << " ";
- }
- std::string nom_album{ flux.str() };
- if (nom_album == "")
- {
- nom_album = "Album inconnu";
- }
- delSpaces(nom_album);
- morceau.album.nom = nom_album;
- flux.str(std::string{});
- // Récupération du nom de l'artiste.
- while (entree >> mot)
- {
- flux << mot << " ";
- }
- std::string nom_artiste{ flux.str() };
- if (nom_artiste == "")
- {
- nom_artiste = "Artiste inconnu";
- }
- delSpaces(nom_artiste);
- morceau.artiste.nom = nom_artiste;
- flux.str(std::string{});
- return entree;
- }
- void ajouter(std::string const& restEntree, std::vector <Musique> & disco)
- {
- Musique morceau;
- std::istringstream istr (restEntree);
- istr >> morceau;
- disco.push_back(morceau);
- }
- void afficher_morceau(std::vector <Musique> & discographie)
- {
- for(int i {0} ; i < discographie.size() ; i++ ){
- std::cout << "--> " << discographie[i] << std::endl;
- }
- }
- void afficher_album(std::vector <Musique> & discographie)
- {
- std::string nomAlbum = "";
- for(int i {0} ; i < discographie.size() ; i++ ){
- if(discographie[i].album.nom != nomAlbum){
- std::cout << "--> " << discographie[i].album.nom << " | " << discographie[i].artiste << std::endl;
- nomAlbum = discographie[i].album.nom;
- }
- std::cout << " /--> " << discographie[i].nom << std::endl;
- }
- }
- void afficher_artiste(std::vector <Musique> & discographie)
- {
- std::string nomArtiste = "";
- std::string nomAlbum = "";
- for(int i {0} ; i < discographie.size() ; i++ ){
- if(discographie[i].artiste.nom != nomArtiste){
- std::cout << "--> " << discographie[i].artiste << std::endl;
- nomArtiste = discographie[i].artiste.nom;
- }
- if(discographie[i].album.nom != nomAlbum){
- std::cout << " --> "<< discographie[i].album << std::endl;
- nomAlbum = discographie[i].album.nom ;
- }
- std::cout << " --> "<< discographie[i].nom << std::endl;
- }
- }
- void afficher(std::string entree, std::vector <Musique> & discographie)
- {
- delSpaces(entree);
- std::sort(std::begin(discographie), std::end(discographie), [](Musique a , Musique b) -> bool
- {
- if(a.artiste.nom != b.artiste.nom){
- return (a.artiste.nom < b.artiste.nom);
- }
- else if(a.album.nom != b.album.nom){
- return (a.album.nom < b.album.nom);
- }
- else{
- return (a.nom < b.nom);
- }
- });
- if(entree == "morceau"){
- afficher_morceau(discographie);
- }
- if(entree == "album"){
- afficher_album(discographie);
- }
- if(entree == "artiste"){
- afficher_album(discographie);
- }
- }
- void enregister(std::string restEntree, std::vector<Musique> & discographie)
- {
- std::ofstream truc {restEntree};
- for(int i {0} ; i < discographie.size() ; i++ ){
- truc << discographie[i].nom << " | " << discographie[i].album.nom << " | " << discographie[i].artiste.nom << std::endl;
- }
- }
- void charger(std::string restEntree, std::vector<Musique> &discographie){
- std::ifstream fichier { restEntree };
- std::string ligne;
- while( std::getline(fichier, ligne) ){
- auto FirstSeparateur { std::find( std::begin(ligne), std::end(ligne), '|') };
- std::string nomMorceau {std::begin(ligne), FirstSeparateur};
- delSpaces(nomMorceau);
- auto SecondSeparateur {std::find(++FirstSeparateur, std::end(ligne), '|')} ;
- std::string nomAlbum {FirstSeparateur , SecondSeparateur};
- delSpaces(nomAlbum);
- std::string nomArtiste {++SecondSeparateur , std::end(ligne) };
- delSpaces(nomArtiste);
- discographie.push_back( {nomArtiste, nomAlbum, nomMorceau});
- }
- }
- void analyseCommande (std::string entree, std::vector<Musique> & discographie, bool & arret)
- {
- delSpaces(entree);
- std::string commande { std::begin(entree), std::find( std::begin(entree), std::end(entree), ' ') };
- delSpaces(commande);
- std::string restEntree { std::find( std::begin(entree), std::end(entree), ' ') , std::end(entree) };
- delSpaces(restEntree);
- if (commande == "ajouter"){
- ajouter(restEntree, discographie);
- std::cout << discographie.size();
- }
- else if (commande == "enregister"){
- enregister(restEntree, discographie);
- }
- else if (commande == "charger"){
- charger(restEntree, discographie);
- }
- else if (commande == "quitter"){
- arret = true;
- }
- else if (commande == "afficher"){
- afficher(restEntree, discographie);
- }
- else{
- std::cout << "Commande invalide";
- }
- std::cout << "\n\n";
- }
- int main()
- {
- std::vector <Musique> discographie;
- bool stop = false;
- std::string entree;
- do{
- entree_securisee(entree);
- analyseCommande(entree, discographie, stop);
- }while(!stop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement