Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <algorithm>
- #include <iterator>
- #include <conio.h>
- #include <stack>
- #include <stdlib.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- class track
- {
- private:
- string convertToString(int i)
- {
- string res;
- stack <char> digit;
- if (i < 0)
- {
- res = "-";
- i = -i;
- }
- else
- if (i == 0)
- {
- res = '0';
- return res;
- }
- i = abs(i);
- while (i > 0)
- {
- digit.push(i % 10 + 48);
- i = i / 10;
- }
- while (!digit.empty())
- {
- res += digit.top();
- digit.pop();
- }
- return res;
- }
- public:
- string track_name;
- string performer;
- int int_year_of_execution;
- string string_year_of_execution;
- track()
- {
- performer = "unknown";
- track_name = "unknown";
- string_year_of_execution = "-";
- int_year_of_execution = -1;
- };
- track(string strInTrackList)
- {
- performer = "";
- track_name = "";
- string_year_of_execution = "-";
- int i = 0;
- int end = strInTrackList.length();
- int gapFlag = 1;
- while ((gapFlag == 1) && (i < end))
- {
- if ((strInTrackList[i] == ' ') && (strInTrackList[i + 1] == '-'))
- {
- gapFlag = 2;
- i += 3;
- break;
- }
- if ((strInTrackList[i] == ' ') && (strInTrackList[i + 1] == '['))
- {
- gapFlag = 3;;
- i += 2;
- break;
- }
- performer += strInTrackList[i];
- i++;
- }
- while ((gapFlag == 2) && (i < end))
- {
- if ((strInTrackList[i] == ' ') && (strInTrackList[i + 1] == '['))
- {
- gapFlag = 3;;
- i += 2;
- break;
- }
- track_name += strInTrackList[i];
- i++;
- }
- char year[5] = { '-', '-', '-', '-', '\0' };
- int j = 0;
- while ((gapFlag == 3) && (i < end))
- {
- if (strInTrackList[i] == ']')
- {
- //только что считалась скобка, задан ли был год?
- if ((j == 0) || ((j == 1) && (year[0] == '-')))
- {
- int_year_of_execution = -1;
- string_year_of_execution = "-";
- break;
- }
- else
- {
- if (atoi(year) > 999)
- {
- int_year_of_execution = atoi(year);
- string_year_of_execution = year;
- break;
- }
- else
- {
- int_year_of_execution = -2;
- string_year_of_execution = "input error";
- break;
- }
- }
- }
- if (j < 4)
- {
- year[j] = strInTrackList[i];
- j++;
- i++;
- }
- else
- {
- int_year_of_execution = -2;
- string_year_of_execution = "input error";
- break;
- }
- }
- if (track_name == "")
- {
- track_name = performer;
- performer = "unknown";
- }
- }
- track(string in_performer, string in_track_name, int in_year_of_execution)
- {
- performer = in_performer;
- track_name = in_track_name;
- int_year_of_execution = in_year_of_execution;
- string_year_of_execution = convertToString(in_year_of_execution);
- };
- string trackInString()
- {
- string track;
- track = performer + " - " + track_name + " [" + string_year_of_execution + "]";
- return track;
- }
- void showTrack()
- {
- cout << trackInString() << '\n';
- }
- };
- class tracks_map
- {
- private:;
- public:
- map <string, track> track_title;
- void getTrackTitle(track in)
- {
- track_title[in.track_name] = in;
- };
- void showArtistTrackSet()
- {
- for (auto iter = track_title.begin(); iter != track_title.end(); iter++)
- {
- iter->second.showTrack();
- }
- }
- };
- class artists_map
- {
- public:
- map <string, tracks_map> artistsMap;
- void getArtistsMap()
- {
- string strTrack;
- ifstream in_track_list("track_list.txt");
- while (in_track_list.peek() != EOF)
- {
- getline(in_track_list, strTrack);
- track in_track(strTrack);
- artistsMap[in_track.performer].getTrackTitle(in_track);
- }
- in_track_list.close();
- }
- void showNameArtistsMap()
- {
- for (auto iter = artistsMap.begin(); iter != artistsMap.end(); iter++)
- {
- cout << iter->first;
- cout << '\n';
- }
- }
- void showBoxArtistsMap()
- {
- for (auto iter = artistsMap.begin(); iter != artistsMap.end(); iter++)
- {
- iter->second.showArtistTrackSet();
- cout << '\n';
- }
- }
- void showArtistOnlySet(string name)
- {
- if (artistsMap.find(name) != artistsMap.end())
- {
- artistsMap[name].showArtistTrackSet();
- }
- else
- {
- cout << "No this musician" << "\n";
- }
- };
- void renameArtist(string oldname, string newname)
- {
- for (auto iter = artistsMap[oldname].track_title.begin(); iter != artistsMap[oldname].track_title.end(); iter++)
- {
- iter->second.performer = newname;
- }
- for (auto iter = artistsMap[oldname].track_title.begin(); iter != artistsMap[oldname].track_title.end(); iter++)
- {
- artistsMap[iter->second.performer].getTrackTitle(iter->second);
- }
- artistsMap.erase(oldname);
- }
- private:;
- };
- class Music_player
- {
- public:
- artists_map player;
- void console_getline(string& get)
- {
- string ts;
- cin >> get;
- getline(cin, ts);
- get += ts;
- }
- void inTimeYearLimit(int a, int b)
- {
- if (a > b)
- {
- int c = b;
- b = a;
- a = c;
- }
- for (auto iter = player.artistsMap.begin(); iter != player.artistsMap.end(); iter++)
- {
- for (auto iter2 = iter->second.track_title.begin(); iter2 != iter->second.track_title.end(); iter2++)
- {
- if ((iter2->second.int_year_of_execution >= a) && (iter2->second.int_year_of_execution <= b))
- {
- iter2->second.showTrack();
- }
- }
- }
- }
- bool interface_player()
- {
- cout << "enter the number of the action:" << "\n";
- cout << "1. show artist list" << "\n";
- cout << "2. show track list" << "\n";
- cout << "3. show artist songs" << "\n";
- cout << "4. change the stage name of the musician" << "\n";
- cout << "5. remove the artist and his music" << "\n";
- cout << "6. add track" << "\n";
- cout << "7. find music from a time interval" << "\n";
- cout << "8. exit" << "\n";
- char key;
- cin >> key;
- switch (key)
- {
- case '1':
- {
- system("CLS");
- player.showNameArtistsMap();
- cout << "\n";
- return 1;
- }
- case '2':
- {
- system("CLS");
- player.showBoxArtistsMap();
- cout << "\n";
- return 1;
- }
- case '3':
- {
- system("CLS");
- string name;
- player.showNameArtistsMap();
- cout << "\n";
- cout << "enter the name of the musician: ";
- console_getline(name);
- system("CLS");
- cout << "enter the name of the musician: " << name << "\n";
- player.showArtistOnlySet(name);
- cout << "\n";
- return 1;
- }
- case '4':
- {
- system("CLS");
- string oldname, newname;
- player.showNameArtistsMap();
- cout << "\n";
- cout << "enter the old name of the musician: ";
- console_getline(oldname);
- cout << "\n";
- cout << "enter the new name of the musician: ";
- console_getline(newname);
- cout << "\n";
- player.renameArtist(oldname, newname);
- return 1;
- }
- case '5':
- {
- system("CLS");
- string name;
- player.showNameArtistsMap();
- cout << "\n";
- cout << "enter the name of the musician: ";
- console_getline(name);
- cout << "\n";
- player.artistsMap.erase(name);
- return 1;
- }
- case '6':
- {
- system("CLS");
- string newTrackIn;
- cout << "enter the track name in this format:" << "\n";
- cout << "<<artist name>> - <<track name>> [<<year of creation>>]" << "\n";
- cout << "If there is no information about the artist or song title," << "\n"<< "please write \"unknown\"" << "\n";
- cout << "If you do not know the year, it is possible to write nothing or to put \"[]\"" << "\n";
- cout << "for example:" << "\n" << "Edguy - King of Fools [2004]" << "\n";
- cout << "unknown - Get Lucky []" << "\n\n";
- console_getline(newTrackIn);
- track addTrack(newTrackIn);
- player.artistsMap[addTrack.performer].getTrackTitle(addTrack);
- return 1;
- }
- case '7':
- {
- system("CLS");
- int a, b;
- cout << "\n";
- cout << "enter first border: ";
- cin >> a;
- cout << "enter second border: ";
- cin >> b;
- cout << "\n";
- inTimeYearLimit(a, b);
- cout << "\n";
- return 1;
- }
- case '8':
- {
- return 0;
- }
- default: //пользователь ввёл несуществующие вариант
- system("CLS");
- cout << "Invalid input" << '\n';
- return 1;
- }
- }
- private:;
- };
- int main()
- {
- Music_player myPlayer;
- myPlayer.player.getArtistsMap();
- while (myPlayer.interface_player())
- {
- if (!myPlayer.interface_player())
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement