From b2792eafbfaae68eeb558dee6ee142bb5282d5c0 Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Sun, 10 Nov 2024 20:19:58 +0500 Subject: [PATCH 1/7] =?UTF-8?q?Create=20=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=9C=D0=B8=D0=BD=D0=B3=D0=B0=D0=BB=D0=B5=D0=B5=D0=B2=20=D0=A0?= =?UTF-8?q?=D0=98-232702?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\320\265\320\262 \320\240\320\230-232702" | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 "task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" diff --git "a/task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" "b/task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" new file mode 100644 index 0000000..bacd3db --- /dev/null +++ "b/task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" @@ -0,0 +1,112 @@ +#include +#include +#include + +class Matrix { +public: + // Конструктор по умолчанию + Matrix() = default; + + // Конструктор с параметрами + Matrix(int numRows, int numCols) { + Reset(numRows, numCols); + } + + // Метод Reset + void Reset(int numRows, int numCols) { + if (numRows <= 0 || numCols <= 0) { + // Если передано 0 или отрицательное значение, обнуляем размеры + num_rows = 0; + num_cols = 0; + data.clear(); + } else { + num_rows = numRows; + num_cols = numCols; + data.assign(num_rows, std::vector(num_cols, 0)); + } + } + + // Неконстантный метод At + int& At(int row, int col) { + if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { + throw std::out_of_range("Index out of range"); + } + return data[row][col]; + } + + // Константный метод At + const int& At(int row, int col) const { + if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { + throw std::out_of_range("Index out of range"); + } + return data[row][col]; + } + + // Константные методы GetRows и GetCols + int GetRows() const { return num_rows; } + int GetCols() const { return num_cols; } + + // Оператор равенства + bool operator==(const Matrix& m2) { + if (num_rows != m2.num_rows || num_cols != m2.num_cols) { + return false; + } + for (int i = 0; i < num_rows; ++i) { + for (int j = 0; j < num_cols; ++j) { + if (data[i][j] != m2.data[i][j]) { + return false; + } + } + } + return true; + } + + // Оператор неравенства + bool operator!=(const Matrix& m2) { + return !(*this == m2); + } + + // Оператор сложения + Matrix operator+(const Matrix& m2) { + if (num_rows != m2.num_rows || num_cols != m2.num_cols) { + throw std::invalid_argument("Matrices must have the same dimensions for addition"); + } + Matrix result(num_rows, num_cols); + for (int i = 0; i < num_rows; ++i) { + for (int j = 0; j < num_cols; ++j) { + result.At(i, j) = At(i, j) + m2.At(i, j); + } + } + return result; + } + +private: + int num_rows = 0; // Количество строк + int num_cols = 0; // Количество столбцов + std::vector> data; // Данные матрицы +}; + +// Пример использования +int main() { + Matrix mat1(2, 3); + Matrix mat2(2, 3); + + mat1.At(0, 0) = 1; + mat1.At(0, 1) = 2; + mat1.At(0, 2) = 3; + + mat2.At(0, 0) = 4; + mat2.At(0, 1) = 5; + mat2.At(0, 2) = 6; + + Matrix mat3 = mat1 + mat2; + + for (int i = 0; i < mat3.GetRows(); ++i) { + for (int j = 0; j < mat3.GetCols(); ++j) { + std::cout << mat3.At(i, j) << " "; + } + std::cout << std::endl; + } + + return 0; +} From 61f154d7ed75945d3a60d5a7a916cef337abb202 Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Sun, 10 Nov 2024 20:25:16 +0500 Subject: [PATCH 2/7] =?UTF-8?q?Create=20=D0=9C=D0=B8=D0=BD=D0=B3=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5=D0=B5=D0=B2=20=D0=A0.=D0=9C.=20(=D0=A0=D0=98-23270?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\240.\320\234. (\320\240\320\230-232702)" | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 "task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" diff --git "a/task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" "b/task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" new file mode 100644 index 0000000..2bfdc7e --- /dev/null +++ "b/task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" @@ -0,0 +1,143 @@ +#include +#include +#include +#include + +enum class FigureType { + TRIANGLE, + CIRCLE, + RECTANGLE, +}; + +class Figure { +public: + virtual FigureType Type() const = 0; + virtual double Perimeter() const = 0; + virtual double Area() const = 0; + virtual ~Figure() = default; // Виртуальный деструктор +}; + +class Rect : public Figure { +public: + Rect(double width, double height) : width(width), height(height) {} + + FigureType Type() const override { + return FigureType::RECTANGLE; + } + + double Perimeter() const override { + return 2 * (width + height); + } + + double Area() const override { + return width * height; + } + +private: + double width; + double height; +}; + +class Triangle : public Figure { +public: + Triangle(double a, double b, double c) : a(a), b(b), c(c) { + if (a <= 0 || b <= 0 || c <= 0) { + throw LessThanZeroParam("Sides must be greater than zero."); + } + if (!IsValidTriangle(a, b, c)) { + throw WrongTriangle("The provided sides do not form a valid triangle."); + } + } + + FigureType Type() const override { + return FigureType::TRIANGLE; + } + + double Perimeter() const override { + return a + b + c; + } + + double Area() const override { + double s = Perimeter() / 2; // Полупериметр + return std::sqrt(s * (s - a) * (s - b) * (s - c)); // Формула Герона + } + +private: + double a, b, c; + + bool IsValidTriangle(double a, double b, double c) const { + return (a + b > c) && (a + c > b) && (b + c > a); + } +}; + +class Circle : public Figure { +public: + Circle(double radius) : radius(radius) { + if (radius <= 0) { + throw LessThanZeroParam("Radius must be greater than zero."); + } + } + + FigureType Type() const override { + return FigureType::CIRCLE; + } + + double Perimeter() const override { + return 2 * 3.14 * radius; // Используем число Пи, равное 3.14 + } + + double Area() const override { + return 3.14 * radius * radius; // Площадь круга + } + +private: + double radius; +}; + +std::unique_ptr
make_figure(FigureType type, double a, double b = 0, double c = 0) { + switch (type) { + case FigureType::TRIANGLE: + return std::make_unique(a, b, c); + case FigureType::CIRCLE: + return std::make_unique(a); + case FigureType::RECTANGLE: + return std::make_unique(a, b); + default: + throw std::invalid_argument("Invalid figure type"); + } +} + +// Исключения +class WrongTriangle : public std::invalid_argument { +public: + explicit WrongTriangle(const std::string& message) : std::invalid_argument(message) {} +}; + +class LessThanZeroParam : public std::invalid_argument { +public: + explicit LessThanZeroParam(const std::string& message) : std::invalid_argument(message) {} +}; + +// Пример использования +int main() { + try { + auto triangle = make_figure(FigureType::TRIANGLE, 3, 4, 5); + std::cout << "Triangle Area: " << triangle->Area() << std::endl; + std::cout << "Triangle Perimeter: " << triangle->Perimeter() << std::endl; + + auto circle = make_figure(FigureType::CIRCLE, 5); + std::cout << "Circle Area: " << circle->Area() << std::endl; + std::cout << "Circle Perimeter: " << circle->Perimeter() << std::endl; + + auto rectangle = make_figure(FigureType::RECTANGLE, 4, 5); + std::cout << "Rectangle Area: " << rectangle->Area() << std::endl; + std::cout << "Rectangle Perimeter: " << rectangle->Perimeter() << std::endl; + + // Пример исключения + auto invalidTriangle = make_figure(FigureType::TRIANGLE, 1, 2, 3); // Не существует + } catch (const std::invalid_argument& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + + return 0; +} From d261d51127d2fb0795a10c94021b95de00210e90 Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Tue, 19 Nov 2024 22:35:29 +0500 Subject: [PATCH 3/7] Update matrix.cpp --- task1/matrix.cpp | 68 +++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/task1/matrix.cpp b/task1/matrix.cpp index fb3598f..c6588fa 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -2,47 +2,61 @@ #include -Matrix::Matrix(int numRows, int numCols) -{ - // your implementation here +Matrix::Matrix(int numRows, int numCols) : rows(numRows), cols(numCols), data(numRows * numCols) { +if (numRows < 0 || numCols < 0) { +throw std::invalid_argument("Invalid matrix size"); +} } -void Matrix::Reset(int numRows, int numCols) -{ - // your implementation here +void Matrix::Reset(int numRows, int numCols) { +if (numRows < 0 || numCols < 0) { +throw std::invalid_argument("Invalid matrix size"); +} +rows = numRows; +cols = numCols; +data.assign(numRows * numCols, 0); } -int& Matrix::At(int row, int col) -{ - // your implementation here +int& Matrix::At(int row, int col) { +if (row < 0 || row >= rows || col < 0 || col >= cols) { +throw std::out_of_range("Matrix index out of range"); +} +return data[row * cols + col]; } -const int& Matrix::At(int row, int col) const -{ - // your implementation here +const int& Matrix::At(int row, int col) const { +if (row < 0 || row >= rows || col < 0 || col >= cols) { +throw std::out_of_range("Matrix index out of range"); +} +return data[row * cols + col]; } -int Matrix::GetRows() const -{ - // your implementation here +int Matrix::GetRows() const { +return rows; } -int Matrix::GetCols() const -{ - // your implementation here +int Matrix::GetCols() const { +return cols; } -bool Matrix::operator==(const Matrix& m2) -{ - // your implementation here +bool Matrix::operator==(const Matrix& m2) { +if (rows != m2.rows || cols != m2.cols) { +return false; +} +return data == m2.data; } -bool Matrix::operator!=(const Matrix& m2) -{ - // your implementation here +bool Matrix::operator!=(const Matrix& m2) { +return !(*this == m2); } -Matrix Matrix::operator+(const Matrix& m2) -{ - // your implementation here +Matrix Matrix::operator+(const Matrix& m2) { +if (rows != m2.rows || cols != m2.cols) { +throw std::invalid_argument("Matrices must have the same dimensions for addition"); +} +Matrix result(rows, cols); +for (int i = 0; i < data.size(); i++) { +result.data[i] = data[i] + m2.data[i]; +} +return result; } From 556e721528ed8c4cac78aa14341db07a6170ff1c Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Tue, 19 Nov 2024 22:42:47 +0500 Subject: [PATCH 4/7] Update matrix.cpp --- task1/matrix.cpp | 84 ++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/task1/matrix.cpp b/task1/matrix.cpp index c6588fa..c537acf 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -2,61 +2,81 @@ #include -Matrix::Matrix(int numRows, int numCols) : rows(numRows), cols(numCols), data(numRows * numCols) { +Matrix::Matrix(int numRows, int numCols) +{ if (numRows < 0 || numCols < 0) { -throw std::invalid_argument("Invalid matrix size"); +throw std::out_of_range("Number of rows and columns must be non-negative"); } + +data_.resize(numRows, std::vector(numCols, 0)); } -void Matrix::Reset(int numRows, int numCols) { +void Matrix::Reset(int numRows, int numCols) +{ if (numRows < 0 || numCols < 0) { -throw std::invalid_argument("Invalid matrix size"); -} -rows = numRows; -cols = numCols; -data.assign(numRows * numCols, 0); +throw std::out_of_range("Number of rows and columns must be non-negative"); } -int& Matrix::At(int row, int col) { -if (row < 0 || row >= rows || col < 0 || col >= cols) { -throw std::out_of_range("Matrix index out of range"); + +data_.resize(numRows, std::vector(numCols, 0)); } -return data[row * cols + col]; + +int& Matrix::At(int row, int col) +{ +if (row < 0 || row >= data_.size() || col < 0 || col >= data_[0].size()) { +throw std::out_of_range("Index out of range"); } -const int& Matrix::At(int row, int col) const { -if (row < 0 || row >= rows || col < 0 || col >= cols) { -throw std::out_of_range("Matrix index out of range"); + +return data_[row][col]; } -return data[row * cols + col]; + +const int& Matrix::At(int row, int col) const +{ +if (row < 0 || row >= data_.size() || col < 0 || col >= data_[0].size()) { +throw std::out_of_range("Index out of range"); } -int Matrix::GetRows() const { -return rows; +return data_[row][col]; } -int Matrix::GetCols() const { -return cols; +int Matrix::GetRows() const +{ +return data_.size(); } -bool Matrix::operator==(const Matrix& m2) { -if (rows != m2.rows || cols != m2.cols) { -return false; +int Matrix::GetCols() const +{ +if (data_.empty()) { +return 0; +} else { +return data_[0].size(); } -return data == m2.data; } -bool Matrix::operator!=(const Matrix& m2) { -return !(*this == m2); +bool Matrix::operator==(const Matrix& m2) +{ +return data_ == m2.data_; } -Matrix Matrix::operator+(const Matrix& m2) { -if (rows != m2.rows || cols != m2.cols) { -throw std::invalid_argument("Matrices must have the same dimensions for addition"); +bool Matrix::operator!=(const Matrix& m2) +{ +return data_ != m2.data_; } -Matrix result(rows, cols); -for (int i = 0; i < data.size(); i++) { -result.data[i] = data[i] + m2.data[i]; + +Matrix Matrix::operator+(const Matrix& m2) +{ +if (data_.size() != m2.data_.size() || data_[0].size() != m2.data_[0].size()) { +throw std::invalid_argument("Matrices must be of the same size for addition"); } + +Matrix result(data_.size(), data_[0].size()); + +for (int i = 0; i < data_.size(); ++i) { + for (int j = 0; j < data_[0].size(); ++j) { + result.At(i, j) = data_[i][j] + m2.At(i, j); + } +} + return result; } From b8b80a60fb4b21ca12337646388b3883a68e7283 Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Tue, 19 Nov 2024 22:47:16 +0500 Subject: [PATCH 5/7] =?UTF-8?q?Delete=20task1/=D0=A0=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D0=BD=20=D0=9C=D0=B8=D0=BD=D0=B3=D0=B0=D0=BB=D0=B5=D0=B5=D0=B2?= =?UTF-8?q?=20=D0=A0=D0=98-232702?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\320\265\320\262 \320\240\320\230-232702" | 112 ------------------ 1 file changed, 112 deletions(-) delete mode 100644 "task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" diff --git "a/task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" "b/task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" deleted file mode 100644 index bacd3db..0000000 --- "a/task1/\320\240\320\276\320\274\320\260\320\275 \320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240\320\230-232702" +++ /dev/null @@ -1,112 +0,0 @@ -#include -#include -#include - -class Matrix { -public: - // Конструктор по умолчанию - Matrix() = default; - - // Конструктор с параметрами - Matrix(int numRows, int numCols) { - Reset(numRows, numCols); - } - - // Метод Reset - void Reset(int numRows, int numCols) { - if (numRows <= 0 || numCols <= 0) { - // Если передано 0 или отрицательное значение, обнуляем размеры - num_rows = 0; - num_cols = 0; - data.clear(); - } else { - num_rows = numRows; - num_cols = numCols; - data.assign(num_rows, std::vector(num_cols, 0)); - } - } - - // Неконстантный метод At - int& At(int row, int col) { - if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { - throw std::out_of_range("Index out of range"); - } - return data[row][col]; - } - - // Константный метод At - const int& At(int row, int col) const { - if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { - throw std::out_of_range("Index out of range"); - } - return data[row][col]; - } - - // Константные методы GetRows и GetCols - int GetRows() const { return num_rows; } - int GetCols() const { return num_cols; } - - // Оператор равенства - bool operator==(const Matrix& m2) { - if (num_rows != m2.num_rows || num_cols != m2.num_cols) { - return false; - } - for (int i = 0; i < num_rows; ++i) { - for (int j = 0; j < num_cols; ++j) { - if (data[i][j] != m2.data[i][j]) { - return false; - } - } - } - return true; - } - - // Оператор неравенства - bool operator!=(const Matrix& m2) { - return !(*this == m2); - } - - // Оператор сложения - Matrix operator+(const Matrix& m2) { - if (num_rows != m2.num_rows || num_cols != m2.num_cols) { - throw std::invalid_argument("Matrices must have the same dimensions for addition"); - } - Matrix result(num_rows, num_cols); - for (int i = 0; i < num_rows; ++i) { - for (int j = 0; j < num_cols; ++j) { - result.At(i, j) = At(i, j) + m2.At(i, j); - } - } - return result; - } - -private: - int num_rows = 0; // Количество строк - int num_cols = 0; // Количество столбцов - std::vector> data; // Данные матрицы -}; - -// Пример использования -int main() { - Matrix mat1(2, 3); - Matrix mat2(2, 3); - - mat1.At(0, 0) = 1; - mat1.At(0, 1) = 2; - mat1.At(0, 2) = 3; - - mat2.At(0, 0) = 4; - mat2.At(0, 1) = 5; - mat2.At(0, 2) = 6; - - Matrix mat3 = mat1 + mat2; - - for (int i = 0; i < mat3.GetRows(); ++i) { - for (int j = 0; j < mat3.GetCols(); ++j) { - std::cout << mat3.At(i, j) << " "; - } - std::cout << std::endl; - } - - return 0; -} From 7190145099b7295fdf7498892013edca495b6597 Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Tue, 19 Nov 2024 22:47:34 +0500 Subject: [PATCH 6/7] =?UTF-8?q?Delete=20task2/=D0=9C=D0=B8=D0=BD=D0=B3?= =?UTF-8?q?=D0=B0=D0=BB=D0=B5=D0=B5=D0=B2=20=D0=A0.=D0=9C.=20(=D0=A0=D0=98?= =?UTF-8?q?-232702)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\240.\320\234. (\320\240\320\230-232702)" | 143 ------------------ 1 file changed, 143 deletions(-) delete mode 100644 "task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" diff --git "a/task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" "b/task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" deleted file mode 100644 index 2bfdc7e..0000000 --- "a/task2/\320\234\320\270\320\275\320\263\320\260\320\273\320\265\320\265\320\262 \320\240.\320\234. (\320\240\320\230-232702)" +++ /dev/null @@ -1,143 +0,0 @@ -#include -#include -#include -#include - -enum class FigureType { - TRIANGLE, - CIRCLE, - RECTANGLE, -}; - -class Figure { -public: - virtual FigureType Type() const = 0; - virtual double Perimeter() const = 0; - virtual double Area() const = 0; - virtual ~Figure() = default; // Виртуальный деструктор -}; - -class Rect : public Figure { -public: - Rect(double width, double height) : width(width), height(height) {} - - FigureType Type() const override { - return FigureType::RECTANGLE; - } - - double Perimeter() const override { - return 2 * (width + height); - } - - double Area() const override { - return width * height; - } - -private: - double width; - double height; -}; - -class Triangle : public Figure { -public: - Triangle(double a, double b, double c) : a(a), b(b), c(c) { - if (a <= 0 || b <= 0 || c <= 0) { - throw LessThanZeroParam("Sides must be greater than zero."); - } - if (!IsValidTriangle(a, b, c)) { - throw WrongTriangle("The provided sides do not form a valid triangle."); - } - } - - FigureType Type() const override { - return FigureType::TRIANGLE; - } - - double Perimeter() const override { - return a + b + c; - } - - double Area() const override { - double s = Perimeter() / 2; // Полупериметр - return std::sqrt(s * (s - a) * (s - b) * (s - c)); // Формула Герона - } - -private: - double a, b, c; - - bool IsValidTriangle(double a, double b, double c) const { - return (a + b > c) && (a + c > b) && (b + c > a); - } -}; - -class Circle : public Figure { -public: - Circle(double radius) : radius(radius) { - if (radius <= 0) { - throw LessThanZeroParam("Radius must be greater than zero."); - } - } - - FigureType Type() const override { - return FigureType::CIRCLE; - } - - double Perimeter() const override { - return 2 * 3.14 * radius; // Используем число Пи, равное 3.14 - } - - double Area() const override { - return 3.14 * radius * radius; // Площадь круга - } - -private: - double radius; -}; - -std::unique_ptr
make_figure(FigureType type, double a, double b = 0, double c = 0) { - switch (type) { - case FigureType::TRIANGLE: - return std::make_unique(a, b, c); - case FigureType::CIRCLE: - return std::make_unique(a); - case FigureType::RECTANGLE: - return std::make_unique(a, b); - default: - throw std::invalid_argument("Invalid figure type"); - } -} - -// Исключения -class WrongTriangle : public std::invalid_argument { -public: - explicit WrongTriangle(const std::string& message) : std::invalid_argument(message) {} -}; - -class LessThanZeroParam : public std::invalid_argument { -public: - explicit LessThanZeroParam(const std::string& message) : std::invalid_argument(message) {} -}; - -// Пример использования -int main() { - try { - auto triangle = make_figure(FigureType::TRIANGLE, 3, 4, 5); - std::cout << "Triangle Area: " << triangle->Area() << std::endl; - std::cout << "Triangle Perimeter: " << triangle->Perimeter() << std::endl; - - auto circle = make_figure(FigureType::CIRCLE, 5); - std::cout << "Circle Area: " << circle->Area() << std::endl; - std::cout << "Circle Perimeter: " << circle->Perimeter() << std::endl; - - auto rectangle = make_figure(FigureType::RECTANGLE, 4, 5); - std::cout << "Rectangle Area: " << rectangle->Area() << std::endl; - std::cout << "Rectangle Perimeter: " << rectangle->Perimeter() << std::endl; - - // Пример исключения - auto invalidTriangle = make_figure(FigureType::TRIANGLE, 1, 2, 3); // Не существует - } catch (const std::invalid_argument& e) { - std::cerr << "Error: " << e.what() << std::endl; - } - - return 0; -} From 8074ca9380195b049cae3af520ea36d45fc6431c Mon Sep 17 00:00:00 2001 From: Roman-Mingaleev-RI232702 Date: Tue, 19 Nov 2024 22:48:17 +0500 Subject: [PATCH 7/7] Update matrix.cpp --- task1/matrix.cpp | 52 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 43 deletions(-) diff --git a/task1/matrix.cpp b/task1/matrix.cpp index c537acf..fb3598f 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -4,79 +4,45 @@ Matrix::Matrix(int numRows, int numCols) { -if (numRows < 0 || numCols < 0) { -throw std::out_of_range("Number of rows and columns must be non-negative"); -} - -data_.resize(numRows, std::vector(numCols, 0)); + // your implementation here } void Matrix::Reset(int numRows, int numCols) { -if (numRows < 0 || numCols < 0) { -throw std::out_of_range("Number of rows and columns must be non-negative"); -} - - -data_.resize(numRows, std::vector(numCols, 0)); + // your implementation here } int& Matrix::At(int row, int col) { -if (row < 0 || row >= data_.size() || col < 0 || col >= data_[0].size()) { -throw std::out_of_range("Index out of range"); -} - - -return data_[row][col]; + // your implementation here } const int& Matrix::At(int row, int col) const { -if (row < 0 || row >= data_.size() || col < 0 || col >= data_[0].size()) { -throw std::out_of_range("Index out of range"); -} - -return data_[row][col]; + // your implementation here } int Matrix::GetRows() const { -return data_.size(); + // your implementation here } int Matrix::GetCols() const { -if (data_.empty()) { -return 0; -} else { -return data_[0].size(); -} + // your implementation here } bool Matrix::operator==(const Matrix& m2) { -return data_ == m2.data_; + // your implementation here } bool Matrix::operator!=(const Matrix& m2) { -return data_ != m2.data_; + // your implementation here } Matrix Matrix::operator+(const Matrix& m2) { -if (data_.size() != m2.data_.size() || data_[0].size() != m2.data_[0].size()) { -throw std::invalid_argument("Matrices must be of the same size for addition"); -} - -Matrix result(data_.size(), data_[0].size()); - -for (int i = 0; i < data_.size(); ++i) { - for (int j = 0; j < data_[0].size(); ++j) { - result.At(i, j) = data_[i][j] + m2.At(i, j); - } -} - -return result; + // your implementation here }