Pre-end version
This commit is contained in:
1 file changed
+398
-244
@@ -1,244 +1,398 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <typeinfo>
|
#include<string.h>
|
||||||
#include <cstring>
|
#include <cstdlib>
|
||||||
#include <utility>
|
#include <ctime>
|
||||||
using namespace std;
|
#include <tuple>
|
||||||
|
using namespace std;
|
||||||
class Exception: public exception
|
|
||||||
{
|
/*class Exception: public exception
|
||||||
protected:
|
{
|
||||||
// Сообщение об ошибке
|
protected:
|
||||||
char* str;
|
//????????? ?? ??????
|
||||||
public:
|
char* str;
|
||||||
Exception(const char* s)
|
public:
|
||||||
{
|
Exception(const char* s)
|
||||||
str = new char[strlen(s) + 1];
|
{
|
||||||
strcpy(str, s);
|
str = new char[strlen(s) + 1];
|
||||||
}
|
strcpy_s(str, strlen(s) + 1, s);
|
||||||
Exception(const Exception& e)
|
}
|
||||||
{
|
Exception(const Exception& e)
|
||||||
str = new char[strlen(e.str) + 1];
|
{
|
||||||
strcpy(str, e.str);
|
str = new char[strlen(e.str) + 1];
|
||||||
}
|
strcpy_s(str, strlen(e.str) + 1, e.str);
|
||||||
~Exception()
|
}
|
||||||
{
|
~Exception()
|
||||||
delete[] str;
|
{
|
||||||
}
|
delete[] str;
|
||||||
// Функцию вывода можно будет переопределить в производных
|
}
|
||||||
// классах, когда будет ясна конкретика
|
|
||||||
virtual void print()
|
virtual void print()
|
||||||
{
|
{
|
||||||
cout << "Exception: " << str;
|
cout << "Exception: " << str<< "; "<<what();
|
||||||
}
|
}
|
||||||
};
|
};*/
|
||||||
|
|
||||||
class BaseMatrix
|
class Exception : public exception
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
double** ptr;
|
char* str;
|
||||||
int height;
|
public:
|
||||||
int width;
|
Exception(const char* s)
|
||||||
|
{
|
||||||
public:
|
str = new char[strlen(s) + 1];
|
||||||
BaseMatrix(int Height = 2, int Width = 2)
|
strcpy_s(str, strlen(s) + 1, s);
|
||||||
{
|
}
|
||||||
if (Height <= 0 || Width <= 0)
|
Exception(const Exception& e)
|
||||||
throw Exception("Non-positive size of matrix");
|
{
|
||||||
height = Height;
|
str = new char[strlen(e.str) + 1];
|
||||||
width = Width;
|
strcpy_s(str, strlen(e.str) + 1, e.str);
|
||||||
ptr = new double*[height];
|
}
|
||||||
for (int i = 0; i < height; i++)
|
~Exception()
|
||||||
ptr[i] = new double[width];
|
{
|
||||||
}
|
delete[] str;
|
||||||
|
}
|
||||||
BaseMatrix(const BaseMatrix& M) : height(M.height), width(M.width)
|
|
||||||
{
|
virtual void print()
|
||||||
ptr = new double*[height];
|
{
|
||||||
for (int i = 0; i < height; i++)
|
cout << "Exception: " << str << "; " << what();
|
||||||
{
|
}
|
||||||
ptr[i] = new double[width];
|
};
|
||||||
for (int j = 0; j < width; j++)
|
class InvalidOperationException : public Exception {
|
||||||
ptr[i][j] = M.ptr[i][j];
|
public:
|
||||||
}
|
InvalidOperationException(const char* s) : Exception(s) {}
|
||||||
}
|
virtual void print()
|
||||||
|
{
|
||||||
BaseMatrix(double** arr, int Height, int Width) : height(Height), width(Width)
|
cout << "InvalidOperationException: " << str << "; " << what();
|
||||||
{
|
}
|
||||||
ptr = new double*[height];
|
};
|
||||||
for (int i = 0; i < height; i++)
|
|
||||||
{
|
|
||||||
ptr[i] = new double[width];
|
class WrongSizeException : public Exception
|
||||||
for (int j = 0; j < width; j++)
|
{
|
||||||
ptr[i][j] = arr[i][j];
|
protected:
|
||||||
}
|
int Row1, Col1, Row2, Col2;
|
||||||
}
|
public:
|
||||||
|
WrongSizeException(const char* s, int row1, int col1, int row2, int col2) : Exception(s) { Row1 = row1; Col1 = col1; Row2 = row2; Col2 = col2; }
|
||||||
~BaseMatrix()
|
virtual void print()
|
||||||
{
|
{
|
||||||
if (ptr != NULL)
|
cout << "WrongSizeException: " << str << "; " << Row1 << ", " << Col1 << ", " << Row2 << ", " << Col2 << "; " << what();
|
||||||
{
|
}
|
||||||
for (int i = 0; i < height; i++)
|
};
|
||||||
delete[] ptr[i];
|
|
||||||
delete[] ptr;
|
class IndexOutOfBoundsException : public Exception
|
||||||
ptr = NULL;
|
{
|
||||||
}
|
protected:
|
||||||
}
|
int Row, Col;
|
||||||
|
public:
|
||||||
void print()
|
IndexOutOfBoundsException(const char* s, int row, int col) : Exception(s) { Row = row; Col = col; }
|
||||||
{
|
virtual void print()
|
||||||
for (int i = 0; i < height; i++)
|
{
|
||||||
{
|
cout << "IndexOutOfBoundsException: " << str << "; " << Row << ", " << Col << "; " << what();
|
||||||
for (int j = 0; j < width; j++)
|
}
|
||||||
cout << ptr[i][j] << " ";
|
};
|
||||||
cout << "\n";
|
class NonPositiveSizeException : public WrongSizeException {
|
||||||
}
|
public:
|
||||||
}
|
NonPositiveSizeException(const char* s, int row1, int col1, int row2, int col2) : WrongSizeException(s, row1, col1, row2, col2) {}
|
||||||
|
|
||||||
double& operator()(int row, int column)
|
virtual void print()
|
||||||
{
|
{
|
||||||
if (row < 0 || column < 0 || row >= height || column >= width)
|
cout << "NonPositiveSizeException: " << str << "; " << Row1 << ", " << Col1 << ", " << Row2 << ", " << Col2 << "; " << what();
|
||||||
throw Exception("Index is out of bounds");
|
}
|
||||||
return ptr[row][column];
|
};
|
||||||
}
|
|
||||||
|
class TooLargeSizeException : public WrongSizeException {
|
||||||
BaseMatrix& operator=(const BaseMatrix& other)
|
public:
|
||||||
{
|
TooLargeSizeException(const char* s, int row1, int col1, int row2, int col2) : WrongSizeException(s, row1, col1, row2, col2) {}
|
||||||
if (this != &other)
|
|
||||||
{
|
virtual void print()
|
||||||
for (int i = 0; i < height; i++)
|
{
|
||||||
delete[] ptr[i];
|
cout << "TooLargeSizeException: " << str << "; " << Row1 << ", " << Col1 << ", " << Row2 << ", " << Col2 << "; " << what();
|
||||||
delete[] ptr;
|
}
|
||||||
|
};
|
||||||
height = other.height;
|
|
||||||
width = other.width;
|
template<class T>
|
||||||
|
class BaseMatrix
|
||||||
ptr = new double*[height];
|
{
|
||||||
for (int i = 0; i < height; i++)
|
protected:
|
||||||
{
|
T** ptr;
|
||||||
ptr[i] = new double[width];
|
int height;
|
||||||
for (int j = 0; j < width; j++)
|
int width;
|
||||||
ptr[i][j] = other.ptr[i][j];
|
public:
|
||||||
}
|
BaseMatrix(int Height = 2, int Width = 2)
|
||||||
}
|
{
|
||||||
return *this;
|
if (Height <= 0 || Width <= 0) {
|
||||||
}
|
throw NonPositiveSizeException("Attempt to create a matrix of negative or zero size", Height, Width, -1, -1);
|
||||||
|
}
|
||||||
friend ostream& operator<<(ostream& ustream, BaseMatrix obj);
|
height = Height;
|
||||||
friend istream& operator>>(istream& ustream, BaseMatrix& obj);
|
width = Width;
|
||||||
};
|
ptr = new T* [height];
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
ostream& operator<<(ostream& ustream, BaseMatrix obj)
|
ptr[i] = new T[width];
|
||||||
{
|
}
|
||||||
if (typeid(ustream).name() == typeid(ofstream).name())
|
|
||||||
{
|
BaseMatrix(const BaseMatrix& M)
|
||||||
ustream << obj.height << " " << obj.width << "\n";
|
{
|
||||||
for (int i = 0; i < obj.height; i++)
|
height = M.height;
|
||||||
{
|
width = M.width;
|
||||||
for (int j = 0; j < obj.width; j++)
|
ptr = new T* [height];
|
||||||
ustream << obj.ptr[i][j] << "\n";
|
for (int i = 0; i < height; i++)
|
||||||
}
|
{
|
||||||
return ustream;
|
ptr[i] = new T[width];
|
||||||
}
|
for (int j = 0; j < width; j++)
|
||||||
for (int i = 0; i < obj.height; i++)
|
ptr[i][j] = M.ptr[i][j];
|
||||||
{
|
}
|
||||||
for (int j = 0; j < obj.width; j++)
|
}
|
||||||
ustream << obj.ptr[i][j] << " ";
|
|
||||||
ustream << "\n";
|
BaseMatrix operator=(BaseMatrix& V)
|
||||||
}
|
{
|
||||||
return ustream;
|
if (height != V.height && width != V.width)
|
||||||
}
|
{
|
||||||
|
if (ptr != NULL)
|
||||||
istream& operator>>(istream& ustream, BaseMatrix& obj)
|
{
|
||||||
{
|
for (int i = 0; i < height; i++)
|
||||||
if (typeid(ustream) == typeid(ifstream))
|
delete[] ptr[i];
|
||||||
ustream >> obj.height >> obj.width;
|
delete[] ptr;
|
||||||
for (int i = 0; i < obj.height; i++)
|
ptr = NULL;
|
||||||
for (int j = 0; j < obj.width; j++)
|
}
|
||||||
ustream >> obj.ptr[i][j];
|
height = V.height;
|
||||||
return ustream;
|
width = V.width;
|
||||||
}
|
ptr = new double* [height];
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
class MultidimensionalArray : public BaseMatrix
|
ptr[i] = new double[width];
|
||||||
{
|
}
|
||||||
public:
|
for (int i = 0; i < height; i++)
|
||||||
MultidimensionalArray(int Height = 2, int Width = 2) : BaseMatrix(Height, Width) {}
|
for (int j = 0; j < width; j++)
|
||||||
|
ptr[i][j] = V.ptr[i][j];
|
||||||
MultidimensionalArray(const MultidimensionalArray& M) : BaseMatrix(M) {}
|
return *this;
|
||||||
|
}
|
||||||
MultidimensionalArray(double** arr, int Height, int Width) : BaseMatrix(arr, Height, Width) {}
|
|
||||||
|
~BaseMatrix()
|
||||||
MultidimensionalArray& operator=(const MultidimensionalArray& other)
|
{
|
||||||
{
|
if (ptr != NULL)
|
||||||
BaseMatrix::operator=(other);
|
{
|
||||||
return *this;
|
for (int i = 0; i < height; i++)
|
||||||
}
|
delete[] ptr[i];
|
||||||
|
delete[] ptr;
|
||||||
pair<double, double> calculateCenterOfMass()
|
ptr = NULL;
|
||||||
{
|
}
|
||||||
double totalMass = 0.0;
|
}
|
||||||
double weightedSumX = 0.0;
|
|
||||||
double weightedSumY = 0.0;
|
void print()
|
||||||
|
{
|
||||||
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++)
|
||||||
{
|
cout << ptr[i][j] << " ";
|
||||||
double mass = ptr[i][j];
|
cout << "\n";
|
||||||
totalMass += mass;
|
}
|
||||||
weightedSumX += j * mass; // умножаем координату на массу
|
}
|
||||||
weightedSumY += i * mass; // умножаем координату на массу
|
|
||||||
}
|
double& operator()(int row, int col)
|
||||||
}
|
{
|
||||||
|
if (row < 0 || col < 0 || row >= height || col >= width)
|
||||||
if (totalMass == 0.0)
|
throw IndexOutOfBoundsException("IndexOutOfBoundsException in operator()", row, col);
|
||||||
{
|
return ptr[row][col];
|
||||||
return make_pair(-1.0, -1.0); // Или другое значение по умолчанию
|
}
|
||||||
}
|
};
|
||||||
|
template <class T>
|
||||||
double centerX = weightedSumX / totalMass;
|
class Matrix : public BaseMatrix<T>
|
||||||
double centerY = weightedSumY / totalMass;
|
{
|
||||||
|
public:
|
||||||
return make_pair(centerX, centerY);
|
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"; }
|
||||||
int main()
|
~Matrix() { cout << "\nMatrix destructor\n"; }
|
||||||
{
|
Matrix(T** array, int Height, int Width) : BaseMatrix<T>(Height, Width) {
|
||||||
try
|
ptr = new T* [height];
|
||||||
{
|
for (int i = 0; i < height; ++i) {
|
||||||
MultidimensionalArray Wrong(-2, 0);
|
ptr[i] = new T[width];
|
||||||
}
|
for (int j = 0; j < width; ++j) {
|
||||||
catch (Exception e)
|
ptr[i][j] = array[i][j];
|
||||||
{
|
}
|
||||||
cout << "\nException has been caught: ";
|
}
|
||||||
e.print();
|
}
|
||||||
}
|
// Filling a matrix element by the sum of its indices
|
||||||
|
void fillRandom(int a) {
|
||||||
cout << "\n";
|
// Initialization of random number generator with current time
|
||||||
|
srand(time(nullptr));
|
||||||
MultidimensionalArray M;
|
|
||||||
cin >> M;
|
for (int i = 0; i < height; ++i) {
|
||||||
|
for (int j = 0; j < width; ++j) {
|
||||||
ofstream fout("out.txt");
|
// Random number generation
|
||||||
if (fout.is_open())
|
ptr[i][j] = (rand() * (a + 1))%100;
|
||||||
{
|
}
|
||||||
fout << M;
|
}
|
||||||
fout.close();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ifstream fin("out.txt");
|
pair<double, double> calculateCenterOfMass() {
|
||||||
MultidimensionalArray M1;
|
double totalMass = 0;
|
||||||
if (fin)
|
double centerX = 0;
|
||||||
{
|
double centerY = 0;
|
||||||
fin >> M1;
|
|
||||||
fin.close();
|
for (int i = 0; i < this->height; ++i) {
|
||||||
}
|
for (int j = 0; j < this->width; ++j) {
|
||||||
|
totalMass += this->ptr[i][j];
|
||||||
cout << M1;
|
centerX += i * this->ptr[i][j];
|
||||||
|
centerY += j * this->ptr[i][j];
|
||||||
char c1;
|
}
|
||||||
cin >> c1;
|
}
|
||||||
|
if (totalMass != 0) {
|
||||||
return 0;
|
centerX /= totalMass;
|
||||||
}
|
centerY /= totalMass;
|
||||||
|
}
|
||||||
|
return make_pair(centerX, centerY);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
double Trace()
|
||||||
|
{
|
||||||
|
if (height != width) {
|
||||||
|
throw InvalidOperationException("The operation of finding the trace is not defined for a non-square matrix");
|
||||||
|
}
|
||||||
|
double summ = 0;
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
|
summ += ptr[i][i];
|
||||||
|
return summ;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix operator+(Matrix M)
|
||||||
|
{
|
||||||
|
if (height != M.height || width != M.width)
|
||||||
|
throw WrongSizeException("Unequal size of matrices to add in operator+()", height, width, M.height, M.width);
|
||||||
|
Matrix res(height, width);
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
res(i, j) = ptr[i][j] + M.ptr[i][j];
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
// Перегрузка оператора присваивания
|
||||||
|
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) {
|
||||||
|
ptr[i] = new double[width];
|
||||||
|
for (int j = 0; j < width; ++j) {
|
||||||
|
ptr[i][j] = other.ptr[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
friend ostream& operator<<(ostream& s, Matrix<T> M);
|
||||||
|
template<class T>
|
||||||
|
friend istream& operator>>(istream& s, Matrix<T>& M);
|
||||||
|
};
|
||||||
|
template<class T>
|
||||||
|
ostream& operator<<(ostream& s, Matrix<T> M)
|
||||||
|
{
|
||||||
|
if (typeid(s) == typeid(ofstream))
|
||||||
|
{
|
||||||
|
s << M.height << " " << M.width << " ";
|
||||||
|
for (int i = 0; i < M.height; i++)
|
||||||
|
for (int j = 0; j < M.width; j++)
|
||||||
|
s << M.ptr[i][j] << " ";
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < M.height; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < M.width; j++)
|
||||||
|
s << M.ptr[i][j] << " ";
|
||||||
|
s << "\n";
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
istream& operator>>(istream& s, Matrix<T>& M)
|
||||||
|
{
|
||||||
|
if (typeid(s) == typeid(ifstream))
|
||||||
|
{
|
||||||
|
int w, h; s >> h >> w;
|
||||||
|
if (h != M.height || w != M.width)
|
||||||
|
{
|
||||||
|
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];
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
try {
|
||||||
|
Matrix<double> M[10];
|
||||||
|
// Matrix filling and output
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
cout << "Matrix " << i + 1 << ":" << endl;
|
||||||
|
M[i].fillRandom(i);
|
||||||
|
cout << M[i] << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Saving a matrix array to a file
|
||||||
|
ofstream fout("1.txt");
|
||||||
|
if (fout) {
|
||||||
|
fout << "3\n";
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
fout << M[i] << "\n";
|
||||||
|
fout.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// reading a matrix array from a file
|
||||||
|
Matrix<double>* M1;
|
||||||
|
ifstream fin("1.txt");
|
||||||
|
if (fin) {
|
||||||
|
int n;
|
||||||
|
fin >> n;
|
||||||
|
M1 = new Matrix<double>[n];
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
fin >> M1[i];
|
||||||
|
cout << "Matrix from file " << i + 1 << ":" << endl;
|
||||||
|
cout << M1[i] << endl;
|
||||||
|
}
|
||||||
|
delete[] M1;
|
||||||
|
}
|
||||||
|
Matrix<double> A(2, 2);
|
||||||
|
A.fillRandom(4);
|
||||||
|
A.print();
|
||||||
|
Matrix<double> B(2, 2);
|
||||||
|
B.fillRandom(3);
|
||||||
|
B.print();
|
||||||
|
Matrix<double> C = A + B; // matrix1+matrix2
|
||||||
|
cout << "A + B:" << endl << C << endl;
|
||||||
|
|
||||||
|
Matrix<double> D;
|
||||||
|
D = A; //Assignment of matrix
|
||||||
|
cout << "D (assigned from A):" << endl << D << endl;
|
||||||
|
double a, b;
|
||||||
|
A.print();
|
||||||
|
tie(a,b)=A.calculateCenterOfMass(); //calculate Center Of Mass
|
||||||
|
cout << "Center of Mass of A:" << endl;
|
||||||
|
cout << a << ", " << b << "\n";
|
||||||
|
double traceA = A.Trace(); // calculate trace
|
||||||
|
cout << "Trace of A: " << traceA << endl;
|
||||||
|
}
|
||||||
|
catch (IndexOutOfBoundsException& ex) {
|
||||||
|
cout << "IndexOutOfBoundsException has been caught: ";
|
||||||
|
ex.print();
|
||||||
|
}
|
||||||
|
catch (Exception& ex) {
|
||||||
|
cout << "Exception has been caught: ";
|
||||||
|
ex.print();
|
||||||
|
}
|
||||||
|
catch (exception& ex) {
|
||||||
|
cout << "Standard exception has been caught: " << ex.what();
|
||||||
|
}
|
||||||
|
|
||||||
|
// char c;
|
||||||
|
// cin >> c;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user