Advertisement
dxvmxnd

c++_1

Dec 17th, 2024 (edited)
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void outputTask() {
  8. cout << "Эта программа сортирует заданный массив чисел методом подсчета." << endl;
  9. }
  10.  
  11. string inputPath() {
  12. string path;
  13. bool isNotCorrect;
  14.  
  15. do {
  16. isNotCorrect = false;
  17. cin >> path;
  18. ifstream fin(path);
  19. if (fin.is_open() and (size(path) > 4) and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
  20. cout << "Файл успешно открыт!" << endl;
  21. }
  22. else {
  23. cout << "Ошибка открытия файла!" << endl;
  24. isNotCorrect = true;
  25. }
  26. fin.close();
  27. } while (isNotCorrect);
  28.  
  29.  
  30. return path;
  31. }
  32.  
  33. void outputArray(int* array, int size) {
  34. cout << "Введенный массив: " << endl;
  35. for (int i = 0; i < size; i++) {
  36. cout << array[i];
  37. cout << " ";
  38. }
  39. cout << endl;
  40. }
  41.  
  42.  
  43. void sortArray(int*& array, int size){
  44.  
  45. int max_value = array[0];
  46. for (int i = 1; i < size; i++) {
  47. if (array[i] > max_value) {
  48. max_value = array[i];
  49. }
  50. }
  51.  
  52. int* count = new int[max_value + 1];
  53.  
  54. for (int i = 0; i < size; i++) {
  55. count[array[i]]++;
  56. }
  57.  
  58. int index = 0;
  59. for (int i = 0; i <= max_value; i++) {
  60. while (count[i] > 0) {
  61. array[index++] = i;
  62. count[i]--;
  63. }
  64. }
  65.  
  66. delete[] count;
  67. }
  68.  
  69. int* outputAnswer(int* array, int size) {
  70. string path;
  71.  
  72. cout << "Отсортированный массив: " << endl;
  73. for (int i = 0; i < (size); i++) {
  74. cout << array[i] << " " ;
  75. }
  76. cout << endl;
  77.  
  78. cout << "Введите путь файла для вывода: " << endl;
  79. path = inputPath();
  80. ofstream fout(path);
  81. fout << "Отсортированный массив: " << endl;
  82. for (int i = 0; i < (size); i++) {
  83. fout << array[i] << " " ;
  84. }
  85. fout.close();
  86. cout << "Данные успешно записаны в файл." << endl;
  87. return 0;
  88. }
  89.  
  90. int inputSizeFromConsole() {
  91. int size;
  92. bool isNotCorrect;
  93.  
  94. do {
  95. cout << "Введите размер массива: " << endl;
  96. isNotCorrect = false;
  97. cin >> size;
  98. if (cin.fail() or (size < 0)) {
  99. isNotCorrect = true;
  100. cout << "Ошибка ввода!" << endl;
  101. cin.clear();
  102. while (cin.get() != '\n');
  103. }
  104. } while (isNotCorrect);
  105.  
  106. return size;
  107.  
  108. }
  109.  
  110. void fillArrayFromConsole(int*& array, int size) {
  111. bool isNotCorrect;
  112.  
  113. for (int i = 0; i < size; i++) {
  114. do {
  115. isNotCorrect = false;
  116. cout << "Введите [" << i + 1 << "] элемент массива от 0 до 1000: " << endl;
  117. cin >> array[i];
  118. if (cin.fail() or (array[i] > 1000) or (array[i] < 0)) {
  119. cout << "Ошибка ввода! Повторите попытку." << endl;
  120. isNotCorrect = true;
  121. cin.clear();
  122. while (cin.get() != '\n');
  123. }
  124. } while (isNotCorrect);
  125.  
  126. }
  127. }
  128.  
  129. int* inputFromConsole(int& size) {
  130. int* array;
  131.  
  132. size = inputSizeFromConsole();
  133. array = new int[size];
  134. fillArrayFromConsole(array, size);
  135. outputArray(array, size);
  136.  
  137. return array;
  138.  
  139. }
  140.  
  141. void outputSize(int size) {
  142. cout << "Введенный размер с файла: " << size << endl;
  143. }
  144.  
  145. void inputSizeFromFile(string path, int& size) {
  146.  
  147. cout << "Считывание размера..." << endl;
  148. ifstream fin(path);
  149. fin >> size;
  150. if (fin.fail() or (size < 0)) {
  151. cout << "Ошибка ввода размера! Введите размер с клавиатуры." << endl;
  152. size = inputSizeFromConsole();
  153. }
  154. else {
  155. outputSize(size);
  156. }
  157.  
  158. fin.close();
  159. }
  160.  
  161. int exceptionRead(int i) {
  162. bool isNotCorrect;
  163. int num;
  164.  
  165. do {
  166. isNotCorrect = false;
  167. cout << "Введите [" << i + 1 << "] элемент массива от 0 до 1000: " << endl;
  168. cin >> num;
  169. if (cin.fail() or (num > 1000)) {
  170. cout << "Ошибка ввода!" << endl;
  171. isNotCorrect = true;
  172. cin.clear();
  173. while (cin.get() != '\n');
  174. }
  175.  
  176. } while (isNotCorrect);
  177.  
  178. return num;
  179. }
  180.  
  181. void inputArrayFromFile(string path, int size, int*& array) {
  182. string skipLine;
  183.  
  184. cout << "Ввод массива.. " << endl;
  185.  
  186. ifstream fin(path);
  187. getline(fin, skipLine);
  188. for (int i = 0; i < size; i++) {
  189. fin >> array[i];
  190. if (fin.fail() or (array[i] > 1000) or (array[i] < 0)) {
  191. cout << "Ошибка ввода элемента с индексом [" << i + 1 << "]. Введите этот элемент с клавиатуры. " << endl;
  192. fin.clear();
  193. fin.ignore(numeric_limits<streamsize>::max(), ' ');
  194. array[i] = exceptionRead(i);
  195. }
  196. }
  197. fin.close();
  198.  
  199. }
  200.  
  201. int* inputFromFile(int& size) {
  202. string path;
  203. int* array;
  204.  
  205. cout << "При вводе из файла учтите, что на первой строке написан размер массива, а далее - сам массив с новой строки." << endl;
  206. cout << "Введите путь файла с данными: " << endl;
  207. path = inputPath();
  208. inputSizeFromFile(path, size);
  209.  
  210. array = new int[size];
  211.  
  212. inputArrayFromFile(path, size, array);
  213. outputArray(array, size);
  214.  
  215. return array;
  216.  
  217.  
  218. }
  219.  
  220. bool isFromFile() {
  221. bool isNotCorrect;
  222. int chooseNum;
  223.  
  224. do {
  225. isNotCorrect = false;
  226. cout << "Выберите, откуда вводить данные: 1, если с файла; 0, если с консоли" << endl;
  227. cin >> chooseNum;
  228. if (cin.fail() or ((chooseNum != 1) and (chooseNum != 0))) {
  229. cout << "Ошибка ввода!" << endl;
  230. isNotCorrect = true;
  231. cin.clear();
  232. while (cin.get() != '\n');
  233. }
  234. } while (isNotCorrect);
  235.  
  236. return (chooseNum == 1);
  237.  
  238. }
  239.  
  240. int* chooseInput(int& size) {
  241. int* array;
  242.  
  243. if (isFromFile()) {
  244. array = inputFromFile(size);
  245. }
  246. else {
  247. array = inputFromConsole(size);
  248. }
  249.  
  250. return array;
  251. }
  252.  
  253. int main() {
  254. system("chcp 1251");
  255.  
  256. int* array;
  257. int size;
  258.  
  259. outputTask();
  260. array = chooseInput(size);
  261. sortArray(array, size);
  262. outputAnswer(array, size);
  263.  
  264. delete[] array;
  265. return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement