Advertisement
43B69

MusicList

May 15th, 2022
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. // ОБЯЗАТЕЛЬНО ЗАПУСКАЙТЕ В С++17 ИЗ-ЗА FILESYSTEM.
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>  
  5. #include <filesystem>
  6. #include <vector>
  7. #include <stdio.h>
  8. using namespace std;
  9. namespace fs = std::filesystem;
  10.  
  11. struct MusicCard{
  12.     string MusicName;
  13.     string AuthorName;
  14.     string DATE = NULL;
  15.     string FileName;
  16. };
  17.  
  18. vector<MusicCard> MusicInFolder(string FolderName) {
  19.     vector <MusicCard> a;
  20.     string path = FolderName + "/";
  21.     for (auto& p : fs::directory_iterator(path)) {
  22.         string NOF = p.path().string();
  23.         ifstream fin(NOF, ios_base::in);
  24.         string name;
  25.         getline(fin, name);
  26.         string Author;
  27.         getline(fin, Author);
  28.         string Date;
  29.         getline(fin, Date);
  30.         fin.close();
  31.         a.push_back({ name, Author, Date, NOF });
  32.     }
  33.     return a;
  34. }
  35.  
  36. void MusicText(vector<MusicCard> List) {
  37.     system("cls");
  38.     cout << "Выберите музыкальное произведение: " << endl;
  39.     for (int i = 0; i < List.size(); i++) {
  40.         cout << i + 1 << " - " << List[i].MusicName << endl;
  41.     }
  42.     int a; cin >> a;
  43.     while ((a <= 0) || (a > List.size())) {
  44.         cout << "Неверное значение." << endl;
  45.         cin >> a;
  46.     }
  47.     ifstream fin(List[a - 1].FileName, ios_base::in);
  48.     int count = 0; string str;
  49.     while (getline(fin, str)) {
  50.         if (count >= 3) cout << str << endl;
  51.         else count++;
  52.     }
  53.     _getwch();
  54. }
  55.  
  56. void SaveMusic(vector<MusicCard> List) {
  57.     system("cls");
  58.     cout << "Введите название: ";
  59.     string name; getline(cin, name);
  60.     cout << "Введите автора: ";
  61.     string author; getline(cin, author);
  62.     cout << "Введите год (- если не известен): ";
  63.     string year; cin >> year;
  64.     if (year == "-") { year = ""; }
  65.     string NOF = "List/" + name + ".txt";
  66.     List.push_back({ name, author, year, NOF });
  67.     string str; size_t n = List.size() - 1;
  68.     ofstream fout(List[n].FileName, ios_base::out);
  69.     fout << List[n].MusicName << endl << List[n].AuthorName << endl << List[n].DATE;
  70.     cout << "Чтобы закончить ввод, введите строку \'(+)-?*/\'." << endl;
  71.     getline(cin, str);
  72.     while (str != "(+)-?*/") {
  73.         fout << str << endl;
  74.         getline(cin, str);
  75.     }
  76.     fout.close();
  77. }
  78.  
  79. vector<MusicCard> DeleteMusic(vector<MusicCard> List) {
  80.     system("cls");
  81.     cout << "Выберите музыкальное произведение: " << endl;
  82.     for (int i = 0; i < List.size(); i++) {
  83.         cout << i + 1 << " - " << List[i].MusicName << endl;
  84.     }
  85.     int a; cin >> a;
  86.     while ((a <= 0) || (a > List.size())) {
  87.         cout << "Неверное значение." << endl;
  88.         cin >> a;
  89.     }
  90.     remove(List[a - 1].FileName.c_str());
  91.     List.pop_back();
  92.     return List;
  93. }
  94.  
  95. void FindAuthorMusic(vector<MusicCard> List) {
  96.     system("cls");
  97.     cout << "Введите имя автора: "; int count = 1;
  98.     string name; getline(cin, name);
  99.     for (int i = 0; i < List.size(); i++) {
  100.         ifstream fin(List[i].FileName, ios_base::in);
  101.         string str;
  102.         getline(fin, str);
  103.         getline(fin, str);
  104.         if (str == name) { cout << count << " - " << List[i].MusicName << endl; count++; }
  105.     }
  106.     if (count == 1) { cout << "Музыка этого автора не найдена."; }
  107.     _getwch();
  108. }
  109.  
  110. void FindWordInMusic(vector<MusicCard> List) {
  111.     system("cls");
  112.     cout << "Введите слово для поиска: ";
  113.     string target; cin >> target; int count = 1;
  114.     for (int i = 0; i < List.size(); i++) {
  115.         ifstream fin(List[i].FileName, ios_base::in);
  116.         string str;
  117.         while (fin >> str) {
  118.             if (str == target) {
  119.                 cout << count << " - " << List[i].MusicName << endl; count++;
  120.                 break;
  121.             }
  122.         }
  123.         fin.close();
  124.     }
  125.     if (count == 1) { cout << "Слово не найдено."; }
  126.     _getwch();
  127. }
  128.  
  129. vector<string> FiletoSplit(string FilePath) {
  130.     vector <string> Split;
  131.     fstream fin(FilePath, ios_base::in);
  132.     string str;
  133.     while (getline(fin, str)) Split.push_back(str);
  134.     fin.close();
  135.     return Split;
  136. }
  137.  
  138. void EditSong(vector<MusicCard> List) {
  139.     system("cls");
  140.     cout << "Выберите музыкальное произведение: " << endl;
  141.     for (int i = 0; i < List.size(); i++) {
  142.         cout << i + 1 << " - " << List[i].MusicName << endl;
  143.     }
  144.     int a; cin >> a;
  145.     while ((a <= 0) || (a > List.size())) {
  146.         cout << "Неверное значение." << endl;
  147.         cin >> a;
  148.     }
  149.     a = a - 1;
  150.     while (a != -1) {
  151.         system ("cls");
  152.         ifstream f(List[a].FileName, ios_base::in);
  153.         cout << "Музыка: " << List[a].MusicName << "; Автор: " << List[a].AuthorName << endl;
  154.         string str; int count = 1;
  155.         while (getline(f, str)) {
  156.             if (count > 3) { cout << count << " | " << str << endl;}
  157.             count++;
  158.         }
  159.         f.close();
  160.         cout << "----------------" << endl << "Выберите номер строки: ";
  161.         int b; cin >> b; cin.get();
  162.         if ((b <= 3) || (b >= count)) {
  163.             break;
  164.         }
  165.         b = b - 1;
  166.         cout << "Введите строку: ";
  167.         string x; cin >> x;
  168.         vector <string> Split = FiletoSplit(List[a].FileName);
  169.         ofstream fout(List[a].FileName, ios_base::trunc);
  170.         for (int i = 0; i < b; i++) fout << Split[i] << endl;
  171.         fout << x << endl;
  172.         for (int i = b+1; i < Split.size(); i++) fout << Split[i] << endl;
  173.         fout.close();
  174.     }
  175. }
  176.  
  177. void main_menu() {
  178.     string x;
  179.     cout << "Введите имя папки с текстами: ";
  180.     getline (cin, x);
  181.     system("cls"); int a = 1;
  182.     while (a != 0) {
  183.         system ("cls");
  184.         cout << "Каталог текстов песен" << endl;
  185.         cout << "---------------------" << endl;
  186.         cout << "Выберите действие: " << endl;
  187.         cout << "[1] - Вывести текст песни." << endl;
  188.         cout << "[2] - Сохранить текст песни в файл." << endl;
  189.         cout << "[3] - Удалить песню из каталога." << endl;
  190.         cout << "[4] - Поиск песен одного автора." << endl;
  191.         cout << "[5] - Поиск слова в песнях." << endl;
  192.         cout << "[6] - Изменение текста песни." << endl;
  193.         cout << "[0] - Выход из программы." << endl;
  194.         cin >> a;
  195.         while ((a < 0) || (a > 6)) {
  196.             cout << "Неверное значение." << endl;
  197.             cin >> a;
  198.         }
  199.         cin.get();
  200.         vector<MusicCard> List = MusicInFolder(x);
  201.         if (a == 1) MusicText(List);
  202.         else if (a == 2) SaveMusic(List);
  203.         else if (a == 3) DeleteMusic(List);
  204.         else if (a == 4) FindAuthorMusic(List);
  205.         else if (a == 5) FindWordInMusic(List);
  206.         else if (a == 6) EditSong(List);
  207.         else if (a == 0) break;
  208.     }
  209.        
  210. }
  211.  
  212. int main() {
  213.     system("chcp 1251");
  214.     system("cls");
  215.     main_menu();
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement