This commit is contained in:
2024-05-19 21:54:53 +03:00
parent 56d58f4d28
commit edda5c1750

65
pr5.cpp
View File

@@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include<string.h> #include <string.h>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <tuple> #include <tuple>
@@ -58,6 +58,7 @@ public:
cout << "Exception: " << str << "; " << what(); cout << "Exception: " << str << "; " << what();
} }
}; };
class InvalidOperationException : public Exception { class InvalidOperationException : public Exception {
public: public:
InvalidOperationException(const char* s) : Exception(s) {} InvalidOperationException(const char* s) : Exception(s) {}
@@ -91,7 +92,8 @@ public:
cout << "IndexOutOfBoundsException: " << str << "; " << Row << ", " << Col << "; " << what(); cout << "IndexOutOfBoundsException: " << str << "; " << Row << ", " << Col << "; " << what();
} }
}; };
class NonPositiveSizeException : public WrongSizeException { class NonPositiveSizeException : public WrongSizeException
{
public: public:
NonPositiveSizeException(const char* s, int row1, int col1, int row2, int col2) : WrongSizeException(s, row1, col1, row2, col2) {} NonPositiveSizeException(const char* s, int row1, int col1, int row2, int col2) : WrongSizeException(s, row1, col1, row2, col2) {}
@@ -101,7 +103,8 @@ public:
} }
}; };
class TooLargeSizeException : public WrongSizeException { class TooLargeSizeException : public WrongSizeException
{
public: public:
TooLargeSizeException(const char* s, int row1, int col1, int row2, int col2) : WrongSizeException(s, row1, col1, row2, col2) {} TooLargeSizeException(const char* s, int row1, int col1, int row2, int col2) : WrongSizeException(s, row1, col1, row2, col2) {}
@@ -121,7 +124,8 @@ protected:
public: public:
BaseMatrix(int Height = 2, int Width = 2) BaseMatrix(int Height = 2, int Width = 2)
{ {
if (Height <= 0 || Width <= 0) { if (Height <= 0 || Width <= 0)
{
throw NonPositiveSizeException("Attempt to create a matrix of negative or zero size", Height, Width, -1, -1); throw NonPositiveSizeException("Attempt to create a matrix of negative or zero size", Height, Width, -1, -1);
} }
height = Height; height = Height;
@@ -202,24 +206,31 @@ public:
using BaseMatrix<T>::ptr; using BaseMatrix<T>::ptr;
using BaseMatrix<T>::height; using BaseMatrix<T>::height;
using BaseMatrix<T>::width; using BaseMatrix<T>::width;
Matrix(int Height = 2, int Width = 2) : BaseMatrix<T>(Height, Width) { cout << "\nMatrix constructor\n"; } Matrix(int Height = 2, int Width = 2) : BaseMatrix<T>(Height, Width) { cout << "\nMatrix constructor\n"; }
~Matrix() { cout << "\nMatrix destructor\n"; } ~Matrix() { cout << "\nMatrix destructor\n"; }
Matrix(T** array, int Height, int Width) : BaseMatrix<T>(Height, Width) { Matrix(T** array, int Height, int Width) : BaseMatrix<T>(Height, Width)
{
ptr = new T* [height]; ptr = new T* [height];
for (int i = 0; i < height; ++i) { for (int i = 0; i < height; ++i)
{
ptr[i] = new T[width]; ptr[i] = new T[width];
for (int j = 0; j < width; ++j) { for (int j = 0; j < width; ++j)
{
ptr[i][j] = array[i][j]; ptr[i][j] = array[i][j];
} }
} }
} }
// Filling a matrix element by the sum of its indices // Filling a matrix element by the sum of its indices
void fillRandom(int a) { void fillRandom(int a)
{
// Initialization of random number generator with current time // Initialization of random number generator with current time
srand(time(nullptr)); srand(time(nullptr));
for (int i = 0; i < height; ++i) { for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j) { {
for (int j = 0; j < width; ++j)
{
// Random number generation // Random number generation
ptr[i][j] = (rand() * (a + 1))%100; ptr[i][j] = (rand() * (a + 1))%100;
} }
@@ -227,19 +238,23 @@ public:
} }
pair<double, double> calculateCenterOfMass() { pair<double, double> calculateCenterOfMass()
{
double totalMass = 0; double totalMass = 0;
double centerX = 0; double centerX = 0;
double centerY = 0; double centerY = 0;
for (int i = 0; i < this->height; ++i) { for (int i = 0; i < this->height; ++i)
for (int j = 0; j < this->width; ++j) { {
for (int j = 0; j < this->width; ++j)
{
totalMass += this->ptr[i][j]; totalMass += this->ptr[i][j];
centerX += i * this->ptr[i][j]; centerX += i * this->ptr[i][j];
centerY += j * this->ptr[i][j]; centerY += j * this->ptr[i][j];
} }
} }
if (totalMass != 0) { if (totalMass != 0)
{
centerX /= totalMass; centerX /= totalMass;
centerY /= totalMass; centerY /= totalMass;
} }
@@ -249,7 +264,8 @@ public:
double Trace() double Trace()
{ {
if (height != width) { if (height != width)
{
throw InvalidOperationException("The operation of finding the trace is not defined for a non-square matrix"); throw InvalidOperationException("The operation of finding the trace is not defined for a non-square matrix");
} }
double summ = 0; double summ = 0;
@@ -269,17 +285,21 @@ public:
return res; return res;
} }
// Перегрузка оператора присваивания // Перегрузка оператора присваивания
Matrix& operator=(const Matrix& other) { Matrix& operator=(const Matrix& other)
if (this != &other) { {
if (this != &other)
{
this->~Matrix(); this->~Matrix();
height = other.height; height = other.height;
width = other.width; width = other.width;
ptr = new double* [height]; ptr = new double* [height];
for (int i = 0; i < height; ++i) { for (int i = 0; i < height; ++i)
{
ptr[i] = new double[width]; ptr[i] = new double[width];
for (int j = 0; j < width; ++j) { for (int j = 0; j < width; ++j)
{
ptr[i][j] = other.ptr[i][j]; ptr[i][j] = other.ptr[i][j];
} }
} }
@@ -291,6 +311,7 @@ public:
template<class T> template<class T>
friend istream& operator>>(istream& s, Matrix<T>& M); friend istream& operator>>(istream& s, Matrix<T>& M);
}; };
template<class T> template<class T>
ostream& operator<<(ostream& s, Matrix<T> M) ostream& operator<<(ostream& s, Matrix<T> M)
{ {
@@ -310,6 +331,7 @@ ostream& operator<<(ostream& s, Matrix<T> M)
} }
return s; return s;
} }
template<class T> template<class T>
istream& operator>>(istream& s, Matrix<T>& M) istream& operator>>(istream& s, Matrix<T>& M)
{ {
@@ -321,6 +343,7 @@ istream& operator>>(istream& s, Matrix<T>& M)
throw InvalidOperationException("The dimensions of the read matrix are different from those of the original matrix."); throw InvalidOperationException("The dimensions of the read matrix are different from those of the original matrix.");
} }
} }
for (int i = 0; i < M.height; i++) for (int i = 0; i < M.height; i++)
for (int j = 0; j < M.width; j++) for (int j = 0; j < M.width; j++)
s >> M.ptr[i][j]; s >> M.ptr[i][j];
@@ -349,6 +372,7 @@ int main() {
// reading a matrix array from a file // reading a matrix array from a file
Matrix<double>* M1; Matrix<double>* M1;
ifstream fin("1.txt"); ifstream fin("1.txt");
if (fin) { if (fin) {
int n; int n;
fin >> n; fin >> n;
@@ -392,7 +416,6 @@ int main() {
cout << "Standard exception has been caught: " << ex.what(); cout << "Standard exception has been caught: " << ex.what();
} }
// char c;
// cin >> c;
return 0; return 0;
} }