Advertisement
MaksNew

Untitled

Mar 3rd, 2021
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 KB | None | 0 0
  1. #include "SLList.h"
  2. #include <iostream>
  3. #include <regex>
  4. #include <sstream>
  5. #include <filesystem>
  6. #include <fstream>
  7.  
  8.     using namespace std;
  9.  
  10.     template<typename T>
  11.     class SinglyLinkedList
  12.     {
  13.     public:
  14.         SinglyLinkedList();
  15.         SinglyLinkedList(const SinglyLinkedList& other);
  16.         ~SinglyLinkedList();
  17.         void push_back(T data);
  18.         void push_front(T data);
  19.         void pop_front();
  20.         void clear();
  21.         void build_empty(size_t sizeOfList);
  22.         void removeAt(int index);
  23.         void pop_back();
  24.         int getSize() { return size; }
  25.         T operator[] (const int index);
  26.     private:
  27.         template<typename T>
  28.         class Node
  29.         {
  30.         public:
  31.             Node* pNext;
  32.             T data;
  33.             Node(T data = T(), Node* pNext = nullptr)
  34.             {
  35.                 this->data = data;
  36.                 this->pNext = pNext;
  37.             }
  38.         };
  39.         Node<T>* head;
  40.         size_t size;
  41.     };
  42.  
  43.     bool SLList::isFileCorrect(string path)
  44.     {
  45.         string inputline;
  46.         regex regular("^(\\d+\\s*)+$");
  47.         fstream fin(path, ios_base::in);
  48.         bool isCorrect = true;
  49.         while (!fin.eof() && isCorrect)
  50.         {
  51.             getline(fin, inputline);
  52.             if (!(regex_match(inputline.c_str(), regular)))
  53.                 isCorrect = false;
  54.         }
  55.         fin.close();
  56.         return isCorrect;
  57.     }
  58.  
  59.     string SLList::readFilePath(bool flag)
  60.     {
  61.         string path;
  62.         bool isIncorrect;
  63.         do
  64.         {
  65.             isIncorrect = false;
  66.             cout << "Введите абсолютный путь к файлу: " << endl;
  67.             cin >> path;
  68.             if (!filesystem::exists(path))
  69.             {
  70.                 cout << "Файл не найден. Проверьте введённый путь." << endl;
  71.                 isIncorrect = true;
  72.             }
  73.             else
  74.             {
  75.                 if (flag && !isFileCorrect(path))
  76.                 {
  77.                     cout << "Ошибка при чтении файла! Проверьте данные и попробуйте ещё раз!" << endl;
  78.                     isIncorrect = true;
  79.                 }
  80.             }
  81.  
  82.         } while (isIncorrect);
  83.         return path;
  84.     }
  85.  
  86.     int SLList::readElement(int data)
  87.     {
  88.         string inputLine;
  89.         bool isIncorrect;
  90.         do
  91.         {
  92.             isIncorrect = false;
  93.             try
  94.             {
  95.                 cin >> inputLine;
  96.                 data = stoi(inputLine);
  97.             }
  98.             catch (...)
  99.             {
  100.                 isIncorrect = true;
  101.                 cout << "Введите целое число!" << endl;
  102.             }
  103.         } while (isIncorrect);
  104.         return data;
  105.     }
  106.  
  107.     void SLList::getListElementFromConsole(SinglyLinkedList<int>& list)
  108.     {
  109.         int data = 0;
  110.         cout << "Введите элемент списка: \n";
  111.         data = readElement(data);
  112.         list.push_back(data);
  113.     }
  114.  
  115.     int SLList::getMainMenuItem(bool item5)
  116.     {
  117.         bool isIncorrect;
  118.         string inputLine;
  119.         int item = 0;
  120.         do
  121.         {
  122.             isIncorrect = false;
  123.             try
  124.             {
  125.                 cin >> inputLine;
  126.                 item = stoi(inputLine);
  127.             }
  128.             catch (...)
  129.             {
  130.                 isIncorrect = true;
  131.                 cout << "Введите целое число!" << endl;
  132.             }
  133.             if (!item5 && (item != 1) && (item != 2) && (item != 3) && !isIncorrect)
  134.             {
  135.                 cout << "Выберете один из пунктов меню!\n";
  136.                 isIncorrect = true;
  137.             }
  138.             if (item5 && (item != 1) && (item != 2) && (item != 3) && (item != 4) && (item != 5) && !isIncorrect)
  139.             {
  140.                 cout << "Выберете один из пунктов меню!\n";
  141.                 isIncorrect = true;
  142.             }
  143.         } while (isIncorrect);
  144.         return item;
  145.     }
  146.  
  147.     int SLList::readSizeOfEmptyList()
  148.     {
  149.         bool isIncorrect;
  150.         string inputLine;
  151.         int size;
  152.         do
  153.         {
  154.             isIncorrect = false;
  155.             try
  156.             {
  157.                 cin >> inputLine;
  158.                 size = stoi(inputLine);
  159.             }
  160.             catch (...)
  161.             {
  162.                 isIncorrect = true;
  163.                 cout << "Введите целое число!" << endl;
  164.             }
  165.             if (!isIncorrect && (size < 1 || size > 2000000))
  166.             {
  167.                 cout << "Введите положительное число до 2*10^6!\n";
  168.                 isIncorrect = true;
  169.             }
  170.         } while (isIncorrect);
  171.         return size;
  172.     }
  173.  
  174.     size_t SLList::readDeletableElementIndex(size_t index, SinglyLinkedList<int>& list)
  175.     {
  176.         bool isIncorrect;
  177.         string inputLine;
  178.         do
  179.         {
  180.             isIncorrect = false;
  181.             try
  182.             {
  183.                 cin >> inputLine;
  184.                 index = stoi(inputLine);
  185.             }
  186.             catch (...)
  187.             {
  188.                 isIncorrect = true;
  189.                 cout << "Введите целое число!" << endl;
  190.             }
  191.             if (!isIncorrect && (index < 1 || index > list.getSize()))
  192.             {
  193.                 cout << "Введите индекс в преедлах размера списка!\n";
  194.                 isIncorrect = true;
  195.             }
  196.         } while (isIncorrect);
  197.         return index-1;
  198.     }
  199.  
  200.     void SLList::saveList(SinglyLinkedList<int>& list)
  201.     {
  202.         ofstream fout;
  203.         string path = readFilePath(false);
  204.         fout.open(path);
  205.         for (size_t i = 0; i < list.getSize(); ++i)
  206.             fout << list[i] << " ";
  207.         fout.close();
  208.         cout << "Список успешно сохранён в файл!" << endl;
  209.     }
  210.  
  211.     void SLList::deleteElement(SinglyLinkedList<int>& list)
  212.     {
  213.         size_t index = 0;
  214.         cout << "Введите индекс удаляемого элемента:" << endl;
  215.         if (!(list.getSize() == 0))
  216.         {
  217.             index = readDeletableElementIndex(index, list);
  218.             list.removeAt(index);
  219.         }
  220.         else
  221.             cout << "Список пуст!" << endl;
  222.     }
  223.  
  224.     void SLList::selectMenuItem(int menuItem, SinglyLinkedList<int>& list)
  225.     {
  226.         int data = 0;
  227.         size_t index = 0;
  228.         switch (menuItem)
  229.         {
  230.         case 1:
  231.             cout << "Введите элемент списка:" << endl;
  232.             data = readElement(data);
  233.             list.push_back(data);
  234.             break;
  235.         case 2:
  236.             deleteElement(list);
  237.             break;
  238.         case 3:
  239.             cout << "Введите элемент:\n";
  240.             data = readElement(data);
  241.             cout << "Вхождений элемента " << data << " в список: " << showAllEntryOfElement(data, list) << endl;
  242.             break;
  243.         case 4:
  244.             saveList(list);
  245.             break;
  246.         case 5:
  247.             exit(0);
  248.             break;
  249.         }
  250.         print(list);
  251.     }
  252.  
  253.     void SLList::showMenu(SinglyLinkedList<int> list)
  254.     {
  255.         bool item5 = true;
  256.         cout << "Выберете один из пунктов меню :" << endl;
  257.         cout << "1. Добавить новый элемент." << endl;
  258.         cout << "2. Удалить элемент." << endl;
  259.         cout << "3. Подсчитать количество всех вхождений заданного элемента." << endl;
  260.         cout << "4. Сохранить список в файл." << endl;
  261.         cout << "5. Завершить программу." << endl;
  262.         selectMenuItem(getMainMenuItem(item5), list);
  263.         showMenu(list);
  264.     }
  265.  
  266.     void SLList::selectMainMenuItem(int menuItem)
  267.     {
  268.         SinglyLinkedList<int> list;
  269.         switch (menuItem)
  270.         {
  271.         case 1:
  272.             getListFromFile(list);
  273.             break;
  274.         case 2:
  275.             getListElementFromConsole(list);
  276.             break;
  277.         case 3:
  278.             cout << "Введите размер списка\n";
  279.             list.build_empty(readSizeOfEmptyList());
  280.             break;
  281.         }
  282.         print(list);
  283.         showMenu(list);
  284.     }
  285.  
  286.     void SLList::showMainMenu()
  287.     {
  288.         bool item5 = false;
  289.         cout << "Выберете один из пунктов меню :" << endl;
  290.         cout << "1. Открыть список из файла." << endl;
  291.         cout << "2. Создать новый список." << endl;
  292.         cout << "3. Создать новый пустой список указанного размера." << endl;
  293.         selectMainMenuItem(getMainMenuItem(item5));
  294.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement