negtab

2.19.4 C++

Oct 30th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void printTask()
  6. {
  7. cout << "Эта программа объединяет две последовательности чисел" << endl;
  8. }
  9.  
  10. string readPathFile()
  11. {
  12. string pathToFile;
  13. bool isInCorrect;
  14. do
  15. {
  16. isInCorrect = false;
  17. cout << "Введите путь к файлу с расширением.txt с количеством точек и координатами точек: ";
  18. cin >> pathToFile;
  19. if (pathToFile.size() < 5 || pathToFile[pathToFile.length() - 4] != '.' || pathToFile[pathToFile.length() - 3] != 't' || pathToFile[pathToFile.length() - 2] != 'x' || pathToFile[pathToFile.length() - 1] != 't')
  20. {
  21. cout << "Расширение файла не .txt!\n";
  22. isInCorrect = true;
  23. }
  24. } while (isInCorrect);
  25. return pathToFile;
  26. }
  27.  
  28. bool isNotExists(string& pathToFile)
  29. {
  30. bool isRight = true;
  31. ifstream file(pathToFile);
  32. if (file.good())
  33. isRight = false;
  34. file.close();
  35. return isRight;
  36. }
  37. bool isNotAbleToReading(string& pathToFile)
  38. {
  39. bool isRight;
  40. isRight = true;
  41. ifstream file(pathToFile);
  42. if (file.is_open())
  43. isRight = false;
  44. file.close();
  45. return isRight;
  46. }
  47. bool isNotAbleToWriting(string& pathToFile)
  48. {
  49. bool isRight;
  50. isRight = true;
  51. ofstream file(pathToFile, ios::app);
  52. if (file.is_open())
  53. isRight = false;
  54. file.close();
  55. return isRight;
  56. }
  57. bool isEmpty(string& pathToFile)
  58. {
  59. bool isRight;
  60. isRight = false;
  61. ifstream file(pathToFile);
  62. if (file.peek() == ifstream::traits_type::eof())
  63. isRight = true;
  64. file.close();
  65. return isRight;
  66. }
  67.  
  68. void getFileNormalReading(string& pathToFile)
  69. {
  70. bool isInCorrect;
  71. do
  72. {
  73. isInCorrect = false;
  74. pathToFile = readPathFile();
  75. if (isNotExists(pathToFile))
  76. {
  77. isInCorrect = true;
  78. cout << "Проверьте корректность ввода пути к файлу!" << endl;
  79. }
  80. if (isInCorrect || isNotAbleToReading(pathToFile))
  81. {
  82. isInCorrect = true;
  83. cout << "Файл закрыт для чтения!" << endl;
  84. }
  85. if (isInCorrect || isEmpty(pathToFile))
  86. {
  87. isInCorrect = true;
  88. cout << "Файл пуст!" << endl;
  89. }
  90. } while (isInCorrect);
  91. }
  92.  
  93. void getFileNormalWriting(string& pathToFile)
  94. {
  95. bool isInCorrect;
  96. do
  97. {
  98. isInCorrect = false;
  99. pathToFile = readPathFile();
  100. if (isNotExists(pathToFile))
  101. {
  102. isInCorrect = true;
  103. cout << "Проверьте корректность ввода пути к файлу!" << endl;
  104. }
  105. if (isInCorrect && isNotAbleToWriting(pathToFile))
  106. {
  107. isInCorrect = true;
  108. cout << "Файл закрыт для записи!" << endl;
  109. }
  110. } while (isInCorrect);
  111. }
  112.  
  113. int inputInt(int min, int max)
  114. {
  115. int n;
  116. bool isInCorrect;
  117.  
  118. n = 0;
  119. isInCorrect = true;
  120.  
  121. do
  122. {
  123. isInCorrect = false;
  124. cin >> n;
  125. if (cin.fail() || n < min || n > max)
  126. {
  127. isInCorrect = true;
  128. cin.clear();
  129. while (cin.get() != '\n');
  130. cout << "Неверный ввод" << endl;
  131. }
  132. } while (isInCorrect);
  133.  
  134. return n;
  135. }
  136.  
  137. int inputIntWithText(string s, double min, double max) {
  138. int n;
  139. bool isInCorrect;
  140.  
  141. do {
  142. cout << s << " от " << min << " до " << max << " : ";
  143. isInCorrect = false;
  144. cin >> n;
  145. if (cin.fail() || n > max || n < min)
  146. {
  147. isInCorrect = true;
  148. cin.clear();
  149. while (cin.get() != '\n');
  150. cout << "Неверный ввод" << endl;
  151. }
  152. } while (isInCorrect);
  153.  
  154. return n;
  155. }
  156.  
  157. int* inputIntArray(string s,int min, int max, int size)
  158. {
  159. int* array;
  160. bool isInCorrect;
  161.  
  162. array = new int[size];
  163.  
  164. for(int i = 0; i < size; i++)
  165. {
  166. do {
  167. cout << s << " от " << min << " до " << max << ": ";
  168. isInCorrect = false;
  169. cin >> array[i];
  170. if (cin.fail() || array[i] < min || array[i] > max)
  171. {
  172. isInCorrect = true;
  173. cin.clear();
  174. while (cin.get() != '\n');
  175. cout << "Неверный ввод" << endl;
  176. }
  177. } while (isInCorrect);
  178. }
  179. return array;
  180. }
  181.  
  182. void readFileSizeOfArrays(string& pathToFile, int& size1, int& size2, int min, int max, bool& isEndOfFile)
  183. {
  184. string nullNum;
  185. ifstream file(pathToFile);
  186.  
  187. file >> size1;
  188. if (file.fail() || size1 > max || size1 < min)
  189. {
  190. file.clear();
  191. while (file.get() != '\n');
  192. size1 = inputIntWithText("Неверный ввод размера первой числовой последовательности, введите с консоли", min, max);
  193. }
  194.  
  195. if(file.eof())
  196. {
  197. if(isEndOfFile)
  198. {
  199. isEndOfFile = true;
  200. cout << "В файле не хватает чисел для числовой последовательности";
  201. }
  202. }
  203. else
  204. {
  205. for(int i = 0; i < size1; i++)
  206. {
  207. if(file.eof())
  208. {
  209. if(!isEndOfFile)
  210. {
  211. isEndOfFile = true;
  212. cout << "В файле не хватает чисел для числовой последовательности";
  213. }
  214. }
  215. else
  216. {
  217. file >> nullNum;
  218. }
  219. }
  220. if(file.eof())
  221. {
  222. if(!isEndOfFile)
  223. {
  224. isEndOfFile = true;
  225. cout << "В файле не хватает чисел для числовой последовательности";
  226. }
  227. }
  228. else
  229. {
  230. file >> size2;
  231. if (file.fail() || size2 > max || size2 < min)
  232. {
  233. file.clear();
  234. while (file.get() != '\n');
  235. cout << "Неверный ввод размера второй числовой последовательности, введите с консоли: ";
  236. size2 = inputInt(min, max);
  237. }
  238. }
  239. }
  240. file.close();
  241. }
  242.  
  243. void readFileIntArray(string& pathToFile, int min, int max, int*& array1, int*& array2, int size1, int size2, bool& isEndOfFile)
  244. {
  245. string nullElement;
  246. ifstream file(pathToFile);
  247.  
  248. array1 = new int[size1];
  249. array2 = new int[size2];
  250. file >> nullElement;
  251.  
  252. for(int i = 0; i < size1; i++)
  253. {
  254. file >> array1[i];
  255. if (file.fail() || array1[i] > max || array1[i] < min)
  256. {
  257. file.clear();
  258. while (file.get() != '\n');
  259. cout << "Неверный ввод в первой числовой последовательности, введите с консоли: ";
  260. array1[i] = inputInt(min, max);
  261. }
  262. }
  263.  
  264. file >> nullElement;
  265. for(int j = 0; j < size2; j++)
  266. {
  267. if(file.eof())
  268. {
  269. if(!isEndOfFile)
  270. {
  271. cout << "В файле не хватает чисел для числовой последовательности";
  272. isEndOfFile = true;
  273. }
  274. }
  275. else
  276. {
  277. file >> array2[j];
  278. if (file.fail() || array2[j] > max || array2[j] < min)
  279. {
  280. file.clear();
  281. while (file.get() != '\n');
  282. cout << "Неверный ввод в первой числовой последовательности, введите с консоли: ";
  283. array2[j] = inputInt(min, max);
  284. }
  285. }
  286. }
  287. \
  288. }
  289.  
  290. void sortArray(int*& array, int size)
  291. {
  292. for(int i = 0; i < size; i++)
  293. {
  294. for(int j = 0; j < size - i - 1; j++)
  295. {
  296. if(array[j] > array[j + 1])
  297. {
  298. array[j] = array[j] + array[j + 1];
  299. array[j + 1] = array[j] - array[j + 1];
  300. array[j] = array[j] - array[j + 1];
  301. }
  302. }
  303. }
  304. }
  305.  
  306. int* removeZeros(int* result, int& size, bool isOneZero)
  307. {
  308. int newSize = 0;
  309. int* newArray;
  310.  
  311. for (int i = 0; i < size; i++)
  312. {
  313. if (result[i] != 0)
  314. {
  315. newSize++;
  316. }
  317. }
  318. if(isOneZero)
  319. {
  320. newSize +=1;
  321. }
  322. if (newSize == 0)
  323. {
  324. newArray = new int[1]{0};
  325. size = 1;
  326. }
  327. else
  328. {
  329. newArray = new int[newSize];
  330. int index = 0;
  331.  
  332. for (int i = 0; i < size; i++)
  333. {
  334. if(result[i] != 0)
  335. {
  336. newArray[index++] = result[i];
  337. }
  338. else
  339. {
  340. if(result[i] == 0 && isOneZero)
  341. {
  342. newArray[index++] = 0;
  343. isOneZero = false;
  344. }
  345. }
  346. }
  347. size = newSize;
  348. }
  349.  
  350.  
  351. return newArray;
  352. }
  353. void unificationArray (int* array1, int size1, int* array2, int size2, int*& resultArray, int& resultSize, bool isEndOfFile)
  354. {
  355. if(!isEndOfFile)
  356. {
  357. bool isUnic;
  358. bool isFirst;
  359.  
  360. isFirst = false;
  361. resultSize = size1 + size2;
  362.  
  363. sortArray(array1, size1);
  364. sortArray(array2, size2);
  365. resultArray = new int[resultSize];
  366.  
  367. for(int i = 0; i < size1; i++)
  368. {
  369. resultArray[i] = array1[i];
  370. }
  371.  
  372. for(int i = 0; i < size2; i++)
  373. {
  374. isUnic = true;
  375. if(!isFirst && array2[i] == 0)
  376. {
  377. isFirst = true;
  378. }
  379. for(int j = 0; j < size1; j++)
  380. {
  381. if(!isFirst && array1[i] == 0)
  382. {
  383. isFirst = true;
  384. }
  385. if(array1[j] == array2[i])
  386. {
  387. isUnic = false;
  388. }
  389. }
  390. if(isUnic)
  391. {
  392. resultArray[size1 + i] = array2[i];
  393. }
  394. }
  395.  
  396. resultArray = removeZeros(resultArray, resultSize, isFirst);
  397. sortArray(resultArray, resultSize);
  398. }
  399. }
  400.  
  401. void writeSolveToFile(string& pathToFile, int* array, int size)
  402. {
  403. ofstream file(pathToFile);
  404. file << "Совмещенная последовательность: ";
  405. for(int i = 0; i < size; i++)
  406. file << array[i] << " ";
  407. file.close();
  408. }
  409.  
  410. int chooseTheInput()
  411. {
  412. cout << "Выберите ввод из консоли(1) или из файла(2): ";
  413. return inputInt(1, 2 );
  414. }
  415.  
  416. int chooseTheOutput()
  417. {
  418. cout << "Выберите вывод в консоль(1) или в файл(2): ";
  419. return inputInt(1, 2);
  420. }
  421.  
  422. void input(int& size1, int& size2, int*& array1, int*& array2, bool& isEndOfFile)
  423. {
  424. int intChooseTheInput;
  425. intChooseTheInput = chooseTheInput();
  426. if(intChooseTheInput == 1)
  427. {
  428. size1 = inputIntWithText("Введите количество элементов числовой последовательности",1, 100);
  429. array1 = inputIntArray("Введите члены числовой последовательности", -10000, 10000, size1);
  430. size2 = inputIntWithText("Введите количество элементов числовой последовательности", 1, 100);
  431. array2 = inputIntArray("Введите члены числовой последовательности", -10000, 10000, size2);
  432. }
  433. else if(intChooseTheInput == 2)
  434. {
  435. isEndOfFile = false;
  436. string pathToFile;
  437.  
  438. getFileNormalReading(pathToFile);
  439.  
  440. readFileSizeOfArrays(pathToFile,size1, size2, 1, 100, isEndOfFile);
  441. readFileIntArray(pathToFile, -10000, 10000, array1, array2, size1, size2, isEndOfFile);
  442. }
  443. }
  444.  
  445. void output(int size, int* array, bool isEndOfFile)
  446. {
  447. if(!isEndOfFile)
  448. {
  449. int intChooseTheOutput;
  450. intChooseTheOutput = chooseTheOutput();
  451. if(intChooseTheOutput == 1)
  452. {
  453. cout<< "Полученная числова последовательность: ";
  454. for(int i = 0; i < size; i++)
  455. cout << array[i] << " ";
  456. }
  457. else if(intChooseTheOutput == 2)
  458. {
  459. string pathToFile;
  460.  
  461. getFileNormalWriting(pathToFile);
  462.  
  463. cout<< "Полученная числова последовательноть записана в файл равна: ";
  464. for(int i = 0; i < size; i++)
  465. cout << array[i] << " ";
  466. writeSolveToFile(pathToFile, array, size);
  467. cout << endl;
  468. }
  469. }
  470. }
  471.  
  472. int main()
  473. {
  474. int size1, size2, size3;
  475. int *array1, *array2, *array3;
  476. bool isEndOfFile;
  477. size1 = 0;
  478. size2 = 0;
  479. size3 = 0;
  480. array1 = new int[1];
  481. array2 = new int[1];
  482. array3 = new int[1];
  483. isEndOfFile = false;
  484.  
  485. printTask();
  486. input(size1, size2, array1, array2, isEndOfFile);
  487. unificationArray(array1, size1, array2, size2,array3,size3, isEndOfFile);
  488. output(size3, array3, isEndOfFile);
  489.  
  490. delete[] array1;
  491. delete[] array2;
  492. delete[] array3;
  493. }
  494.  
Advertisement
Add Comment
Please, Sign In to add comment