Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- 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 MATRIX_LENGTH_MIN = 1;
- constexpr int MATRIX_LENGTH_MAX = 100;
- constexpr int MATRIX_VALUES_MIN = 0;
- constexpr int MATRIX_VALUES_MAX = 10;
- constexpr int ERR_READ_INT_VALUE = -100000000;
- int calculate(int** matrix, int* goods, int m, int n)
- {
- int nowGoods = 0;
- for (int i = 0; i < m; i++) {
- int j = 0;
- bool isGood = true;
- while (j < n && isGood) {
- if (matrix[i][j] < 6 || matrix[i][j] > 8)
- isGood = false;
- j++;
- }
- if (isGood) {
- goods[nowGoods] = i;
- nowGoods++;
- }
- }
- return nowGoods;
- }
- 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;
- while(isInCorrect) {
- if (input)
- cout << "Введите путь до входного файла: ";
- else
- cout << "Введите путь до выходного файла: ";
- cin >> value;
- isInCorrect = false;
- if (!checkFileAvailability(value, input)) {
- isInCorrect = true;
- cout << "Путь ведёт до файла, который недоступен или который не является текстовым файлом!\n";
- }
- }
- return value;
- }
- int takeIntValueFromFile(ifstream &reader)
- {
- int value;
- reader >> value;
- if (reader.fail())
- value = ERR_READ_INT_VALUE;
- return value;
- }
- int** readMatrixFromFile(int* m, int* n)
- {
- bool isInCorrect = true;
- int** matrix;
- while (isInCorrect)
- {
- string filePath = takeCorrectFile(true);
- isInCorrect = false;
- ifstream file(filePath);
- *m = takeIntValueFromFile(file);
- *n = takeIntValueFromFile(file);
- if (*m > (MATRIX_LENGTH_MIN - 1))
- {
- matrix = new int*[*m];
- for (int i = 0; i < *m; i++)
- {
- matrix[i] = new int[*n];
- for (int j = 0; j < *n; j++)
- {
- matrix[i][j] = takeIntValueFromFile(file);
- if (matrix[i][j] == ERR_READ_INT_VALUE)
- isInCorrect = true;
- }
- }
- }
- else
- {
- isInCorrect = true;
- cout << "Матрица не может быть размером меньше, чем " + to_string(MATRIX_LENGTH_MIN) + "x" + to_string(MATRIX_LENGTH_MIN) + "!";
- }
- if (*n > MATRIX_LENGTH_MIN && isInCorrect)
- {
- cout << "В файле содержится неверные значения! Введите путь до файла с верным содержимым!\n";
- }
- }
- return matrix;
- }
- int** readMatrixFromConsole(int* m, int* n)
- {
- *m = takeIntValueInRangeFromConsole("Введите количество учеников (значение должно быть в границах от " + to_string(MATRIX_LENGTH_MIN) + " и до " + to_string(MATRIX_LENGTH_MAX) + "): ", MATRIX_LENGTH_MIN, MATRIX_LENGTH_MAX);
- *n = takeIntValueInRangeFromConsole("Введите количество отметок (значение должно быть в границах от " + to_string(MATRIX_LENGTH_MIN) + " и до " + to_string(MATRIX_LENGTH_MAX) + "): ", MATRIX_LENGTH_MIN, MATRIX_LENGTH_MAX);
- int** matrix = new int*[*m];
- for (int i = 0; i < *m; i++)
- {
- matrix[i] = new int[*n];
- for (int j = 0; j < *n; j++) {
- matrix[i][j] = takeIntValueInRangeFromConsole("Отметка " + to_string(j + 1) + " человека " + to_string(i + 1) + " (значение должно быть в границах от " + to_string(MATRIX_VALUES_MIN) + " и до " + to_string(MATRIX_VALUES_MAX) + "): ", MATRIX_VALUES_MIN, MATRIX_VALUES_MAX);
- }
- }
- return matrix;
- }
- int** readMatrix(int* m, int* n)
- {
- int type;
- int** matrix;
- cout << "\nКак считать значения для поиска решения?\n";
- cout << "0 - Из ввода с клавиатуры (консоль)\n";
- cout << "1 - Из файла\n";
- type = takeIntValueInRangeFromConsole("Выбранный вариант ввода: ", SCAN_TYPE_MIN, SCAN_TYPE_MAX);
- if (type == 1)
- matrix = readMatrixFromFile(m, n);
- else
- matrix = readMatrixFromConsole(m, n);
- return matrix;
- }
- bool saveResultIntoFile(const string &filePath, const int* goods, const int goodsCount)
- {
- int i;
- bool saved;
- saved = true;
- ofstream file(filePath);
- if (file.good()) {
- for (i = 0; i < goodsCount; i++)
- file << (goods[i] + 1) << " ";
- } else
- saved = false;
- file.close();
- return saved;
- }
- void printResultIntoConsole(const int* goods, const int goodsCount) {
- cout << "\nНомера хорошистов: \n";
- for (int i = 0; i < goodsCount; i++) {
- cout << (goods[i] + 1) << " ";
- }
- cout << "\n";
- }
- void printResult(const int* goods, const int goodsCount)
- {
- 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(goods, goodsCount);
- break;
- case 1:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, goods, goodsCount);
- break;
- case 2:
- output = takeCorrectFile(false);
- saved = saveResultIntoFile(output, goods, goodsCount);
- printResultIntoConsole(goods, goodsCount);
- break;
- }
- if (saved)
- cout << "Результат записан в выходной файл!" << "\n";
- }
- int main()
- {
- int** matrix;
- int* goods;
- int goodsCount, m, n;
- matrix = readMatrix(&m, &n);
- goods = new int[m];
- goodsCount = calculate(matrix, goods, m, n);
- printResult(goods, goodsCount);
- for (int i = 0; i < m; i++) {
- delete[] matrix[i];
- }
- delete[] matrix;
- delete[] goods;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment