Advertisement
MaksNew

Untitled

Dec 17th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <filesystem>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. int getMatrixSize()
  9. {
  10.     int order;
  11.     bool IsNotCorrect;
  12.     string inputLine;
  13.     do
  14.     {
  15.         cout << "Введите порядок матрицы: " << endl;
  16.         IsNotCorrect = false;
  17.         getline(cin, inputLine);
  18.         try
  19.         {
  20.             order = stoi(inputLine);
  21.         }
  22.         catch (...)
  23.         {
  24.             IsNotCorrect = true;
  25.             cout << "Порядок матрицы должен быть положительным числом." << endl;
  26.         }
  27.         if (!IsNotCorrect && (((order < 1) || (order > 10000))))
  28.         {
  29.             cout << "Порядок матрицы должен принадлежать промежутку от 1 до 10000" << endl;
  30.             IsNotCorrect = true;
  31.         }
  32.     } while (IsNotCorrect);
  33.     return order;
  34. }
  35.  
  36. int getMatrixElement()
  37. {
  38.     int element;
  39.     bool IsNotCorrect;
  40.     string inputLine;
  41.     do
  42.     {
  43.         cout << "Введите элемент матрицы: " << endl;
  44.         IsNotCorrect = false;
  45.         getline(cin, inputLine);
  46.         try
  47.         {
  48.             element = stoi(inputLine);
  49.         }
  50.         catch (...)
  51.         {
  52.             IsNotCorrect = true;
  53.             cout << "Элемент матрицы должен быть положительным числом." << endl;
  54.         }
  55.         if (!IsNotCorrect && (((element < -2147000000) || (element > 2147000000))))
  56.         {
  57.             cout << "Элемент матрицы должен принадлежать промежутку от -2147000000 до 2147000000" << endl;
  58.             IsNotCorrect = true;
  59.         }
  60.     } while (IsNotCorrect);
  61.     return element;
  62. }
  63.  
  64. int** сreateMatrix(int order)
  65. {
  66.     int** matrix = new int* [order];
  67.     for (int i = 0; i < order; i++) {
  68.         matrix[i] = new int[order];
  69.         for (int j = 0; j < order; j++)
  70.             matrix[i][j] = getMatrixElement();
  71.     }
  72.     return matrix;
  73. }
  74.  
  75. int** сreateEmptyMatrix(int order)
  76. {
  77.     int** matrix = new int* [order];
  78.     for (int i = 0; i < order; i++) {
  79.         matrix[i] = new int[order];
  80.     }
  81.     return matrix;
  82. }
  83.  
  84. int calculateSumByCondition(int** &matrix, int size)
  85. {
  86.     int sum = 0;
  87.     int j = 0;
  88.     for (int i = 0; i < size; i++)
  89.     {
  90.         j = 0;
  91.         while ((j < size) && (i != j))
  92.         {
  93.             if (matrix[i][j] > 0)
  94.                 sum += matrix[i][j];
  95.             j++;
  96.         }
  97.     }
  98.     return sum;
  99. }
  100.  
  101. void printMatrix(int** &matrix, int order)
  102. {
  103.     for (int i = 0; i < order; i++) {
  104.         for (int j = 0; j < order; j++)
  105.         {
  106.             cout << matrix[i][j] << "\t";
  107.         }
  108.         cout << endl;
  109.     }
  110.     cout << endl;
  111. }
  112.  
  113. string getOutputDirectory()
  114. {
  115.     string patho;
  116.     bool isIncorrect;
  117.     isIncorrect = true;
  118.     do
  119.     {
  120.         cout << "Введите директорию, в которую хотите сохранить результат:" << endl;
  121.         cin >> patho;
  122.         if (filesystem::exists(patho))
  123.         {
  124.             isIncorrect = false;
  125.         }
  126.         else
  127.         {
  128.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  129.         }
  130.  
  131.     } while (isIncorrect);
  132.     return patho;
  133. }
  134.  
  135. void printResultToFile(int** &matrix, int order, int sum)
  136. {
  137.     string path;
  138.     path = getOutputDirectory();
  139.     ofstream fout;
  140.     fout.open(path + "\output.txt");
  141.     fout<< "Для матрицы" << endl;
  142.     for (int i = 0; i < order; i++)
  143.     {
  144.         for (int j = 0; j < order; j++)
  145.             fout << setw(6) << matrix[i][j];
  146.         fout << endl;
  147.     }
  148.     fout << "Сумма положительных элементов под главной диагональю = " << sum << endl;
  149.     fout.close();
  150.     cout << "Ответ сохранен по указанному пути.";
  151. }
  152.  
  153. void workWithConsole()
  154. {
  155.     int size = getMatrixSize();
  156.     int** matrix = сreateMatrix(size);
  157.     printMatrix(matrix, size);
  158.     int sum = calculateSumByCondition(matrix, size);
  159.     cout << "Сумма положительных элементов под главной диагональю: " << sum << endl;
  160.     printResultToFile(matrix, size, sum);
  161. }
  162.  
  163. bool isFileCorrect(string path)
  164. {
  165.     int iSize, jSize, num;
  166.     bool isCorrect;
  167.     string inputline;
  168.     fstream matrixFile(path, std::ios_base::in);
  169.     iSize = 0;
  170.     jSize = 0;
  171.     isCorrect = true;
  172.     try
  173.     {
  174.         getline(matrixFile, inputline);
  175.         int order = stoi(inputline);
  176.         while (!matrixFile.eof() && isCorrect)
  177.         {
  178.             getline(matrixFile, inputline);
  179.             stringstream strstream;
  180.             strstream << inputline;
  181.             while (!strstream.eof() && jSize <= order)
  182.             {
  183.                 strstream >> num;
  184.                 jSize++;
  185.             }
  186.             if (inputline != "")
  187.             {
  188.                 iSize++;
  189.                 if (jSize != order)
  190.                 {
  191.                     isCorrect = false;
  192.                 }
  193.             }
  194.             jSize = 0;
  195.         }
  196.         if (iSize != order)
  197.         {
  198.             isCorrect = false;
  199.         }
  200.     }
  201.     catch(...)
  202.     {
  203.         isCorrect = false;
  204.     }
  205.     return isCorrect;
  206. }
  207.  
  208. string getMatrixFilePath()
  209. {
  210.     string path;
  211.     bool isIncorrect = true;
  212.     do
  213.     {
  214.         cout << "Введите абсолютный путь к файлу: " << endl;
  215.         cin >> path;
  216.         if (!std::filesystem::exists(path))
  217.         {
  218.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  219.         }
  220.         else
  221.         {
  222.             if (isFileCorrect(path))
  223.             {
  224.                 isIncorrect = false;
  225.             }
  226.             else
  227.             {
  228.                 cout << "Проверьте данные в файле. Это должны быть числа, записанные в виде матрицы. Порядок матрицы должен соответствовать введённому." << endl;
  229.             }
  230.         }
  231.     } while (isIncorrect);
  232.     return path;
  233. }
  234.  
  235. void сreateMatrixFromFile(int**& matrix, int& order, string path)
  236. {
  237.     fstream matrixFile(path, fstream::in);
  238.     string inputLine;
  239.     getline(matrixFile, inputLine);
  240.     order = stoi(inputLine);
  241.     matrix = сreateEmptyMatrix(order);
  242.     for (int i = 0; i < order; i++)
  243.     {
  244.         getline(matrixFile, inputLine);
  245.         stringstream strstream;
  246.         strstream << inputLine;
  247.         for (int j = 0; j < order; j++)
  248.         {
  249.             strstream >> matrix[i][j];
  250.         }
  251.     }
  252. }
  253.  
  254. void workWithFile()
  255. {
  256.     int size;
  257.     int** matrix;
  258.     сreateMatrixFromFile(matrix, size, getMatrixFilePath());
  259.     printMatrix(matrix, size);
  260.     int sum = calculateSumByCondition(matrix, size);
  261.     cout << "Сумма положительных элементов под главной диагональю: " << sum << endl;
  262.     printResultToFile(matrix, size, sum);
  263. }
  264.  
  265. void setProgramWorkType()
  266. {
  267.     int answer;
  268.     bool isNotCorrect;
  269.     string inputLine;
  270.     do
  271.     {
  272.         cout << "1 - работать с консолью, 2 - с файлом: " << endl;
  273.         isNotCorrect = false;
  274.         getline(cin, inputLine);
  275.         try
  276.         {
  277.             answer = stoi(inputLine);
  278.         }
  279.         catch (...)
  280.         {
  281.             isNotCorrect = true;
  282.             cout << "Введите число!" << endl;
  283.         }
  284.         if (!isNotCorrect && (((answer < 1) || (answer > 2))))
  285.         {
  286.             cout << "Выбирете один из предложенных вариантов!" << endl;
  287.             isNotCorrect = true;
  288.         }
  289.     } while (isNotCorrect);
  290.     if (answer == 1)
  291.         workWithConsole();
  292.     else
  293.         workWithFile();
  294. }
  295.  
  296. int main()
  297. {
  298.     setlocale(LC_ALL, "ru");
  299.     setProgramWorkType();
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement