#include <iostream>
#include <random>
#include <ctime>
#include <iomanip>
using namespace std;
class Matrica
{
public:
int n;
double** a;
Matrica()
{
cout << "Porydok matricy" << endl;
cin >> n;
a = new double* [n];
cout << "Zapolnite matricy" << endl;
for (int i = 0; i < 3; i++)
{
a[i] = new double[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
}
Matrica(const Matrica& ob)
{
n = ob.n;
a = new double* [n];
for (int i = 0; i < n; i++)
a[i] = new double[n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = ob.a[i][j];
cout << this << endl;
}
~Matrica()
{
for (int i = 0; i < n; i++)
{
delete a[i];
}
delete[] a;
}
void Print()
{
cout << "Matrica:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
double Opredelitel()
{
return a[1][1] * a[2][2] * a[3][3] + a[1][2] * a[2][3] * a[3][1] + a[2][1] * a[3][2] * a[1][3] - a[1][1] * a[2][3] * a[3][2] - a[1][3] * a[2][2] * a[3][1] - a[3][3] * a[1][2] * a[2][1];
}
Matrica& operator=(const Matrica& ob)
{
if (n != ob.n)
{
// освобождение памяти в левом операнде
for (int i = 0; i < n; i++)
delete[] a[i];
delete[] a;
// выделение памяти в левом операнде
n = ob.n;
a = new double* [n];
for (int i = 0; i < n; i++)
a[i] = new double[n];
}
//Копирование данных правого операнда в левый
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = ob.a[i][j];
return *this;
}
Matrica operator-(const Matrica& ob)
{
Matrica result;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
result.a[i][j] = a[i][j] - ob.a[i][j];
return result;
}
//Перегрузка оператора смены знака
Matrica operator-() const
{
Matrica result;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
result.a[i][j] = -a[i][j];
return result;
}
Matrica operator+(const Matrica& ob)
{
Matrica result;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
result.a[i][j] = a[i][j] + ob.a[i][j];
return result;
}
Matrica operator*(int p)
{
Matrica result;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
result.a[i][j] = a[i][j] * p;
return result;
}
Matrica operator* (const Matrica& ob)
{
Matrica result;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
result.a[i][j] += a[i][k] * ob.a[k][j];
return result;
}
};
std::ostream& operator<<(std::ostream& os, const Matrica& ob)
{
for (int i = 0; i < ob.n; ++i)
{
for (int j = 0; j < ob.n; ++j)
os << setprecision(3) << ob.a[i][j] << "\t";
os << "\n";
}
return os;
}
int main()
{
Matrica A;
A.Print();
Matrica B;
B.Print();
//Matrica D;
//D = A * B * 3 + (A - B) * A;
//D.Print();
cout << A * B * 3 + (A - B) * A;
return 0;
}
Matrica operator+(const Matrica& ob)
{
Matrica result; // Вот здесь
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
result.a[i][j] = a[i][j] + ob.a[i][j];
return result;
}
Matrica operator* (const Matrica& ob)
{
Matrica result; // и здесь
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
result.a[i][j] += a[i][k] * ob.a[k][j];
return result;
}
Matrica operator-(const Matrica& ob)
{
Matrica result; // и здесь
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
result.a[i][j] = a[i][j] - ob.a[i][j];
return result;
}
a[1][1] * a[2][2] * a[3][3] + a[1][2] * a[2][3] * a[3][1] + a[2][1] * a[3][2] * a[1][3] - a[1][1] * a[2][3] * a[3][2] - a[1][3] * a[2][2] * a[3][1] - a[3][3] * a[1][2] * a[2][1];