Advertisement
MaksNew

CPP

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