Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <vector>
- using namespace std;
- class CD
- {
- //структура песни: имя песни, ее автор и длительность
- struct Song
- { string name, author, length; };
- //элементы, передаваемые классу Catalog
- protected:
- //вектор disk всех песен на диске
- vector <Song> disk;
- //вспомогательная функция для вывода на экран
- string space (int i) { return (i / 10)? " ": " "; }
- public:
- //имя диска
- string name;
- //конструктор по умолчанию
- CD() {}
- //еще один конструктор
- CD (string n) : name(n) {}
- //перегрузка функции push: добавление песни в конец диска или добавление песни в конкретную позицию
- void push (string name, string author, string length)
- {
- Song s = { name, author, length };
- disk.push_back(s);
- }
- void push (string name, string author, string length, int pos)
- {
- Song s = { name, author, length };
- disk.insert(disk.begin() + pos, s);
- }
- //удаление песни из диска
- void erase (int pos) { disk.erase(disk.begin() + pos); }
- //просмотр диска
- void view ()
- {
- cout << name << "\n\n";
- cout << left << setw(4) << "No." << setw(26) << "Title" << setw(45) << "Artists" << setw(6) << "Length\n";
- for (int i = 0; i < disk.size(); ++i)
- cout << i + 1 << '.' << space(i + 1) << setw(26) << disk[i].name << setw(45) << disk[i].author << setw(6) << disk[i].length << '\n';
- }
- //просмотр всех песен данного автора
- void check (string artist, int &count)
- {
- for (int i = 0; i < disk.size(); ++i)
- if (disk[i].author.find(artist) != string :: npos)
- {
- ++count;
- if (count == 1) cout << '\n' << left << setw(4) << "No." << setw(17) << "CD name" << setw(26) << "Title" << setw(45) << "Artists" << setw(6) << "Length\n";
- cout << left << count << "." << space(i + 1) << setw(17) << name << setw(26) << disk[i].name << setw(45) << disk[i].author << setw(6) << disk[i].length << '\n';
- }
- }
- };
- class Catalog: public CD
- {
- //вектор компакт-дисков
- vector <CD> disks;
- void checkName (string name, bool &b, int &pos)
- {
- for (int i = 0; i < disks.size(); ++i)
- if (disks[i].name == name) { pos = i; b = 1; break; }
- }
- public:
- //конструктор по умолчанию
- Catalog() {}
- //перегрузка функции push: добавление диска в конец вектора, добавление песни в диск с именем n
- void push (CD disk) { disks.push_back(disk); }
- void push (string n, string name, string author, string length)
- {
- bool b = 0; int pos; checkName(n, b, pos);
- if (b) disks[pos].push(name, author, length);
- else cout << "Error : there are no CD with that name\n\n";
- }
- //перегрузка функции erase: удаление диска из вектора по названию диска и удаление песни из диска
- void erase (string name)
- {
- bool b = 0; int pos; checkName(name, b, pos);
- if (b) disks.erase(disks.begin() + pos);
- else cout << "Error : there are no CD with that name\n\n";
- }
- void erase (string name, int p)
- {
- bool b = 0; int pos; checkName(name, b, pos);
- if (b) disks[pos].erase(p);
- else cout << "Error : there are no CD with that name\n\n";
- }
- //перегрузка функции view: просмотр конкретного компакт-диска по номеру позиции или по названию диска
- //или просмотр целого диапазона дисков
- void view (int pos)
- {
- disks[pos - 1].view();
- cout << '\n';
- }
- void view (string name)
- {
- if (name == "all") { view(1, disks.size()); return; }
- else
- {
- for (int i = 0; i < disks.size(); ++i)
- if (disks[i].name == name) { disks[i].view(); cout << '\n'; return; }
- cout << "Your search - " << name << " - did not match any CD\n\n";
- }
- }
- void view (int pos1, int pos2)
- {
- for (int i = pos1 - 1; i < pos2; ++i, cout << '\n')
- disks[i].view();
- }
- //просмотр всех песен данного автора
- void check (string author)
- {
- cout << "Showing results for " << author << ":\n";
- int count = 0;
- for (int i = 0; i < disks.size(); ++i)
- disks[i].check(author, count);
- if (count == 0) cout << "There are no results for your search\n\n";
- else cout << '\n';
- }
- };
- int main()
- {
- ifstream fin ("input.txt");
- Catalog database;
- while(fin.peek() != EOF)
- {
- string s; fin >> s;
- CD disk(s);
- for (int i = 0; i < 5; ++i)
- {
- string n, a, l;
- fin >> n >> a >> l;
- disk.push(n, a, l);
- }
- database.push(disk);
- }
- fin.close();
- cout << "Первоначальный каталог компакт-дисков:\n\n";
- database.view("all"); //или database.view(1, 6);
- cout << "Просмотрим содержимое компакт-дисков с заданными именами: Top_Songs_2016 и Top_Songs_2021:\n\n";
- database.view("Top_Songs_2016");
- database.view("Top_Songs_2021");
- cout << "Удалим из каталога компакт-диск Top_Songs_2010 и вместо него добавим диск Top_Songs_2020:\n\n";
- database.erase("Top_Songs_2010");
- CD disk("Top_Songs_2020");
- cout << "Введите песни из диска Top_Songs_2020:\n";
- //Top_Songs_2020 (Ctrl + Shift + V)
- //Blinding_Lights The_Weeknd 3:22
- //Circles Post_Malone 3:34
- //The_Box Roddy_Ricch 3:16
- //Don't_Start_Now Dua_Lipa 3:03
- //Rockstar DaBaby_ft._Roddy_Ricch 3:01
- for (int i = 0; i < 5; ++i)
- {
- string n, a, l;
- cin >> n >> a >> l;
- disk.push(n, a, l);
- }
- database.push(disk);
- cout << "\nВ диск Top_Songs_2019 добавим еще 5 песен:\n";
- //Happier Marshmello_&_Bastille 3:34
- //7_Rings Ariana_Grande 2:58
- //Talk Khalid 3:17
- //Sicko_Mode Travis_Scott 5:12
- //Sucker Jonas_Brothers 3:01
- for (int i = 0; i < 5; ++i)
- {
- string n, a, l;
- cin >> n >> a >> l;
- database.push("Top_Songs_2019", n, a, l);
- }
- cout << "\nИз диска Top_Songs_2017 удалим 2 последние песни\n\n";
- database.erase("Top_Songs_2017", 4);
- database.erase("Top_Songs_2017", 3);
- cout << "Теперь наш каталог компакт-дисков выглядит так:\n\n";
- database.view("all");
- cout << "Найдем все песни исполнителей Drake и Post_Molone:\n\n";
- database.check("Drake");
- database.check("Post_Molone");
- return 0;
- }
- //input.txt
- Top_Songs_2015
- Uptown_Funk! Mark_Ronson_ft._Bruno_Mars 4:30
- Thinking_Out_Loud Ed_Sheeran 4:41
- See_You_Again Wiz_Khalifa_ft._Charlie_Puth 3:49
- Trap_Queen Fetty_Wap 3:35
- Sugar Maroon_5 3:55
- Top_Songs_2016
- Love_Yourself Justin_Bieber 3:53
- Sorry Justin_Bieber 3:20
- One_Dance Drake_ft._WizKid_&_Kyla 2:54
- Work Rihanna_ft._Drake 3:39
- Stressed_Out twenty_one_pilots 3:22
- Top_Songs_2017
- Shape_Of_You Ed_Sheeran 3:53
- Despacito Luis_Fonsi_&_Daddy_Yankee_ft._Justin_Bieber 3:47
- That's_What_I_Like Bruno_Mars 3:26
- Humble. Kendrick_Lamar 2:57
- Something_Just_Like_This The_Chainsmokers_&_Coldplay 4:07
- Top_Songs_2010
- TiK_ToK Ke$ha 3:20
- Need_You_Now Lady_Antebellum 4:37
- Hey,_Soul_Sister Train 3:37
- California_Gurls Katy_Perry_ft._Snoop_Dogg 3:56
- OMG Usher_ft._will.i.am 4:45
- Top_Songs_2018
- God's_Plan Drake 3:18
- Perfect Ed_Sheeran 4:23
- Meant_To_Be Bebe_Rexha_&_Florida_Georgia_Line 2:43
- Havana Camila_Cabello_ft._Young_Thug 3:36
- Rockstar Post_Malone_ft._21_Savage 3:38
- Top_Songs_2019
- Old_Town_Road Lil_Nas_X_ft._Billy_Ray_Cyrus 1:53
- Sunflower Post_Malone_&_Swae_Lee 2:39
- Without_Me Halsey 3:22
- Bad_Guy Billie_Eilish 3:14
- Wow. Post_Malone 2:29
- //untitled.txt
- Blinding_Lights The_Weeknd 3:22
- Circles Post_Malone 3:34
- The_Box Roddy_Ricch 3:16
- Don't_Start_Now Dua_Lipa 3:03
- Rockstar DaBaby_ft._Roddy_Ricch 3:01
- Happier Marshmello_&_Bastille 3:34
- 7_Rings Ariana_Grande 2:58
- Talk Khalid 3:17
- Sicko_Mode Travis_Scott 5:12
- Sucker Jonas_Brothers 3:01
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement