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 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
83ee753
Some work
krozzzis Nov 19, 2024
2d67764
Update matrix.hpp
krozzzis Nov 19, 2024
d2c2801
Some work
krozzzis Nov 19, 2024
2118de7
Update circular_queue.hpp
krozzzis Nov 19, 2024
81211c3
Update circular_queue.cpp
krozzzis Nov 19, 2024
d302f1c
Update circular_queue.cpp
krozzzis Nov 20, 2024
d12ebae
Update circular_queue.hpp
krozzzis Nov 20, 2024
481a4eb
Update circular_queue.hpp
krozzzis Nov 20, 2024
3c253f1
Update circular_queue.cpp
krozzzis Nov 20, 2024
7fa1635
Update circular_queue.cpp
krozzzis Nov 20, 2024
5ba759c
Update circular_queue.cpp
krozzzis Nov 20, 2024
58a9af0
Update circular_queue.hpp
krozzzis Nov 20, 2024
0e10861
Update circular_queue.cpp
krozzzis Nov 20, 2024
fce891d
Update circular_queue.hpp
krozzzis Nov 21, 2024
5024b9a
Update circular_queue.cpp
krozzzis Nov 21, 2024
14edbc6
Update circular_queue.hpp
krozzzis Nov 21, 2024
644faa7
Update circular_queue.hpp
krozzzis Nov 21, 2024
e760e90
Update circular_queue.cpp
krozzzis Nov 21, 2024
4ed07fd
Update circular_queue.cpp
krozzzis Nov 21, 2024
c31ef71
Update circular_queue.hpp
krozzzis Nov 21, 2024
b6e6b78
Update circular_queue.cpp
krozzzis Nov 21, 2024
7573515
Update circular_queue.cpp
krozzzis Nov 21, 2024
42065b0
Update circular_queue.hpp
krozzzis Nov 21, 2024
92a8a4d
Update circular_queue.hpp
krozzzis Nov 21, 2024
1fc3f72
Update circular_queue.cpp
krozzzis Nov 21, 2024
fa709e7
Update circular_queue.hpp
krozzzis Nov 21, 2024
6edb722
Update circular_queue.hpp
krozzzis Nov 21, 2024
3eef040
Update README.md
krozzzis Jan 9, 2025
46548c5
Update matrix.hpp
krozzzis Jan 9, 2025
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 <stdexcept>

CircularQueue::CircularQueue(size_t size)
{
// your implementation here
CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) {
buffer = new int[size];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нужно позаботиться о деаллокации памяти, тут утечка

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

исправляй, отправляй снова

}

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;
}
23 changes: 16 additions & 7 deletions task3/circular_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
#include <cstddef>

class CircularQueue {
private:
int* 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
};