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;
- bool isFileCorrect(string path, int order)
- {
- int iSize, jSize, num;
- bool isCorrect;
- string inputline;
- fstream matrixFile(path, std::ios_base::in);
- iSize = 0;
- jSize = 0;
- isCorrect = true;
- 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;
- }
- return isCorrect;
- }
- string FilePath(int order)
- {
- string path;
- bool isIncorrect = true;
- do
- {
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- if (!std::filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- }
- else
- {
- if (isFileCorrect(path, order))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Проверьте данные в файле. Это должны быть числа, записанные в виде матрицы. Порядок матрицы должен соответствовать введённому." << endl;
- }
- }
- } while (isIncorrect);
- return path;
- }
- int InputOrder()
- {
- int order;
- bool IsNotCorrect;
- string inputLine;
- do
- {
- cout << "Введите порядок матрицы: " << endl;
- IsNotCorrect = false;
- getline(cin, inputLine);
- try
- {
- order = stoi(inputLine);
- }
- catch (...)
- {
- IsNotCorrect = true;
- cout << "Порядок матрицы должен быть числом." << endl;
- }
- if ((((order < 1) || (order > 10000))) && !IsNotCorrect)
- {
- cout << "Размер матрицы должен принадлежать промежутку от 1 до 10000" << endl;
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return order;
- }
- int** MatrixCreate(int& order)
- {
- int** matrix = new int* [order];
- for (int i = 0; i < order; i++) {
- matrix[i] = new int[order];
- }
- return matrix;
- }
- int** FileToMatrix(int** matrix, int& order, string path)
- {
- fstream matrixFile(path, fstream::in);
- for (int i = 0; i < order; i++)
- {
- for (int j = 0; j < order; j++)
- {
- matrixFile >> matrix[i][j];
- }
- }
- cout << endl;
- return matrix;
- }
- 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;
- }
- void printResult(int count) {
- cout << "Количество столбцов матрицы, в которых имеются нулевые элементы:" << count << endl;
- }
- int Condition(int** matrix, int order, int count)
- {
- int j;
- for (int i = 0; i < order; i++) {
- j = 0;
- while (j < order) {
- if (matrix[j][i]==0) {
- count++;
- j = 3;
- }
- j++;
- }
- }
- return count;
- }
- string Output()
- {
- 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 ConditionToFile(int count)
- {
- string path;
- path = Output();
- ofstream fout;
- fout.open(path + "\output.txt");
- fout << "Количество столбцов матрицы, в которых имеются нулевые элементы: " << count << endl;
- fout.close();
- cout << "Результат сохранён по указанному пути.";
- }
- int main()
- {
- setlocale(LC_ALL, "ru");
- int order;
- int count;
- int** matrix;
- string path;
- count = 0;
- order = InputOrder();
- path = FilePath(order);
- matrix = MatrixCreate(order);
- matrix = FileToMatrix(matrix, order, path);
- cout << "Матрица, для которой производились вычисления:" << endl;
- PrintMatrix(matrix, order);
- count = Condition(matrix, order, count);
- printResult(count);
- ConditionToFile(count);
- }
Advertisement
Add Comment
Please, Sign In to add comment