Advertisement
FOOZBY

Untitled

Dec 4th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <time.h>
  4. #include "fuhkcii.h"
  5. #include <ctime>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     setlocale(0, "ru");
  13.     int rows=10, cols=10;
  14.     int** ARR = new int* [rows];
  15.     for (int i = 0; i < rows; i++)
  16.     {
  17.         for (int i = 0; i < rows; i++)
  18.             ARR[i] = new int[cols];
  19.     }
  20.     cout << "метод заполнения\n";
  21.     cout << "0.ручками\n";
  22.     cout << "1.рандом\n";
  23.     cout << "2с файла//не работает\n";
  24.     int option = failcin();
  25.     switch (option)
  26.     {
  27.     case 0:
  28.         cout << "введите количевство строк в массиве: ";
  29.         rows = failcin();
  30.         cout << "введите количевство столбцов в массиве: ";
  31.         cols = failcin();
  32.         for (int i = 0; i < rows; ++i)
  33.         {
  34.             for (int j = 0; j < cols; ++j) {
  35.                 cout << "Введите значение элемента с индексами [" << i + 1 << "][" << j + 1 << "]";
  36.                 cin >> ARR[i][j];
  37.             }
  38.         }
  39.         exist = 1;
  40.         break;
  41.     case 1:
  42.         srand(time(NULL));
  43.         rows = rand() % 5;
  44.         cols = rows;
  45.         for (int i = 0; i < rows; ++i)
  46.         {
  47.             for (int j = 0; j < cols; ++j) {
  48.                 ARR[i][j] = rand() % 100;
  49.             }
  50.         }
  51.         exist = 1;
  52.         if (rows == 0)
  53.         {
  54.             cout << "нет массива" << endl;
  55.             exist = 0;
  56.         }
  57.         break;
  58.     case 2:
  59.         inputARRFromFile(ARR, rows, cols);
  60.     }
  61.     if (exist == 1)
  62.     {
  63.         showmas(ARR, rows,cols);
  64.         if (rows == cols)
  65.         {
  66.             magicSquare(ARR, rows);
  67.         }
  68.         else
  69.         {
  70.             cout << "матрица не квадратная,магический квадрат не вычислить\n";
  71.         }
  72.         cout << "определитель матрицы = " << Determinant(ARR, rows) << endl;
  73.         rankOfMatrix(ARR, rows, cols);
  74.         cout << endl;
  75.     }
  76.     else
  77.     {
  78.         cout << "матрицы несуществует\n";
  79.     }
  80.     /////////////////////////////////////////
  81.     deleteARR(ARR, rows, cols);
  82.     system("pause");
  83. }
  84. #include <iostream>
  85. #include <Windows.h>
  86. #include <fstream>
  87. #include <ctime>
  88. #include <vector>
  89. #include <time.h>
  90. #include <conio.h>
  91. using namespace std;
  92. int exist;
  93. int failcin()
  94. {
  95.     while (true) {
  96.         int userNumber;
  97.         cin >> userNumber;
  98.         if (cin.fail() || userNumber < 0) {
  99.             cin.clear();
  100.             cin.ignore(cin.rdbuf()->in_avail());
  101.             cout << endl << "Попробуйте ввести корректное число: ";
  102.         }
  103.         else {
  104.             cin.ignore(cin.rdbuf()->in_avail());
  105.             return userNumber;
  106.         }
  107.     }
  108. }
  109. void inputARRFromFile(int** A, int rows, int cols)
  110. {
  111.     int count = 0;
  112.     ifstream in("array.txt");
  113.     if (in.is_open())
  114.     {
  115.         long double temp;
  116.         while (!in.eof())
  117.         {
  118.             in >> temp;
  119.             ++count;
  120.         }
  121.         in.seekg(0, ios::beg);
  122.         in.clear();
  123.         int count_space = 0;
  124.         char symbol;
  125.         while (!in.eof()) {
  126.             in.get(symbol);
  127.             if (symbol == ' ')
  128.                 ++count_space;
  129.             if (symbol == '\n')
  130.                 break;
  131.         }
  132.         in.seekg(0, ios::beg);
  133.         in.clear();
  134.         rows = count / (count_space + 1);
  135.         cols = count_space + 1;
  136.         int** ARR = new int* [rows];
  137.         for (int i = 0; i < rows; i++)
  138.             ARR[i] = new int[cols];
  139.         for (int i = 0; i < rows; ++i) {
  140.             for (int j = 0; j < cols; ++j)
  141.                 in >> A[i][j];
  142.         }
  143.         in.close();
  144.         exist = 1;
  145.     }
  146.     else
  147.     {
  148.         cout << "Файл с именем 'array.txt' не найден." << endl << endl;
  149.         exist = 0;
  150.     }
  151. }
  152. void magicSquare(int** A, int order) {
  153.     int pairsCount = 0;
  154.     bool rowsCondition = 0, columnsCondition = 0, mainDiagonalCondition = 0, secondaryDiagonalCondition = 0;
  155.     long double sum1 = 0, sum2 = 0;
  156.     for (int j = 0, i = 0; j < order; ++j)
  157.         sum1 = sum1 + A[i][j];
  158.     for (int i = 1; i < order; ++i) {
  159.         for (int j = 0; j < order; ++j)
  160.             sum2 = sum2 + A[i][j];
  161.         if (sum2 == sum1)
  162.             ++pairsCount;
  163.         sum2 = 0;
  164.     }
  165.     if (pairsCount == order - 1)
  166.         rowsCondition = 1;
  167.     pairsCount = 0;
  168.     for (int j = 0; j < order; ++j) {
  169.         for (int i = 0; i < order; ++i)
  170.             sum2 = sum2 + A[i][j];
  171.         if (sum2 == sum1)
  172.             ++pairsCount;
  173.         sum2 = 0;
  174.     }
  175.     if (pairsCount == order)
  176.         columnsCondition = 1;
  177.     for (int i = 0; i < order; ++i) {
  178.         for (int j = 0; j < order; ++j) {
  179.             if (i == j)
  180.                 sum2 = sum2 + A[i][j];
  181.         }
  182.     }
  183.     if (sum2 == sum1)
  184.         mainDiagonalCondition = 1;
  185.     sum2 = 0;
  186.     for (int i = order - 1; i >= 0; --i) {
  187.         for (int j = order - 1; j >= 0; --j) {
  188.             if (i + j == order - 1)
  189.                 sum2 = sum2 + A[i][j];
  190.         }
  191.     }
  192.     if (sum2 == sum1)
  193.         secondaryDiagonalCondition = 1;
  194.     if (rowsCondition == 1 && columnsCondition == 1 && mainDiagonalCondition == 1 && secondaryDiagonalCondition == 1)
  195.         cout << endl << "Матрица является магическим квадратом." << endl;
  196.     else
  197.         cout << endl << "Матрица не является магическим квадратом." << endl;
  198. }
  199. void rankOfMatrix(int** A, int rows, int columns) {
  200.     int rank = rows <= columns ? columns : rows;
  201.     vector<char> line_used(rows);
  202.     for (int i = 0; i < columns; ++i) {
  203.         int j;
  204.         for (j = 0; j < rows; ++j)
  205.             if (!line_used[j] && A[j][i] != 0)
  206.                 break;
  207.         if (j == rows)
  208.             --rank;
  209.         else {
  210.             line_used[j] = true;
  211.             for (int p = i + 1; p < columns; ++p)
  212.                 A[j][p] /= A[j][i];
  213.             for (int k = 0; k < rows; ++k)
  214.                 if (k != j && A[j][i] != 0)
  215.                     for (int p = i + 1; p < columns; ++p)
  216.                         A[k][p] -= A[j][p] * A[k][i];
  217.         }
  218.     }
  219.     cout << endl << "Ранг матрицы: " << rank << endl << endl;
  220. }
  221. void GetMatr(int** mas, int** p, int i, int j, int m)
  222. {
  223.     int ki, kj, di, dj;
  224.     di = 0;
  225.     for (ki = 0; ki < m - 1; ki++) { // проверка индекса строки
  226.         if (ki == i) di = 1;
  227.         dj = 0;
  228.         for (kj = 0; kj < m - 1; kj++) { // проверка индекса столбца
  229.             if (kj == j) dj = 1;
  230.             p[ki][kj] = mas[ki + di][kj + dj];
  231.         }
  232.     }
  233. }
  234. void deleteARR(int** A, int rows, int columns);
  235. int Determinant(int** mas, int m)
  236. {
  237.     int i, j, d, k, n;
  238.     int** p = new int* [m];
  239.     for (int i = 0; i < m; i++)
  240.         p[i] = new int[m];
  241.     j = 0; d = 0;
  242.     k = 1;
  243.     n = m - 1;
  244.     switch (m)
  245.     {
  246.     case 1:
  247.         d = mas[0][0];
  248.         return(d);
  249.         break;
  250.     case 2:
  251.         d = mas[0][0] * mas[1][1] - (mas[0][1] * mas[1][0]);
  252.         return(d);
  253.         break;
  254.     default:
  255.         for (i = 0, j = 0; j < m; j++) {
  256.             GetMatr(mas, p, 0, j, m);
  257.             d = d + k * mas[0][j] * Determinant(p, n);
  258.             k = -k;
  259.         }
  260.         return(d);
  261.         break;
  262.     }
  263.     deleteARR(p, m, m);
  264. }
  265. void deleteARR(int** A, int rows, int columns)
  266. {
  267.     for (int i = 0; i < rows; ++i)
  268.         delete[] A[i];
  269.     delete[] A;
  270.     A = 0;
  271. }
  272. /*void mas()
  273. {
  274.     srand(unsigned(time(NULL)));
  275.     setlocale(0, "ru");
  276.     cout << "метод заполнения\n";
  277.     cout << "0.ручками\n";
  278.     cout << "1.рандом\n";
  279.     cout << "2.с файла\n";
  280.     int option = failcin();
  281.     switch (option)
  282.     {
  283.     case 0:
  284.     {
  285.         int rows, cols;
  286.         cout << "введите количевство строк в массиве: ";
  287.         rows = failcin();
  288.         cout << "введите количевство столбцов в массиве: ";
  289.         cols = failcin();
  290.         int** ARR = new int* [rows];
  291.         for (int i = 0; i < rows; i++)
  292.             ARR[i] = new int[cols];
  293.         for (int i = 0; i < rows; ++i)
  294.         {
  295.             for (int j = 0; j < cols; ++j) {
  296.                 cout << "Введите значение элемента с индексами [" << i + 1 << "][" << j + 1 << "]";
  297.                 cin >> ARR[i][j];
  298.             }
  299.         }
  300.         magicSquare(ARR, rows);
  301.         if (rows < 1)
  302.             cout << "определитель матрицы вычислить невозможно";
  303.         else
  304.             cout << "определитель матрицы равен " << Determinant(ARR, rows) << "\n";
  305.         rankOfMatrix(ARR, rows, cols);
  306.         //showmas(ARR, rows, cols);
  307.         delete[] ARR;
  308.         break;
  309.     }
  310.     case 1:
  311.     {
  312.         system("pause");
  313.         int rows, cols;
  314.         cout << "введите количевство строк в массиве: ";
  315.         rows = failcin();
  316.         cout << "введите количевство столбцов в массиве: ";
  317.         cols = failcin();
  318.         int** ARR = new int* [rows];
  319.         for (int i = 0; i < rows; i++)
  320.             ARR[i] = new int[cols];
  321.         for (int i = 0; i < rows; ++i)
  322.         {
  323.             for (int j = 0; j < cols; ++j) {
  324.                 ARR[i][j] = rand() % 100;
  325.             }
  326.         }
  327.         if (rows == 0)
  328.             cout << "нет массива" << endl;
  329.         else
  330.         {
  331.             for (int i = 0; i < rows; ++i)
  332.             {
  333.                 for (int j = 0; i < cols; ++j)
  334.                 {
  335.                     cout << ARR[i][j] << " ";
  336.                 }
  337.                 cout << endl;
  338.             }
  339.         }
  340.         magicSquare(ARR, rows);
  341.         if (rows < 1)
  342.             cout << "определитель матрицы вычислить невозможно";
  343.         else
  344.             cout << "определитель матрицы равен " << Determinant(ARR, rows) << "\n";
  345.         rankOfMatrix(ARR, rows, cols);
  346.         deleteARR(ARR, rows, cols);
  347.         break;
  348.     }
  349.     case 2:
  350.     {
  351.         int rows = 10, cols = 10;
  352.         int** ARR = new int* [rows];
  353.         for (int i = 0; i < rows; i++)
  354.             ARR[i] = new int[cols];
  355.         inputARRFromFile(ARR, rows, cols);
  356.         if (nomat = 0)
  357.         {
  358.             magicSquare(ARR, rows);
  359.             if (rows < 1)
  360.                 cout << "определитель матрицы вычислить невозможно";
  361.             else
  362.                 cout << "определитель матрицы равен " << Determinant(ARR, rows) << "\n";
  363.             rankOfMatrix(ARR, rows, cols);
  364.         }
  365.         delete[] ARR;
  366.         cout << endl;
  367.         break;
  368.     }
  369.     }
  370.     system("pause");
  371. }*/
  372. void showmas(int** A, int m,int n)
  373. {
  374.     for (int i = 0; i < m; i++)
  375.     {
  376.         for (int j = 0; j < n; j++)
  377.         {
  378.             cout << A[i][j] << " ";
  379.         }
  380.         cout << endl;
  381.     }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement