Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <filesystem>
- #include <sstream>
- using namespace std;
- int getMatrixSize()
- {
- int order;
- bool IsNotCorrect;
- string inputLine;
- do
- {
- cout << "Введите порядок матрицы: " << endl;
- IsNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- order = stoi(inputLine);
- }
- catch (...)
- {
- IsNotCorrect = true;
- cout << "Порядок матрицы должен быть положительным числом." << endl;
- }
- if (!IsNotCorrect && (((order < 1) || (order > 10000))))
- {
- cout << "Порядок матрицы должен принадлежать промежутку от 1 до 10000" << endl;
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return order;
- }
- int getMatrixElement()
- {
- int element;
- bool IsNotCorrect;
- string inputLine;
- do
- {
- cout << "Введите элемент матрицы: " << endl;
- IsNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- element = stoi(inputLine);
- }
- catch (...)
- {
- IsNotCorrect = true;
- cout << "Элемент матрицы должен быть положительным числом." << endl;
- }
- if (!IsNotCorrect && (((element < -2147000000) || (element > 2147000000))))
- {
- cout << "Элемент матрицы должен принадлежать промежутку от -2147000000 до 2147000000" << endl;
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return element;
- }
- int** сreateMatrix(int order)
- {
- int** matrix = new int* [order];
- for (int i = 0; i < order; i++) {
- matrix[i] = new int[order];
- for (int j = 0; j < order; j++)
- matrix[i][j] = getMatrixElement();
- }
- return matrix;
- }
- int** сreateEmptyMatrix(int order)
- {
- int** matrix = new int* [order];
- for (int i = 0; i < order; i++) {
- matrix[i] = new int[order];
- }
- return matrix;
- }
- int calculateSumByCondition(int** &matrix, int size)
- {
- int sum = 0;
- int j = 0;
- for (int i = 0; i < size; i++)
- {
- j = 0;
- while ((j < size) && (i != j))
- {
- if (matrix[i][j] > 0)
- sum += matrix[i][j];
- j++;
- }
- }
- return sum;
- }
- void printMatrix(int** &matrix, int order)
- {
- for (int i = 0; i < order; i++) {
- for (int j = 0; j < order; j++)
- {
- cout << matrix[i][j] << "\t";
- }
- cout << endl;
- }
- cout << endl;
- }
- string getOutputDirectory()
- {
- string patho;
- bool isIncorrect;
- isIncorrect = true;
- do
- {
- cout << "Введите директорию, в которую хотите сохранить результат:" << endl;
- cin >> patho;
- if (filesystem::exists(patho))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
- }
- } while (isIncorrect);
- return patho;
- }
- void printResultToFile(int** &matrix, int order, int sum)
- {
- string path;
- path = getOutputDirectory();
- ofstream fout;
- fout.open(path + "\output.txt");
- fout<< "Для матрицы" << endl;
- for (int i = 0; i < order; i++)
- {
- for (int j = 0; j < order; j++)
- fout << setw(6) << matrix[i][j];
- fout << endl;
- }
- fout << "Сумма положительных элементов под главной диагональю = " << sum << endl;
- fout.close();
- cout << "Ответ сохранен по указанному пути.";
- }
- void workWithConsole()
- {
- int size = getMatrixSize();
- int** matrix = сreateMatrix(size);
- printMatrix(matrix, size);
- int sum = calculateSumByCondition(matrix, size);
- cout << "Сумма положительных элементов под главной диагональю: " << sum << endl;
- printResultToFile(matrix, size, sum);
- }
- bool isFileCorrect(string path)
- {
- int iSize, jSize, num;
- bool isCorrect;
- string inputline;
- fstream matrixFile(path, std::ios_base::in);
- iSize = 0;
- jSize = 0;
- isCorrect = true;
- try
- {
- getline(matrixFile, inputline);
- int order = stoi(inputline);
- while (!matrixFile.eof() && isCorrect)
- {
- getline(matrixFile, inputline);
- stringstream strstream;
- strstream << inputline;
- while (!strstream.eof() && jSize <= order)
- {
- strstream >> num;
- jSize++;
- }
- if (inputline != "")
- {
- iSize++;
- if (jSize != order)
- {
- isCorrect = false;
- }
- }
- jSize = 0;
- }
- if (iSize != order)
- {
- isCorrect = false;
- }
- }
- catch(...)
- {
- isCorrect = false;
- }
- return isCorrect;
- }
- string getMatrixFilePath()
- {
- string path;
- bool isIncorrect = true;
- do
- {
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- if (!std::filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- }
- else
- {
- if (isFileCorrect(path))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Проверьте данные в файле. Это должны быть числа, записанные в виде матрицы. Порядок матрицы должен соответствовать введённому." << endl;
- }
- }
- } while (isIncorrect);
- return path;
- }
- void сreateMatrixFromFile(int**& matrix, int& order, string path)
- {
- fstream matrixFile(path, fstream::in);
- string inputLine;
- getline(matrixFile, inputLine);
- order = stoi(inputLine);
- matrix = сreateEmptyMatrix(order);
- for (int i = 0; i < order; i++)
- {
- getline(matrixFile, inputLine);
- stringstream strstream;
- strstream << inputLine;
- for (int j = 0; j < order; j++)
- {
- strstream >> matrix[i][j];
- }
- }
- }
- void workWithFile()
- {
- int size;
- int** matrix;
- сreateMatrixFromFile(matrix, size, getMatrixFilePath());
- printMatrix(matrix, size);
- int sum = calculateSumByCondition(matrix, size);
- cout << "Сумма положительных элементов под главной диагональю: " << sum << endl;
- printResultToFile(matrix, size, sum);
- }
- void setProgramWorkType()
- {
- int answer;
- bool isNotCorrect;
- string inputLine;
- do
- {
- cout << "1 - работать с консолью, 2 - с файлом: " << endl;
- isNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- answer = stoi(inputLine);
- }
- catch (...)
- {
- isNotCorrect = true;
- cout << "Введите число!" << endl;
- }
- if (!isNotCorrect && (((answer < 1) || (answer > 2))))
- {
- cout << "Выбирете один из предложенных вариантов!" << endl;
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- if (answer == 1)
- workWithConsole();
- else
- workWithFile();
- }
- int main()
- {
- setlocale(LC_ALL, "ru");
- setProgramWorkType();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement