From 56d58f4d28f545efe4d19bc219b727de6c1f3564 Mon Sep 17 00:00:00 2001 From: GandC Date: Fri, 10 May 2024 19:52:53 +0300 Subject: [PATCH] Pre-end version --- pr5.cpp | 642 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 398 insertions(+), 244 deletions(-) diff --git a/pr5.cpp b/pr5.cpp index e70b938..f3c7152 100644 --- a/pr5.cpp +++ b/pr5.cpp @@ -1,244 +1,398 @@ -#include -#include -#include -#include -#include -using namespace std; - -class Exception: public exception -{ -protected: - // Сообщение об ошибке - char* str; -public: - Exception(const char* s) - { - str = new char[strlen(s) + 1]; - strcpy(str, s); - } - Exception(const Exception& e) - { - str = new char[strlen(e.str) + 1]; - strcpy(str, e.str); - } - ~Exception() - { - delete[] str; - } - // Функцию вывода можно будет переопределить в производных - // классах, когда будет ясна конкретика - virtual void print() - { - cout << "Exception: " << str; - } -}; - -class BaseMatrix -{ -protected: - double** ptr; - int height; - int width; - -public: - BaseMatrix(int Height = 2, int Width = 2) - { - if (Height <= 0 || Width <= 0) - throw Exception("Non-positive size of matrix"); - height = Height; - width = Width; - ptr = new double*[height]; - for (int i = 0; i < height; i++) - ptr[i] = new double[width]; - } - - BaseMatrix(const BaseMatrix& M) : height(M.height), width(M.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] = M.ptr[i][j]; - } - } - - BaseMatrix(double** arr, int Height, int Width) : height(Height), width(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] = arr[i][j]; - } - } - - ~BaseMatrix() - { - if (ptr != NULL) - { - for (int i = 0; i < height; i++) - delete[] ptr[i]; - delete[] ptr; - ptr = NULL; - } - } - - void print() - { - for (int i = 0; i < height; i++) - { - for (int j = 0; j < width; j++) - cout << ptr[i][j] << " "; - cout << "\n"; - } - } - - double& operator()(int row, int column) - { - if (row < 0 || column < 0 || row >= height || column >= width) - throw Exception("Index is out of bounds"); - return ptr[row][column]; - } - - BaseMatrix& operator=(const BaseMatrix& other) - { - if (this != &other) - { - for (int i = 0; i < height; i++) - delete[] ptr[i]; - delete[] ptr; - - 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; - } - - friend ostream& operator<<(ostream& ustream, BaseMatrix obj); - friend istream& operator>>(istream& ustream, BaseMatrix& obj); -}; - -ostream& operator<<(ostream& ustream, BaseMatrix obj) -{ - if (typeid(ustream).name() == typeid(ofstream).name()) - { - ustream << obj.height << " " << obj.width << "\n"; - for (int i = 0; i < obj.height; i++) - { - for (int j = 0; j < obj.width; j++) - ustream << obj.ptr[i][j] << "\n"; - } - return ustream; - } - for (int i = 0; i < obj.height; i++) - { - for (int j = 0; j < obj.width; j++) - ustream << obj.ptr[i][j] << " "; - ustream << "\n"; - } - return ustream; -} - -istream& operator>>(istream& ustream, BaseMatrix& obj) -{ - if (typeid(ustream) == typeid(ifstream)) - ustream >> obj.height >> obj.width; - for (int i = 0; i < obj.height; i++) - for (int j = 0; j < obj.width; j++) - ustream >> obj.ptr[i][j]; - return ustream; -} - -class MultidimensionalArray : public BaseMatrix -{ -public: - MultidimensionalArray(int Height = 2, int Width = 2) : BaseMatrix(Height, Width) {} - - MultidimensionalArray(const MultidimensionalArray& M) : BaseMatrix(M) {} - - MultidimensionalArray(double** arr, int Height, int Width) : BaseMatrix(arr, Height, Width) {} - - MultidimensionalArray& operator=(const MultidimensionalArray& other) - { - BaseMatrix::operator=(other); - return *this; - } - - pair calculateCenterOfMass() - { - double totalMass = 0.0; - double weightedSumX = 0.0; - double weightedSumY = 0.0; - - for (int i = 0; i < height; ++i) - { - for (int j = 0; j < width; ++j) - { - double mass = ptr[i][j]; - totalMass += mass; - weightedSumX += j * mass; // умножаем координату на массу - weightedSumY += i * mass; // умножаем координату на массу - } - } - - if (totalMass == 0.0) - { - return make_pair(-1.0, -1.0); // Или другое значение по умолчанию - } - - double centerX = weightedSumX / totalMass; - double centerY = weightedSumY / totalMass; - - return make_pair(centerX, centerY); - } -}; - -int main() -{ - try - { - MultidimensionalArray Wrong(-2, 0); - } - catch (Exception e) - { - cout << "\nException has been caught: "; - e.print(); - } - - cout << "\n"; - - MultidimensionalArray M; - cin >> M; - - ofstream fout("out.txt"); - if (fout.is_open()) - { - fout << M; - fout.close(); - } - - ifstream fin("out.txt"); - MultidimensionalArray M1; - if (fin) - { - fin >> M1; - fin.close(); - } - - cout << M1; - - char c1; - cin >> c1; - - return 0; -} +#include +#include +#include +#include +#include +#include +using namespace std; + +/*class Exception: public exception +{ +protected: + //????????? ?? ?????? + char* str; +public: + Exception(const char* s) + { + str = new char[strlen(s) + 1]; + strcpy_s(str, strlen(s) + 1, s); + } + Exception(const Exception& e) + { + str = new char[strlen(e.str) + 1]; + strcpy_s(str, strlen(e.str) + 1, e.str); + } + ~Exception() + { + delete[] str; + } + + virtual void print() + { + cout << "Exception: " << str<< "; "< +class BaseMatrix +{ +protected: + T** ptr; + int height; + int width; +public: + BaseMatrix(int Height = 2, int Width = 2) + { + if (Height <= 0 || Width <= 0) { + throw NonPositiveSizeException("Attempt to create a matrix of negative or zero size", Height, Width, -1, -1); + } + height = Height; + width = Width; + ptr = new T* [height]; + for (int i = 0; i < height; i++) + ptr[i] = new T[width]; + } + + BaseMatrix(const BaseMatrix& M) + { + height = M.height; + width = M.width; + ptr = new T* [height]; + for (int i = 0; i < height; i++) + { + ptr[i] = new T[width]; + for (int j = 0; j < width; j++) + ptr[i][j] = M.ptr[i][j]; + } + } + + BaseMatrix operator=(BaseMatrix& V) + { + if (height != V.height && width != V.width) + { + if (ptr != NULL) + { + for (int i = 0; i < height; i++) + delete[] ptr[i]; + delete[] ptr; + ptr = NULL; + } + height = V.height; + width = V.width; + ptr = new double* [height]; + for (int i = 0; i < height; i++) + ptr[i] = new double[width]; + } + for (int i = 0; i < height; i++) + for (int j = 0; j < width; j++) + ptr[i][j] = V.ptr[i][j]; + return *this; + } + + ~BaseMatrix() + { + if (ptr != NULL) + { + for (int i = 0; i < height; i++) + delete[] ptr[i]; + delete[] ptr; + ptr = NULL; + } + } + + void print() + { + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + cout << ptr[i][j] << " "; + cout << "\n"; + } + } + + double& operator()(int row, int col) + { + if (row < 0 || col < 0 || row >= height || col >= width) + throw IndexOutOfBoundsException("IndexOutOfBoundsException in operator()", row, col); + return ptr[row][col]; + } +}; +template +class Matrix : public BaseMatrix +{ +public: + using BaseMatrix::ptr; + using BaseMatrix::height; + using BaseMatrix::width; + Matrix(int Height = 2, int Width = 2) : BaseMatrix(Height, Width) { cout << "\nMatrix constructor\n"; } + ~Matrix() { cout << "\nMatrix destructor\n"; } + Matrix(T** array, int Height, int Width) : BaseMatrix(Height, Width) { + ptr = new T* [height]; + for (int i = 0; i < height; ++i) { + ptr[i] = new T[width]; + 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) { + // 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) { + // Random number generation + ptr[i][j] = (rand() * (a + 1))%100; + } + } + } + + + pair 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) { + totalMass += this->ptr[i][j]; + centerX += i * this->ptr[i][j]; + centerY += j * this->ptr[i][j]; + } + } + if (totalMass != 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 + friend ostream& operator<<(ostream& s, Matrix M); + template + friend istream& operator>>(istream& s, Matrix& M); +}; +template +ostream& operator<<(ostream& s, Matrix 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 +istream& operator>>(istream& s, Matrix& 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 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* M1; + ifstream fin("1.txt"); + if (fin) { + int n; + fin >> n; + M1 = new Matrix[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 A(2, 2); + A.fillRandom(4); + A.print(); + Matrix B(2, 2); + B.fillRandom(3); + B.print(); + Matrix C = A + B; // matrix1+matrix2 + cout << "A + B:" << endl << C << endl; + + Matrix 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; +}