Skip to content
86 changes: 58 additions & 28 deletions task1/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
#include "matrix.hpp"

#include <stdexcept>
#include <vector>

using namespace std;

Matrix::Matrix(int numRows, int numCols)
{
// your implementation here
Matrix::Matrix(int rows, int cols) {
if (rows < 0 || cols < 0) {
throw out_of_range("Rows and columns must be non-negative");
}
if (rows == 0 || cols == 0) {
num_rows = 0;
num_cols = 0;
data.clear();
} else {
num_rows = rows;
num_cols = cols;
data.assign(rows, vector<int>(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("Rows and columns must be non-negative");
}
if (rows == 0 || cols == 0) {
num_rows = 0;
num_cols = 0;
data.clear();
} else {
num_rows = rows;
num_cols = cols;
data.assign(rows, vector<int>(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("Index 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("Index 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("Matrices must have the same dimensions for addition");
}
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;
}
43 changes: 34 additions & 9 deletions task1/matrix.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
#pragma once
#include <iostream>
#include <vector>
#include <stdexcept>

class Matrix
{
class Matrix {
public:
Matrix() = default;
Matrix(int numRows, int numCols);
Matrix() : num_rows(0), num_cols(0) {}
explicit Matrix(int rows, int cols);

void Reset(int numRows, int numCols);
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 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<std::vector<int>> data;
};
77 changes: 76 additions & 1 deletion task2/figures.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
#include "figures.hpp"
#include <cmath>
#include <stdexcept>

static constexpr double PI = 3.14;
Rect::Rect(double width, double height) : width(width), height(height) {
if (width < 0 || height < 0) {
throw LessThanZeroParam();
}
}

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 a, double b, double c) : a(a), b(b), c(c) {
if (a < 0 || b < 0 || c < 0) {
throw LessThanZeroParam();
}
if (a + b <= c || a + c <= b || b + c <= a) {
throw WrongTriangle();
}
}

FigureType Triangle::Type() const {
return FigureType::TRIANGLE;
}

double Triangle::Perimeter() const {
return a + b + c;
}

double Triangle::Area() const {
double p = Perimeter() / 2;
return std::sqrt(p * (p - a) * (p - b) * (p - c));
}

Circle::Circle(double radius) : radius(radius) {
if (radius < 0) {
throw LessThanZeroParam();
}
}

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<Figure> 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<Rect>(a, b);
case FigureType::CIRCLE:
return std::make_unique<Circle>(a);
case FigureType::TRIANGLE:
return std::make_unique<Triangle>(a, b, c);
default:
return nullptr;
}
}
62 changes: 45 additions & 17 deletions task2/figures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,67 @@
#include <memory>
#include <stdexcept>

enum class FigureType
{
TRIANGLE,
CIRCLE,
RECTANGLE,
};
static constexpr double PI = 3.14;

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
{
class Rect : public Figure {
public:
Rect(double width, double height);
FigureType Type() const override;
double Perimeter() const override;
double Area() const override;
private:
double width;
double height;
};

class Triangle : public Figure
{
class Triangle : public Figure {
public:
Triangle(double a, double b, double c);
FigureType Type() const override;
double Perimeter() const override;
double Area() const override;

private:
double a;
double b;
double c;
};

class Circle : public Figure
{
class Circle : public Figure {
public:
Circle(double radius);
FigureType Type() const override;
double Perimeter() const override;
double Area() const override;

private:
double radius;
};


std::unique_ptr<Figure> make_figure(FigureType type, double a, double b = 0, double c = 0);

class WrongTriangle : public std::invalid_argument
{

class WrongTriangle : public std::invalid_argument {
public:
WrongTriangle() : std::invalid_argument("Invalid triangle parameters") {}
};

class LessThanZeroParam : public std::invalid_argument
{

class LessThanZeroParam : public std::invalid_argument {
public:
LessThanZeroParam() : std::invalid_argument("Parameters must be greater than zero") {}
};

52 changes: 31 additions & 21 deletions task3/circular_queue.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
#include "circular_queue.hpp"
#include <stdexcept>

CircularQueue::CircularQueue(size_t size)
{
// your implementation here
}
: buffer(size), head(0), tail(0), capacity(size), size(0) {}

bool CircularQueue::Push(int value)
{
// your implementation here
bool CircularQueue::Push(int value) {
if (Full()) {
return false;
}
buffer[tail] = value;
tail = (tail + 1) % capacity;
size++;
return true;
}

bool CircularQueue::Pop()
{
// your implementation here
bool CircularQueue::Pop() {
if (Empty()) {
return false;
}
head = (head + 1) % capacity;
size--;
return true;
}

int CircularQueue::Front() const
{
// your implementation here
int CircularQueue::Front() const {
if (Empty()) {
return -1;
}
return buffer[head];
}

int CircularQueue::Back() const
{
// your implementation here
int CircularQueue::Back() const {
if (Empty()) {
return -1;
}
return buffer[(tail + capacity - 1) % 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;
}
Loading