Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.36 KB | None | 0 0
  1. #include <map>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <conio.h>
  5. #include <stack>
  6. #include <stdlib.h>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. class track
  15. {
  16. private:
  17.     string convertToString(int i)
  18.     {
  19.         string res;
  20.        
  21.         stack <char> digit;
  22.  
  23.         if (i < 0)
  24.         {
  25.             res = "-";
  26.             i = -i;
  27.         }
  28.         else
  29.             if (i == 0)
  30.             {
  31.                 res = '0';
  32.                 return res;
  33.             }
  34.  
  35.         i = abs(i);
  36.  
  37.         while (i > 0)
  38.         {
  39.             digit.push(i % 10 + 48);
  40.             i = i / 10;
  41.         }
  42.  
  43.         while (!digit.empty())
  44.         {
  45.             res += digit.top();
  46.             digit.pop();
  47.         }
  48.         return res;
  49.     }
  50. public:
  51.  
  52.     string track_name;
  53.     string performer;
  54.     int int_year_of_execution;
  55.     string string_year_of_execution;
  56.  
  57.     track()
  58.     {
  59.         performer = "unknown";
  60.         track_name = "unknown";
  61.         string_year_of_execution = "-";
  62.         int_year_of_execution = -1;
  63.     };
  64.  
  65.     track(string strInTrackList)
  66.     {
  67.         performer = "";
  68.         track_name = "";
  69.         string_year_of_execution = "-";
  70.  
  71.         int i = 0;
  72.         int end = strInTrackList.length();
  73.         int gapFlag = 1;       
  74.         while ((gapFlag == 1) && (i < end))
  75.         {
  76.  
  77.             if ((strInTrackList[i] == ' ') && (strInTrackList[i + 1] == '-'))
  78.             {
  79.                 gapFlag = 2;
  80.                 i += 3;
  81.                 break;
  82.             }
  83.  
  84.             if ((strInTrackList[i] == ' ') && (strInTrackList[i + 1] == '['))
  85.             {
  86.                 gapFlag = 3;;
  87.                 i += 2;
  88.                 break;
  89.             }
  90.  
  91.             performer += strInTrackList[i];
  92.             i++;
  93.         }
  94.  
  95.         while ((gapFlag == 2) && (i < end))
  96.         {
  97.  
  98.             if ((strInTrackList[i] == ' ') && (strInTrackList[i + 1] == '['))
  99.             {
  100.                 gapFlag = 3;;
  101.                 i += 2;
  102.                 break;
  103.             }
  104.             track_name += strInTrackList[i];
  105.             i++;
  106.         }
  107.  
  108.         char year[5] = { '-', '-', '-', '-', '\0' };
  109.         int j = 0;
  110.         while ((gapFlag == 3) && (i < end))
  111.         {
  112.             if (strInTrackList[i] == ']')
  113.             {
  114.                 //только что считалась скобка, задан ли был год?
  115.                 if ((j == 0) || ((j == 1) && (year[0] == '-')))
  116.                 {
  117.                     int_year_of_execution = -1;
  118.                     string_year_of_execution = "-";
  119.                     break;
  120.                 }
  121.                 else
  122.                 {
  123.                     if (atoi(year) > 999)
  124.                     {
  125.                         int_year_of_execution = atoi(year);
  126.                         string_year_of_execution = year;
  127.                         break;
  128.                     }
  129.                     else
  130.                     {
  131.                         int_year_of_execution = -2;
  132.                         string_year_of_execution = "input error";
  133.                         break;
  134.                     }
  135.                 }
  136.             }
  137.  
  138.             if (j < 4)
  139.             {
  140.                 year[j] = strInTrackList[i];
  141.                 j++;
  142.                 i++;
  143.             }
  144.             else
  145.             {
  146.  
  147.                 int_year_of_execution = -2;
  148.                 string_year_of_execution = "input error";
  149.                 break;
  150.             }
  151.  
  152.         }
  153.  
  154.         if (track_name == "")
  155.         {
  156.             track_name = performer;
  157.             performer = "unknown";
  158.         }
  159.  
  160.     }
  161.  
  162.     track(string in_performer, string in_track_name, int in_year_of_execution)
  163.     {
  164.         performer = in_performer;
  165.         track_name = in_track_name;
  166.         int_year_of_execution = in_year_of_execution;
  167.         string_year_of_execution = convertToString(in_year_of_execution);
  168.     };
  169.  
  170.     string trackInString()
  171.     {
  172.         string track;
  173.         track = performer + " - " + track_name + " [" + string_year_of_execution + "]";
  174.         return track;
  175.     }
  176.  
  177.     void showTrack()
  178.     {
  179.         cout << trackInString() << '\n';
  180.     }
  181. };
  182.  
  183. class tracks_map
  184. {
  185. private:;
  186. public:
  187.    
  188.     map <string, track> track_title;
  189.  
  190.     void getTrackTitle(track in)
  191.     {
  192.         track_title[in.track_name] = in;
  193.     };
  194.  
  195.     void showArtistTrackSet()
  196.     {
  197.         for (auto iter = track_title.begin(); iter != track_title.end(); iter++)
  198.         {
  199.             iter->second.showTrack();
  200.         }
  201.     }  
  202. };
  203.  
  204. class artists_map
  205. {
  206. public:
  207.  
  208.     map <string, tracks_map> artistsMap;
  209.  
  210.     void getArtistsMap()
  211.     {
  212.         string strTrack;
  213.         ifstream in_track_list("track_list.txt");
  214.         while (in_track_list.peek() != EOF)
  215.         {
  216.             getline(in_track_list, strTrack);
  217.             track in_track(strTrack);
  218.             artistsMap[in_track.performer].getTrackTitle(in_track);
  219.  
  220.         }
  221.         in_track_list.close();
  222.     }
  223.  
  224.     void showNameArtistsMap()
  225.     {
  226.         for (auto iter = artistsMap.begin(); iter != artistsMap.end(); iter++)
  227.         {
  228.             cout << iter->first;
  229.             cout << '\n';
  230.         }
  231.     }
  232.  
  233.     void showBoxArtistsMap()
  234.     {
  235.         for (auto iter = artistsMap.begin(); iter != artistsMap.end(); iter++)
  236.         {
  237.             iter->second.showArtistTrackSet();
  238.             cout << '\n';
  239.         }
  240.     }  
  241.  
  242.     void showArtistOnlySet(string name)
  243.     {      
  244.         if (artistsMap.find(name) != artistsMap.end())
  245.         {
  246.             artistsMap[name].showArtistTrackSet();
  247.         }
  248.         else
  249.         {
  250.             cout << "No this musician" << "\n";
  251.            
  252.         }
  253.     };
  254.  
  255.     void renameArtist(string oldname, string newname)
  256.     {      
  257.        
  258.         for (auto iter = artistsMap[oldname].track_title.begin(); iter != artistsMap[oldname].track_title.end(); iter++)
  259.         {
  260.             iter->second.performer = newname;
  261.         }
  262.        
  263.         for (auto iter = artistsMap[oldname].track_title.begin(); iter != artistsMap[oldname].track_title.end(); iter++)
  264.         {
  265.             artistsMap[iter->second.performer].getTrackTitle(iter->second);
  266.         }      
  267.        
  268.         artistsMap.erase(oldname); 
  269.        
  270.     }
  271.  
  272.    
  273. private:;
  274.  
  275. };
  276.  
  277. class Music_player
  278. {
  279. public:
  280.     artists_map player;
  281.  
  282.     void console_getline(string& get)
  283.     {
  284.         string ts;
  285.         cin >> get;
  286.         getline(cin, ts);
  287.         get += ts;
  288.     }
  289.  
  290.     void inTimeYearLimit(int a, int b)
  291.     {
  292.         if (a > b)
  293.         {
  294.             int c = b;
  295.             b = a;
  296.             a = c;
  297.         }
  298.  
  299.         for (auto iter = player.artistsMap.begin(); iter != player.artistsMap.end(); iter++)
  300.         {
  301.             for (auto iter2 = iter->second.track_title.begin(); iter2 != iter->second.track_title.end(); iter2++)
  302.             {
  303.  
  304.                 if ((iter2->second.int_year_of_execution >= a) && (iter2->second.int_year_of_execution <= b))
  305.                 {
  306.                     iter2->second.showTrack();
  307.                 }
  308.             }
  309.         }
  310.     }
  311.  
  312.     bool interface_player()
  313.     {
  314.  
  315.         cout << "enter the number of the action:" << "\n";
  316.         cout << "1. show artist list" << "\n";
  317.         cout << "2. show track list" << "\n";
  318.         cout << "3. show artist songs" << "\n";
  319.         cout << "4. change the stage name of the musician" << "\n";
  320.         cout << "5. remove the artist and his music" << "\n";
  321.         cout << "6. add track" << "\n";
  322.         cout << "7. find music from a time interval" << "\n";
  323.         cout << "8. exit" << "\n";
  324.  
  325.         char key;
  326.         cin >> key;
  327.  
  328.         switch (key)
  329.         {
  330.  
  331.         case '1':
  332.         {
  333.  
  334.             system("CLS");
  335.             player.showNameArtistsMap();
  336.             cout << "\n";
  337.             return 1;
  338.         }
  339.         case '2':
  340.         {
  341.  
  342.             system("CLS");
  343.             player.showBoxArtistsMap();
  344.             cout << "\n";
  345.             return 1;
  346.         }
  347.         case '3':
  348.         {
  349.             system("CLS");
  350.             string name;
  351.             player.showNameArtistsMap();
  352.             cout << "\n";
  353.             cout << "enter the name of the musician: ";
  354.             console_getline(name);
  355.             system("CLS");
  356.             cout << "enter the name of the musician: " << name << "\n";
  357.             player.showArtistOnlySet(name);
  358.             cout << "\n";
  359.             return 1;
  360.         }
  361.         case '4':
  362.         {
  363.  
  364.         system("CLS");
  365.         string oldname, newname;
  366.         player.showNameArtistsMap();
  367.         cout << "\n";
  368.         cout << "enter the old name of the musician: ";
  369.         console_getline(oldname);
  370.         cout << "\n";
  371.         cout << "enter the new name of the musician: ";
  372.         console_getline(newname);
  373.         cout << "\n";
  374.         player.renameArtist(oldname, newname);
  375.         return 1;
  376.         }
  377.         case '5':
  378.         {
  379.         system("CLS");
  380.         string name;
  381.         player.showNameArtistsMap();
  382.         cout << "\n";
  383.         cout << "enter the name of the musician: ";
  384.         console_getline(name);
  385.         cout << "\n";
  386.         player.artistsMap.erase(name);
  387.         return 1;
  388.         }
  389.         case '6':
  390.         {
  391.         system("CLS");
  392.         string newTrackIn;
  393.        
  394.         cout << "enter the track name in this format:" << "\n";
  395.         cout << "<<artist name>> - <<track name>> [<<year of creation>>]" << "\n";
  396.         cout << "If there is no information about the artist or song title," << "\n"<< "please write \"unknown\"" << "\n";
  397.         cout << "If you do not know the year, it is possible to write nothing or to put \"[]\"" << "\n";
  398.         cout << "for example:" << "\n" << "Edguy - King of Fools [2004]" << "\n";
  399.         cout << "unknown - Get Lucky []" << "\n\n";
  400.  
  401.         console_getline(newTrackIn);
  402.         track addTrack(newTrackIn);
  403.         player.artistsMap[addTrack.performer].getTrackTitle(addTrack);
  404.         return 1;
  405.         }
  406.         case '7':
  407.         {
  408.  
  409.             system("CLS");
  410.             int a, b;
  411.             cout << "\n";
  412.             cout << "enter first border: ";
  413.             cin >> a;          
  414.             cout << "enter second border: ";
  415.             cin >> b;
  416.             cout << "\n";
  417.             inTimeYearLimit(a, b);
  418.             cout << "\n";
  419.             return 1;
  420.         }
  421.         case '8':
  422.         {
  423.             return 0;
  424.         }
  425.  
  426.         default: //пользователь ввёл несуществующие вариант
  427.  
  428.             system("CLS");
  429.             cout << "Invalid input" << '\n';
  430.             return 1;
  431.  
  432.         }
  433.     }
  434.    
  435. private:;
  436. };
  437.  
  438.  
  439. int main()
  440. {
  441.     Music_player myPlayer;
  442.     myPlayer.player.getArtistsMap();
  443.  
  444.     while (myPlayer.interface_player())
  445.     {
  446.         if (!myPlayer.interface_player())
  447.             return 0;
  448.     }
  449.  
  450.     return 0;
  451.  
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement