Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- const int MAX_SIZE = 100;
- int takeNum(int Min, int Max) {
- const string error = "Проверьте корректность введнных данных!\n";
- bool isIncorrect;
- int num;
- do {
- isIncorrect = false;
- cin >> num;
- if (cin.fail()) {
- isIncorrect = true;
- cout << error;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && cin.get() != '\n') {
- cin.clear();
- while (cin.get() != '\n');
- cout << error;
- isIncorrect = true;
- }
- if (!isIncorrect && (num < Min || num > Max)) {
- isIncorrect = true;
- cout << error;
- }
- } while (isIncorrect);
- return num;
- }
- string wayToFile() {
- fstream checkFile;
- string way;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Введите путь к файлу: ";
- cin >> way;
- checkFile.open(way);
- if (!checkFile.is_open()) {
- cout << "Проверьте правильность и нахождение вашего файла по заданному пути\n";
- isIncorrect = true;
- }
- else {
- isIncorrect = false;
- }
- checkFile.close();
- } while (isIncorrect);
- return way;
- }
- void readNumInFile(int arr[], int& size) {
- fstream file;
- int num;
- string fileName = wayToFile();
- file.open(fileName);
- if (!file.bad()) {
- while (file >> num && size < MAX_SIZE)
- arr[size++] = num;
- }
- file.close();
- }
- void sortElements(int arr[], int size_f) {
- int even_numbers[MAX_SIZE], odd_numbers[MAX_SIZE];
- int size_even = 0, size_odd = 0;
- for (int i = 0; i < size_f; ++i) {
- if (arr[i] % 2 == 0) {
- even_numbers[size_even++] = arr[i];
- }
- else {
- odd_numbers[size_odd++] = arr[i];
- }
- }
- int i = 0, index_even = 0, index_odd = 0;
- while (i < size_f) {
- for (int count = 0; count < 2 && index_even < size_even; ++count) {
- arr[i++] = even_numbers[index_even++];
- }
- for (int count = 0; count < 2 && index_odd < size_odd; ++count) {
- arr[i++] = odd_numbers[index_odd++];
- }
- }
- }
- void writeNumInFile(int arr[], int size) {
- string fileName = "g.txt";
- ofstream file(fileName);
- for (int i = 0; i < size; ++i) {
- file << arr[i] << " ";
- }
- cout << "Файл успешно создан!";
- file.close();
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- int arr[MAX_SIZE];
- int size_f = 0;
- readNumInFile(arr, size_f);
- sortElements(arr, size_f);
- writeNumInFile(arr, size_f);
- }
Advertisement
Add Comment
Please, Sign In to add comment