From 96ad473321894fb7a94ee3d6ce4813530751cc66 Mon Sep 17 00:00:00 2001 From: unsignedNegator Date: Wed, 11 Dec 2024 19:05:48 +0500 Subject: [PATCH 1/4] Update matrix.hpp --- task1/matrix.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/task1/matrix.hpp b/task1/matrix.hpp index d314915..037c99a 100644 --- a/task1/matrix.hpp +++ b/task1/matrix.hpp @@ -2,6 +2,8 @@ class Matrix { +private: + vector> mat; public: Matrix() = default; Matrix(int numRows, int numCols); From 9dde126a93758967795151859545b71fc5e91fa2 Mon Sep 17 00:00:00 2001 From: unsignedNegator Date: Sat, 14 Dec 2024 18:19:28 +0500 Subject: [PATCH 2/4] Update matrix.cpp --- task1/matrix.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 9 deletions(-) diff --git a/task1/matrix.cpp b/task1/matrix.cpp index fb3598f..0437657 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -1,48 +1,129 @@ #include "matrix.hpp" +#include +#include +#include +#include #include Matrix::Matrix(int numRows, int numCols) { - // your implementation here + vector l(NULL,numRows); + for (int i = 0; i < numCols - 1; i++) + { + mat.push_back(l); + } } void Matrix::Reset(int numRows, int numCols) { - // your implementation here + mat.resize(0); + vector l(NULL, numCols); + for (int i = 0; i < numRows - 1; i++) + { + mat.push_back(l); + } } int& Matrix::At(int row, int col) { - // your implementation here + try + { + return mat[row][col]; + } + catch (out_of_range) + { + cout << "Out of range: referring to element (" << row << ", " << col << "), but matrix size is " << mat.size() << "x" << mat[0].size(); + } } const int& Matrix::At(int row, int col) const { - // your implementation here + try + { + return mat[row][col]; + } + catch (out_of_range) + { + cout << "Out of range: referring to element (" << row << ", " << col << "), but matrix size is " << mat.size() << "x" << mat[0].size(); + } } int Matrix::GetRows() const { - // your implementation here + return mat.size(); } int Matrix::GetCols() const { - // your implementation here + return mat[0].size(); } bool Matrix::operator==(const Matrix& m2) { - // your implementation here + if (mat.size() == m2.GetRows() && mat[0].size() == m2.GetCols()) + { + for (int i = 0; i < mat.size() - 1; i++) + { + for (int j = 0; j < mat[0].size() - 1; j++) + { + if (mat[i][j] != m2.At(i, j)) + { + return false; + } + } + } + return true; + } + else { + return false; + } } bool Matrix::operator!=(const Matrix& m2) { - // your implementation here + if (mat.size() == m2.GetRows() && mat[0].size() == m2.GetCols()) + { + for (int i = 0; i < mat.size() - 1; i++) + { + for (int j = 0; j < mat[0].size() - 1; j++) + { + if (mat[i][j] != m2.At(i, j)) + { + return true; + } + } + } + return false; + } + else { + return true; + } } Matrix Matrix::operator+(const Matrix& m2) { - // your implementation here + try + { + if (mat.size() != m2.GetRows() || mat[0].size() != m2.GetCols()) + { + throw 1; + } + else + { + Matrix mres(m2.GetRows(), m2.GetCols()); + for (int i = 0; i < mres.mat.size() - 1; i++) + { + for (int j = 0; j < mres.mat[0].size() - 1; j++) + { + mres.At(i, j) = mat[i][j] + m2.At(i, j); + } + } + return mres; + } + } + catch (int e) + { + cout << "Can't sum matricies of different sizes"; + } } From 0fa23dcd965dabb46f54fc1601f7f6073c019b06 Mon Sep 17 00:00:00 2001 From: Dimtron Date: Tue, 17 Dec 2024 23:24:50 +0500 Subject: [PATCH 3/4] Update figures.cpp --- task2/figures.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++ task2/figures.hpp | 28 ++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/task2/figures.cpp b/task2/figures.cpp index 3b33880..4279f7a 100644 --- a/task2/figures.cpp +++ b/task2/figures.cpp @@ -1,3 +1,99 @@ #include "figures.hpp" static constexpr double PI = 3.14; + +Rect::Rect(double a, double b) +{ + if (a < 0 || b < 0) + { + throw LessThanZeroParam{ "ERR_RECTANGLE_LESS_THAN_ZERO_PARAM" }; + } + s1 = a; + s2 = b; +} + +FigureType Rect::Type() const +{ + return FigureType::RECTANGLE; +} + +double Rect::Perimeter() const +{ + return s1 * 2.0 + s2 * 2.0; +} + +double Rect::Area() const +{ + return s1 * s2; +} + +Triangle::Triangle(double a, double b, double c) +{ + if (a + b <= c || a + c <= b || b + c <= a) + { + throw WrongTriangle{ "ERR_WRONG_TRIANGLE" }; + } + if (a < 0 || b < 0 || c < 0) + { + throw LessThanZeroParam{ "ERR_TRIANGLE_LESS_THAN_ZERO_PARAM" }; + } + s1 = a; + s2 = b; + s3 = c; +} + +FigureType Triangle::Type() const +{ + return FigureType::TRIANGLE; +} + +double Triangle::Perimeter() const +{ + return s1 + s2 + s3; +} + +double Triangle::Area() const +{ + return sqrt((this->Perimeter() / 2) * (this->Perimeter() / 2 - s1) * (this->Perimeter() / 2 - s2) * (this->Perimeter() / 2 - s3)); +} + +std::unique_ptr
make_figure(FigureType type, double a, double b = 0, double c = 0) +{ + + switch (type) + { + case FigureType::RECTANGLE: + std::make_unique(a, b); + break; + case FigureType::TRIANGLE: + std::make_unique(a, b, c); + break; + case FigureType::CIRCLE: + std::make_unique(a); + break; + } +} + +Circle::Circle(double a) +{ + if (a < 0) + { + throw LessThanZeroParam{ "ERR_CIRCLE_LESS_THAN_ZERO_PARAM" }; + } + r = a; +} + +FigureType Circle::Type() const +{ + return FigureType::CIRCLE; +} + +double Circle::Perimeter() const +{ + return 2 * PI * r; +} + +double Circle::Area() const +{ + return r * r * PI; +} diff --git a/task2/figures.hpp b/task2/figures.hpp index 65a6110..befc110 100644 --- a/task2/figures.hpp +++ b/task2/figures.hpp @@ -19,23 +19,51 @@ class Figure { class Rect : public Figure { +private: + double s1; + double s2; +public: + Rect(double a, double b); + FigureType Type() const; + double Perimeter() const; + double Area() const; }; class Triangle : public Figure { +private: + double s1; + double s2; + double s3; +public: + Triangle(double a, double b, double c); + FigureType Type() const; + double Perimeter() const; + double Area() const; }; class Circle : public Figure { +private: + double r; +public: + Circle(double a); + FigureType Type() const; + double Perimeter() const; + double Area() const; }; std::unique_ptr
make_figure(FigureType type, double a, double b = 0, double c = 0); class WrongTriangle : public std::invalid_argument { +public: + WrongTriangle(const std::string& msg) : std::invalid_argument{ msg }{}; }; class LessThanZeroParam : public std::invalid_argument { +public: + LessThanZeroParam(const std::string& msg) : std::invalid_argument{ msg }{}; }; From 335586a330fd43837bcfed2968a7c4be94f8fdbc Mon Sep 17 00:00:00 2001 From: Dimtron Date: Sat, 21 Dec 2024 18:32:20 +0500 Subject: [PATCH 4/4] Updated circular_queue.cpp. --- task3/circular_queue.cpp | 54 ++++++++++++++++++++++++++++++++++------ task3/circular_queue.hpp | 4 +++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index fa74734..8b86fe1 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -2,35 +2,75 @@ CircularQueue::CircularQueue(size_t size) { - // your implementation here + sz = size; + front = -1; + rear = -1; } bool CircularQueue::Push(int value) { - // your implementation here + if (this->Full()) + { + return false; + } + if (front == -1) + { + front = 0; + } + rear = (rear + 1) % sz; + q.push_back(value); + return true; } bool CircularQueue::Pop() { - // your implementation here + if (this->Empty()) + { + return false; + } + q.erase(q.begin() + front); + if (front == rear) + { + front = -1; + rear = -1; + } + else + { + front = (front + 1) % sz; + } + return true; } int CircularQueue::Front() const { - // your implementation here + return q[front]; } int CircularQueue::Back() const { - // your implementation here + return q[rear]; } bool CircularQueue::Empty() const { - // your implementation here + if (front <= -1 || rear <= -1) + { + return true; + } + else + { + return false; + } } bool CircularQueue::Full() const { - // your implementation here + if ((front == 0 && rear == sz - 1) || (front == (rear + 1) % sz)) + { + return true; + } + else + { + return false; + } } diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 2871091..2d08b83 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -1,8 +1,12 @@ #pragma once #include +#include class CircularQueue { +private: + std::vector q; + int front, rear, sz; public: CircularQueue(size_t size); // создать очередь с определенным размером буффера bool Push(int value); // добавить значение в конец очереди (false, если очередь заполнена)