Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- constexpr string DEFAULT_INPUT_FILE = "./input.txt";
- constexpr string DEFAULT_OUTPUT_FILE = "./output.txt";
- constexpr int PRINT_TYPE_MIN = 0;
- constexpr int PRINT_TYPE_MAX = 2;
- constexpr int SCAN_TYPE_MIN = 0;
- constexpr int SCAN_TYPE_MAX = 1;
- constexpr int ARRAY_LENGTH_MIN = 1;
- constexpr int ARRAY_LENGTH_MAX = 1000;
- constexpr int ARRAY_VALUES_MIN = -10000;
- constexpr int ARRAY_VALUES_MAX = 10000;
- constexpr int ERR_READ_INT_VALUE = 0x10000000;
- void sortArray(int* arr, const int length) {
- cout << "[";
- for (int k = 0; k < length - 1; k++) {
- cout << arr[k] << ", ";
- }
- cout << arr[length - 1] << "]; i = " << 0 << "\n";
- for (int i = 1; i < length; i++) {
- int key = arr[i];
- int j = i - 1;
- while (j >= 0 && arr[j] > key) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key;
- cout << "[";
- for (int k = 0; k < length - 1; k++) {
- cout << arr[k] << ", ";
- }
- cout << arr[length - 1] << "]; i = " << i << "\n";
- }
- }
- bool isTextFile(const string &filePath)
- {
- bool isTxt;
- isTxt = (filePath.length() > 4 &&
- filePath[filePath.length() - 4] == '.' &&
- filePath[filePath.length() - 3] == 't' &&
- filePath[filePath.length() - 2] == 'x' &&
- filePath[filePath.length() - 1] == 't');
- return isTxt;
- }
- bool checkFileAvailability(const string &filePath, const bool read)
- {
- ios::openmode mode;
- if (read)
- mode = ifstream::in;
- else
- mode = ifstream::app;
- fstream file(filePath, mode);
- bool available;
- available = file.good();
- file.close();
- if (available && !isTextFile(filePath))
- available = false;
- return available;
- }
- int takeIntValueFromConsole(const string& text)
- {
- bool isInCorrect;
- int value;
- isInCorrect = true;
- while (isInCorrect)
- {
- cout << text;
- cin >> value;
- isInCorrect = false;
- if (cin.fail())
- {
- cout << "Введите число, а не строку или что-то иное!\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- isInCorrect = true;
- }
- }
- return value;
- }
- int takeIntValueInRangeFromConsole(const string& text, const int min, const int max)
- {
- bool isInCorrect;
- int value;
- isInCorrect = true;
- value = 0;
- while (isInCorrect)
- {
- value = takeIntValueFromConsole(text);
- isInCorrect = false;
- if (value < min || value > max)
- {
- cout << "Число должно находится в границах от " << min << " до " << max << "\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- isInCorrect = true;
- }
- }
- return value;
- }
- string takeCorrectFile(const bool input)
- {
- bool isInCorrect;
- string value;
- isInCorrect = true;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- while(isInCorrect) {
- if (input)
- cout << "Введите путь до входного файла (пустая строка – " << DEFAULT_INPUT_FILE << "): ";
- else
- cout << "Введите путь до выходного файла (пустая строка – " << DEFAULT_OUTPUT_FILE << "): ";
- getline(cin, value);
- if (value.empty())
- {
- if (input)
- value = DEFAULT_INPUT_FILE;
- else
- value = DEFAULT_OUTPUT_FILE;
- }
- isInCorrect = false;
- if (!checkFileAvailability(value, input)) {
- isInCorrect = true;
- cout << "Путь ведёт до файла, который недоступен или который не является текстовым файлом!\n";
- }
- }
- return value;
- }
- int takeIntValueFromFile(ifstream &reader)
- {
- int value;
- reader >> value;
- if (reader.fail() || reader.get() == ',')
- value = ERR_READ_INT_VALUE;
- reader.clear();
- return value;
- }
- int* readArrayFromFile(int* n)
- {
- bool isInCorrect = true;
- int* sequences;
- while (isInCorrect)
- {
- string filePath = takeCorrectFile(true);
- isInCorrect = false;
- ifstream file(filePath);
- *n = takeIntValueFromFile(file);
- if (*n > (ARRAY_LENGTH_MIN - 1) && *n < (ARRAY_LENGTH_MAX + 1))
- {
- sequences = new int[*n];
- for (int i = 0; i < *n; i++)
- {
- sequences[i] = takeIntValueFromFile(file);
- if (sequences[i] == ERR_READ_INT_VALUE)
- isInCorrect = true;
- }
- }
- else
- isInCorrect = true;
- if (*n == ERR_READ_INT_VALUE && isInCorrect)
- {
- cout << "В файле содержится неверные значения! Введите путь до файла с верным содержимым!\n";
- }
- else if (isInCorrect)
- {
- cout << "Массив не может быть размером меньше, чем " + to_string(ARRAY_LENGTH_MIN) + "!\n";
- }
- }
- return sequences;
- }
- int* readArrayFromConsole(int* n)
- {
- *n = takeIntValueInRangeFromConsole("Введите длину массива (значение должно быть в границах от " + to_string(ARRAY_LENGTH_MIN) + " и до " + to_string(ARRAY_LENGTH_MAX) + "): ", ARRAY_LENGTH_MIN, ARRAY_LENGTH_MAX);
- int* sequences;
- sequences = new int[*n];
- for (int i = 0; i < *n; i++)
- sequences[i] = takeIntValueInRangeFromConsole("Элемент " + to_string(i + 1) + " последовательности A (значение должно быть в границах от " + to_string(ARRAY_VALUES_MIN) + " и до " + to_string(ARRAY_VALUES_MAX) + "): ", ARRAY_VALUES_MIN, ARRAY_VALUES_MAX);
- return sequences;
- }
- int* readArray(int* n)
- {
- int type;
- int* sequences;
- cout << "\nКак считать значения для поиска решения?\n";
- cout << "0 - Из ввода с клавиатуры (консоль)\n";
- cout << "1 - Из файла\n";
- type = takeIntValueInRangeFromConsole("Выбранный вариант ввода: ", SCAN_TYPE_MIN, SCAN_TYPE_MAX);
- if (type == 1)
- sequences = readArrayFromFile(n);
- else
- sequences = readArrayFromConsole(n);
- return sequences;
- }
- bool saveResultIntoFile(const string &filePath, const int* arr, const int length)
- {
- bool saved;
- saved = true;
- ofstream file(filePath);
- if (file.good())
- {
- for (int i = 0; i < length; i++)
- file << arr[i] << " ";
- }
- else
- saved = false;
- file.close();
- return saved;
- }
- void printResultIntoConsole(const int* arr, const int length)
- {
- cout << "\nОтсортированный массив:\n";
- for (int i = 0; i < length; i++)
- cout << arr[i] << " ";
- cout << "\n";
- }
- void printResult(const int* arr, const int length)
- {
- bool saved;
- int type;
- cout << "\nКуда вывести результат решения?\n";
- cout << "0 - Только консоль\n";
- cout << "1 - Только в файл\n";
- cout << "2 - И в файл, и в консоль\n";
- type = takeIntValueInRangeFromConsole("Выбранный вариант вывода: ", PRINT_TYPE_MIN, PRINT_TYPE_MAX);
- saved = false;
- string output;
- switch (type)
- {
- case 0:
- printResultIntoConsole(arr, length);
- break;
- case 1:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, arr, length);
- break;
- case 2:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, arr, length);
- printResultIntoConsole(arr, length);
- break;
- }
- if (saved)
- cout << "Результат записан в выходной файл!" << "\n";
- }
- int main()
- {
- cout << "3. Сортировка методом простых вставок.\n";
- int* array;
- int n;
- array = readArray(&n);
- sortArray(array, n);
- printResult(array, n);
- delete[] array;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment