MaksNew

Untitled

Oct 30th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <filesystem>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. bool isFileCorrect(string path, int order)
  10. {
  11.     int iSize, jSize, num;
  12.     bool isCorrect;
  13.     string inputline;
  14.     fstream matrixFile(path, std::ios_base::in);
  15.     iSize = 0;
  16.     jSize = 0;
  17.     isCorrect = true;
  18.     while (!matrixFile.eof() && isCorrect)
  19.     {
  20.         getline(matrixFile, inputline);
  21.         stringstream strstream;
  22.         strstream << inputline;
  23.         while (!strstream.eof() && jSize <= order)
  24.         {
  25.             strstream >> num;
  26.             jSize++;
  27.         }
  28.         if (inputline != "")
  29.         {
  30.             iSize++;
  31.             if (jSize != order)
  32.             {
  33.                 isCorrect = false;
  34.             }
  35.         }
  36.         jSize = 0;
  37.     }
  38.     if (iSize != order)
  39.     {
  40.         isCorrect = false;
  41.     }
  42.     return isCorrect;
  43. }
  44.  
  45. string FilePath(int order)
  46. {
  47.     string path;
  48.     bool isIncorrect = true;
  49.     do
  50.     {
  51.         cout << "Введите абсолютный путь к файлу: " << endl;
  52.         cin >> path;
  53.         if (!std::filesystem::exists(path))
  54.         {
  55.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  56.         }
  57.         else
  58.         {
  59.             if (isFileCorrect(path, order))
  60.             {
  61.                 isIncorrect = false;
  62.             }
  63.             else
  64.             {
  65.                 cout << "Проверьте данные в файле. Это должны быть числа, записанные в виде матрицы. Порядок матрицы должен соответствовать введённому." << endl;
  66.             }
  67.         }
  68.     } while (isIncorrect);
  69.     return path;
  70. }
  71.  
  72.  
  73.  
  74. int InputOrder()
  75. {
  76.     int order;
  77.     bool IsNotCorrect;
  78.     string inputLine;
  79.     do
  80.     {
  81.         cout << "Введите порядок матрицы: " << endl;
  82.         IsNotCorrect = false;
  83.         getline(cin, inputLine);
  84.         try
  85.         {
  86.             order = stoi(inputLine);
  87.         }
  88.         catch (...)
  89.         {
  90.             IsNotCorrect = true;
  91.             cout << "Порядок матрицы должен быть числом." << endl;
  92.         }
  93.         if ((((order < 1) || (order > 10000))) && !IsNotCorrect)
  94.         {
  95.             cout << "Размер матрицы должен принадлежать промежутку от 1 до 10000" << endl;
  96.             IsNotCorrect = true;
  97.         }
  98.     } while (IsNotCorrect);
  99.     return order;
  100. }
  101.  
  102. int** MatrixCreate(int& order)
  103. {
  104.     int** matrix = new int* [order];
  105.     for (int i = 0; i < order; i++) {
  106.         matrix[i] = new int[order];
  107.     }
  108.     return matrix;
  109. }
  110.  
  111. int** FileToMatrix(int** matrix, int& order, string path)
  112. {
  113.     fstream matrixFile(path, fstream::in);
  114.     for (int i = 0; i < order; i++)
  115.     {
  116.         for (int j = 0; j < order; j++)
  117.         {
  118.             matrixFile >> matrix[i][j];
  119.         }
  120.     }
  121.     cout << endl;
  122.     return matrix;
  123. }
  124.  
  125. void PrintMatrix(int** matrix, int order)
  126. {
  127.     for (int i = 0; i < order; i++) {
  128.         for (int j = 0; j < order; j++)
  129.         {
  130.             cout << matrix[i][j] << "\t";
  131.         }
  132.         cout << endl;
  133.     }
  134.     cout << endl;
  135. }
  136.  
  137. void printResult(int count) {
  138.     cout << "Количество столбцов матрицы, в которых имеются нулевые элементы:" << count << endl;
  139. }
  140.  
  141. int Condition(int** matrix, int order, int count)
  142. {
  143.     int j;
  144.     for (int i = 0; i < order; i++) {
  145.         j = 0;
  146.         while (j < order) {
  147.             if (matrix[j][i]==0) {
  148.                 count++;
  149.                 j = 3;
  150.             }
  151.             j++;
  152.         }
  153.     }
  154.     return count;
  155. }
  156.  
  157. string Output()
  158. {
  159.     string patho;
  160.     bool isIncorrect;
  161.     isIncorrect = true;
  162.     do
  163.     {
  164.         cout << "Введите директорию, в которую хотите сохранить матрицу:" << endl;
  165.         cin >> patho;
  166.         if (filesystem::exists(patho))
  167.         {
  168.             isIncorrect = false;
  169.         }
  170.         else
  171.         {
  172.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  173.         }
  174.  
  175.     } while (isIncorrect);
  176.     return patho;
  177. }
  178.  
  179. void ConditionToFile(int count)
  180. {
  181.     string path;
  182.     path = Output();
  183.     ofstream fout;
  184.     fout.open(path + "\output.txt");
  185.     fout << "Количество столбцов матрицы, в которых имеются нулевые элементы: " << count << endl;
  186.     fout.close();
  187.     cout << "Результат сохранён по указанному пути.";
  188.  
  189. }
  190.  
  191.  
  192. int main()
  193. {
  194.     setlocale(LC_ALL, "ru");
  195.     int order;
  196.     int count;
  197.     int** matrix;
  198.     string path;
  199.     count = 0;
  200.     order = InputOrder();
  201.     path = FilePath(order);
  202.     matrix = MatrixCreate(order);
  203.     matrix = FileToMatrix(matrix, order, path);
  204.     cout << "Матрица, для которой производились вычисления:" << endl;
  205.     PrintMatrix(matrix, order);
  206.     count = Condition(matrix, order, count);
  207.     printResult(count);
  208.     ConditionToFile(count);
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment