diff --git a/README.md b/README.md index 944c956..8404aab 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,5 @@ - Форкаем реп - После выполнения задания нужно сделать pull request на преподавателя со всеми пройденными тестами - Тестирование срабатывает при любом пуше в реп, а так же при создании pull request + +dfgh diff --git a/task1/matrix.cpp b/task1/matrix.cpp index fb3598f..3964e86 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -1,48 +1,77 @@ #include "matrix.hpp" - #include -Matrix::Matrix(int numRows, int numCols) -{ - // your implementation here +using namespace std; + +Matrix::Matrix(int rows, int cols) { + if (rows < 0 || cols < 0) { + throw out_of_range(""); + } + if (rows == 0 || cols == 0) { + num_rows = 0; + num_cols = 0; + data.clear(); + } else { + num_rows = rows; + num_cols = cols; + data.assign(rows, vector(cols, 0)); + } } -void Matrix::Reset(int numRows, int numCols) -{ - // your implementation here +void Matrix::Reset(int rows, int cols) { + if (rows < 0 || cols < 0) { + throw out_of_range(""); + } + if (rows == 0 || cols == 0) { + num_rows = 0; + num_cols = 0; + data.clear(); + } else { + num_rows = rows; + num_cols = cols; + data.assign(rows, vector(cols, 0)); + } } -int& Matrix::At(int row, int col) -{ - // your implementation here +int Matrix::At(int row, int col) const { + if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { + throw out_of_range(""); + } + return data[row][col]; } -const int& Matrix::At(int row, int col) const -{ - // your implementation here +int& Matrix::At(int row, int col) { + if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { + throw out_of_range(""); + } + return data[row][col]; } -int Matrix::GetRows() const -{ - // your implementation here +int Matrix::GetRows() const { + return num_rows; } -int Matrix::GetCols() const -{ - // your implementation here +int Matrix::GetCols() const { + return num_cols; } -bool Matrix::operator==(const Matrix& m2) -{ - // your implementation here +bool operator==(const Matrix& lhs, const Matrix& rhs) { + return lhs.num_rows == rhs.num_rows && lhs.num_cols == rhs.num_cols && lhs.data == rhs.data; } -bool Matrix::operator!=(const Matrix& m2) -{ - // your implementation here +bool operator!=(const Matrix& lhs, const Matrix& rhs) { + return !(lhs == rhs); } -Matrix Matrix::operator+(const Matrix& m2) -{ - // your implementation here +Matrix operator+(const Matrix& lhs, const Matrix& rhs) { + if (lhs.GetRows() != rhs.GetRows() || lhs.GetCols() != rhs.GetCols()) { + throw invalid_argument(""); + } + Matrix result(lhs.GetRows(), lhs.GetCols()); + for (int i = 0; i < lhs.GetRows(); ++i) { + for (int j = 0; j < lhs.GetCols(); ++j) { + result.At(i, j) = lhs.At(i, j) + rhs.At(i, j); + } + } + return result; } diff --git a/task1/matrix.hpp b/task1/matrix.hpp index d314915..40b8a80 100644 --- a/task1/matrix.hpp +++ b/task1/matrix.hpp @@ -1,18 +1,45 @@ #pragma once +#include +#include +#include -class Matrix -{ +/* sdfgsdfg */ + +class Matrix { public: - Matrix() = default; - Matrix(int numRows, int numCols); + Matrix() : num_rows(0), num_cols(0) {} + explicit Matrix(int rows, int cols); + + Matrix(const Matrix& other) = default; + Matrix& operator=(const Matrix& other) = default; + + Matrix(Matrix&& other) noexcept : num_rows(other.num_rows), num_cols(other.num_cols), data(std::move(other.data)) { + other.num_rows = 0; + other.num_cols = 0; + } + Matrix& operator=(Matrix&& other) noexcept { + if (this != &other) { + num_rows = other.num_rows; + num_cols = other.num_cols; + data = std::move(other.data); + other.num_rows = 0; + other.num_cols = 0; + } + return *this; + } - void Reset(int numRows, int numCols); + void Reset(int rows, int cols); + int At(int row, int col) const; int& At(int row, int col); - const int& At(int row, int col) const; int GetRows() const; int GetCols() const; - bool operator==(const Matrix& m2); - bool operator!=(const Matrix& m2); - Matrix operator+(const Matrix& m2); + friend bool operator==(const Matrix& lhs, const Matrix& rhs); + friend bool operator!=(const Matrix& lhs, const Matrix& rhs); + friend Matrix operator+(const Matrix& lhs, const Matrix& rhs); + +private: + int num_rows; + int num_cols; + std::vector> data; }; diff --git a/task2/figures.cpp b/task2/figures.cpp index 3b33880..c450839 100644 --- a/task2/figures.cpp +++ b/task2/figures.cpp @@ -1,3 +1,70 @@ #include "figures.hpp" +#include -static constexpr double PI = 3.14; +Rect::Rect(double a, double b) : width(a), height(b) {} + +FigureType Rect::Type() const { + return FigureType::RECTANGLE; +} + +double Rect::Perimeter() const { + return 2 * (width + height); +} + +double Rect::Area() const { + return width * height; +} + +Triangle::Triangle(double side_a, double side_b, double side_c) + : a(side_a), b(side_b), c(side_c) {} + +FigureType Triangle::Type() const { + return FigureType::TRIANGLE; +} + +double Triangle::Perimeter() const { + return a + b + c; +} + +double Triangle::Area() const { + double s = Perimeter() / 2; + return std::sqrt(s * (s - a) * (s - b) * (s - c)); +} + +Circle::Circle(double r) : radius(r) {} + +FigureType Circle::Type() const { + return FigureType::CIRCLE; +} + +double Circle::Perimeter() const { + return 2 * PI * radius; +} + +double Circle::Area() const { + return PI * radius * radius; +} + + +std::unique_ptr
make_figure(FigureType type, double a, double b, double c) { + if (a < 0 || b < 0 || c < 0) { + throw LessThanZeroParam(""); + } + + switch (type) { + case FigureType::RECTANGLE: + return std::make_unique(a, b); + + case FigureType::TRIANGLE: + if (a + b <= c || b + c <= a || a + c <= b) { + throw WrongTriangle(""); + } + return std::make_unique(a, b, c); + + case FigureType::CIRCLE: + return std::make_unique(a); + + default: + return nullptr; + } +} diff --git a/task2/figures.hpp b/task2/figures.hpp index 65a6110..d27e188 100644 --- a/task2/figures.hpp +++ b/task2/figures.hpp @@ -3,6 +3,9 @@ #include #include +// Make PI accessible to tests and other code +constexpr double PI = 3.14; + enum class FigureType { TRIANGLE, @@ -12,6 +15,7 @@ enum class FigureType class Figure { public: + virtual ~Figure() = default; // Add virtual destructor for proper cleanup virtual FigureType Type() const = 0; virtual double Perimeter() const = 0; virtual double Area() const = 0; @@ -19,23 +23,51 @@ class Figure { class Rect : public Figure { +private: + double width; + double height; + +public: + Rect(double a, double b); + FigureType Type() const override; + double Perimeter() const override; + double Area() const override; }; class Triangle : public Figure { +private: + double a, b, c; + +public: + Triangle(double side_a, double side_b, double side_c); + FigureType Type() const override; + double Perimeter() const override; + double Area() const override; }; class Circle : public Figure { +private: + double radius; + +public: + Circle(double r); + FigureType Type() const override; + double Perimeter() const override; + double Area() const override; }; std::unique_ptr
make_figure(FigureType type, double a, double b = 0, double c = 0); class WrongTriangle : public std::invalid_argument { +public: + using std::invalid_argument::invalid_argument; }; class LessThanZeroParam : public std::invalid_argument { +public: + using std::invalid_argument::invalid_argument; }; - diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index fa74734..c23c9b1 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,36 +1,47 @@ #include "circular_queue.hpp" +#include -CircularQueue::CircularQueue(size_t size) -{ - // your implementation here +CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) { + buffer.resize(size); // Pre-allocate exact size } -bool CircularQueue::Push(int value) -{ - // your implementation here +bool CircularQueue::Push(int value) { + if (Full()) { + return false; + } + buffer[rear] = value; + rear = (rear + 1) % capacity; + size++; + return true; } -bool CircularQueue::Pop() -{ - // your implementation here +bool CircularQueue::Pop() { + if (Empty()) { + return false; + } + front = (front + 1) % capacity; + size--; + return true; } -int CircularQueue::Front() const -{ - // your implementation here +int CircularQueue::Front() const { + if (Empty()) { + return -1; + } + return buffer[front]; } -int CircularQueue::Back() const -{ - // your implementation here +int CircularQueue::Back() const { + if (Empty()) { + return -1; + } + return buffer[(rear - 1 + capacity) % capacity]; } -bool CircularQueue::Empty() const -{ - // your implementation here +bool CircularQueue::Empty() const { + return size == 0; } -bool CircularQueue::Full() const -{ - // your implementation here +bool CircularQueue::Full() const { + return size == capacity; } diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 2871091..32606b9 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -1,14 +1,23 @@ #pragma once -#include +#include class CircularQueue { +private: + std::vector buffer; // Dynamic array to store elements + size_t capacity; // Maximum size of the queue + size_t size; // Current count of elements + size_t front; // Index of the front element + size_t rear; // Index of the rear element + public: - CircularQueue(size_t size); // создать очередь с определенным размером буффера - bool Push(int value); // добавить значение в конец очереди (false, если очередь заполнена) - bool Pop(); // удалить значение из начала очереди (false, если очередь пустая) - int Front() const; // получить значение из начала очереди (-1, если очередь пустая) - int Back() const; // получить значение из конца очереди (-1, если очередь пустая) - bool Empty() const; // проверить пустая ли очередь - bool Full() const; // проверить заполнена ли очередь + CircularQueue(size_t size); // Constructor + + bool Push(int value); // Add value to the end + bool Pop(); // Remove value from the front + int Front() const; // Get front value + int Back() const; // Get back value + + bool Empty() const; // Check if empty + bool Full() const; // Check if full };