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