MaksNew

Untitled

Nov 20th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <filesystem>
  4. #include <sstream>
  5. #include <string>
  6. #include <set>
  7. #include <iomanip>
  8. #include <Windows.h>
  9. using namespace std;
  10.  
  11. struct slist
  12. {
  13. int score;
  14. string name;
  15. };
  16.  
  17. int getsize(int size)
  18. {
  19. bool IsNotCorrect;
  20. string inputLine;
  21. cout << "Введите количество участников: " << endl;
  22. do
  23. {
  24. IsNotCorrect = false;
  25. try
  26. {
  27. cin >> inputLine;
  28. size = stoi(inputLine);
  29. }
  30. catch (...)
  31. {
  32. IsNotCorrect = true;
  33. cout << "Введите целое число!" << endl;
  34. }
  35. if ((size <= 0) && !IsNotCorrect)
  36. {
  37. cout << "Введите положительное число!" << endl;
  38. IsNotCorrect = true;
  39. }
  40. } while (IsNotCorrect);
  41. return size;
  42. }
  43.  
  44. struct slist* getListFromConsole(int size, struct slist* aol)
  45. {
  46. bool IsNotCorrect;
  47. string inputLine;
  48. string buf;
  49. for (int i = 0; i < size; i++)
  50. {
  51. do
  52. {
  53. IsNotCorrect = false;
  54. cout << "Введите фамилию участника: " << endl;
  55. //cin.ignore();
  56. //getline(cin, aol[i].name);
  57. cin >> aol[i].name;
  58. buf = aol[i].name;
  59. for (int j = 0; j < buf.length(); j++)
  60. {
  61. if (!(('А' <= buf[j]) && (buf[j] <= 'Я') || ('а' <= buf[j]) && (buf[j] <= 'я')))
  62. {
  63. IsNotCorrect = true;
  64. }
  65. }
  66. if (IsNotCorrect == true)
  67. {
  68. cout << "Фамилия должна состоять из русских букв" << endl;
  69. }
  70. } while (IsNotCorrect);
  71. do
  72. {
  73. IsNotCorrect = false;
  74. cout << "Введите счёт участника: " << endl;
  75. try
  76. {
  77. cin >> inputLine;
  78. aol[i].score = stoi(inputLine);
  79. }
  80. catch (...)
  81. {
  82. IsNotCorrect = true;
  83. cout << "Введите целое число!" << endl;
  84. }
  85. if ((aol[i].score < 0) && !IsNotCorrect)
  86. {
  87. cout << "Введите неотрицательное число!" << endl;
  88. IsNotCorrect = true;
  89. }
  90. } while (IsNotCorrect);
  91. }
  92. return aol;
  93. }
  94.  
  95. void printListBeforeSorting(struct slist* aol, int size)
  96. {
  97. cout << "Список участников до сортировки: " << endl;
  98. for (int i = 0; i < size; i++)
  99. {
  100. cout << aol[i].name << " " << aol[i].score << endl;
  101. }
  102. cout << endl;
  103. }
  104.  
  105. void sorting(struct slist*& aol, int size)
  106. {
  107. int bufi;
  108. string bufs;
  109. for (int i = 0; i < size-1; i++)
  110. {
  111. for (int j = i + 1; j < size; j++)
  112. {
  113. if (aol[i].score < aol[j].score)
  114. {
  115. bufi= aol[j].score;
  116. aol[j].score = aol[i].score;
  117. aol[i].score = bufi;
  118. bufs = aol[i].name;
  119. aol[i].name = aol[j].name;
  120. aol[j].name = bufs;
  121. }
  122. }
  123. }
  124. }
  125.  
  126. void Top3Output(struct slist*& aol, int size)
  127. {
  128. int ind, bufOne, bufTwo;
  129. ind = 0;
  130. cout << "Участники, показавшие 3 лучших результата: " << endl;
  131. cout << aol[0].name << ' ' << aol[0].score << endl;
  132. for (int i = 0; i < size; i++)
  133. {
  134. if ((aol[i].score == aol[0].score) and (aol[i].name != aol[0].name))
  135. {
  136. cout << aol[i].name << ' ' << aol[i].score << endl;
  137. ind = i;
  138. }
  139. }
  140. bufOne = ind;
  141. cout << aol[ind+1].name << ' ' << aol[ind+1].score << endl;
  142. for (int i = ind + 1; i < size; i++)
  143. {
  144. if((aol[i].score == aol[ind + 1].score) and (aol[i].name != aol[ind + 1].name))
  145. {
  146. cout << aol[i].name << ' ' << aol[i].score << endl;
  147. ind = i;
  148. }
  149. }
  150. bufTwo = ind;
  151. if (bufOne != bufTwo)
  152. {
  153. cout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
  154. for (int i = ind + 1; i < size; i++)
  155. {
  156. if ((aol[i].score == aol[ind + 1].score) && (aol[i].name != aol[ind + 1].name))
  157. {
  158. cout << aol[i].name << ' ' << aol[i].score << endl;
  159. }
  160. }
  161. }
  162. else
  163. {
  164. cout << aol[ind + 2].name << ' ' << aol[ind + 2].score << endl;
  165. for (int i = ind + 2; i < size; i++)
  166. {
  167. if((aol[i].score = aol[ind + 2].score) && (aol[i].name != aol[ind + 2].name))
  168. {
  169. cout << aol[i].name << ' ' << aol[i].score << endl;
  170. }
  171. }
  172. }
  173. }
  174.  
  175. bool isFileCorrect(int size, string path)
  176. {
  177. int i;
  178. bool IsCorrect;
  179. string bufstring, bts, nums;
  180. fstream listFile(path, fstream::in);
  181. IsCorrect = true;
  182. i = 1;
  183. while (!listFile.eof() && IsCorrect)
  184. {
  185. if (i % 2 == 1)
  186. {
  187. getline(listFile, bufstring);
  188. stringstream strstream;
  189. strstream << bufstring;
  190. while (!strstream.eof() && IsCorrect)
  191. {
  192. strstream >> bts;
  193. for (int j = 0; j < bts.length(); j++)
  194. {
  195. if (!(('А' <= bts[j]) && (bts[j] <= 'Я') || ('а' <= bts[j]) && (bts[j] <= 'я')) && IsCorrect)
  196. {
  197. IsCorrect = false;
  198. }
  199. }
  200. }
  201. }
  202. else
  203. {
  204. getline(listFile, bufstring);
  205. stringstream strstream;
  206. strstream << bufstring;
  207. while (!strstream.eof() && IsCorrect)
  208. {
  209. strstream >> nums;
  210. for (int j = 0; j < nums.length(); j++)
  211. {
  212. if (!(('0' <= nums[j]) && (nums[j] <= '9')) && IsCorrect)
  213. {
  214. IsCorrect = false;
  215. }
  216. }
  217. }
  218. }
  219. i++;
  220. }
  221. if (i != size*2+1)
  222. {
  223. IsCorrect = false;
  224. }
  225. listFile.close();
  226. return IsCorrect;
  227.  
  228. }
  229.  
  230. string getFilePath(int size)
  231. {
  232. string path;
  233. bool isIncorrect;
  234. isIncorrect = true;
  235. do
  236. {
  237. cout << "Введите абсолютный путь к файлу: " << endl;
  238. cin >> path;
  239. if (!std::filesystem::exists(path))
  240. {
  241. cout << "Файл не найден. Проверьте введённый путь." << endl;
  242. }
  243. else
  244. {
  245. if (isFileCorrect(size, path))
  246. {
  247. isIncorrect = false;
  248. }
  249. else
  250. {
  251. cout << "Данные в файле некорректны\n";
  252. }
  253. }
  254. } while (isIncorrect);
  255. return path;
  256. }
  257.  
  258. struct slist* getListfromFile(struct slist* aol, int size)
  259. {
  260. string path;
  261. path = getFilePath(size);
  262. fstream ListFile(path, fstream::in);
  263. for (int i = 0; i < size; i++)
  264. {
  265. getline(ListFile, aol[i].name);
  266. ListFile >> aol[i].score;
  267. ListFile.clear();
  268. ListFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  269. }
  270. ListFile.close();
  271. return aol;
  272. }
  273.  
  274. int getInputType(int& value)
  275. {
  276. bool IsNotCorrect;
  277. string inputLine;
  278. cout << "Выберите способ ввода предложения: 1 - файл, 2 - консоль. " << endl;
  279. do
  280. {
  281. IsNotCorrect = false;
  282. try
  283. {
  284. cin >> inputLine;
  285. value = stoi(inputLine);
  286. }
  287. catch (...)
  288. {
  289. IsNotCorrect = true;
  290. cout << "Введите одно из предложенных чисел" << endl;
  291. }
  292. if ((((value < 1) || (value > 2))) && !IsNotCorrect)
  293. {
  294. cout << "Выберете одно из двух значений! 1 для ввода из файла, или 2 для ввода консоли!" << endl;
  295. IsNotCorrect = true;
  296. }
  297. } while (IsNotCorrect);
  298. return value;
  299. }
  300.  
  301. struct slist* getChoiceType(int value, struct slist* aol, string path, int size)
  302. {
  303. value = getInputType(value);
  304. if (value == 1)
  305. {
  306. aol = getListfromFile(aol, size);
  307. }
  308. if (value == 2)
  309. {
  310. aol = getListFromConsole(size, aol);
  311. }
  312. return aol;
  313. }
  314.  
  315. void printListAfterSorting(struct slist* aol, int size)
  316. {
  317. cout << "Список участников после сортировки: " << endl;
  318. for (int i = 0; i < size; i++)
  319. {
  320. cout << aol[i].name << " " << aol[i].score << endl;
  321. }
  322. cout << endl;
  323. }
  324.  
  325. void printSentenceAfterEditing(string sentence)
  326. {
  327. if (sentence.length() < 1)
  328. {
  329. sentence = "Ни один символ не прошёл проверку! От вашей строки ничего не осталось!";
  330. }
  331. else
  332. {
  333. cout << "Исправленное предложение:" << endl;
  334. }
  335. cout << sentence << endl;
  336. }
  337.  
  338. string Output()
  339. {
  340. string patho;
  341. bool isIncorrect;
  342. isIncorrect = true;
  343. do
  344. {
  345. cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
  346. cin >> patho;
  347. if (filesystem::exists(patho))
  348. {
  349. isIncorrect = false;
  350. }
  351. else
  352. {
  353. cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  354. }
  355.  
  356. } while (isIncorrect);
  357. return patho;
  358. }
  359.  
  360. void saveListToFile(struct slist*& aol, int size)
  361. {
  362. string path;
  363. int ind, bufOne, bufTwo;
  364. ind = 0;
  365. path = Output();
  366. ofstream fout;
  367. fout.open(path + "\output.txt");
  368. fout << "Участники, показавшие 3 лучших результата: " << endl;
  369. fout << aol[0].name << ' ' << aol[0].score << endl;
  370. for (int i = 0; i < size; i++)
  371. {
  372. if ((aol[i].score == aol[0].score) and (aol[i].name != aol[0].name))
  373. {
  374. fout << aol[i].name << ' ' << aol[i].score << endl;
  375. ind = i;
  376. }
  377. }
  378. bufOne = ind;
  379. fout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
  380. for (int i = ind + 1; i < size; i++)
  381. {
  382. if ((aol[i].score == aol[ind + 1].score) and (aol[i].name != aol[ind + 1].name))
  383. {
  384. fout << aol[i].name << ' ' << aol[i].score << endl;
  385. ind = i;
  386. }
  387. }
  388. bufTwo = ind;
  389. if (bufOne != bufTwo)
  390. {
  391. fout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
  392. for (int i = ind + 1; i < size; i++)
  393. {
  394. if ((aol[i].score == aol[ind + 1].score) && (aol[i].name != aol[ind + 1].name))
  395. {
  396. fout << aol[i].name << ' ' << aol[i].score << endl;
  397. }
  398. }
  399. }
  400. else
  401. {
  402. fout << aol[ind + 2].name << ' ' << aol[ind + 2].score << endl;
  403. for (int i = ind + 2; i < size; i++)
  404. {
  405. if ((aol[i].score = aol[ind + 2].score) && (aol[i].name != aol[ind + 2].name))
  406. {
  407. fout << aol[i].name << ' ' << aol[i].score << endl;
  408. }
  409. }
  410. }
  411. fout.close();
  412. cout << "Результат сохранён по указанному пути.";
  413.  
  414. }
  415.  
  416.  
  417. int main()
  418. {
  419. SetConsoleCP(1251);
  420. SetConsoleOutputCP(1251);
  421. setlocale(LC_ALL, "ru");
  422. int size = 0;
  423. string path;
  424. int value;
  425. value = 0;
  426. size = getsize(size);
  427. struct slist* aol = new struct slist[size];
  428. aol = getChoiceType(value, aol, path, size);
  429. printListBeforeSorting(aol, size);
  430. sorting(aol, size);
  431. printListAfterSorting(aol, size);
  432. Top3Output(aol, size);
  433. saveListToFile(aol, size);
  434. }
  435.  
Advertisement
Add Comment
Please, Sign In to add comment