Filage

Files1

Mar 19th, 2024
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_SIZE = 100;
  8.  
  9. int takeNum(int Min, int Max) {
  10.     const string error = "Проверьте корректность введнных данных!\n";
  11.     bool isIncorrect;
  12.     int num;
  13.     do {
  14.         isIncorrect = false;
  15.         cin >> num;
  16.         if (cin.fail()) {
  17.             isIncorrect = true;
  18.             cout << error;
  19.             cin.clear();
  20.             while (cin.get() != '\n');
  21.         }
  22.         if (!isIncorrect && cin.get() != '\n') {
  23.             cin.clear();
  24.             while (cin.get() != '\n');
  25.             cout << error;
  26.             isIncorrect = true;
  27.         }
  28.         if (!isIncorrect && (num < Min || num > Max)) {
  29.             isIncorrect = true;
  30.             cout << error;
  31.         }
  32.     } while (isIncorrect);
  33.     return num;
  34. }
  35.  
  36. string wayToFile() {
  37.     fstream checkFile;
  38.     string way;
  39.     bool isIncorrect;
  40.     do {
  41.         isIncorrect = false;
  42.         cout << "Введите путь к файлу: ";
  43.         cin >> way;
  44.         checkFile.open(way);
  45.         if (!checkFile.is_open()) {
  46.             cout << "Проверьте правильность и нахождение вашего файла по заданному пути\n";
  47.             isIncorrect = true;
  48.         }
  49.         else {
  50.             isIncorrect = false;
  51.         }
  52.         checkFile.close();
  53.     } while (isIncorrect);
  54.     return way;
  55. }
  56.  
  57. void readNumInFile(int arr[], int& size) {
  58.     fstream file;
  59.     int num;
  60.     string fileName = wayToFile();
  61.     file.open(fileName);
  62.     if (!file.bad()) {
  63.         while (file >> num && size < MAX_SIZE)
  64.             arr[size++] = num;
  65.     }
  66.     file.close();
  67. }
  68.  
  69. void sortElements(int arr[], int size_f) {
  70.     int even_numbers[MAX_SIZE], odd_numbers[MAX_SIZE];
  71.     int size_even = 0, size_odd = 0;
  72.     for (int i = 0; i < size_f; ++i) {
  73.         if (arr[i] % 2 == 0) {
  74.             even_numbers[size_even++] = arr[i];
  75.         }
  76.         else {
  77.             odd_numbers[size_odd++] = arr[i];
  78.         }
  79.     }
  80.     int i = 0, index_even = 0, index_odd = 0;
  81.     while (i < size_f) {
  82.         for (int count = 0; count < 2 && index_even < size_even; ++count) {
  83.             arr[i++] = even_numbers[index_even++];
  84.         }
  85.         for (int count = 0; count < 2 && index_odd < size_odd; ++count) {
  86.             arr[i++] = odd_numbers[index_odd++];
  87.         }
  88.     }
  89. }
  90.  
  91. void writeNumInFile(int arr[], int size) {
  92.     string fileName = "g.txt";
  93.     ofstream file(fileName);
  94.     for (int i = 0; i < size; ++i) {
  95.         file << arr[i] << " ";
  96.     }
  97.     cout << "Файл успешно создан!";
  98.     file.close();
  99. }
  100.  
  101. int main() {
  102.     setlocale(LC_ALL, "Russian");
  103.     int arr[MAX_SIZE];
  104.     int size_f = 0;
  105.     readNumInFile(arr, size_f);
  106.     sortElements(arr, size_f);
  107.     writeNumInFile(arr, size_f);
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment