Advertisement
Usow_Maxim

Lab_8.10_module.cpp

Feb 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.36 KB | None | 0 0
  1. /////////////
  2. //module.cpp
  3. ////////////
  4.  
  5. #include "module.h"
  6.  
  7. struct Date
  8. {
  9.     int day;
  10.     int month;
  11.     int year;
  12. };
  13.  
  14. struct Music
  15. {
  16.     char* author;
  17.     char* name;
  18.     Date date;
  19.     int rating;
  20. };
  21.  
  22. /*Music* DefaultMusics(int &Size)
  23. {
  24.     Size = 1;
  25.     Music* musics = new Music[Size];
  26.     musics[0].author = (char*)"author";
  27.     musics[0].date.day = 1;
  28.     musics[0].date.month = 1;
  29.     musics[0].date.year = 1;
  30.     musics[0].name = (char*)"name";
  31.     musics[0].rating = 0;
  32.     return musics;
  33. }*/
  34.  
  35. Music DefaultMusic()
  36. {
  37.     Music music;
  38.     music.author = (char*)"author";
  39.     music.date.day = 1;
  40.     music.date.month = 1;
  41.     music.date.year = 1;
  42.     music.name = (char*)"name";
  43.     music.rating = 0;
  44.     return music;
  45. }
  46.  
  47. void FileWrite(Music* musics, int Size)
  48. {
  49.     FILE* fout = fopen("Musics.txt", "w");
  50.     if(Size <= 0 || fout == NULL)
  51.     {
  52.         printf("Ошибка записи в файл.");
  53.     }
  54.     else
  55.     {
  56.         fprintf(fout, "%d\n", Size);
  57.         for(int i = 0; i < Size; i++)
  58.         {
  59.             fprintf(fout, "%s ", musics[i].author);
  60.             fprintf(fout, "%s ", musics[i].name);
  61.             fprintf(fout, "%d %d %d ", musics[i].date.day, musics[i].date.month, musics[i].date.year);
  62.             fprintf(fout, "%d\n", musics[i].rating);
  63.         }
  64.     }
  65.     fclose(fout);
  66. }
  67.  
  68. char* cstc(string s){
  69.     char *cstr = new char[s.length() + 1];
  70.     strcpy(cstr, s.c_str());
  71.     return cstr;
  72. }
  73.  
  74. Music* FileRead(int &Size){
  75.     Music* musics = NULL;
  76.     ifstream f;
  77.     f.open("Musics.txt");
  78.     if(f.is_open())
  79.     {
  80.         string str;
  81.         getline(f, str, '\n');
  82.         Size = atoi(cstc(str))? atoi(cstc(str)) : 0;
  83.         if(Size > 0)
  84.         {
  85.             musics = new Music[Size];
  86.             for(int i = 0; i < Size; i++)
  87.             {
  88.                 getline(f, str, ' ');
  89.                 musics[i].author = cstc(str);
  90.                 getline(f, str, ' ');
  91.                 musics[i].name = cstc(str);
  92.                 getline(f, str, ' ');
  93.                 musics[i].date.day = atoi(cstc(str));
  94.                 getline(f, str, ' ');
  95.                 musics[i].date.month = atoi(cstc(str));
  96.                 getline(f, str, ' ');
  97.                 musics[i].date.year = atoi(cstc(str));
  98.                 getline(f, str, '\n');
  99.                 musics[i].rating = atoi(cstc(str));
  100.             }
  101.         }
  102.     }
  103.     f.close();
  104.     return musics;
  105. }
  106.  
  107. int Write_int(bool zero)
  108. {
  109.     char str[10];
  110.     int value;
  111.     while(true)
  112.     {
  113.         scanf("%s", str);
  114.         value = abs(atoi(str))? abs(atoi(str)) : 0;
  115.         if (zero) break;
  116.         if (!zero && value > 0)
  117.             break;
  118.         else
  119.             printf("Введите значение |n| > 0: ");
  120.     }
  121.     return value;
  122. }
  123.  
  124. Music* Music_Add(Music* musics, int &Size, Music newMusic)
  125. {
  126.     Size++;
  127.     Music* copyMusics = new Music[Size];
  128.     for (int i = 0; i < Size - 1; i++)
  129.         copyMusics[i] = musics[i];
  130.     copyMusics[Size - 1].author = newMusic.author;
  131.     copyMusics[Size - 1].name = newMusic.name;
  132.     copyMusics[Size - 1].date = newMusic.date;
  133.     copyMusics[Size - 1].rating = newMusic.rating;
  134.     delete[] musics;
  135.     return copyMusics;
  136. }
  137.  
  138. Music* Music_Remove(Music* musics, int &Size, int Index)
  139. {
  140.     Size--;
  141.     Music* copyMusics = new Music[Size];
  142.     for (int i = 0; i < Index; ++i)
  143.         copyMusics[i] = musics[i];
  144.     for (int i = Index; i < Size; ++i)
  145.         copyMusics[i] = musics[i + 1];
  146.     delete[] musics;
  147.     return copyMusics;
  148. }
  149.  
  150. int DateCmp(Date d1, Date d2)
  151. {
  152.     if(d1.year > d2.year)
  153.         return -1;
  154.     else
  155.         if(d1.year < d2.year)
  156.             return 1;
  157.         else
  158.         {
  159.             if(d1.month > d2.month)
  160.                 return -1;
  161.             else
  162.                 if(d1.month < d2.month)
  163.                     return 1;
  164.                 else
  165.                 {
  166.                     if(d1.day > d2.day)
  167.                         return -1;
  168.                     else
  169.                         if(d1.day < d2.day)
  170.                             return 1;
  171.                         else
  172.                             return 0;
  173.                 }
  174.         }
  175. }
  176.  
  177. char* strEdit(bool Number = false)
  178. {
  179.     system("cls");
  180.  
  181.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  182.     COORD pos;
  183.     pos.Y = 1;
  184.     pos.X = 0;
  185.     char* cloneStr = NULL;
  186.     char* str = NULL;
  187.  
  188.     printf("Вводите:\n");
  189.  
  190.     while(true)
  191.     {
  192.         int symbol = _getch();
  193.  
  194.         cloneStr = new char[pos.X + 1];
  195.         for (int i = 0; i < pos.X; i++)
  196.             if (str[i] != '\0')
  197.                 cloneStr[i] = str[i];
  198.         str = new char[pos.X + 1];
  199.         for (int i = 0; i < pos.X; i++)
  200.             str[i] = cloneStr[i];
  201.         delete[] cloneStr;
  202.  
  203.         if (symbol >= 97 && symbol <= 122 && !Number && pos.X < 50)
  204.         {
  205.             str[pos.X] = symbol;
  206.             printf("%c", str[pos.X]);
  207.             pos.X++;
  208.         }
  209.         if(symbol == 32 && !Number && pos.X < 50)
  210.         {
  211.             str[pos.X] = '_';
  212.             printf("%c", ' ');
  213.             pos.X++;
  214.         }
  215.  
  216.         if(symbol >= 48 && symbol <= 58 && pos.X < 50 && Number) //Цифры
  217.         {
  218.             str[pos.X] = symbol;
  219.             printf("%c", str[pos.X]);
  220.             pos.X++;
  221.         }
  222.  
  223.         if (symbol == 8 && pos.X > 0) //Backspace
  224.         {
  225.             str[pos.X] = ' ';
  226.             pos.X--;
  227.             SetConsoleCursorPosition(hConsole, pos); //Функция изменяет позицию курсора в консоли(HANDLE hConsole) на определённую позицию(COORD pos)
  228.             printf("%s", " ");
  229.         }
  230.  
  231.         SetConsoleCursorPosition(hConsole, pos);
  232.         if (symbol == 13)
  233.         {
  234.             str[pos.X] = '\0';
  235.             break;
  236.         }
  237.     }
  238.     delete[] cloneStr;
  239.     return str;
  240. }
  241.  
  242. Date dateEdit()
  243. {
  244.     system("cls");
  245.  
  246.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  247.     COORD cursor;
  248.     cursor.X = 0;
  249.     cursor.Y = 1;
  250.  
  251.     char* cloneStr = NULL;
  252.     char* str = NULL;
  253.  
  254.     printf("[Дата]:\n  /  /");
  255.     SetConsoleCursorPosition(hConsole, cursor);
  256.     while(true)
  257.     {
  258.         int symbol = _getch();
  259.  
  260.         cloneStr = new char[cursor.X + 1];
  261.         for (int i = 0; i < cursor.X; i++)
  262.             if (str[i] != '\0')
  263.                 cloneStr[i] = str[i];
  264.         str = new char[cursor.X + 1];
  265.         for (int i = 0; i < cursor.X; i++)
  266.             str[i] = cloneStr[i];
  267.         delete[] cloneStr;
  268.  
  269.         if(symbol >= 48 && symbol <= 58 && cursor.X < 10)
  270.         {
  271.             str[cursor.X] = symbol;
  272.             printf("%c", str[cursor.X]);
  273.             cursor.X++;
  274.             if(cursor.X == 2 || cursor.X == 5)
  275.                 cursor.X++;
  276.         }
  277.  
  278.         if (symbol == 8 && cursor.X > 0)
  279.         {
  280.             str[cursor.X] = ' ';
  281.             cursor.X--;
  282.             if(cursor.X == 2 || cursor.X == 5)
  283.                 cursor.X--;
  284.             SetConsoleCursorPosition(hConsole, cursor);
  285.             printf("%s", " ");
  286.         }
  287.  
  288.         SetConsoleCursorPosition(hConsole, cursor);
  289.         if (symbol == 13 && cursor.X == 10)
  290.         {
  291.             str[cursor.X] = '\0';
  292.             break;
  293.         }
  294.     }
  295.     Date date;
  296.     char* temp = new char[3];
  297.     temp[0] = str[0]; temp[1] = str[1]; temp[2] = '\0';
  298.     date.day = atoi(temp);
  299.  
  300.     temp[0] = str[3]; temp[1] = str[4]; temp[2] = '\0';
  301.     date.month = atoi(temp);
  302.  
  303.     delete[] temp; temp = new char[5];
  304.     temp[0] = str[6]; temp[1] = str[7]; temp[2] = str[8]; temp[3] = str[9]; temp[4] = '\0';
  305.     date.year = atoi(temp);
  306.  
  307.     delete[] temp;
  308.     delete[] str;
  309.     return date;
  310. }
  311.  
  312.  
  313. Music Music_Edit(Music music, bool isCreated = false)
  314. {
  315.     Music copyMusic;
  316.     if(isCreated)
  317.         copyMusic = music;
  318.     else
  319.         copyMusic = DefaultMusic();
  320.     bool RUN = true;
  321.     while(RUN)
  322.     {
  323.         system("cls");
  324.         printf("[Редактор книг]\n1.Автор\n2.Название\n3.Дата\n4.Рейтинг\n5.Сохранить\n6.Не сохранять.\nВыбор: ");
  325.         switch(Write_int())
  326.         {
  327.             case 1:
  328.                 copyMusic.author = strEdit();
  329.                 break;
  330.             case 2:
  331.                 copyMusic.name = strEdit();
  332.                 break;
  333.             case 3:
  334.                 copyMusic.date = dateEdit();
  335.                 break;
  336.             case 4:
  337.                 copyMusic.rating = atoi(strEdit(true));
  338.                 break;
  339.             case 5:
  340.                 music = copyMusic;
  341.                 RUN = false;
  342.                 break;
  343.             case 6:
  344.                 if(!isCreated)
  345.                     music = DefaultMusic();
  346.                 RUN = false;
  347.                 break;
  348.             default:
  349.                 break;
  350.         }
  351.     }
  352.     return music;
  353. }
  354.  
  355. Music* InputMusic(Music* musics, int &Size)
  356. {
  357.     bool RUN = true;
  358.     while(RUN){
  359.         system("cls");
  360.         printf("[Редактирование списка]\n1.Добавить\n2.Изменить\n3.Удалить\n4.Назад\nВыбор: ");
  361.         switch(Write_int())
  362.         {
  363.             case 1:
  364.             {
  365.                 system("cls");
  366.                 Music newMusic = DefaultMusic();
  367.                 newMusic = Music_Edit(newMusic);
  368.                 musics = Music_Add(musics, Size, newMusic);
  369.                 printf("Музыка успешно была добавлена в список музыки.\n");
  370.                 printf("Для продолжения нажмите любую клавишу...\n");
  371.                 _getch();
  372.                 break;
  373.             }
  374.             case 2:
  375.             {
  376.                 system("cls");
  377.                 printf("[Изменить]\nИндекс: ");
  378.                 int index = Write_int(true);
  379.                 if(index >= Size)
  380.                 {
  381.                     printf("Вы ввели индекс, который привышает размер списка музыки.\n");
  382.                     printf("Для продолжения нажмите любую клавишу...\n");
  383.                     _getch();
  384.                     break;
  385.                 }
  386.                 musics[index] = Music_Edit(musics[index], true);
  387.                 printf("Музыка успешно была изменена в список музыки.\n");
  388.                 printf("Для продолжения нажмите любую клавишу...\n");
  389.                 _getch();
  390.                 break;
  391.             }
  392.             case 3:
  393.             {
  394.                 printf("[Удаление]\nВведите индекс:\n");
  395.                 int index = Write_int(true);
  396.                 if(index >= Size)
  397.                 {
  398.                     printf("Вы ввели индекс, который привышает размер списка музыки.\n");
  399.                     printf("Для продолжения нажмите любую клавишу...\n");
  400.                     _getch();
  401.                     break;
  402.                 }
  403.                 musics = Music_Remove(musics, Size, index);
  404.                 printf("Книга удалена.\n");
  405.                 printf("Для продолжения нажмите любую клавишу...\n");
  406.                 _getch();
  407.                 break;
  408.             }
  409.             case 4:
  410.                 RUN = false;
  411.                 break;
  412.             default:
  413.                 break;
  414.         }
  415.     }
  416.     return musics;
  417. }
  418.  
  419. void OutputMusic(Music* musics, int Size)
  420. {
  421.     int* index = new int[Size];
  422.     for(int i = 0; i < Size; i++)
  423.         index[i] = i;
  424.     int temp = 0;
  425.     bool output = false;
  426.     system("cls");
  427.     printf("[Вывод списка]\n1.По индексу\n2.По Автору\n3.По Названию\n4.По Дате\n5.По Рейтингу\nВыбор: ");
  428.     switch(Write_int())
  429.     {
  430.         case 1:
  431.             output = true;
  432.             break;
  433.         case 2:
  434.             for (int i = 0; i < Size - 1; i++)
  435.                 for (int j = 0; j < Size - i - 1; j++)
  436.                     if (strcmp(musics[index[j]].author, musics[index[j + 1]].author)> 0)
  437.                     {
  438.                         temp = index[j];
  439.                         index[j] = index[j + 1];
  440.                         index[j + 1] = temp;
  441.                     }
  442.             output = true;
  443.             break;
  444.         case 3:
  445.             for (int i = 0; i < Size - 1; i++)
  446.                 for (int j = 0; j < Size - i - 1; j++)
  447.                     if (strcmp(musics[index[j]].name, musics[index[j + 1]].name)> 0)
  448.                     {
  449.                         temp = index[j];
  450.                         index[j] = index[j + 1];
  451.                         index[j + 1] = temp;
  452.                     }
  453.             output = true;
  454.             break;
  455.         case 4:
  456.             for (int i = 0; i < Size - 1; i++)
  457.                 for (int j = 0; j < Size - i - 1; j++)
  458.                     if (DateCmp(musics[index[j]].date, musics[index[j + 1]].date) > 0) //Используем вместо strcmp DateCmp, так как нам надо сравнить даты. >(-1) <(1) ==(0)
  459.                     {
  460.                         temp = index[j];
  461.                         index[j] = index[j + 1];
  462.                         index[j + 1] = temp;
  463.                     }
  464.             output = true;
  465.             break;
  466.         case 5:
  467.             for (int i = 0; i < Size - 1; i++)
  468.                 for (int j = 0; j < Size - i - 1; j++)
  469.                     if (musics[index[j]].rating > musics[index[j + 1]].rating)
  470.                     {
  471.                         temp = index[j];
  472.                         index[j] = index[j + 1];
  473.                         index[j + 1] = temp;
  474.                     }
  475.             output = true;
  476.             break;
  477.         default:
  478.             break;
  479.     }
  480.     if(output)
  481.     {
  482.         system("cls");
  483.         printf("[Вывод списка/По индексу]\n");
  484.         for (int i = 0; i < Size; i++)
  485.             printf("%d) %s %s %d/%d/%d %d\n", index[i], musics[index[i]].author,
  486.                    musics[index[i]].name, musics[index[i]].date.day,
  487.                    musics[index[i]].date.month, musics[index[i]].date.year,
  488.                    musics[index[i]].rating);
  489.     }
  490.     delete[] index;
  491.  
  492.     printf("Для продолжения нажмите любую клавишу...\n");
  493.     _getch();
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement