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

никифоров д.а. ри 232701 #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
99 changes: 90 additions & 9 deletions task1/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,129 @@
#include "matrix.hpp"

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <stdexcept>

Matrix::Matrix(int numRows, int numCols)
{
// your implementation here
vector<int> 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<int> 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";
}
}
2 changes: 2 additions & 0 deletions task1/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Matrix
{
private:
vector<vector<float>> mat;
public:
Matrix() = default;
Matrix(int numRows, int numCols);
Expand Down
96 changes: 96 additions & 0 deletions task2/figures.cpp
Original file line number Diff line number Diff line change
@@ -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<Figure> make_figure(FigureType type, double a, double b = 0, double c = 0)
{

switch (type)
{
case FigureType::RECTANGLE:
std::make_unique<Rect>(a, b);
break;
case FigureType::TRIANGLE:
std::make_unique<Triangle>(a, b, c);
break;
case FigureType::CIRCLE:
std::make_unique<Circle>(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;
}
28 changes: 28 additions & 0 deletions task2/figures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Figure> 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 }{};
};

54 changes: 47 additions & 7 deletions task3/circular_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions task3/circular_queue.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include <cstddef>
#include <vector>

class CircularQueue {
private:
std::vector<int> q;
int front, rear, sz;
public:
CircularQueue(size_t size); // создать очередь с определенным размером буффера
bool Push(int value); // добавить значение в конец очереди (false, если очередь заполнена)
Expand Down