MaksNew

Untitled

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