MaksNew

Untitled

Nov 20th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 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 = true;
  122. i = 1;
  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] <= 'я')) && IsNotCorrect)
  135. {
  136. IsNotCorrect = false;
  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')) && IsNotCorrect)
  151. {
  152. IsNotCorrect = false;
  153. }
  154. }
  155. }
  156. }
  157. i++;
  158. }
  159. if (i != size*2+1)
  160. {
  161. IsNotCorrect = true;
  162. }
  163. listFile.close();
  164. return IsNotCorrect;
  165.  
  166. }
  167.  
  168. string getFilePath(int size)
  169. {
  170. string path;
  171. bool isIncorrect;
  172. do
  173. {
  174. cout << "Введите абсолютный путь к файлу: " << endl;
  175. cin >> path;
  176. isIncorrect = true;
  177. if (!std::filesystem::exists(path))
  178. {
  179. cout << "Файл не найден. Проверьте введённый путь." << endl;
  180. }
  181. else
  182. {
  183. if (isFileCorrect(size, path))
  184. {
  185. isIncorrect = false;
  186. }
  187. else
  188. {
  189. cout << "Данные в файле некорректны\n";
  190. }
  191. }
  192. } while (isIncorrect);
  193. cout << "didifidg";
  194. return path;
  195. }
  196.  
  197. struct slist* getListfromFile(struct slist* aol, int size)
  198. {
  199. string path;
  200. path = getFilePath(size);
  201. fstream ListFile(path, fstream::in);
  202. for (int i = 0; i < size; i++)
  203. {
  204. getline(ListFile, aol[i].name);
  205. ListFile >> aol[i].score;
  206. ListFile.clear();
  207. ListFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  208. }
  209. ListFile.close();
  210. return aol;
  211. }
  212.  
  213. int getInputType(int& value)
  214. {
  215. bool IsNotCorrect;
  216. string inputLine;
  217. cout << "Выберите способ ввода предложения: 1 - файл, 2 - консоль. " << endl;
  218. do
  219. {
  220. IsNotCorrect = false;
  221. try
  222. {
  223. cin >> inputLine;
  224. value = stoi(inputLine);
  225. }
  226. catch (...)
  227. {
  228. IsNotCorrect = true;
  229. cout << "Введите одно из предложенных чисел" << endl;
  230. }
  231. if ((((value < 1) || (value > 2))) && !IsNotCorrect)
  232. {
  233. cout << "Выберете одно из двух значений! 1 для ввода из файла, или 2 для ввода консоли!" << endl;
  234. IsNotCorrect = true;
  235. }
  236. } while (IsNotCorrect);
  237. return value;
  238. }
  239.  
  240. struct slist* getChoiceType(int value, struct slist* aol, string path, int size)
  241. {
  242. value = getInputType(value);
  243. if (value == 1)
  244. {
  245. aol = getListfromFile(aol, size);
  246. }
  247. if (value == 2)
  248. {
  249. aol = getListFromConsole(size, aol);
  250. }
  251. return aol;
  252. }
  253.  
  254. void printSentenceBeforeEditing(string sentence)
  255. {
  256. cout << "Исходное предложение: " << endl;
  257. cout << sentence << endl;
  258. cout << endl;
  259. }
  260.  
  261. string editing(string sentence)
  262. {
  263. int i;
  264. i = 0;
  265. while (i < sentence.length())
  266. {
  267. if (!(('0' <= sentence[i]) && (sentence[i] <= '9') || ('A' <= sentence[i]) && (sentence[i] <='Z') || ('a' <= sentence[i]) && (sentence[i] <= 'z') || (sentence[i] == ' ')))
  268. {
  269. sentence.erase(i, 1);
  270. i--;
  271. }
  272. i++;
  273. }
  274. return sentence;
  275. }
  276.  
  277. void printSentenceAfterEditing(string sentence)
  278. {
  279. if (sentence.length() < 1)
  280. {
  281. sentence = "Ни один символ не прошёл проверку! От вашей строки ничего не осталось!";
  282. }
  283. else
  284. {
  285. cout << "Исправленное предложение:" << endl;
  286. }
  287. cout << sentence << endl;
  288. }
  289.  
  290. string Output()
  291. {
  292. string patho;
  293. bool isIncorrect;
  294. isIncorrect = true;
  295. do
  296. {
  297. cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
  298. cin >> patho;
  299. if (filesystem::exists(patho))
  300. {
  301. isIncorrect = false;
  302. }
  303. else
  304. {
  305. cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  306. }
  307.  
  308. } while (isIncorrect);
  309. return patho;
  310. }
  311.  
  312. void saveResultToFile(string sentence)
  313. {
  314. string path;
  315. path = Output();
  316. ofstream fout;
  317. fout.open(path + "\output.txt");
  318. fout << sentence << endl;
  319. fout.close();
  320. cout << "Результат сохранён по указанному пути.";
  321.  
  322. }
  323.  
  324.  
  325. int main()
  326. {
  327. //SetConsoleCP(1251);
  328. //SetConsoleOutputCP(1251);
  329. setlocale(LC_ALL, "ru");
  330. int size = 0;
  331. string path;
  332. int value;
  333. value = 0;
  334. size = getsize(size);
  335. struct slist* aol = new struct slist[size];
  336. aol = getChoiceType(value, aol, path, size);
  337. sorting(aol, size);
  338. for (int i = 0; i < size; i++)
  339. {
  340. cout << aol[i].name << " " << aol[i].score << endl;
  341. }
  342.  
  343. //printSentenceBeforeEditing(sentence);
  344. //sentence = editing(sentence);
  345. //printSentenceAfterEditing(sentence);
  346. //saveResultToFile(sentence);
  347. }
  348.  
Advertisement
Add Comment
Please, Sign In to add comment