Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 1,2,3. Шумов #13

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 57 additions & 28 deletions task1/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,77 @@
#include "matrix.hpp"

#include <stdexcept>

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<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("");
}
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("");
}
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;
}
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);

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<std::vector<int>> data;
};
69 changes: 68 additions & 1 deletion task2/figures.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,70 @@
#include "figures.hpp"
#include <cmath>

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<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::TRIANGLE:
if (a + b <= c || b + c <= a || a + c <= b) {
throw WrongTriangle("");
}
return std::make_unique<Triangle>(a, b, c);

case FigureType::CIRCLE:
return std::make_unique<Circle>(a);

default:
return nullptr;
}
}
34 changes: 33 additions & 1 deletion task2/figures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <memory>
#include <stdexcept>

// Make PI accessible to tests and other code
constexpr double PI = 3.14;

enum class FigureType
{
TRIANGLE,
Expand All @@ -12,30 +15,59 @@ 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;
};

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<Figure> 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;
};

53 changes: 32 additions & 21 deletions task3/circular_queue.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
#include "circular_queue.hpp"
#include <vector>

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;
}
Loading