Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <fstream>
  6. #include <cassert>
  7. #include <cctype>
  8. #include <sstream>
  9.  
  10. template<typename T>
  11. void entree_securisee(T & variable)
  12. {
  13. while (!(std::cin >> variable))
  14. {
  15. std::cout << "Entrée invalide. Recommence." << std::endl;
  16. std::cin.clear();
  17. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  18. }
  19. }
  20.  
  21. template <typename T, typename Predicat>
  22. void entree_securisee(T & variable, Predicat predicat)
  23. {
  24. while (!(std::cin >> variable) || !predicat(variable))
  25. {
  26. std::cout << "Entrée invalide. Recommence." << std::endl;
  27. std::cin.clear();
  28. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  29. }
  30. }
  31.  
  32. enum Commande {afficher_, enregister_, charger_, quitter_, ajouter_};
  33.  
  34. typedef struct Album
  35. {
  36. std::string nom;
  37. }Album;
  38.  
  39. typedef struct Chanteur
  40. {
  41. std::string nom;
  42. }Chanteur;
  43.  
  44. typedef struct Musique
  45. {
  46. Chanteur artiste;
  47. Album album;
  48. std::string nom;
  49. }Musique;
  50.  
  51. void delSpaces(std::string & text)
  52. {
  53. auto firstNonSpace { std::find_if_not(std::begin(text), std::end(text), isspace) };
  54. text.erase(std::begin(text), firstNonSpace);
  55.  
  56. std::reverse(std::begin(text), std::end(text));
  57.  
  58. firstNonSpace = std::find_if_not(std::begin(text), std::end(text), isspace) ;
  59. text.erase(std::begin(text), firstNonSpace);
  60.  
  61. std::reverse(std::begin(text), std::end(text));
  62. }
  63.  
  64. std::ostream & operator<<(std::ostream & sortie, Chanteur const & artiste)
  65. {
  66. sortie << artiste.nom;
  67. return sortie;
  68. }
  69.  
  70. std::ostream & operator<<(std::ostream & sortie, Album const & album)
  71. {
  72. sortie << album.nom;
  73. return sortie;
  74. }
  75.  
  76. std::ostream & operator<<(std::ostream & sortie, Musique const & morceau)
  77. {
  78. sortie << morceau.nom << " | " << morceau.album << " | " << morceau.artiste;
  79. return sortie;
  80. }
  81.  
  82. std::istream & operator>>(std::istream & entree, Musique & morceau)
  83. {
  84. std::string mot{};
  85. std::ostringstream flux{};
  86.  
  87. // Récupération du nom du morceau.
  88. while (entree >> mot && mot != "|")
  89. {
  90. flux << mot << " ";
  91. }
  92.  
  93. std::string nom_morceau{ flux.str() };
  94. if (nom_morceau == "")
  95. {
  96. nom_morceau = "Morceau inconnu";
  97. }
  98. delSpaces(nom_morceau);
  99. morceau.nom = nom_morceau;
  100. flux.str(std::string{});
  101.  
  102. // Récupération du nom de l'album.
  103. while (entree >> mot && mot != "|")
  104. {
  105. flux << mot << " ";
  106. }
  107.  
  108. std::string nom_album{ flux.str() };
  109. if (nom_album == "")
  110. {
  111. nom_album = "Album inconnu";
  112. }
  113. delSpaces(nom_album);
  114. morceau.album.nom = nom_album;
  115. flux.str(std::string{});
  116.  
  117. // Récupération du nom de l'artiste.
  118. while (entree >> mot)
  119. {
  120. flux << mot << " ";
  121. }
  122.  
  123. std::string nom_artiste{ flux.str() };
  124. if (nom_artiste == "")
  125. {
  126. nom_artiste = "Artiste inconnu";
  127. }
  128. delSpaces(nom_artiste);
  129. morceau.artiste.nom = nom_artiste;
  130. flux.str(std::string{});
  131.  
  132. return entree;
  133. }
  134.  
  135.  
  136. void ajouter(std::string const& restEntree, std::vector <Musique> & disco)
  137. {
  138. Musique morceau;
  139. std::istringstream istr (restEntree);
  140.  
  141. istr >> morceau;
  142.  
  143. disco.push_back(morceau);
  144. }
  145.  
  146. void afficher_morceau(std::vector <Musique> & discographie)
  147. {
  148. for(int i {0} ; i < discographie.size() ; i++ ){
  149. std::cout << "--> " << discographie[i] << std::endl;
  150. }
  151. }
  152.  
  153. void afficher_album(std::vector <Musique> & discographie)
  154. {
  155. std::string nomAlbum = "";
  156.  
  157. for(int i {0} ; i < discographie.size() ; i++ ){
  158. if(discographie[i].album.nom != nomAlbum){
  159. std::cout << "--> " << discographie[i].album.nom << " | " << discographie[i].artiste << std::endl;
  160. nomAlbum = discographie[i].album.nom;
  161. }
  162. std::cout << " /--> " << discographie[i].nom << std::endl;
  163. }
  164. }
  165.  
  166. void afficher_artiste(std::vector <Musique> & discographie)
  167. {
  168. std::string nomArtiste = "";
  169. std::string nomAlbum = "";
  170.  
  171. for(int i {0} ; i < discographie.size() ; i++ ){
  172. if(discographie[i].artiste.nom != nomArtiste){
  173. std::cout << "--> " << discographie[i].artiste << std::endl;
  174. nomArtiste = discographie[i].artiste.nom;
  175. }
  176.  
  177. if(discographie[i].album.nom != nomAlbum){
  178. std::cout << " --> "<< discographie[i].album << std::endl;
  179. nomAlbum = discographie[i].album.nom ;
  180. }
  181. std::cout << " --> "<< discographie[i].nom << std::endl;
  182. }
  183. }
  184.  
  185. void afficher(std::string entree, std::vector <Musique> & discographie)
  186. {
  187. delSpaces(entree);
  188. std::sort(std::begin(discographie), std::end(discographie), [](Musique a , Musique b) -> bool
  189. {
  190. if(a.artiste.nom != b.artiste.nom){
  191. return (a.artiste.nom < b.artiste.nom);
  192. }
  193. else if(a.album.nom != b.album.nom){
  194. return (a.album.nom < b.album.nom);
  195. }
  196. else{
  197. return (a.nom < b.nom);
  198. }
  199. });
  200.  
  201. if(entree == "morceau"){
  202. afficher_morceau(discographie);
  203. }
  204.  
  205. if(entree == "album"){
  206. afficher_album(discographie);
  207. }
  208.  
  209. if(entree == "artiste"){
  210. afficher_album(discographie);
  211. }
  212. }
  213.  
  214. void enregister(std::string restEntree, std::vector<Musique> & discographie)
  215. {
  216. std::ofstream truc {restEntree};
  217.  
  218. for(int i {0} ; i < discographie.size() ; i++ ){
  219. truc << discographie[i].nom << " | " << discographie[i].album.nom << " | " << discographie[i].artiste.nom << std::endl;
  220. }
  221. }
  222.  
  223. void charger(std::string restEntree, std::vector<Musique> &discographie){
  224.  
  225. std::ifstream fichier { restEntree };
  226. std::string ligne;
  227.  
  228. while( std::getline(fichier, ligne) ){
  229.  
  230. auto FirstSeparateur { std::find( std::begin(ligne), std::end(ligne), '|') };
  231. std::string nomMorceau {std::begin(ligne), FirstSeparateur};
  232.  
  233. delSpaces(nomMorceau);
  234.  
  235. auto SecondSeparateur {std::find(++FirstSeparateur, std::end(ligne), '|')} ;
  236. std::string nomAlbum {FirstSeparateur , SecondSeparateur};
  237.  
  238. delSpaces(nomAlbum);
  239.  
  240. std::string nomArtiste {++SecondSeparateur , std::end(ligne) };
  241.  
  242. delSpaces(nomArtiste);
  243.  
  244. discographie.push_back( {nomArtiste, nomAlbum, nomMorceau});
  245. }
  246. }
  247.  
  248. void analyseCommande (std::string entree, std::vector<Musique> & discographie, bool & arret)
  249. {
  250. delSpaces(entree);
  251. std::string commande { std::begin(entree), std::find( std::begin(entree), std::end(entree), ' ') };
  252. delSpaces(commande);
  253. std::string restEntree { std::find( std::begin(entree), std::end(entree), ' ') , std::end(entree) };
  254. delSpaces(restEntree);
  255.  
  256. if (commande == "ajouter"){
  257. ajouter(restEntree, discographie);
  258. std::cout << discographie.size();
  259. }
  260. else if (commande == "enregister"){
  261. enregister(restEntree, discographie);
  262. }
  263. else if (commande == "charger"){
  264. charger(restEntree, discographie);
  265. }
  266. else if (commande == "quitter"){
  267. arret = true;
  268. }
  269. else if (commande == "afficher"){
  270. afficher(restEntree, discographie);
  271. }
  272. else{
  273. std::cout << "Commande invalide";
  274. }
  275.  
  276. std::cout << "\n\n";
  277. }
  278.  
  279. int main()
  280. {
  281. std::vector <Musique> discographie;
  282.  
  283. bool stop = false;
  284. std::string entree;
  285.  
  286. do{
  287. entree_securisee(entree);
  288. analyseCommande(entree, discographie, stop);
  289.  
  290. }while(!stop);
  291.  
  292. return 0;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement