Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <time.h>
- #include "fuhkcii.h"
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- int main()
- {
- setlocale(0, "ru");
- int rows=10, cols=10;
- int** ARR = new int* [rows];
- for (int i = 0; i < rows; i++)
- {
- for (int i = 0; i < rows; i++)
- ARR[i] = new int[cols];
- }
- cout << "метод заполнения\n";
- cout << "0.ручками\n";
- cout << "1.рандом\n";
- cout << "2с файла//не работает\n";
- int option = failcin();
- switch (option)
- {
- case 0:
- cout << "введите количевство строк в массиве: ";
- rows = failcin();
- cout << "введите количевство столбцов в массиве: ";
- cols = failcin();
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j) {
- cout << "Введите значение элемента с индексами [" << i + 1 << "][" << j + 1 << "]";
- cin >> ARR[i][j];
- }
- }
- exist = 1;
- break;
- case 1:
- srand(time(NULL));
- rows = rand() % 5;
- cols = rows;
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j) {
- ARR[i][j] = rand() % 100;
- }
- }
- exist = 1;
- if (rows == 0)
- {
- cout << "нет массива" << endl;
- exist = 0;
- }
- break;
- case 2:
- inputARRFromFile(ARR, rows, cols);
- }
- if (exist == 1)
- {
- showmas(ARR, rows,cols);
- if (rows == cols)
- {
- magicSquare(ARR, rows);
- }
- else
- {
- cout << "матрица не квадратная,магический квадрат не вычислить\n";
- }
- cout << "определитель матрицы = " << Determinant(ARR, rows) << endl;
- rankOfMatrix(ARR, rows, cols);
- cout << endl;
- }
- else
- {
- cout << "матрицы несуществует\n";
- }
- /////////////////////////////////////////
- deleteARR(ARR, rows, cols);
- system("pause");
- }
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- #include <ctime>
- #include <vector>
- #include <time.h>
- #include <conio.h>
- using namespace std;
- int exist;
- int failcin()
- {
- while (true) {
- int userNumber;
- cin >> userNumber;
- if (cin.fail() || userNumber < 0) {
- cin.clear();
- cin.ignore(cin.rdbuf()->in_avail());
- cout << endl << "Попробуйте ввести корректное число: ";
- }
- else {
- cin.ignore(cin.rdbuf()->in_avail());
- return userNumber;
- }
- }
- }
- void inputARRFromFile(int** A, int rows, int cols)
- {
- int count = 0;
- ifstream in("array.txt");
- if (in.is_open())
- {
- long double temp;
- while (!in.eof())
- {
- in >> temp;
- ++count;
- }
- in.seekg(0, ios::beg);
- in.clear();
- int count_space = 0;
- char symbol;
- while (!in.eof()) {
- in.get(symbol);
- if (symbol == ' ')
- ++count_space;
- if (symbol == '\n')
- break;
- }
- in.seekg(0, ios::beg);
- in.clear();
- rows = count / (count_space + 1);
- cols = count_space + 1;
- int** ARR = new int* [rows];
- for (int i = 0; i < rows; i++)
- ARR[i] = new int[cols];
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j)
- in >> A[i][j];
- }
- in.close();
- exist = 1;
- }
- else
- {
- cout << "Файл с именем 'array.txt' не найден." << endl << endl;
- exist = 0;
- }
- }
- void magicSquare(int** A, int order) {
- int pairsCount = 0;
- bool rowsCondition = 0, columnsCondition = 0, mainDiagonalCondition = 0, secondaryDiagonalCondition = 0;
- long double sum1 = 0, sum2 = 0;
- for (int j = 0, i = 0; j < order; ++j)
- sum1 = sum1 + A[i][j];
- for (int i = 1; i < order; ++i) {
- for (int j = 0; j < order; ++j)
- sum2 = sum2 + A[i][j];
- if (sum2 == sum1)
- ++pairsCount;
- sum2 = 0;
- }
- if (pairsCount == order - 1)
- rowsCondition = 1;
- pairsCount = 0;
- for (int j = 0; j < order; ++j) {
- for (int i = 0; i < order; ++i)
- sum2 = sum2 + A[i][j];
- if (sum2 == sum1)
- ++pairsCount;
- sum2 = 0;
- }
- if (pairsCount == order)
- columnsCondition = 1;
- for (int i = 0; i < order; ++i) {
- for (int j = 0; j < order; ++j) {
- if (i == j)
- sum2 = sum2 + A[i][j];
- }
- }
- if (sum2 == sum1)
- mainDiagonalCondition = 1;
- sum2 = 0;
- for (int i = order - 1; i >= 0; --i) {
- for (int j = order - 1; j >= 0; --j) {
- if (i + j == order - 1)
- sum2 = sum2 + A[i][j];
- }
- }
- if (sum2 == sum1)
- secondaryDiagonalCondition = 1;
- if (rowsCondition == 1 && columnsCondition == 1 && mainDiagonalCondition == 1 && secondaryDiagonalCondition == 1)
- cout << endl << "Матрица является магическим квадратом." << endl;
- else
- cout << endl << "Матрица не является магическим квадратом." << endl;
- }
- void rankOfMatrix(int** A, int rows, int columns) {
- int rank = rows <= columns ? columns : rows;
- vector<char> line_used(rows);
- for (int i = 0; i < columns; ++i) {
- int j;
- for (j = 0; j < rows; ++j)
- if (!line_used[j] && A[j][i] != 0)
- break;
- if (j == rows)
- --rank;
- else {
- line_used[j] = true;
- for (int p = i + 1; p < columns; ++p)
- A[j][p] /= A[j][i];
- for (int k = 0; k < rows; ++k)
- if (k != j && A[j][i] != 0)
- for (int p = i + 1; p < columns; ++p)
- A[k][p] -= A[j][p] * A[k][i];
- }
- }
- cout << endl << "Ранг матрицы: " << rank << endl << endl;
- }
- void GetMatr(int** mas, int** p, int i, int j, int m)
- {
- int ki, kj, di, dj;
- di = 0;
- for (ki = 0; ki < m - 1; ki++) { // проверка индекса строки
- if (ki == i) di = 1;
- dj = 0;
- for (kj = 0; kj < m - 1; kj++) { // проверка индекса столбца
- if (kj == j) dj = 1;
- p[ki][kj] = mas[ki + di][kj + dj];
- }
- }
- }
- void deleteARR(int** A, int rows, int columns);
- int Determinant(int** mas, int m)
- {
- int i, j, d, k, n;
- int** p = new int* [m];
- for (int i = 0; i < m; i++)
- p[i] = new int[m];
- j = 0; d = 0;
- k = 1;
- n = m - 1;
- switch (m)
- {
- case 1:
- d = mas[0][0];
- return(d);
- break;
- case 2:
- d = mas[0][0] * mas[1][1] - (mas[0][1] * mas[1][0]);
- return(d);
- break;
- default:
- for (i = 0, j = 0; j < m; j++) {
- GetMatr(mas, p, 0, j, m);
- d = d + k * mas[0][j] * Determinant(p, n);
- k = -k;
- }
- return(d);
- break;
- }
- deleteARR(p, m, m);
- }
- void deleteARR(int** A, int rows, int columns)
- {
- for (int i = 0; i < rows; ++i)
- delete[] A[i];
- delete[] A;
- A = 0;
- }
- /*void mas()
- {
- srand(unsigned(time(NULL)));
- setlocale(0, "ru");
- cout << "метод заполнения\n";
- cout << "0.ручками\n";
- cout << "1.рандом\n";
- cout << "2.с файла\n";
- int option = failcin();
- switch (option)
- {
- case 0:
- {
- int rows, cols;
- cout << "введите количевство строк в массиве: ";
- rows = failcin();
- cout << "введите количевство столбцов в массиве: ";
- cols = failcin();
- int** ARR = new int* [rows];
- for (int i = 0; i < rows; i++)
- ARR[i] = new int[cols];
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j) {
- cout << "Введите значение элемента с индексами [" << i + 1 << "][" << j + 1 << "]";
- cin >> ARR[i][j];
- }
- }
- magicSquare(ARR, rows);
- if (rows < 1)
- cout << "определитель матрицы вычислить невозможно";
- else
- cout << "определитель матрицы равен " << Determinant(ARR, rows) << "\n";
- rankOfMatrix(ARR, rows, cols);
- //showmas(ARR, rows, cols);
- delete[] ARR;
- break;
- }
- case 1:
- {
- system("pause");
- int rows, cols;
- cout << "введите количевство строк в массиве: ";
- rows = failcin();
- cout << "введите количевство столбцов в массиве: ";
- cols = failcin();
- int** ARR = new int* [rows];
- for (int i = 0; i < rows; i++)
- ARR[i] = new int[cols];
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; j < cols; ++j) {
- ARR[i][j] = rand() % 100;
- }
- }
- if (rows == 0)
- cout << "нет массива" << endl;
- else
- {
- for (int i = 0; i < rows; ++i)
- {
- for (int j = 0; i < cols; ++j)
- {
- cout << ARR[i][j] << " ";
- }
- cout << endl;
- }
- }
- magicSquare(ARR, rows);
- if (rows < 1)
- cout << "определитель матрицы вычислить невозможно";
- else
- cout << "определитель матрицы равен " << Determinant(ARR, rows) << "\n";
- rankOfMatrix(ARR, rows, cols);
- deleteARR(ARR, rows, cols);
- break;
- }
- case 2:
- {
- int rows = 10, cols = 10;
- int** ARR = new int* [rows];
- for (int i = 0; i < rows; i++)
- ARR[i] = new int[cols];
- inputARRFromFile(ARR, rows, cols);
- if (nomat = 0)
- {
- magicSquare(ARR, rows);
- if (rows < 1)
- cout << "определитель матрицы вычислить невозможно";
- else
- cout << "определитель матрицы равен " << Determinant(ARR, rows) << "\n";
- rankOfMatrix(ARR, rows, cols);
- }
- delete[] ARR;
- cout << endl;
- break;
- }
- }
- system("pause");
- }*/
- void showmas(int** A, int m,int n)
- {
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement