Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 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(std::string entree, std::vector <Musique> & discographie)
  147. {
  148. delSpaces(entree);
  149.  
  150. if(entree == "morceau"){
  151. std::sort(std::begin(discographie), std::end(discographie), [](Musique a , Musique b) -> bool
  152. {
  153. return (a.nom < b.nom);
  154. });
  155.  
  156. for(int i {0} ; i < discographie.size() ; i++ ){
  157. std::cout << "--> " << discographie[i] << std::endl;
  158. }
  159. }
  160.  
  161. if(entree == "album"){
  162. std::sort(std::begin(discographie), std::end(discographie), [](Musique a , Musique b) -> bool
  163. {
  164. if(a.album.nom != b.album.nom){
  165. return (a.album.nom < b.album.nom);
  166. }
  167. else{
  168. return (a.nom < b.nom);
  169. }
  170. });
  171.  
  172. std::string nomAlbum = "";
  173.  
  174. for(int i {0} ; i < discographie.size() ; i++ ){
  175. if(discographie[i].album.nom != nomAlbum){
  176. std::cout << "--> " << discographie[i].album.nom << " | " << discographie[i].artiste << std::endl;
  177.  
  178. nomAlbum = discographie[i].album.nom;
  179. }
  180. std::cout << " /--> " << discographie[i].nom << std::endl;
  181. }
  182. }
  183.  
  184. if(entree == "artiste"){
  185. std::sort(std::begin(discographie), std::end(discographie), [](Musique a , Musique b) -> bool
  186. {
  187. if(a.artiste.nom != b.artiste.nom){
  188. return (a.artiste.nom < b.artiste.nom);
  189. }
  190. else if(a.album.nom != b.album.nom){
  191. return (a.album.nom < b.album.nom);
  192. }
  193. else{
  194. return (a.nom < b.nom);
  195. }
  196. });
  197. std::string nomArtiste = "";
  198.  
  199. std::string nomAlbum = "";
  200.  
  201. for(int i {0} ; i < discographie.size() ; i++ ){
  202. if(discographie[i].artiste.nom != nomArtiste){
  203. std::cout << "--> " << discographie[i].artiste << std::endl;
  204. nomArtiste = discographie[i].artiste.nom;
  205. }
  206.  
  207. if(discographie[i].album.nom != nomAlbum){
  208. std::cout << " --> "<< discographie[i].album << std::endl;
  209. nomAlbum = discographie[i].album.nom ;
  210. }
  211. std::cout << " --> "<< discographie[i].nom << std::endl;
  212. }
  213. }
  214. }
  215.  
  216. void enregister(std::string restEntree, std::vector<Musique> & discographie)
  217. {
  218. std::ofstream truc {restEntree};
  219.  
  220. for(int i {0} ; i < discographie.size() ; i++ ){
  221. truc << discographie[i].nom << " | " << discographie[i].album.nom << " | " << discographie[i].artiste.nom << std::endl;
  222. }
  223. }
  224.  
  225. void charger(std::string restEntree, std::vector<Musique> &discographie){
  226.  
  227. std::ifstream fichier { restEntree };
  228. std::string ligne;
  229.  
  230. while( std::getline(fichier, ligne) ){
  231.  
  232. auto FirstSeparateur { std::find( std::begin(ligne), std::end(ligne), '|') };
  233. std::string nomMorceau {std::begin(ligne), FirstSeparateur};
  234.  
  235. delSpaces(nomMorceau);
  236.  
  237. auto SecondSeparateur {std::find(++FirstSeparateur, std::end(ligne), '|')} ;
  238. std::string nomAlbum {FirstSeparateur , SecondSeparateur};
  239.  
  240. delSpaces(nomAlbum);
  241.  
  242. std::string nomArtiste {++SecondSeparateur , std::end(ligne) };
  243.  
  244. delSpaces(nomArtiste);
  245.  
  246. discographie.push_back( {nomArtiste, nomAlbum, nomMorceau});
  247. }
  248. }
  249.  
  250. void analyseCommande (std::string entree, std::vector<Musique> & discographie)
  251. {
  252. delSpaces(entree);
  253. std::string commande { std::begin(entree), std::find( std::begin(entree), std::end(entree), ' ') };
  254. delSpaces(commande);
  255. std::string restEntree { std::find( std::begin(entree), std::end(entree), ' ') , std::end(entree) };
  256. delSpaces(restEntree);
  257.  
  258. if (commande == "ajouter"){
  259. ajouter(restEntree, discographie);
  260. }
  261. else if (commande == "enregister"){
  262. enregister(restEntree, discographie);
  263. }
  264. else if (commande == "charger"){
  265. charger(restEntree, discographie);
  266. }
  267. else if (commande == "quitter"){
  268.  
  269. }
  270. else if (commande == "afficher"){
  271. afficher(restEntree, discographie);
  272. }
  273. else{
  274. std::cout << "Commande invalide";
  275. }
  276. }
  277.  
  278. int main()
  279. {
  280. std::vector <Musique> discographie;
  281.  
  282. std::string commande {"ajouter machin | truc | bidule"};
  283.  
  284. analyseCommande(commande, discographie);
  285.  
  286. commande = "afficher morceau";
  287.  
  288. analyseCommande(commande, discographie);
  289. std::cout << "\n\n";
  290.  
  291. commande = "enregister machin.disc";
  292.  
  293. analyseCommande(commande, discographie);
  294.  
  295. commande = "charger truc.disc";
  296.  
  297. analyseCommande(commande, discographie);
  298.  
  299. commande = "afficher morceau";
  300.  
  301. analyseCommande(commande, discographie);
  302. std::cout << "\n\n";
  303.  
  304. commande = "afficher album";
  305.  
  306. analyseCommande(commande, discographie);
  307. std::cout << "\n\n";
  308.  
  309.  
  310. commande = "afficher artiste";
  311.  
  312. analyseCommande(commande, discographie);
  313.  
  314. return 0;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement