Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SLList.h"
- #include <iostream>
- #include <regex>
- #include <sstream>
- #include <filesystem>
- #include <fstream>
- using namespace std;
- bool SLList::isFileCorrect(string path)
- {
- string inputline;
- regex regular("^(\\d+\\s*)+$");
- fstream fin(path, ios_base::in);
- bool isCorrect = true;
- while (!fin.eof() && isCorrect)
- {
- getline(fin, inputline);
- if (!(regex_match(inputline.c_str(), regular)))
- isCorrect = false;
- }
- fin.close();
- return isCorrect;
- }
- string SLList::readFilePath(bool flag)
- {
- string path;
- bool isIncorrect;
- do
- {
- isIncorrect = false;
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- if (!filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- isIncorrect = true;
- }
- else
- {
- if (flag && !isFileCorrect(path))
- {
- cout << "Ошибка при чтении файла! Проверьте данные и попробуйте ещё раз!" << endl;
- isIncorrect = true;
- }
- }
- } while (isIncorrect);
- return path;
- }
- int SLList::readElement(int data)
- {
- string inputLine;
- bool isIncorrect;
- do
- {
- isIncorrect = false;
- try
- {
- cin >> inputLine;
- data = stoi(inputLine);
- }
- catch (...)
- {
- isIncorrect = true;
- cout << "Введите целое число!" << endl;
- }
- } while (isIncorrect);
- return data;
- }
- void SLList::getListElementFromConsole(SinglyLinkedList<int>& list)
- {
- int data = 0;
- cout << "Введите элемент списка: \n";
- data = readElement(data);
- list.push_back(data);
- }
- int SLList::getMainMenuItem(bool item5)
- {
- bool isIncorrect;
- string inputLine;
- int item = 0;
- do
- {
- isIncorrect = false;
- try
- {
- cin >> inputLine;
- item = stoi(inputLine);
- }
- catch (...)
- {
- isIncorrect = true;
- cout << "Введите целое число!" << endl;
- }
- if (!item5 && (item != 1) && (item != 2) && (item != 3) && !isIncorrect)
- {
- cout << "Выберете один из пунктов меню!\n";
- isIncorrect = true;
- }
- if (item5 && (item != 1) && (item != 2) && (item != 3) && (item != 4) && (item != 5) && !isIncorrect)
- {
- cout << "Выберете один из пунктов меню!\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return item;
- }
- int SLList::readSizeOfEmptyList()
- {
- bool isIncorrect;
- string inputLine;
- int size;
- do
- {
- isIncorrect = false;
- try
- {
- cin >> inputLine;
- size = stoi(inputLine);
- }
- catch (...)
- {
- isIncorrect = true;
- cout << "Введите целое число!" << endl;
- }
- if (!isIncorrect && (size < 1 || size > 2000000))
- {
- cout << "Введите положительное число до 2*10^6!\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return size;
- }
- size_t SLList::readDeletableElementIndex(size_t index, SinglyLinkedList<int>& list)
- {
- bool isIncorrect;
- string inputLine;
- do
- {
- isIncorrect = false;
- try
- {
- cin >> inputLine;
- index = stoi(inputLine);
- }
- catch (...)
- {
- isIncorrect = true;
- cout << "Введите целое число!" << endl;
- }
- if (!isIncorrect && (index < 1 || index > list.getSize()))
- {
- cout << "Введите индекс в преедлах размера списка!\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return index-1;
- }
- void SLList::saveList(SinglyLinkedList<int>& list)
- {
- ofstream fout;
- string path = readFilePath(false);
- fout.open(path);
- for (size_t i = 0; i < list.getSize(); ++i)
- fout << list[i] << " ";
- fout.close();
- cout << "Список успешно сохранён в файл!" << endl;
- }
- void SLList::deleteElement(SinglyLinkedList<int>& list)
- {
- size_t index = 0;
- cout << "Введите индекс удаляемого элемента:" << endl;
- if (!(list.getSize() == 0))
- {
- index = readDeletableElementIndex(index, list);
- list.removeAt(index);
- }
- else
- cout << "Список пуст!" << endl;
- }
- void SLList::selectMenuItem(int menuItem, SinglyLinkedList<int>& list)
- {
- int data = 0;
- size_t index = 0;
- switch (menuItem)
- {
- case 1:
- cout << "Введите элемент списка:" << endl;
- data = readElement(data);
- list.push_back(data);
- break;
- case 2:
- deleteElement(list);
- break;
- case 3:
- cout << "Введите элемент:\n";
- data = readElement(data);
- cout << "Вхождений элемента " << data << " в список: " << showAllEntryOfElement(data, list) << endl;
- break;
- case 4:
- saveList(list);
- break;
- case 5:
- exit(0);
- break;
- }
- print(list);
- }
- void SLList::showMenu(SinglyLinkedList<int> list)
- {
- bool item5 = true;
- cout << "Выберете один из пунктов меню :" << endl;
- cout << "1. Добавить новый элемент." << endl;
- cout << "2. Удалить элемент." << endl;
- cout << "3. Подсчитать количество всех вхождений заданного элемента." << endl;
- cout << "4. Сохранить список в файл." << endl;
- cout << "5. Завершить программу." << endl;
- selectMenuItem(getMainMenuItem(item5), list);
- showMenu(list);
- }
- void SLList::selectMainMenuItem(int menuItem)
- {
- SinglyLinkedList<int> list;
- switch (menuItem)
- {
- case 1:
- getListFromFile(list);
- break;
- case 2:
- getListElementFromConsole(list);
- break;
- case 3:
- cout << "Введите размер списка\n";
- list.build_empty(readSizeOfEmptyList());
- break;
- }
- print(list);
- showMenu(list);
- }
- void SLList::showMainMenu()
- {
- bool item5 = false;
- cout << "Выберете один из пунктов меню :" << endl;
- cout << "1. Открыть список из файла." << endl;
- cout << "2. Создать новый список." << endl;
- cout << "3. Создать новый пустой список указанного размера." << endl;
- selectMainMenuItem(getMainMenuItem(item5));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement