From ef0f4155cf3a10748d42b17d9961c52d1b03c8de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 16 Sep 2025 21:18:29 +0300 Subject: [PATCH 001/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20Tvector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 694 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 694 insertions(+) create mode 100644 lib_vector/vector.h diff --git a/lib_vector/vector.h b/lib_vector/vector.h new file mode 100644 index 00000000..ea1c7777 --- /dev/null +++ b/lib_vector/vector.h @@ -0,0 +1,694 @@ +// Copyright 2025 Zabytina Julia. All rights reserved. +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +template +class Tvector { +public: + enum State { + empty, busy, deleted + }; +private: + static constexpr size_t RESERVE_MEMORY = 15; + static constexpr size_t MAX_PERCENT_DELETED = 15; + + size_t _size; + size_t _capacity; + T* _data; + State* _states; + size_t _deleted; + + size_t get_real_position(size_t busy_index) const noexcept; + void resize(size_t new_size); + void resize(size_t new_size, const T& value); + void shrink_to_fit(); + void reserve(size_t new_capacity); + void compact_storage(); + inline bool is_full() const noexcept { + return _size == _capacity; + } +public: + Tvector() noexcept; + Tvector(size_t size); + Tvector(T* data, size_t size); + Tvector(const Tvector& other_vector); + ~Tvector() noexcept; + + inline bool is_empty() const noexcept { + return _size == 0; + } + inline const T* get_data() const noexcept { + return _data; + } + inline T* get_data() noexcept { + return _data; + } + inline const State* get_states() const noexcept { + return _states; + } + inline State* get_states() noexcept { + return _states; + } + inline T& front(); + inline T& back(); + inline const T& front() const; + inline const T& back() const; + size_t get_size() const noexcept { + return _size; + } + size_t get_deleted() const noexcept { + return _deleted; + } + size_t get_capacity() const noexcept { + return _capacity; + } + inline T* begin() const noexcept { + return _data; + } + inline T* end() const noexcept { + return _data + _size; + } + bool operator==(const Tvector& vector) const; + bool operator!=(const Tvector& vector) const; + Tvector& operator=(const Tvector& vector); + inline const T& operator[](size_t index) const; + inline T& operator[](size_t index); + T& at(size_t index); + const T& at(size_t index) const; + void assign(const Tvector& vector); + void clear(); + void emplace(size_t index, const T& value); + void push_front(const T& value); + void insert(const T& value, size_t position); + void push_back(const T& value); + void pop_back(); + void erase(size_t position); + void pop_front(); + + template + friend std::ostream& operator<<(std::ostream&, const Tvector& vector); + template friend void shell_sort(Tvector& object) noexcept; + template friend void shuffle(Tvector& object) noexcept; + template friend size_t find_first_element(const Tvector& object, const U& value); + template friend size_t find_last_element(const Tvector& object, const U& value); + template friend size_t find_count_of_all_suitable_elements(const Tvector& object, const U& value); +}; +template +size_t Tvector::get_real_position(size_t busy_index) const noexcept { + size_t busy_count = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == busy) { + if (busy_count == busy_index) { + return i; + } + ++busy_count; + } + } + return _size; +} +template +void Tvector::resize(size_t new_size) { + if (new_size == _size) { + return; + } + else if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) { + _deleted--; + } + _data[i].~T(); + _states[i] = State::empty; + } + _size = new_size; + } + else { + size_t new_capacity = new_size + RESERVE_MEMORY; + T* new_data = static_cast(::operator new(new_capacity * sizeof(T))); + State* new_states = new State[new_capacity]; + for (size_t i = new_size; i < new_capacity; ++i) { + new_states[i] = State::empty; + } + try { + for (size_t i = 0; i < _size; ++i) { + new (new_data + i) T(std::move(_data[i])); + new_states[i] = _states[i]; + } + for (size_t i = _size; i < new_size; ++i) { + new (new_data + i) T(); + new_states[i] = State::busy; + } + for (size_t i = 0; i < _size; ++i) { + _data[i].~T(); + } + ::operator delete(_data); + delete[] _states; + + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = new_size; + } + catch (...) { + for (size_t i = 0; i < new_size; ++i) { + new_data[i].~T(); + } + ::operator delete(new_data); + delete[] new_states; + throw; + } + } +} +template +void Tvector::resize(size_t new_size, const T& value) { + if (new_size == _size) { + return; + } + else if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) { + _deleted--; + } + _data[i].~T(); + _states[i] = State::empty; + } + _size = new_size; + } + else { + size_t new_capacity = new_size + RESERVE_MEMORY; + T* new_data = static_cast(::operator new(new_capacity * sizeof(T))); + State* new_states = new State[new_capacity]; + for (size_t i = new_size; i < new_capacity; ++i) { + new_states[i] = State::empty; + } + try { + for (size_t i = 0; i < _size; ++i) { + new (new_data + i) T(std::move(_data[i])); + new_states[i] = _states[i]; + } + for (size_t i = _size; i < new_size; ++i) { + new (new_data + i) T(value); + new_states[i] = State::busy; + } + for (size_t i = 0; i < _size; ++i) { + _data[i].~T(); + } + ::operator delete(_data); + delete[] _states; + + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = new_size; + } + catch (...) { + for (size_t i = 0; i < new_size; ++i) { + new_data[i].~T(); + } + ::operator delete(new_data); + delete[] new_states; + throw; + } + } +} +template +void Tvector::shrink_to_fit() { + if (_size >= _capacity) { + return; + } + else if (_size == 0) { + delete[] _data; + delete[] _states; + _data = nullptr; + _states = nullptr; + _capacity = 0; + _deleted = 0; + } + else { + T* new_data = new T[_size]; + State* new_states = new State[_size]; + for (size_t i = 0; i < _size; ++i) { + new_data[i] = std::move(_data[i]); + new_states[i] = _states[i]; + } + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = _size; + } +} +template +void Tvector::reserve(size_t new_capacity) { + if (new_capacity <= _capacity) { + return; + } + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; + std::fill_n(new_states, new_capacity, State::empty); + for (size_t i = 0; i < _size; ++i) { + new_data[i] = std::move(_data[i]); + new_states[i] = _states[i]; + } + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = new_capacity; +} +template +void Tvector::compact_storage() { + size_t busy_count = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + busy_count++; + } + } + std::cout << "compact_storage(): busy_count = " << busy_count << "\n"; + size_t new_capacity = busy_count + RESERVE_MEMORY; + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; + size_t new_index = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + new_data[new_index] = std::move(_data[i]); + new_states[new_index] = State::busy; + new_index++; + } + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] != State::empty) { + _data[i].~T(); + } + } + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = busy_count; + _deleted = 0; +} +template +Tvector::Tvector() noexcept { + _size = 0; + _capacity = 0; + _data = nullptr; + _states = nullptr; + _deleted = 0; +} +template +Tvector::Tvector(size_t size) { + _size = size; + _capacity = size + RESERVE_MEMORY; + _data = new T[_capacity]; + + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; + throw; + } + _deleted = 0; + + for (size_t i = 0; i < _capacity; ++i) { + _states[i] = i < _size ? State::busy : State::empty; + } +} +template +Tvector::Tvector(T* data, size_t size) { + if (size > 0 && data == nullptr) { + throw std::invalid_argument("Null data pointer with non-zero size"); + } + _size = size; + _capacity = _size + RESERVE_MEMORY; + _data = new T[_capacity]; + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; + throw; + } + for (size_t i = 0; i < _capacity; ++i) { + if (i < _size) { + _data[i] = data[i]; + _states[i] = State::busy; + } + else { + _states[i] = State::empty; + } + } + _deleted = 0; +} +template +Tvector::Tvector(const Tvector& other_vector) { + _size = other_vector._size; + _capacity = other_vector._capacity; + _data = nullptr; + _states = nullptr; + try { + _data = new T[_capacity]; + } + catch (const std::bad_alloc&) { + throw; + } + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; + throw; + } + _deleted = other_vector._deleted; + + for (size_t i = 0; i < other_vector._size; ++i) { + _data[i] = other_vector._data[i]; + _states[i] = other_vector._states[i]; + } + for (size_t i = other_vector._size; i < other_vector._capacity; ++i) { + _states[i] = State::empty; + } +} +template +Tvector::~Tvector() noexcept { + delete[] _data; + delete[] _states; +} +template +inline T& Tvector::front() { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[0]; +} +template +inline T& Tvector::back() { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[_size - 1]; +} +template +inline const T& Tvector::front() const { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[0]; +} +template +inline const T& Tvector::back() const { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[_size - 1]; +} +template +bool Tvector::operator==(const Tvector& vector) const { + if (this->_size != vector._size) + return false; + + for (size_t i = 0; i < _size; ++i) { + if ((*this)[i] != vector[i]) + return false; + } + return true; +} +template +bool Tvector::operator!=(const Tvector& vector) const { + return !(*this == vector); +} +template +Tvector& Tvector::operator=(const Tvector& vector) { + if (this == &vector) { + return *this; + } + T* new_data = new T[vector._capacity]; + State* new_states = new State[vector._capacity]; + for (size_t i = 0; i < vector._size; ++i) { + new_data[i] = vector._data[i]; + new_states[i] = vector._states[i]; + } + for (size_t i = vector._size; i < vector._capacity; ++i) { + new_states[i] = empty; + } + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = vector._capacity; + _size = vector._size; + _deleted = vector._deleted; + + return *this; +} +template +inline const T& Tvector::operator[](size_t index) const { + size_t real_index = get_real_position(index); + return _data[real_index]; +} +template +inline T& Tvector::operator[](size_t index) { + size_t real_index = get_real_position(index); + return _data[real_index]; +} +template +T& Tvector::at(size_t index) { + if (index >= _size) { + throw std::out_of_range("Index out of range"); + } + if (_states[index] != busy) { + throw std::logic_error("Element at this index is not available (deleted or empty)"); + } + size_t real_index = get_real_position(index); + return _data[real_index]; +} +template +const T& Tvector::at(size_t index) const { + size_t real_index = get_real_position(index); + return const_cast(this)->at(real_index); +} +template +void Tvector::assign(const Tvector& vector) { + if (this == &vector) { + return; + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _data[i].~T(); + } + } + delete[] _data; + delete[] _states; + _capacity = vector._capacity; + _size = vector._size; + _deleted = vector._deleted; + + _data = new T[_capacity]; + _states = new State[_capacity]; + for (size_t i = 0; i < _size; ++i) { + if (vector._states[i] == State::busy) { + new (&_data[i]) T(vector._data[i]); + } + _states[i] = vector._states[i]; + } + std::fill(_states + _size, _states + _capacity, State::empty); +} +template +void Tvector::clear() { + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _states[i] = State::empty; + } + } + _size = 0; + _deleted = 0; +} +template +void Tvector::emplace(size_t index, const T& value) { + if (index >= _size) { + throw std::out_of_range("Index out of range"); + } + size_t real_index = get_real_position(index); + if (_states[real_index] == State::deleted) { + --_deleted; + } + _data[real_index] = value; + _states[real_index] = State::busy; +} +template +void Tvector::push_front(const T& value) { + if (_size >= _capacity) { + reserve(_capacity + RESERVE_MEMORY); + } + for (size_t i = _size; i > 0; --i) { + _data[i] = std::move(_data[i - 1]); + _states[i] = _states[i - 1]; + } + _data[0] = value; + _states[0] = State::busy; + _size++; +} +template +void Tvector::insert(const T& value, size_t position) { + if (_size == 0) { + resize(RESERVE_MEMORY); + } + if (position >= _size) { + throw std::out_of_range("Insert position out of range"); + } + size_t real_pos = get_real_position(position); + for (size_t i = _size; i > real_pos; --i) { + _data[i] = std::move(_data[i - 1]); + _states[i] = _states[i - 1]; + } + _data[real_pos] = value; + _states[real_pos] = State::busy; + _size++; +} +template +void Tvector::push_back(const T& value) { + if (_size >= _capacity) { + reserve(_capacity + RESERVE_MEMORY); + } + _data[_size] = value; + _states[_size] = State::busy; + _size++; +} +template +void Tvector::pop_back() { + if (_size == 0) { + throw std::out_of_range("Vector has size = 0"); + } + _states[_size - 1] = State::empty; + _size--; +} +template +void Tvector::erase(size_t position) { + if (position >= _size) { + throw std::out_of_range("Invalid position"); + } + size_t real_pos = get_real_position(position); + _states[real_pos] = State::deleted; + _deleted++; + if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { + compact_storage(); + } +} +template +void Tvector::pop_front() { + if (_size == 0) { + throw std::out_of_range("Vector has size = 0"); + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _states[i] = State::deleted; + _deleted++; + if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { + compact_storage(); + } + return; + } + } +} + +template +std::ostream& operator<<(std::ostream& out, const Tvector& vector) { + out << "["; + bool first = true; + for (size_t i = 0; i < vector._size; ++i) { + if (vector._states[i] == Tvector::State::busy) { + if (!first) out << ", "; + out << vector._data[i]; + first = false; + } + } + out << "]"; + return out; +} +template +void shell_sort(Tvector& object) noexcept { + if (object._size < 2 || object._data == nullptr + || object._states == nullptr) { + return; + } + size_t h = 1; + while (h < object._size / 3) { + h = 3 * h + 1; + } + while (h >= 1) { + for (size_t i = h; i < object._size; ++i) { + for (size_t j = i; j >= h; j -= h) { + size_t previous = j - h; + if (object._data[j] < object._data[previous]) { + std::swap(object._data[j], object._data[previous]); + std::swap(object._states[j], object._states[previous]); + } + else { + break; + } + } + } + h /= 3; + } +} +template +void shuffle(Tvector& object) noexcept { + if (object._size < 2 || object._data == nullptr || object._states == nullptr) { + return; + } + std::random_device rd; + std::mt19937 gen(rd()); + for (size_t i = object._size - 1; i > 0; --i) { + std::uniform_int_distribution dist(0, i); + size_t j = dist(gen); + std::swap(object._data[i], object._data[j]); + std::swap(object._states[i], object._states[j]); + } +} +template +size_t find_first_element(const Tvector& object, const U& value) { + size_t result = 0; + for (size_t i = 0; i < object._size; i++) { + if (object._states[i] == object.State::deleted) { + continue; + } + else if (object._data[i] == value && object._states[i] == object.State::busy) { + return result + 1; + } + result++; + } + return 0; +} +template +size_t find_last_element(const Tvector& object, const U& value) { + size_t last_pos = 0; + size_t current_pos = 0; + for (size_t i = 0; i < object._size; i++) { + if (object._states[i] == object.State::deleted) { + continue; + } + current_pos++; + if (object._data[i] == value && object._states[i] == object.State::busy) { + last_pos = current_pos; + } + } + return last_pos; +} +template +size_t find_count_of_all_suitable_elements(const Tvector& object, const U& value) { + size_t count = 0; + for (size_t i = 0; i < object._size; ++i) { + if (object._data[i] == value && object._states[i] == object.State::busy) { + count++; + } + } + return count; +} From 93de87bad2fcb0382ea63eff5f5cacd8d526651d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 16 Sep 2025 21:19:29 +0300 Subject: [PATCH 002/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20vector.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_vector/vector.cpp diff --git a/lib_vector/vector.cpp b/lib_vector/vector.cpp new file mode 100644 index 00000000..863a6a8a --- /dev/null +++ b/lib_vector/vector.cpp @@ -0,0 +1 @@ +#include "vector.h" \ No newline at end of file From 123493f83c903707bb1ba71f641d340456ac454d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 16 Sep 2025 21:26:17 +0300 Subject: [PATCH 003/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20Tvector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 544 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 544 insertions(+) create mode 100644 tests/test_vector.cpp diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp new file mode 100644 index 00000000..a2efd98d --- /dev/null +++ b/tests/test_vector.cpp @@ -0,0 +1,544 @@ +// Copyright 2025 Julia Zabytina +#include +#include "../lib_easy_example/easy_example.h" +#include "../lib_vector/vector.h" +TEST(TestVectorLib, default_constructor) { + // Arrange + Tvector vector; + // Act + bool actual_result = vector.get_size() == 0 + && vector.get_deleted() == 0 + && vector.get_capacity() == 0 + && vector.get_data() == nullptr + && vector.get_states() == nullptr; + + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, constructor_with_size) { + // Arrange + size_t size = 10; + Tvector vector(size); + // Act + bool actual_result = vector.get_size() == size + && vector.get_capacity() == 25 + && vector.get_data() != nullptr + && vector.get_states() != nullptr + && vector.get_deleted() == 0;; + + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, constructor_with_array_and_size) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + bool actual_result = vector.get_size() == size + && vector.get_capacity() == 18 + && vector.get_data() != nullptr + && vector.get_states() != nullptr + && vector.get_data()[0] == data[0] + && vector.get_data()[1] == data[1] + && vector.get_data()[2] == data[2] + && vector.get_deleted() == 0; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, copy_constructor) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector old_vector(data, size); + Tvector new_vector(old_vector); + // Act + bool actual_result = old_vector == new_vector; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, is_empty) { + // Arrange + Tvector vector; + // Act + bool actual_result = vector.is_empty(); + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, get_data) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + int* data_ptr = data; + bool different_pointers = data_ptr != vector.get_data(); + bool same_data = + data[0] == vector.get_data()[0] + && data[1] == vector.get_data()[1] + && data[2] == vector.get_data()[2]; + // Act + bool actual_result = different_pointers && same_data; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, front) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + int actual_result = vector.front(); + // Assert + int expected_result = vector.get_data()[0]; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, back) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + int actual_result = vector.back(); + // Assert + int expected_result = vector.get_data()[vector.get_size() - 1]; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, get_size) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + size_t actual_result = vector.get_size(); + // Assert + size_t expected_result = size; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, get_deleted) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + size_t deleted = 0; + Tvector vector(data, size); + // Act + size_t actual_result = vector.get_deleted(); + // Assert + size_t expected_result = deleted; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, get_capacity) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + size_t capacity = 18; + Tvector vector(data, size); + // Act + size_t actual_result = vector.get_capacity(); + // Assert + size_t expected_result = capacity; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, get_begin) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + int* actual_result = vector.begin(); + // Assert + int* expected_result = &(vector.get_data()[0]); + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, get_end) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + int* actual_result = vector.end(); + // Assert + int* expected_result = &(vector.get_data()[0]) + size; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, сomparison_operator_true) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector1(data, size); + Tvector vector2(data, size); + // Act + bool actual_result = vector1 == vector2; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, сomparison_operator_false) { + // Arrange + int data1[3] = { 1,2,3 }; + size_t size1 = 3; + int data2[4] = { 1,2,3,4 }; + size_t size2 = 4; + Tvector vector1(data1, size1); + Tvector vector2(data2, size2); + // Act + bool actual_result = vector1 == vector2; + // Assert + bool expected_result = false; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, not_сomparison_operator_true) { + // Arrange + int data1[3] = { 1,2,3 }; + size_t size1 = 3; + int data2[4] = { 1,2,3,4 }; + size_t size2 = 4; + Tvector vector1(data1, size1); + Tvector vector2(data2, size2); + // Act + bool actual_result = vector1 != vector2; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, not_сomparison_operator_false) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector1(data, size); + Tvector vector2(data, size); + // Act + bool actual_result = vector1 != vector2; + // Assert + bool expected_result = false; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, operator_assign) { + // Arrange + int data1[3] = { 1,2,3 }; + size_t size1 = 3; + int data2[4] = { 1,2,3,4 }; + size_t size2 = 4; + Tvector vector1(data1, size1); + Tvector vector2(data2, size2); + vector1 = vector2; + // Act + bool actual_result = vector1 == vector2; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, operator_staples) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + int actual_result = vector[1]; + // Assert + int expected_result = data[1]; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, at) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + // Act + int actual_result = vector.at(1); + // Assert + int expected_result = data[1]; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, assign) { + // Arrange + int data[3] = { 1,1,1 }; + size_t size = 3; + Tvector vector1; + Tvector vector2(data, size); + vector1.assign(vector2); + // Act + Tvector actual_result = vector1; + // Assert + Tvector expected_result = vector2; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, clear) { + // Arrange + int data[3] = { 1, 2, 3 }; + Tvector vector(data, 3); + + // Act + vector.clear(); + + // Assert + EXPECT_EQ(0, vector.get_size()); + EXPECT_EQ(0, vector.get_deleted()); +} +TEST(TestVectorLib, emplace) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + vector.emplace(2, 100); + // Act + int actual_result = vector[2]; + // Assert + int expected_result = 100; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, push_front1) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + vector.push_front(200); + // Act + int actual_result = vector[0]; + // Assert + int expected_result = 200; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, push_front2) { + // Arrange + int data[1] = { 1 }; + size_t size = 1; + Tvector vector(data, size); + for (size_t i = 1; i < 17; ++i) { + vector.push_front(1); + } + // Act + bool actual_result = vector.get_capacity() == 31 + && vector.get_size() == 17; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, insert1) { + // Arrange + int data[6] = { 1, 2, 3, 4, 5, 6 }; + size_t size = 6; + Tvector vector(data, size); + vector.insert(225, 5); + // Act + int actual_result = vector[5]; + // Assert + int expected_result = 225; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, insert2) { + // Arrange + size_t size = 10; + Tvector vector(size); + vector.insert(225, 5); + // Act + int actual_result = vector[5]; + // Assert + int expected_result = 225; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, insert3) { + // Arrange + Tvector vector; + vector.insert(225, 5); + // Act + int actual_result = vector[5]; + // Assert + int expected_result = 225; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, insert4) { + // Arrange + bool actual_result = true; + int data[5] = { 1, 2, 3, 4, 5 }; + size_t size = 5; + Tvector vector(data, size); + try { + vector.insert(225, 5); + } + catch (...) { + actual_result = false; + } + // Assert + bool expected_result = false; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, push_back1) { + // Arrange + int data[1] = { 1 }; + size_t size = 1; + Tvector vector(data, size); + for (size_t i = 1; i < 17; ++i) { + vector.push_back(400); + } + // Act + int actual_result = vector[5]; + // Assert + int expected_result = 400; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, push_back2) { + // Arrange + int data[3] = { 1, 2, 3 }; + size_t size = 3; + Tvector vector(data, size); + vector.push_back(9); + // Act + int actual_result = vector[3]; + // Assert + int expected_result = 9; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, pop_back1) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + vector.pop_back(); + // Act + bool actual_result = vector.get_size() == 2 + && vector[0] == 1 + && vector[1] == 2; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, pop_back2) { + // Arrange + bool actual_result = true; + Tvector vector; + try { + vector.pop_back(); + } + catch (...) { + actual_result = false; + } + // Assert + bool expected_result = false; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, erase1) { + // Arrange + int data[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + size_t size = 15; + Tvector vector(data, size); + vector.erase(1); + vector.erase(1); + // Act + size_t actual_result = vector.get_deleted(); + // Assert + size_t expected_result = 2; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, erase2) { + // Arrange + int data[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + size_t size = 15; + Tvector vector(data, size); + vector.erase(1); + vector.erase(1); + vector.erase(1); + // Act + bool actual_result = vector.get_deleted() == 0 + && vector.get_size() == 12 + && vector.get_capacity() == 27; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, erase3) { + // Arrange + bool actual_result = true; + int data[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + size_t size = 15; + Tvector vector(data, size); + try { + vector.erase(20); + } + catch (...) { + actual_result = false; + } + // Assert + bool expected_result = false; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, pop_front1) { + // Arrange + int data[3] = { 1,2,3 }; + size_t size = 3; + Tvector vector(data, size); + vector.pop_front(); + // Act + bool actual_result = vector.get_size() == 2 + && vector[0] == 2 + && vector[1] == 3; + // Assert + bool expected_result = true; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, pop_front2) { + // Arrange + bool actual_result = true; + Tvector vector; + try { + vector.pop_front(); + } + catch (...) { + actual_result = false; + } + // Assert + bool expected_result = false; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, sort_of_shell) { + // Arrange + int data[10] = { 1223343, 1546, 8777, 1000, 2, 3, 7, 9, 888, 99 }; + size_t size = 10; + Tvector vector(data, size); + int sorted_data[10] = { 2, 3, 7, 9, 99, 888, 1000, 1546, 8777, 1223343 }; + Tvector sorted_vector(sorted_data, size); + shell_sort(vector); + // Act + Tvector actual_result = vector; + // Assert + Tvector expected_result = sorted_vector; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, find_first) { + // Arrange + int data[6] = { 1 ,3, 3, 4, 5, 3 }; + size_t size = 6; + Tvector vector(data, size); + // Act + size_t actual_result = find_first_element(vector, 3); + // Assert + size_t expected_result = 2; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, find_last) { + // Arrange + int data[6] = { 1 ,3, 3, 4, 5, 3 }; + size_t size = 6; + Tvector vector(data, size); + // Act + size_t actual_result = find_last_element(vector, 3); + // Assert + size_t expected_result = 6; + EXPECT_EQ(expected_result, actual_result); +} +TEST(TestVectorLib, find_count_of_suitable_elements) { + // Arrange + int data[7] = { 1 ,3, 3, 4, 5, 3, 3 }; + size_t size = 7; + Tvector vector(data, size); + // Act + size_t actual_result = find_count_of_all_suitable_elements(vector, 3); + // Assert + size_t expected_result = 4; + EXPECT_EQ(expected_result, actual_result); +} From 6154b19e3f69dadcbbdcf6f7e6ad9ee052c0ab5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 21:33:43 +0300 Subject: [PATCH 004/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Mat?= =?UTF-8?q?hVector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib_math_vector/math_vector.h diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h new file mode 100644 index 00000000..06fade6a --- /dev/null +++ b/lib_math_vector/math_vector.h @@ -0,0 +1,21 @@ +#pragma once +#include "../lib_vector/vector.h" +template +class MathVector : public Tvector { +public: + MathVector(); + MathVector(size_t size); + MathVector(T* data, size_t size); + MathVector(const MathVector& other); + virtual ~MathVector(); +}; +template +MathVector::MathVector() : Tvector() {} +template +MathVector::MathVector(size_t size) : Tvector(size) {} +template +MathVector::MathVector(T* data, size_t size) : Tvector(data, size) {} +template +MathVector::MathVector(const MathVector& other) : Tvector(other) {} +template +MathVector::~MathVector() = default; From 651f9e2ba5d6367a364910ad332e9dd6e49a85ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 21:38:06 +0300 Subject: [PATCH 005/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20MathVector=20?= =?UTF-8?q?=D1=81=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE=D1=87?= =?UTF-8?q?=D0=BD=D1=8B=D0=BC=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_math_vector/math_vector.cpp diff --git a/lib_math_vector/math_vector.cpp b/lib_math_vector/math_vector.cpp new file mode 100644 index 00000000..9e86fff9 --- /dev/null +++ b/lib_math_vector/math_vector.cpp @@ -0,0 +1 @@ +#include "math_vector.h" From cf7252ef0cc3850f6d3cfc475dec244c4d008472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 21:50:37 +0300 Subject: [PATCH 006/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Mat?= =?UTF-8?q?rix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 185 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 lib_matrix/matrix.h diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h new file mode 100644 index 00000000..ac69b563 --- /dev/null +++ b/lib_matrix/matrix.h @@ -0,0 +1,185 @@ +#pragma once +#include "../lib_math_vector/math_vector.h" +template +class Matrix : public MathVector> { +protected: + size_t _M; + size_t _N; +public: + Matrix(); + Matrix(size_t M, size_t N); + Matrix(T* data, size_t M, size_t N); + Matrix(const Matrix& other); + virtual ~Matrix(); + size_t getM() const { + return _M; + } + size_t getN() const { + return _N; + } + Matrix operator + (T value) const; + Matrix operator - (T value) const; + Matrix operator * (T value) const; + Matrix operator / (T value) const; + + Matrix& operator += (T value); + Matrix& operator -= (T value); + Matrix& operator *= (T value); + Matrix& operator /= (T value); + + Matrix operator + (const MathVector& vector) const; + Matrix operator - (const MathVector& vector) const; + MathVector operator * (const MathVector& vector) const; + + Matrix& operator += (const MathVector& vector); + Matrix& operator -= (const MathVector& vector); + + Matrix operator + (const Matrix& other_matrix) const; + Matrix operator - (const Matrix& other_matrix) const; + Matrix operator * (const Matrix& other_matrix) const; + + Matrix& operator += (const Matrix& other_matrix); + Matrix& operator -= (const Matrix& other_matrix); + Matrix& operator *= (const Matrix& other_matrix); + + friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix); + MathVector& operator[](size_t index); + const MathVector& operator[](size_t index) const; + + +}; +template +Matrix::Matrix() : MathVector>(), _M(0), _N(0) {} +template +Matrix::Matrix(size_t M, size_t N) : MathVector>(M) { + _M = M; + _N = N; + for (size_t i = 0; i < M; ++i) { + (*this)[i] = MathVector(N); + } +} +template +Matrix::Matrix(T* data, size_t M, size_t N) : MathVector>(M) { + _M = M; + _N = N; + for (size_t i = 0; i < M; ++i) { + (*this)[i] = MathVector(data + i * N, N); + } +} +template +Matrix::Matrix(const Matrix& other) : + MathVector>(other), _M(other._M), _N(other._N) {} +template +Matrix::~Matrix() = default; + +template +std::ostream& operator<<(std::ostream& out, const Matrix& matrix) { + for (size_t i = 0; i < matrix.getM(); ++i) { + for (size_t j = 0; j < matrix.getN(); ++j) { + out << matrix[i][j] << "\t"; + } + out << std::endl; + } + return out; +} + +template +Matrix Matrix::operator + (T value) const { + Matrix matrix; + return matrix; +} +template +Matrix Matrix::operator - (T value) const { + Matrix matrix; + return matrix; +} +template +Matrix Matrix::operator * (T value) const { + Matrix matrix; + return matrix; +} +template +Matrix Matrix::operator / (T value) const { + Matrix matrix; + return matrix; +} + +template +Matrix& Matrix::operator += (T value) { + return *this; +} +template +Matrix& Matrix::operator -= (T value) { + return *this; + +} +template +Matrix& Matrix::operator *= (T value) { + return *this; +} +template +Matrix& Matrix::operator /= (T value) { + return *this; +} + +template +Matrix Matrix::operator + (const MathVector& vector) const { + Matrix matrix; + return matrix; +} +template +Matrix Matrix::operator - (const MathVector& vector) const { + Matrix matrix; + return matrix; +} +template +MathVector Matrix::operator * (const MathVector& vector) const { + Matrix matrix; + return matrix; +} + +template +Matrix& Matrix::operator += (const MathVector& vector) { + return *this; +} +template +Matrix& Matrix::operator -= (const MathVector& vector) { + return *this; +} + +template +Matrix Matrix::operator + (const Matrix& other_matrix) const { + Matrix matrix; + return matrix; +} +template +Matrix Matrix::operator - (const Matrix& other_matrix) const { + Matrix matrix; + return matrix; +} +template +Matrix Matrix::operator * (const Matrix& other_matrix) const { + Matrix matrix; + return matrix; +} + +template +Matrix& Matrix::operator += (const Matrix& other_matrix) { + return *this; +} +template +Matrix& Matrix::operator -= (const Matrix& other_matrix) { + return *this; +} +template +Matrix& Matrix::operator *= (const Matrix& other_matrix) { + return *this; +} +template +MathVector& Matrix::operator[](size_t index) { + return MathVector>::operator[](index); +} +template +const MathVector& Matrix::operator[](size_t index) const { + return MathVector>::operator[](index); +} From 2eb45df9db623d6c2f54e140d2008f1fb7917f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 21:56:47 +0300 Subject: [PATCH 007/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20(=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B8?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index ac69b563..4ff9c5c9 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -42,7 +42,15 @@ class Matrix : public MathVector> { Matrix& operator -= (const Matrix& other_matrix); Matrix& operator *= (const Matrix& other_matrix); - friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix); + friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { + for (size_t i = 0; i < matrix.getM(); ++i) { + for (size_t j = 0; j < matrix.getN(); ++j) { + out << matrix[i][j] << "\t"; + } + out << std::endl; + } + return out; + } MathVector& operator[](size_t index); const MathVector& operator[](size_t index) const; @@ -72,17 +80,6 @@ Matrix::Matrix(const Matrix& other) : template Matrix::~Matrix() = default; -template -std::ostream& operator<<(std::ostream& out, const Matrix& matrix) { - for (size_t i = 0; i < matrix.getM(); ++i) { - for (size_t j = 0; j < matrix.getN(); ++j) { - out << matrix[i][j] << "\t"; - } - out << std::endl; - } - return out; -} - template Matrix Matrix::operator + (T value) const { Matrix matrix; From ce081481f7c12cb8b2b124f94e8c16a6e0b38df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 21:57:42 +0300 Subject: [PATCH 008/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Tri?= =?UTF-8?q?angle=20Matrix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 145 ++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 lib_triangle_matrix/triangle_matrix.h diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h new file mode 100644 index 00000000..452dd223 --- /dev/null +++ b/lib_triangle_matrix/triangle_matrix.h @@ -0,0 +1,145 @@ +#pragma once +#include "../lib_matrix/matrix.h" +template +class TriangleMatrix : public Matrix { +public: + TriangleMatrix(); + TriangleMatrix(size_t M, size_t N); + TriangleMatrix(T* data, size_t M, size_t N); + TriangleMatrix(const TriangleMatrix& other); + ~TriangleMatrix(); + + TriangleMatrix operator + (T value) const; + TriangleMatrix operator - (T value) const; + TriangleMatrix operator * (T value) const; + TriangleMatrix operator / (T value) const; + + TriangleMatrix& operator += (T value); + TriangleMatrix& operator -= (T value); + TriangleMatrix& operator *= (T value); + TriangleMatrix& operator /= (T value); + + TriangleMatrix operator + (const MathVector& vector) const; + TriangleMatrix operator - (const MathVector& vector) const; + MathVector operator * (const MathVector& vector) const; + + TriangleMatrix& operator += (const MathVector& vector); + TriangleMatrix& operator -= (const MathVector& vector); + + TriangleMatrix operator + (const TriangleMatrix& other_matrix) const; + TriangleMatrix operator - (const TriangleMatrix& other_matrix) const; + TriangleMatrix operator * (const TriangleMatrix& other_matrix) const; + + TriangleMatrix& operator += (const TriangleMatrix& other_matrix); + TriangleMatrix& operator -= (const TriangleMatrix& other_matrix); + TriangleMatrix& operator *= (const TriangleMatrix& other_matrix); + + friend std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { + for (size_t i = 0; i < matrix.getM(); ++i) { + for (size_t j = 0; j < matrix.getN(); ++j) { + out << matrix[i][j] << "\t"; + } + out << std::endl; + } + return out; + } +}; +template +TriangleMatrix::TriangleMatrix() : Matrix() {} +template +TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} +template +TriangleMatrix::~TriangleMatrix() = default; + +template +TriangleMatrix TriangleMatrix::operator + (T value) const { + TriangleMatrix matrix; + return matrix; +} +template +TriangleMatrix TriangleMatrix::operator - (T value) const { + TriangleMatrix matrix; + return matrix; +} +template +TriangleMatrix TriangleMatrix::operator * (T value) const { + TriangleMatrix matrix; + return matrix; +} +template +TriangleMatrix TriangleMatrix::operator / (T value) const { + TriangleMatrix matrix; + return matrix; +} + +template +TriangleMatrix& TriangleMatrix::operator += (T value) { + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator -= (T value) { + return *this; + +} +template +TriangleMatrix& TriangleMatrix::operator *= (T value) { + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator /= (T value) { + return *this; +} + +template +TriangleMatrix TriangleMatrix::operator + (const MathVector& vector) const { + TriangleMatrix matrix; + return matrix; +} +template +TriangleMatrix TriangleMatrix::operator - (const MathVector& vector) const { + TriangleMatrix matrix; + return matrix; +} +template +MathVector TriangleMatrix::operator * (const MathVector& vector) const { + TriangleMatrix matrix; + return matrix; +} + +template +TriangleMatrix& TriangleMatrix::operator += (const MathVector& vector) { + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator -= (const MathVector& vector) { + return *this; +} + +template +TriangleMatrix TriangleMatrix::operator + (const TriangleMatrix& other_matrix) const { + TriangleMatrix matrix; + return matrix; +} +template +TriangleMatrix TriangleMatrix::operator - (const TriangleMatrix& other_matrix) const { + TriangleMatrix matrix; + return matrix; +} +template +TriangleMatrix TriangleMatrix::operator * (const TriangleMatrix& other_matrix) const { + TriangleMatrix matrix; + return matrix; +} + +template +TriangleMatrix& TriangleMatrix::operator += (const TriangleMatrix& other_matrix) { + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator -= (const TriangleMatrix& other_matrix) { + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator *= (const TriangleMatrix& other_matrix) { + return *this; +} From bf8c4e62e1cfc6ba93d6acd3422610ca987044d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 22:40:35 +0300 Subject: [PATCH 009/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D1=83=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D1=8B=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=20(=D0=B1=D1=8B=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20=D1=81=20=D1=82?= =?UTF-8?q?=D0=B8=D0=BF=D0=BE=D0=BC=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0?= =?UTF-8?q?=D1=89=D0=B0=D0=B5=D0=BC=D0=BE=D0=B3=D0=BE=20=D0=B7=D0=BD=D0=B0?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 4ff9c5c9..501d93f5 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -131,8 +131,8 @@ Matrix Matrix::operator - (const MathVector& vector) const { } template MathVector Matrix::operator * (const MathVector& vector) const { - Matrix matrix; - return matrix; + MathVector math_vector; + return math_vector; } template From 7117587eb8c6fb503c75ae167f1cea3ec601d7c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 22:51:02 +0300 Subject: [PATCH 010/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D0=B0=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB?= =?UTF-8?q?=D1=8F=D1=82=D0=BE=D1=80=D0=B0=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 282 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 278 insertions(+), 4 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 217f8971..0bc47c59 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,8 +1,9 @@ -// Copyright 2024 Marina Usova +// Copyright 2024 Julia Zabytia +//#define EASY_EXAMPLE +//#define CIRCLES_AND_SPHERES -#define EASY_EXAMPLE +#define MATRIX #ifdef EASY_EXAMPLE - #include #include #include "../lib_easy_example/easy_example.h" @@ -34,4 +35,277 @@ int main() { return 0; } -#endif // EASY_EXAMPLE +#endif +#ifdef CIRCLES_AND_SPHERES +#include +#include +#include "../lib_sphere/sphere.h" +#include "../lib_circle/circle.h" +#include "../lib_point/point.h" +#include "../lib_point3d/point3d.h" +enum result_of_compare +{ + Equal, + Intersecting, + NotIntersecting, + FirstTouchSecond, + OneInsideOther, + OneInsideOtherTouching +}; +result_of_compare Compare(int d, int r1, int r2, int sum, int diff) { + if (d == 0 && r1 == r2) { + return Equal; + } + if (d > sum) { + return NotIntersecting; + } + if (d == sum) { + return FirstTouchSecond; + } + if (d > diff && d < sum) { + return Intersecting; + } + if (d == diff && d > 0) { + return OneInsideOtherTouching; + } + if (d < diff) { + return OneInsideOther; + } + return result_of_compare::NotIntersecting; +} +result_of_compare CompareCircles(const Circle& circle1, const Circle& circle2) { + int d = circle1.getDistanceTo(circle2); + int r1 = circle1.getR(); + int r2 = circle2.getR(); + int sum = r1 + r2; + int diff = std::abs(r1 - r2); + return Compare(d, r1, r2, sum, diff); +} +result_of_compare CompareSpheres(const Sphere& sphere1, const Sphere& sphere2) { + int d = sphere1.getDistanceTo(sphere2); + int r1 = sphere1.getR(); + int r2 = sphere2.getR(); + int sum = r1 + r2; + int diff = std::abs(r1 - r2); + return Compare(d, r1, r2, sum, diff); +} +int main() { + + Point center1(0, 0); + Circle circle1(center1, 5); + Point center2(6, 0); + Circle circle2(center2, 4); + result_of_compare circle_result = CompareCircles(circle1, circle2); + + switch (circle_result) { + case Equal: + std::cout << "Circles is equal\n"; + break; + case Intersecting: + std::cout << "Circles is intersecting\n"; + break; + case NotIntersecting: + std::cout << "Circles is not intersecting\n"; + break; + case FirstTouchSecond: + std::cout << "First circle touch second circle\n"; + break; + case OneInsideOther: + std::cout << "One circle inside other circle\n"; + break; + case OneInsideOtherTouching: + std::cout << "One circle inside other circle (with touching)\n"; + break; + default: + std::cout << "Unknown result\n"; + break; + } + return 0; +} +#endif +#ifdef MATRIX +#include +#include "../lib_math_vector/math_vector.h" +#include "../lib_matrix/matrix.h" +#include "../lib_triangle_matrix/triangle_matrix.h" + +void printMainMenu() { + std::cout << "==========================================\n"; + std::cout << "| КАЛЬКУЛЯТОР ДЛЯ РАЗЛИЧНЫХ МАТРИЦ |\n"; + std::cout << "| (обычные и треугольные) |\n"; + std::cout << "==========================================\n"; + std::cout << "\n"; + std::cout << "ГЛАВНОЕ МЕНЮ =====\n"; + std::cout << " 1.Сложение матриц\n"; + std::cout << " 2.Вычитание матриц\n"; + std::cout << " 3.Умножение матриц\n"; + std::cout << " 4.Прибавить скаляр к матрице\n"; + std::cout << " 5.Вычесть скаляр из матрицы\n"; + std::cout << " 6.Умножить матрицу на скаляр\n"; + std::cout << " 7.Деление на скаляр\n"; + std::cout << " 8.Прибавить к матрице вектор\n"; + std::cout << " 9.Вычесть вектор из матрицы\n"; + std::cout << " 10.Умножить матрицу на вектор\n"; + std::cout << " 11.Выйти\n"; +} +int main() { + setlocale(LC_ALL, "Russian"); + int user_choice; + do { + printMainMenu(); + std::cout << "Ваш выбор: "; + std::cin >> user_choice; + + if (user_choice == 11) { + break; + } + int matrix_type; + std::cout << "Выберите тип матрицы:\n"; + std::cout << "1. Обычная матрица\n"; + std::cout << "2. Треугольная матрица\n"; + std::cin >> matrix_type; + + size_t M, N; + std::cout << "Введите размеры матрицы 1 (M - кол-во строк, N - кол-во столбцов)\n"; + std::cout << "M = "; + std::cin >> M; + std::cout << "N = "; + std::cin >> N; + + Matrix matrix1(M, N); + + std::cout << "Введите элементы матрицы 1:\n"; + system("pause"); + + switch (user_choice) { + case 1: { + size_t K, L; + std::cout << "Введите размеры матрицы 2 (K - кол-во строк, L - кол-во столбцов)\n"; + std::cout << "K = "; + std::cin >> K; + std::cout << "L = "; + std::cin >> L; + Matrix matrix2(K, L); + + std::cout << "Введите элементы матрицы 2:\n"; + system("pause"); + Matrix result = matrix1 + matrix2; + std::cout << "Результат:\n" << result; + break; + } + case 2: { + size_t K, L; + std::cout << "Введите размеры матрицы 2 (K - кол-во строк, L - кол-во столбцов)\n"; + std::cout << "K = "; + std::cin >> K; + std::cout << "L = "; + std::cin >> L; + Matrix matrix2(K, L); + + std::cout << "Введите элементы матрицы 2:\n"; + system("pause"); + Matrix result = matrix1 - matrix2; + std::cout << "Результат:\n" << result; + break; + } + case 3: { + size_t K, L; + std::cout << "Введите размеры матрицы 2 (K - кол-во строк, L - кол-во столбцов)\n"; + std::cout << "K = "; + std::cin >> K; + std::cout << "L = "; + std::cin >> L; + Matrix matrix2(K, L); + + std::cout << "Введите элементы матрицы 2:\n"; + system("pause"); + Matrix result = matrix1 * matrix2; + std::cout << "Результат:\n" << result; + break; + } + case 4: { + int scalar; + std::cout << "Введите скаляр:\n"; + std::cin >> scalar; + system("pause"); + Matrix result = matrix1 + scalar; + std::cout << "Результат:\n" << result; + break; + } + case 5: { + int scalar; + std::cout << "Введите скаляр:\n"; + std::cin >> scalar; + system("pause"); + Matrix result = matrix1 - scalar; + std::cout << "Результат:\n" << result; + break; + } + case 6: { + int scalar; + std::cout << "Введите скаляр:\n"; + std::cin >> scalar; + system("pause"); + Matrix result = matrix1 * scalar; + std::cout << "Результат:\n" << result; + break; + } + case 7: { + int scalar; + std::cout << "Введите скаляр:\n"; + std::cin >> scalar; + system("pause"); + Matrix result = matrix1 / scalar; + std::cout << "Результат:\n" << result; + break; + } + case 8: { + size_t size; + std::cout << "Введите размер вектора:\n"; + std::cin >> size; + MathVector mathvector(size); + + std::cout << "Введите элементы вектора:\n"; + system("pause"); + Matrix result = matrix1 + mathvector; + std::cout << "Результат:\n" << result; + break; + } + case 9: { + size_t size; + std::cout << "Введите размер вектора:\n"; + std::cin >> size; + MathVector mathvector(size); + + std::cout << "Введите элементы вектора:\n"; + system("pause"); + Matrix result = matrix1 - mathvector; + std::cout << "Результат:\n" << result; + break; + } + case 10: { + size_t size; + std::cout << "Введите размер вектора:\n"; + std::cin >> size; + MathVector mathvector(size); + + std::cout << "Введите элементы вектора:\n"; + system("pause"); + MathVector result = matrix1 * mathvector; + std::cout << "Результат:\n" << result; + break; + } + default: { + std::cout << "Неверный ввод! Попробуйте снова\n"; + break; + } + } + std::cout << "\n Нажмите Enter для продолжения..."; + std::cin.ignore(); + std::cin.get(); + } while (true); + + return 0; +} +#endif + From 3a852a2672981a621ac31d6dde231cda0497b22e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 18 Sep 2025 22:58:10 +0300 Subject: [PATCH 011/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D0=B0=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB?= =?UTF-8?q?=D1=8F=D1=82=D0=BE=D1=80=D0=B0=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=86=20=D1=81=20=D0=BD=D0=B0=D0=B4=D0=BF=D0=B8=D1=81=D1=8F?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=BD=D0=B0=20=D0=B0=D0=BD=D0=B3=D0=BB=D0=B8?= =?UTF-8?q?=D0=B9=D1=81=D0=BA=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 101 +++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 0bc47c59..e65391ee 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -131,42 +131,43 @@ int main() { void printMainMenu() { std::cout << "==========================================\n"; - std::cout << "| КАЛЬКУЛЯТОР ДЛЯ РАЗЛИЧНЫХ МАТРИЦ |\n"; - std::cout << "| (обычные и треугольные) |\n"; + std::cout << "| CALCULATOR FOR VARIOUS MATRICES |\n"; + std::cout << "| (REGULAR AND TRIANGULAR) |\n"; std::cout << "==========================================\n"; std::cout << "\n"; - std::cout << "ГЛАВНОЕ МЕНЮ =====\n"; - std::cout << " 1.Сложение матриц\n"; - std::cout << " 2.Вычитание матриц\n"; - std::cout << " 3.Умножение матриц\n"; - std::cout << " 4.Прибавить скаляр к матрице\n"; - std::cout << " 5.Вычесть скаляр из матрицы\n"; - std::cout << " 6.Умножить матрицу на скаляр\n"; - std::cout << " 7.Деление на скаляр\n"; - std::cout << " 8.Прибавить к матрице вектор\n"; - std::cout << " 9.Вычесть вектор из матрицы\n"; - std::cout << " 10.Умножить матрицу на вектор\n"; - std::cout << " 11.Выйти\n"; + std::cout << "MAIN MENU =====\n"; + std::cout << " 1. Matrix addition\n"; + std::cout << " 2. Matrix subtraction\n"; + std::cout << " 3. Matrix multiplication\n"; + std::cout << " 4. Add scalar to matrix\n"; + std::cout << " 5. Subtract scalar from matrix\n"; + std::cout << " 6. Multiply matrix by scalar\n"; + std::cout << " 7. Division by scalar\n"; + std::cout << " 8. Add vector to matrix\n"; + std::cout << " 9. Subtract vector from matrix\n"; + std::cout << " 10. Multiply matrix by vector\n"; + std::cout << " 11. Exit\n"; } + int main() { setlocale(LC_ALL, "Russian"); int user_choice; do { printMainMenu(); - std::cout << "Ваш выбор: "; + std::cout << "Your choice: "; std::cin >> user_choice; if (user_choice == 11) { break; } int matrix_type; - std::cout << "Выберите тип матрицы:\n"; - std::cout << "1. Обычная матрица\n"; - std::cout << "2. Треугольная матрица\n"; + std::cout << "Choose matrix type:\n"; + std::cout << "1. Regular matrix\n"; + std::cout << "2. Triangular matrix\n"; std::cin >> matrix_type; size_t M, N; - std::cout << "Введите размеры матрицы 1 (M - кол-во строк, N - кол-во столбцов)\n"; + std::cout << "Enter dimensions of matrix 1 (M - rows, N - columns)\n"; std::cout << "M = "; std::cin >> M; std::cout << "N = "; @@ -174,133 +175,133 @@ int main() { Matrix matrix1(M, N); - std::cout << "Введите элементы матрицы 1:\n"; + std::cout << "Enter elements of matrix 1:\n"; system("pause"); switch (user_choice) { case 1: { size_t K, L; - std::cout << "Введите размеры матрицы 2 (K - кол-во строк, L - кол-во столбцов)\n"; + std::cout << "Enter dimensions of matrix 2 (K - rows, L - columns)\n"; std::cout << "K = "; std::cin >> K; std::cout << "L = "; std::cin >> L; Matrix matrix2(K, L); - std::cout << "Введите элементы матрицы 2:\n"; + std::cout << "Enter elements of matrix 2:\n"; system("pause"); Matrix result = matrix1 + matrix2; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 2: { size_t K, L; - std::cout << "Введите размеры матрицы 2 (K - кол-во строк, L - кол-во столбцов)\n"; + std::cout << "Enter dimensions of matrix 2 (K - rows, L - columns)\n"; std::cout << "K = "; std::cin >> K; std::cout << "L = "; std::cin >> L; Matrix matrix2(K, L); - std::cout << "Введите элементы матрицы 2:\n"; + std::cout << "Enter elements of matrix 2:\n"; system("pause"); Matrix result = matrix1 - matrix2; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 3: { size_t K, L; - std::cout << "Введите размеры матрицы 2 (K - кол-во строк, L - кол-во столбцов)\n"; + std::cout << "Enter dimensions of matrix 2 (K - rows, L - columns)\n"; std::cout << "K = "; std::cin >> K; std::cout << "L = "; std::cin >> L; Matrix matrix2(K, L); - std::cout << "Введите элементы матрицы 2:\n"; + std::cout << "Enter elements of matrix 2:\n"; system("pause"); Matrix result = matrix1 * matrix2; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 4: { int scalar; - std::cout << "Введите скаляр:\n"; + std::cout << "Enter scalar:\n"; std::cin >> scalar; system("pause"); Matrix result = matrix1 + scalar; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 5: { int scalar; - std::cout << "Введите скаляр:\n"; + std::cout << "Enter scalar:\n"; std::cin >> scalar; system("pause"); Matrix result = matrix1 - scalar; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 6: { int scalar; - std::cout << "Введите скаляр:\n"; + std::cout << "Enter scalar:\n"; std::cin >> scalar; system("pause"); Matrix result = matrix1 * scalar; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 7: { int scalar; - std::cout << "Введите скаляр:\n"; + std::cout << "Enter scalar:\n"; std::cin >> scalar; system("pause"); Matrix result = matrix1 / scalar; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 8: { size_t size; - std::cout << "Введите размер вектора:\n"; + std::cout << "Enter vector size:\n"; std::cin >> size; MathVector mathvector(size); - std::cout << "Введите элементы вектора:\n"; + std::cout << "Enter vector elements:\n"; system("pause"); Matrix result = matrix1 + mathvector; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 9: { size_t size; - std::cout << "Введите размер вектора:\n"; + std::cout << "Enter vector size:\n"; std::cin >> size; MathVector mathvector(size); - std::cout << "Введите элементы вектора:\n"; + std::cout << "Enter vector elements:\n"; system("pause"); Matrix result = matrix1 - mathvector; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; } case 10: { size_t size; - std::cout << "Введите размер вектора:\n"; + std::cout << "Enter vector size:\n"; std::cin >> size; MathVector mathvector(size); - std::cout << "Введите элементы вектора:\n"; + std::cout << "Enter vector elements:\n"; system("pause"); MathVector result = matrix1 * mathvector; - std::cout << "Результат:\n" << result; + std::cout << "Result:\n" << result; break; - } + } default: { - std::cout << "Неверный ввод! Попробуйте снова\n"; + std::cout << "Invalid input! Please try again\n"; break; } - } - std::cout << "\n Нажмите Enter для продолжения..."; + } + std::cout << "\n Press Enter to continue..."; std::cin.ignore(); std::cin.get(); } while (true); From 60a7ff1afb3da1365ad4236196d6f70ccfe64ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 26 Sep 2025 18:27:29 +0300 Subject: [PATCH 012/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=20*,=20/,=20*=3D,=20/=3D=20(=D0=B2=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=20=D0=B8=20=D1=87=D0=B8=D1=81=D0=BB?= =?UTF-8?q?=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 06fade6a..ab55d7cc 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -1,4 +1,5 @@ #pragma once +#include #include "../lib_vector/vector.h" template class MathVector : public Tvector { @@ -8,6 +9,14 @@ class MathVector : public Tvector { MathVector(T* data, size_t size); MathVector(const MathVector& other); virtual ~MathVector(); + + MathVector operator * (T value) const; + MathVector operator / (T value) const; + + MathVector& operator *= (T value); + MathVector& operator /= (T value); + + }; template MathVector::MathVector() : Tvector() {} @@ -19,3 +28,42 @@ template MathVector::MathVector(const MathVector& other) : Tvector(other) {} template MathVector::~MathVector() = default; + +template +MathVector MathVector::operator * (T value) const { + MathVector result(this->get_size()); + for (size_t i = 0; i < this->get_size(); ++i) { + result[i] = this->get_data()[i] * value; + } + return result; +} +template +MathVector MathVector::operator / (T value) const { + if (value == 0) { + throw std::logic_error("Can't divide by zero!"); + } + MathVector result(this->get_size()); + for (size_t i = 0; i < this->get_size(); ++i) { + result[i] = this->get_data()[i] / value; + } + return result; +} +template +MathVector& MathVector::operator *= (T value) { + for (size_t i = 0; i < this->get_size(); ++i) { + this->get_data()[i] *= value; + } + return *this; +} +template +MathVector& MathVector::operator /= (T value) { + if (value == 0) { + throw std::logic_error("Can't divide by zero!"); + } + for (size_t i = 0; i < this->get_size(); ++i) { + this->get_data()[i] /= value; + } + return *this; +} + + From 545ef539f73c18e8895d0fe500ec7b4ec6c147c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 26 Sep 2025 18:56:17 +0300 Subject: [PATCH 013/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20+,=20-,=20*,=20+=3D,=20-=3D=20(=D0=B2=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=20=D0=B8=20=D0=B2=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index ab55d7cc..42ea2dda 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -16,6 +16,12 @@ class MathVector : public Tvector { MathVector& operator *= (T value); MathVector& operator /= (T value); + MathVector operator + (const MathVector& vector) const; + MathVector operator - (const MathVector& vector) const; + T operator * (const MathVector& vector) const; + + MathVector& operator += (const MathVector& vector); + MathVector& operator -= (const MathVector& vector); }; template @@ -65,5 +71,66 @@ MathVector& MathVector::operator /= (T value) { } return *this; } +template +MathVector MathVector::operator + (const MathVector& vector) const { + if (this->get_size() != vector.get_size()) { + throw std::logic_error("Vectors must have the same dimension"); + } + MathVector result(this->get_size()); + for (size_t i = 0; i < this->get_size(); ++i) { + result[i] = this->get_data()[i] + vector.get_data()[i]; + } + return result; +} +template +MathVector MathVector::operator - (const MathVector& vector) const { + if (this->get_size() != vector.get_size()) { + throw std::logic_error("Vectors must have the same dimension"); + } + MathVector result(this->get_size()); + for (size_t i = 0; i < this->get_size(); ++i) { + result[i] = this->get_data()[i] - vector.get_data()[i]; + } + return result; +} +template +T MathVector::operator * (const MathVector& vector) const { + if (this->get_size() != vector.get_size()) { + throw std::logic_error("Vectors must have the same dimension"); + } + T result{}; + for (size_t i = 0; i < this->get_size(); ++i) { + result += this->get_data()[i] * vector.get_data()[i]; + } + return result; +} +template +MathVector& MathVector::operator += (const MathVector& vector) { + if (this->get_size() != vector.get_size()) { + throw std::logic_error("Vectors must have the same dimension"); + } + for (size_t i = 0; i < this->get_size(); ++i) { + this->get_data()[i] += vector.get_data()[i]; + } + return *this; +} +template +MathVector& MathVector::operator -= (const MathVector& vector) { + if (this->get_size() != vector.get_size()) { + throw std::logic_error("Vectors must have the same dimension"); + } + for (size_t i = 0; i < this->get_size(); ++i) { + this->get_data()[i] -= vector.get_data()[i]; + } + return *this; +} + + + + + + + + From 40f0e251d50deb311bfa7c102d2a19984817068b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 26 Sep 2025 19:18:41 +0300 Subject: [PATCH 014/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0,=20=D0=B8=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D0=BD=D0=BE=20this->get=5Fdata()[i]=20=D0=BD?= =?UTF-8?q?=D0=B0=20(*this)[i]=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BD=D1=82=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B8=20=D0=B2=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 44 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 42ea2dda..5811dee7 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -9,7 +9,6 @@ class MathVector : public Tvector { MathVector(T* data, size_t size); MathVector(const MathVector& other); virtual ~MathVector(); - MathVector operator * (T value) const; MathVector operator / (T value) const; @@ -22,7 +21,7 @@ class MathVector : public Tvector { MathVector& operator += (const MathVector& vector); MathVector& operator -= (const MathVector& vector); - + friend std::ostream& operator<<(std::ostream& out, const MathVector& vector); }; template MathVector::MathVector() : Tvector() {} @@ -34,12 +33,11 @@ template MathVector::MathVector(const MathVector& other) : Tvector(other) {} template MathVector::~MathVector() = default; - template MathVector MathVector::operator * (T value) const { MathVector result(this->get_size()); for (size_t i = 0; i < this->get_size(); ++i) { - result[i] = this->get_data()[i] * value; + result[i] = (*this)[i] * value; } return result; } @@ -50,14 +48,14 @@ MathVector MathVector::operator / (T value) const { } MathVector result(this->get_size()); for (size_t i = 0; i < this->get_size(); ++i) { - result[i] = this->get_data()[i] / value; + result[i] = (*this)[i] / value; } return result; } template MathVector& MathVector::operator *= (T value) { for (size_t i = 0; i < this->get_size(); ++i) { - this->get_data()[i] *= value; + (*this)[i] *= value; } return *this; } @@ -67,7 +65,7 @@ MathVector& MathVector::operator /= (T value) { throw std::logic_error("Can't divide by zero!"); } for (size_t i = 0; i < this->get_size(); ++i) { - this->get_data()[i] /= value; + (*this)[i] /= value; } return *this; } @@ -78,7 +76,7 @@ MathVector MathVector::operator + (const MathVector& vector) const { } MathVector result(this->get_size()); for (size_t i = 0; i < this->get_size(); ++i) { - result[i] = this->get_data()[i] + vector.get_data()[i]; + result[i] = (*this)[i] + vector[i]; } return result; } @@ -89,7 +87,7 @@ MathVector MathVector::operator - (const MathVector& vector) const { } MathVector result(this->get_size()); for (size_t i = 0; i < this->get_size(); ++i) { - result[i] = this->get_data()[i] - vector.get_data()[i]; + result[i] = (*this)[i] - vector[i]; } return result; } @@ -100,7 +98,7 @@ T MathVector::operator * (const MathVector& vector) const { } T result{}; for (size_t i = 0; i < this->get_size(); ++i) { - result += this->get_data()[i] * vector.get_data()[i]; + result += (*this)[i] * vector[i]; } return result; } @@ -110,7 +108,7 @@ MathVector& MathVector::operator += (const MathVector& vector) { throw std::logic_error("Vectors must have the same dimension"); } for (size_t i = 0; i < this->get_size(); ++i) { - this->get_data()[i] += vector.get_data()[i]; + (*this)[i] += vector[i]; } return *this; } @@ -120,17 +118,19 @@ MathVector& MathVector::operator -= (const MathVector& vector) { throw std::logic_error("Vectors must have the same dimension"); } for (size_t i = 0; i < this->get_size(); ++i) { - this->get_data()[i] -= vector.get_data()[i]; + (*this)[i] -= vector[i]; } return *this; } - - - - - - - - - - +template +std::ostream& operator<<(std::ostream& out, const MathVector& vector) { + out << "["; + for (size_t i = 0; i < vector.get_size(); ++i) { + out << vector[i]; + if (i < vector.get_size() - 1) { + out << ", "; + } + } + out << "]"; + return out; +} From 4853d6f5d2832120ddfbdbd8099055a5809bf48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 14:20:27 +0300 Subject: [PATCH 015/275] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B2=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D1=85=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index a2efd98d..b1a6e933 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -1,8 +1,7 @@ // Copyright 2025 Julia Zabytina #include -#include "../lib_easy_example/easy_example.h" #include "../lib_vector/vector.h" -TEST(TestVectorLib, default_constructor) { +TEST(TestVectorLib, vector_default_constructor) { // Arrange Tvector vector; // Act @@ -16,7 +15,7 @@ TEST(TestVectorLib, default_constructor) { bool expected_result = true; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, constructor_with_size) { +TEST(TestVectorLib, vector_constructor_with_size) { // Arrange size_t size = 10; Tvector vector(size); @@ -31,7 +30,7 @@ TEST(TestVectorLib, constructor_with_size) { bool expected_result = true; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, constructor_with_array_and_size) { +TEST(TestVectorLib, vector_constructor_with_array_and_size) { // Arrange int data[3] = { 1,2,3 }; size_t size = 3; @@ -49,7 +48,7 @@ TEST(TestVectorLib, constructor_with_array_and_size) { bool expected_result = true; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, copy_constructor) { +TEST(TestVectorLib, vector_copy_constructor) { // Arrange int data[3] = { 1,2,3 }; size_t size = 3; From 7bb41956f5be8e0059bc8af233283ae1c45f1294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 14:44:28 +0300 Subject: [PATCH 016/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/test_math_vector.cpp diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp new file mode 100644 index 00000000..e69de29b From 46eacd1072b400b485630bac61db902287fe7fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 14:48:44 +0300 Subject: [PATCH 017/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B2=D0=BD=D1=83=D1=82?= =?UTF-8?q?=D1=80=D0=B8=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20MathVecto?= =?UTF-8?q?r,=20=D0=B2=D0=BE=20=D0=B8=D0=B7=D0=B1=D0=B5=D0=B6=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20=D0=BB?= =?UTF-8?q?=D0=B8=D0=BD=D0=BA=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=D0=B5=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 5811dee7..927b79e5 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -21,7 +21,17 @@ class MathVector : public Tvector { MathVector& operator += (const MathVector& vector); MathVector& operator -= (const MathVector& vector); - friend std::ostream& operator<<(std::ostream& out, const MathVector& vector); + friend std::ostream& operator<<(std::ostream& out, const MathVector& vector) { + out << "["; + for (size_t i = 0; i < vector.get_size(); ++i) { + out << vector[i]; + if (i < vector.get_size() - 1) { + out << ", "; + } + } + out << "]"; + return out; + } }; template MathVector::MathVector() : Tvector() {} @@ -122,15 +132,3 @@ MathVector& MathVector::operator -= (const MathVector& vector) { } return *this; } -template -std::ostream& operator<<(std::ostream& out, const MathVector& vector) { - out << "["; - for (size_t i = 0; i < vector.get_size(); ++i) { - out << vector[i]; - if (i < vector.get_size() - 1) { - out << ", "; - } - } - out << "]"; - return out; -} From 44c007187f8bc5f73baf7f89004e2acb70abae2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 14:54:06 +0300 Subject: [PATCH 018/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 50 +++++++++++-------------------------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index b1a6e933..dd758365 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -2,63 +2,39 @@ #include #include "../lib_vector/vector.h" TEST(TestVectorLib, vector_default_constructor) { - // Arrange + // Arrange & Act Tvector vector; - // Act - bool actual_result = vector.get_size() == 0 - && vector.get_deleted() == 0 - && vector.get_capacity() == 0 - && vector.get_data() == nullptr - && vector.get_states() == nullptr; - // Assert - bool expected_result = true; - EXPECT_EQ(expected_result, actual_result); + EXPECT_TRUE(vector.is_empty()); } TEST(TestVectorLib, vector_constructor_with_size) { - // Arrange + // Arrange & Act size_t size = 10; Tvector vector(size); - // Act - bool actual_result = vector.get_size() == size - && vector.get_capacity() == 25 - && vector.get_data() != nullptr - && vector.get_states() != nullptr - && vector.get_deleted() == 0;; - // Assert - bool expected_result = true; - EXPECT_EQ(expected_result, actual_result); + EXPECT_LE(vector.get_size(), vector.get_capacity()); + EXPECT_EQ(vector.get_size(), size); } TEST(TestVectorLib, vector_constructor_with_array_and_size) { - // Arrange + // Arrange & Act int data[3] = { 1,2,3 }; size_t size = 3; Tvector vector(data, size); - // Act - bool actual_result = vector.get_size() == size - && vector.get_capacity() == 18 - && vector.get_data() != nullptr - && vector.get_states() != nullptr - && vector.get_data()[0] == data[0] - && vector.get_data()[1] == data[1] - && vector.get_data()[2] == data[2] - && vector.get_deleted() == 0; // Assert - bool expected_result = true; - EXPECT_EQ(expected_result, actual_result); + EXPECT_EQ(vector.get_size(), size); + EXPECT_EQ(vector[0], data[0]); + EXPECT_EQ(vector[1], data[1]); + EXPECT_EQ(vector[2], data[2]); + EXPECT_LE(vector.get_size(), vector.get_capacity()); } TEST(TestVectorLib, vector_copy_constructor) { - // Arrange + // Arrange & Act int data[3] = { 1,2,3 }; size_t size = 3; Tvector old_vector(data, size); Tvector new_vector(old_vector); - // Act - bool actual_result = old_vector == new_vector; // Assert - bool expected_result = true; - EXPECT_EQ(expected_result, actual_result); + EXPECT_EQ(old_vector, new_vector); } TEST(TestVectorLib, is_empty) { // Arrange From 192eeac504eb943c9287431e3b36ad548faa00c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 15:21:34 +0300 Subject: [PATCH 019/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20*,=20/=20(=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=87=D0=B8=D1=81?= =?UTF-8?q?=D0=BB=D0=B5,=20=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B8=20=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BD=D0=B0=200)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 75 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index e69de29b..b74974e4 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -0,0 +1,75 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_math_vector/math_vector.h" +TEST(TestMathVectorLib, mathvector_default_constructor) { + // Arrange & Act + MathVector vector; + // Assert + EXPECT_TRUE(vector.is_empty()); +} +TEST(TestMathVectorLib, mathvector_constructor_with_size) { + // Arrange & Act + size_t size = 10; + MathVector vector(size); + // Assert + EXPECT_LE(vector.get_size(), vector.get_capacity()); + EXPECT_EQ(vector.get_size(), size); +} +TEST(TestMathVectorLib, mathvector_constructor_with_array_and_size) { + // Arrange & Act + int data[3] = { 1,2,3 }; + size_t size = 3; + MathVector vector(data, size); + // Assert + EXPECT_EQ(vector.get_size(), size); + EXPECT_EQ(vector[0], data[0]); + EXPECT_EQ(vector[1], data[1]); + EXPECT_EQ(vector[2], data[2]); + EXPECT_LE(vector.get_size(), vector.get_capacity()); +} +TEST(TestMathVectorLib, mathvector_copy_constructor) { + // Arrange & Act + int data[3] = { 1,2,3 }; + size_t size = 3; + MathVector old_vector(data, size); + MathVector new_vector(old_vector); + // Assert + EXPECT_EQ(old_vector, new_vector); +} +TEST(TestMathVectorLib, mathvector_mult_on_value) { + // Arrange & Act + int data[3] = { 1,2,3 }; + size_t size = 3; + int value = 5; + MathVector vector(data, size); + MathVector result; + result = vector * value; + // Assert + EXPECT_EQ(result[0], vector[0] * value); + EXPECT_EQ(result[1], vector[1] * value); + EXPECT_EQ(result[2], vector[2] * value); +} +TEST(TestMathVectorLib, mathvector_div_on_value) { + // Arrange & Act + int data[3] = { 4,8,28 }; + size_t size = 3; + int value = 2; + MathVector vector(data, size); + MathVector result; + result = vector / value; + // Assert + EXPECT_EQ(result[0], vector[0] / value); + EXPECT_EQ(result[1], vector[1] / value); + EXPECT_EQ(result[2], vector[2] / value); +} +TEST(TestMathVectorLib, mathvector_div_on_null_exception) { + // Arrange & Act + int data[3] = { 10, 27, 21 }; + size_t size = 3; + MathVector vector(data, size); + int value = 0; + // Assert + EXPECT_THROW(vector / value, std::logic_error); +} + From 28827801bb5b45d1328e965a0cc435bb52d833c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 15:31:16 +0300 Subject: [PATCH 020/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=81=D0=B8=D0=BD=D1=82=D0=B0=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B8=20=D0=B2=20=D0=BD=D0=B0=D0=B7=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=D1=85=20=D1=82=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index dd758365..ad786875 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -141,7 +141,7 @@ TEST(TestVectorLib, get_end) { int* expected_result = &(vector.get_data()[0]) + size; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, сomparison_operator_true) { +TEST(TestVectorLib, comparison_operator_true) { // Arrange int data[3] = { 1,2,3 }; size_t size = 3; @@ -153,7 +153,7 @@ TEST(TestVectorLib, get_end) { bool expected_result = true; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, сomparison_operator_false) { +TEST(TestVectorLib, comparison_operator_false) { // Arrange int data1[3] = { 1,2,3 }; size_t size1 = 3; @@ -167,7 +167,7 @@ TEST(TestVectorLib, get_end) { bool expected_result = false; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, not_сomparison_operator_true) { +TEST(TestVectorLib, not_comparison_operator_true) { // Arrange int data1[3] = { 1,2,3 }; size_t size1 = 3; @@ -181,7 +181,7 @@ TEST(TestVectorLib, get_end) { bool expected_result = true; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, not_сomparison_operator_false) { +TEST(TestVectorLib, not_comparison_operator_false) { // Arrange int data[3] = { 1,2,3 }; size_t size = 3; From cfd7002e3d94b5b90000f223d86aa54e2fb9cdde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 15:48:14 +0300 Subject: [PATCH 021/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20*=3D,=20/=3D=20(=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BA=D0=B0=D0=BB=D1=8F=D1=80,=20?= =?UTF-8?q?=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=87=D0=B8=D1=81=D0=BB=D0=B5,?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B0=D0=B5=20=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BD=D0=B0=200);=20+,=20-=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=B4=D0=B2=D1=83=D1=85=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2,=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=20*=20=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=B2?= =?UTF-8?q?=D1=83=D1=85=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=20(=D1=81=D0=BA=D0=B0=D0=BB=D1=8F=D1=80=D0=BD=D0=BE=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B8=D0=B7=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 85 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index b74974e4..1c11fca7 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -72,4 +72,89 @@ TEST(TestMathVectorLib, mathvector_div_on_null_exception) { // Assert EXPECT_THROW(vector / value, std::logic_error); } +TEST(TestMathVectorLib, mathvector_mult_assign_on_value) { + // Arrange & Act + int data[3] = { 3, 6, 0 }; + size_t size = 3; + int value = 3; + MathVector vector(data, size); + vector *= value; + // Assert + EXPECT_EQ(vector[0], data[0] * value); + EXPECT_EQ(vector[1], data[1] * value); + EXPECT_EQ(vector[2], data[2] * value); +} +TEST(TestMathVectorLib, mathvector_div_assign_on_value) { + // Arrange & Act + int data[3] = { 4,8,28 }; + size_t size = 3; + int value = 2; + MathVector vector(data, size); + vector /= value; + // Assert + EXPECT_EQ(vector[0], data[0] / value); + EXPECT_EQ(vector[1], data[1] / value); + EXPECT_EQ(vector[2], data[2] / value); +} +TEST(TestMathVectorLib, mathvector_div_assign_on_null_exception) { + // Arrange & Act + int data[3] = { 4,8,28 }; + size_t size = 3; + int value = 0; + MathVector vector(data, size); + // Assert + EXPECT_THROW(vector /= value, std::logic_error); +} +TEST(TestMathVectorLib, mathvector_add_vector) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[3] = { 5, 0, 100 }; + size_t size2 = 3; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + MathVector result; + result = vector1 + vector2; + // Assert + EXPECT_EQ(result[0], vector1[0] + vector2[0]); + EXPECT_EQ(result[1], vector1[1] + vector2[1]); + EXPECT_EQ(result[2], vector1[2] + vector2[2]); +} +TEST(TestMathVectorLib, mathvector_sub_vector) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[3] = { 5, 0, 100 }; + size_t size2 = 3; + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + MathVector result; + result = vector1 - vector2; + // Assert + EXPECT_EQ(result[0], vector1[0] - vector2[0]); + EXPECT_EQ(result[1], vector1[1] - vector2[1]); + EXPECT_EQ(result[2], vector1[2] - vector2[2]); +} +TEST(TestMathVectorLib, mathvector_scalar_product) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[3] = { 5, 0, 100 }; + size_t size2 = 3; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + int result = 0; + int scalar_product = + vector1[0] * vector2[0] + + vector1[1] * vector2[1] + + vector1[2] * vector2[2]; + result = vector1 * vector2; + // Assert + EXPECT_EQ(result, scalar_product); +} From d66794621dcb5870124b3ad850f7e1dfc48b93fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 15:56:24 +0300 Subject: [PATCH 022/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20+,=20-=20=D0=B8=20*=20=D0=B2=20=D1=81=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D0=B5=20=D1=81=20=D0=B4=D0=B2=D1=83=D0=BC=D1=8F=20=D0=B2?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0=D0=BC=D0=B8=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=D0=BD=D0=BE=D0=B9=20=D1=80=D0=B0=D1=81=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D1=80=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20(=D1=81=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B0=D0=B9=20=D1=81=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index 1c11fca7..61c67f0a 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -158,3 +158,45 @@ TEST(TestMathVectorLib, mathvector_scalar_product) { // Assert EXPECT_EQ(result, scalar_product); } +TEST(TestMathVectorLib, mathvector_add_for_vectors_with_different_dimension_exception) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[5] = { 5, 100, 1, 22, 7 }; + size_t size2 = 5; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + + // Assert + EXPECT_THROW(vector1 + vector2, std::logic_error); +} +TEST(TestMathVectorLib, mathvector_sub_for_vectors_with_different_dimension_exception) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[5] = { 5, 100, 1, 22, 7 }; + size_t size2 = 5; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + + // Assert + EXPECT_THROW(vector1 - vector2, std::logic_error); +} +TEST(TestMathVectorLib, mathvector_scalar_product_for_vectors_with_different_dimension_exception) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[5] = { 5, 100, 1, 22, 7 }; + size_t size2 = 5; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + + // Assert + EXPECT_THROW(vector1 * vector2, std::logic_error); +} From 47e33b9aaf8f3d96368d110f09fc60054037a3be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 27 Sep 2025 16:02:30 +0300 Subject: [PATCH 023/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20+=3D,=20-=3D=20=D0=B2=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5?= =?UTF-8?q?=20=D1=81=20=D0=B4=D0=B2=D1=83=D0=BC=D1=8F=20=D0=B2=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B0=D0=BC=D0=B8=20(=D0=B2=20=D1=82=D0=BE?= =?UTF-8?q?=D0=BC=20=D1=87=D0=B8=D1=81=D0=BB=D0=B5,=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B8=20=D1=81=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=D0=BC=D0=B8,?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20=D1=83=20=D0=B2=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B2=20=D1=80=D0=B0=D0=B7=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index 61c67f0a..9a234df9 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -200,3 +200,63 @@ TEST(TestMathVectorLib, mathvector_scalar_product_for_vectors_with_different_dim // Assert EXPECT_THROW(vector1 * vector2, std::logic_error); } +TEST(TestMathVectorLib, mathvector_add_assign_vector) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[3] = { 5, 0, 100 }; + size_t size2 = 3; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + vector1 += vector2; + // Assert + EXPECT_EQ(vector1[0], data1[0] + data2[0]); + EXPECT_EQ(vector1[1], data1[1] + data2[1]); + EXPECT_EQ(vector1[2], data1[2] + data2[2]); +} +TEST(TestMathVectorLib, mathvector_sub_assign_vector) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[3] = { 5, 0, 100 }; + size_t size2 = 3; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + vector1 -= vector2; + // Assert + EXPECT_EQ(vector1[0], data1[0] - data2[0]); + EXPECT_EQ(vector1[1], data1[1] - data2[1]); + EXPECT_EQ(vector1[2], data1[2] - data2[2]); +} +TEST(TestMathVectorLib, mathvector_add_assign_for_vectors_with_different_dimension_exception) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[5] = { 5, 100, 1, 22, 7 }; + size_t size2 = 5; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + + // Assert + EXPECT_THROW(vector1 += vector2, std::logic_error); +} +TEST(TestMathVectorLib, mathvector_sub_assign_for_vectors_with_different_dimension_exception) { + // Arrange & Act + int data1[3] = { 4, 7 ,28 }; + size_t size1 = 3; + + int data2[5] = { 5, 100, 1, 22, 7 }; + size_t size2 = 5; + + MathVector vector1(data1, size1); + MathVector vector2(data2, size2); + + // Assert + EXPECT_THROW(vector1 -= vector2, std::logic_error); +} From 095a0a9f66a663b3d068f1df2e4a60ba691d7677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 09:33:30 +0300 Subject: [PATCH 024/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80=D1=8B?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 501d93f5..b3fe84f8 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -53,7 +53,12 @@ class Matrix : public MathVector> { } MathVector& operator[](size_t index); const MathVector& operator[](size_t index) const; - + size_t getM() const { + return M; + } + size_t getN() const { + return N; + } }; template From 8e18a1be72a4fc9e68852cdc38b9fe6d728b4984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 09:41:30 +0300 Subject: [PATCH 025/275] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=B4=D1=83=D0=B1=D0=BB=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4=D0=B0,=20=D1=81=D0=B2=D1=8F?= =?UTF-8?q?=D0=B0=D0=B7=D0=BD=D0=BD=D0=BE=D0=B5=20=D1=81=20=D0=B3=D0=B5?= =?UTF-8?q?=D1=82=D1=82=D0=B5=D1=80=D0=B0=D0=BC=D0=B8...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index b3fe84f8..d1e4bbaf 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -11,12 +11,8 @@ class Matrix : public MathVector> { Matrix(T* data, size_t M, size_t N); Matrix(const Matrix& other); virtual ~Matrix(); - size_t getM() const { - return _M; - } - size_t getN() const { - return _N; - } + size_t getM() const; + size_t getN() const; Matrix operator + (T value) const; Matrix operator - (T value) const; Matrix operator * (T value) const; @@ -41,7 +37,6 @@ class Matrix : public MathVector> { Matrix& operator += (const Matrix& other_matrix); Matrix& operator -= (const Matrix& other_matrix); Matrix& operator *= (const Matrix& other_matrix); - friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { for (size_t i = 0; i < matrix.getM(); ++i) { for (size_t j = 0; j < matrix.getN(); ++j) { @@ -53,15 +48,16 @@ class Matrix : public MathVector> { } MathVector& operator[](size_t index); const MathVector& operator[](size_t index) const; - size_t getM() const { - return M; - } - size_t getN() const { - return N; - } - }; template +size_t Matrix::getM() const { + return _M; +} +template +size_t Matrix::getN() const { + return _N; +} +template Matrix::Matrix() : MathVector>(), _M(0), _N(0) {} template Matrix::Matrix(size_t M, size_t N) : MathVector>(M) { From 89df2fbd179d1094689935f611d560ba8aa7897b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 09:47:16 +0300 Subject: [PATCH 026/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=81=D0=B8=D0=BD=D1=82=D0=B0=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B8=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index d1e4bbaf..d0bfc723 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -63,16 +63,16 @@ template Matrix::Matrix(size_t M, size_t N) : MathVector>(M) { _M = M; _N = N; - for (size_t i = 0; i < M; ++i) { - (*this)[i] = MathVector(N); + for (size_t i = 0; i < _M; ++i) { + (*this)[i] = MathVector(_N); } } template Matrix::Matrix(T* data, size_t M, size_t N) : MathVector>(M) { _M = M; _N = N; - for (size_t i = 0; i < M; ++i) { - (*this)[i] = MathVector(data + i * N, N); + for (size_t i = 0; i < _M; ++i) { + (*this)[i] = MathVector(data + i * _N, _N); } } template From eb1a89e5d6fff20c995ef201dd56a735c8ec01ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 10:20:27 +0300 Subject: [PATCH 027/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_matrix.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/test_matrix.cpp diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp new file mode 100644 index 00000000..761be5c1 --- /dev/null +++ b/tests/test_matrix.cpp @@ -0,0 +1,57 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_matrix/matrix.h" +TEST(TestMatrixLib, matrix_default_constructor) { + // Arrange & Act + Matrix matrix; + // Assert + EXPECT_EQ(0, matrix.getM()); + EXPECT_EQ(0, matrix.getN()); + EXPECT_TRUE(matrix.is_empty()); +} +TEST(TestMatrixLib, matrix_constructor_with_size) { + // Arrange & Act + size_t M = 100; + size_t N = 100; + Matrix matrix(M, N); + // Assert + EXPECT_EQ(M, matrix.getM()); + EXPECT_EQ(N, matrix.getN()); +} +TEST(TestMatrixLib, matrix_constructor_with_array_and_size) { + // Arrange & Act + size_t M = 2; + size_t N = 2; + int data[4] = { 1, 2, 3, 4 }; + Matrix matrix(data, M, N); + // Assert + EXPECT_EQ(M, matrix.getM()); + EXPECT_EQ(N, matrix.getN()); + EXPECT_EQ(matrix[0][0], data[0]); + EXPECT_EQ(matrix[0][1], data[1]); + EXPECT_EQ(matrix[1][0], data[2]); + EXPECT_EQ(matrix[1][1], data[3]); +} +TEST(TestMatrixLib, matrix_copy_constructor) { + // Arrange & Act + size_t M = 2; + size_t N = 2; + int data[4] = { 1, 2, 3, 4}; + Matrix old_matrix(data, M, N); + Matrix new_matrix(old_matrix); + // Assert + EXPECT_EQ(old_matrix, new_matrix); +} +TEST(TestMatrixLib, matrix_assignment_operator) { + // Arrange + size_t M = 2, N = 2; + int data1[4] = { 1, 2, 3, 4 }; + int data2[4] = { 5, 6, 7, 8 }; + Matrix matrix1(data1, M, N); + Matrix matrix2(data2, M, N); + // Act + matrix1 = matrix2; + // Assert + EXPECT_EQ(matrix1, matrix2); +} From 598097c3435fe00479f2431a5fec3ceb039fca1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 11:04:22 +0300 Subject: [PATCH 028/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20+,=20-,=20*=20=D0=B8=20/=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D1=8B=20=D0=B8=20=D1=81?= =?UTF-8?q?=D0=BA=D0=B0=D0=BB=D1=8F=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index d0bfc723..6b58647a 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -1,4 +1,5 @@ #pragma once +#include #include "../lib_math_vector/math_vector.h" template class Matrix : public MathVector> { @@ -13,6 +14,7 @@ class Matrix : public MathVector> { virtual ~Matrix(); size_t getM() const; size_t getN() const; + Matrix operator + (T value) const; Matrix operator - (T value) const; Matrix operator * (T value) const; @@ -57,6 +59,7 @@ template size_t Matrix::getN() const { return _N; } + template Matrix::Matrix() : MathVector>(), _M(0), _N(0) {} template @@ -83,22 +86,42 @@ Matrix::~Matrix() = default; template Matrix Matrix::operator + (T value) const { - Matrix matrix; + Matrix matrix(_M, _N); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + matrix[i][j] = (*this)[i][j] + value + } + } return matrix; } template Matrix Matrix::operator - (T value) const { - Matrix matrix; + Matrix matrix(_M, _N); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + matrix[i][j] = (*this)[i][j] - value + } + } return matrix; } template Matrix Matrix::operator * (T value) const { - Matrix matrix; + Matrix matrix(_M, _N); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + matrix[i][j] = (*this)[i][j] * value + } + } return matrix; } template Matrix Matrix::operator / (T value) const { - Matrix matrix; + Matrix matrix(_M, _N); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + matrix[i][j] = (*this)[i][j] / value + } + } return matrix; } From a7c4ebdd10776101368ee2b9050e65d70515d1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 11:11:49 +0300 Subject: [PATCH 029/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20+=3D,=20-=3D,=20*=3D,=20/=3D=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D1=8B=20=D0=B8=20?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=D0=BB=D1=8F=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 6b58647a..37814eb1 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -89,7 +89,7 @@ Matrix Matrix::operator + (T value) const { Matrix matrix(_M, _N); for (size_t i = 0; i < _M; ++i) { for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] + value + matrix[i][j] = (*this)[i][j] + value; } } return matrix; @@ -99,7 +99,7 @@ Matrix Matrix::operator - (T value) const { Matrix matrix(_M, _N); for (size_t i = 0; i < _M; ++i) { for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] - value + matrix[i][j] = (*this)[i][j] - value; } } return matrix; @@ -109,40 +109,63 @@ Matrix Matrix::operator * (T value) const { Matrix matrix(_M, _N); for (size_t i = 0; i < _M; ++i) { for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] * value + matrix[i][j] = (*this)[i][j] * value; } } return matrix; } template Matrix Matrix::operator / (T value) const { + if (value == 0) { + throw std::logic_error("Division by zero!"); + } Matrix matrix(_M, _N); for (size_t i = 0; i < _M; ++i) { for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] / value + matrix[i][j] = (*this)[i][j] / value; } } return matrix; } - template Matrix& Matrix::operator += (T value) { + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + (*this)[i][j] = (*this)[i][j] + value; + } + } return *this; } template Matrix& Matrix::operator -= (T value) { + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + (*this)[i][j] = (*this)[i][j] - value; + } + } return *this; - } template Matrix& Matrix::operator *= (T value) { + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + (*this)[i][j] = (*this)[i][j] * value; + } + } return *this; } template Matrix& Matrix::operator /= (T value) { + if (value == 0) { + throw std::logic_error("Division by zero!"); + } + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + (*this)[i][j] = (*this)[i][j] / value; + } + } return *this; } - template Matrix Matrix::operator + (const MathVector& vector) const { Matrix matrix; From ef9a7540ace4dd7305715936815e98ebd81b0777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 12:06:27 +0300 Subject: [PATCH 030/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20+=20=D0=B8=20-=20=D0=B4=D0=BB=D1=8F=20=D0=B4?= =?UTF-8?q?=D0=B2=D1=83=D1=85=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 56 ++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 37814eb1..73330af3 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -25,13 +25,6 @@ class Matrix : public MathVector> { Matrix& operator *= (T value); Matrix& operator /= (T value); - Matrix operator + (const MathVector& vector) const; - Matrix operator - (const MathVector& vector) const; - MathVector operator * (const MathVector& vector) const; - - Matrix& operator += (const MathVector& vector); - Matrix& operator -= (const MathVector& vector); - Matrix operator + (const Matrix& other_matrix) const; Matrix operator - (const Matrix& other_matrix) const; Matrix operator * (const Matrix& other_matrix) const; @@ -166,40 +159,31 @@ Matrix& Matrix::operator /= (T value) { } return *this; } -template -Matrix Matrix::operator + (const MathVector& vector) const { - Matrix matrix; - return matrix; -} -template -Matrix Matrix::operator - (const MathVector& vector) const { - Matrix matrix; - return matrix; -} -template -MathVector Matrix::operator * (const MathVector& vector) const { - MathVector math_vector; - return math_vector; -} - -template -Matrix& Matrix::operator += (const MathVector& vector) { - return *this; -} -template -Matrix& Matrix::operator -= (const MathVector& vector) { - return *this; -} - template Matrix Matrix::operator + (const Matrix& other_matrix) const { - Matrix matrix; - return matrix; + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + Matrix result(_M, _N); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + result[i][j] = (*this)[i][j] + other_matrix[i][j]; + } + } + return result; } template Matrix Matrix::operator - (const Matrix& other_matrix) const { - Matrix matrix; - return matrix; + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + Matrix result(_M, _N); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + result[i][j] = (*this)[i][j] - other_matrix[i][j]; + } + } + return result; } template Matrix Matrix::operator * (const Matrix& other_matrix) const { From f4af57c5946045f8635636c88e38019371380186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 12:08:41 +0300 Subject: [PATCH 031/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20+=20=D0=B8=20-=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86=20(=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=87?= =?UTF-8?q?=D0=B8=D1=81=D0=BB=D0=B5,=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F,=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=D0=BC=D0=B5=D1=80=D1=8B=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=86=20=D1=80=D0=B0=D0=B7=D0=BB=D0=B8=D1=87=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B8=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_matrix.cpp | 200 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 761be5c1..1f4640b4 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -55,3 +55,203 @@ TEST(TestMatrixLib, matrix_assignment_operator) { // Assert EXPECT_EQ(matrix1, matrix2); } +TEST(TestMatrixLib, matrix_add_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 1, 2, 3, 4 }; + int value = 10; + Matrix matrix(data, M, N); + Matrix result(M, N); + // Act + result = matrix + value; + // Assert + EXPECT_EQ(result[0][0], data[0] + value); + EXPECT_EQ(result[0][1], data[1] + value); + EXPECT_EQ(result[1][0], data[2] + value); + EXPECT_EQ(result[1][1], data[3] + value); +} +TEST(TestMatrixLib, matrix_sub_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 1, 2, 3, 4 }; + int value = 10; + Matrix matrix(data, M, N); + Matrix result(M, N); + // Act + result = matrix - value; + // Assert + EXPECT_EQ(result[0][0], data[0] - value); + EXPECT_EQ(result[0][1], data[1] - value); + EXPECT_EQ(result[1][0], data[2] - value); + EXPECT_EQ(result[1][1], data[3] - value); +} +TEST(TestMatrixLib, matrix_mult_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 1, 2, 3, 4 }; + int value = 10; + Matrix matrix(data, M, N); + Matrix result(M, N); + // Act + result = matrix * value; + // Assert + EXPECT_EQ(result[0][0], data[0] * value); + EXPECT_EQ(result[0][1], data[1] * value); + EXPECT_EQ(result[1][0], data[2] * value); + EXPECT_EQ(result[1][1], data[3] * value); +} +TEST(TestMatrixLib, matrix_div_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 2, 2, 10, 4 }; + int value = 2; + Matrix matrix(data, M, N); + Matrix result(M, N); + // Act + result = matrix / value; + // Assert + EXPECT_EQ(result[0][0], data[0] / value); + EXPECT_EQ(result[0][1], data[1] / value); + EXPECT_EQ(result[1][0], data[2] / value); + EXPECT_EQ(result[1][1], data[3] / value); +} +TEST(TestMatrixLib, matrix_div_value_with_exception) { + // Arrange & Act + size_t M = 2, N = 2; + int data[4] = { 1, 2, 3, 4 }; + int value = 0; + Matrix matrix(data, M, N); + // Assert + EXPECT_THROW(matrix / value;, std::logic_error); +} +TEST(TestMatrixLib, matrix_add_assign_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 2, 2, 10, 4 }; + int value = 2; + Matrix matrix(data, M, N); + // Act + matrix += value; + // Assert + EXPECT_EQ(matrix[0][0], data[0] + value); + EXPECT_EQ(matrix[0][1], data[1] + value); + EXPECT_EQ(matrix[1][0], data[2] + value); + EXPECT_EQ(matrix[1][1], data[3] + value); +} +TEST(TestMatrixLib, matrix_sub_assign_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 2, 2, 10, 4 }; + int value = 2; + Matrix matrix(data, M, N); + // Act + matrix -= value; + // Assert + EXPECT_EQ(matrix[0][0], data[0] - value); + EXPECT_EQ(matrix[0][1], data[1] - value); + EXPECT_EQ(matrix[1][0], data[2] - value); + EXPECT_EQ(matrix[1][1], data[3] - value); +} +TEST(TestMatrixLib, matrix_mult_assign_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 2, 2, 10, 4 }; + int value = 2; + Matrix matrix(data, M, N); + // Act + matrix *= value; + // Assert + EXPECT_EQ(matrix[0][0], data[0] * value); + EXPECT_EQ(matrix[0][1], data[1] * value); + EXPECT_EQ(matrix[1][0], data[2] * value); + EXPECT_EQ(matrix[1][1], data[3] * value); +} +TEST(TestMatrixLib, matrix_div_assign_value) { + // Arrange + size_t M = 2, N = 2; + int data[4] = { 2, 2, 10, 4 }; + int value = 2; + Matrix matrix(data, M, N); + // Act + matrix /= value; + // Assert + EXPECT_EQ(matrix[0][0], data[0] / value); + EXPECT_EQ(matrix[0][1], data[1] / value); + EXPECT_EQ(matrix[1][0], data[2] / value); + EXPECT_EQ(matrix[1][1], data[3] / value); +} +TEST(TestMatrixLib, matrix_div_assign_value_with_exception) { + // Arrange & Act + size_t M = 2, N = 2; + int data[4] = { 1, 2, 3, 4 }; + int value = 0; + Matrix matrix(data, M, N); + // Assert + EXPECT_THROW(matrix /= value; , std::logic_error); +} +TEST(TestMatrixLib, matrix_add_matrix) { + // Arrange + size_t M = 2, N = 3; + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[6] = { 8, 2, 3, 0, 5, 7}; + + Matrix matrix1(data1, M, N); + Matrix matrix2(data2, M, N); + Matrix result(M, N); + // Act + result = matrix1 + matrix2; + // Assert + EXPECT_EQ(result[0][0], data1[0] + data2[0]); + EXPECT_EQ(result[0][1], data1[1] + data2[1]); + EXPECT_EQ(result[0][2], data1[2] + data2[2]); + EXPECT_EQ(result[1][0], data1[3] + data2[3]); + EXPECT_EQ(result[1][1], data1[4] + data2[4]); + EXPECT_EQ(result[1][2], data1[5] + data2[5]); +} +TEST(TestMatrixLib, matrix_sub_matrix) { + // Arrange + size_t M = 2, N = 3; + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[6] = { 8, 2, 3, 0, 5, 7 }; + + Matrix matrix1(data1, M, N); + Matrix matrix2(data2, M, N); + Matrix result(M, N); + // Act + result = matrix1 - matrix2; + // Assert + EXPECT_EQ(result[0][0], data1[0] - data2[0]); + EXPECT_EQ(result[0][1], data1[1] - data2[1]); + EXPECT_EQ(result[0][2], data1[2] - data2[2]); + EXPECT_EQ(result[1][0], data1[3] - data2[3]); + EXPECT_EQ(result[1][1], data1[4] - data2[4]); + EXPECT_EQ(result[1][2], data1[5] - data2[5]); +} +TEST(TestMatrixLib, matrix_add_matrix_with_exception) { + // Arrange & Act + size_t M1 = 2, N1 = 3; + size_t M2 = 2, N2 = 2; + + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[4] = { 8, 2, 3, 0 }; + + Matrix matrix1(data1, M1, N1); + Matrix matrix2(data2, M2, N2); + + // Assert + EXPECT_THROW(matrix1 + matrix2, std::logic_error); +} +TEST(TestMatrixLib, matrix_sub_matrix_with_exception) { + // Arrange & Act + size_t M1 = 2, N1 = 3; + size_t M2 = 2, N2 = 2; + + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[4] = { 8, 2, 3, 0 }; + + Matrix matrix1(data1, M1, N1); + Matrix matrix2(data2, M2, N2); + + // Assert + EXPECT_THROW(matrix1 - matrix2, std::logic_error); +} From 373d57f20ee1038aa39359a0ece78d793125d7ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 12:44:31 +0300 Subject: [PATCH 032/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20+=3D=20=D0=B8=20-=3D=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=B2=D1=83=D1=85=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 73330af3..a8eb107e 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -190,13 +190,28 @@ Matrix Matrix::operator * (const Matrix& other_matrix) const { Matrix matrix; return matrix; } - template Matrix& Matrix::operator += (const Matrix& other_matrix) { + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + (*this)[i][j] += other_matrix[i][j]; + } + } return *this; } template Matrix& Matrix::operator -= (const Matrix& other_matrix) { + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + (*this)[i][j] -= other_matrix[i][j]; + } + } return *this; } template From 1b3ded63e37e7ad2c3ee98c678c897a775ecfcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 29 Sep 2025 12:46:02 +0300 Subject: [PATCH 033/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20+=3D,=20-=3D=20(=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=87=D0=B8?= =?UTF-8?q?=D1=81=D0=BB=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B8,=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B3=D0=B4=D0=B0=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D1=8B?= =?UTF-8?q?=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=20=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=BE=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=B8=D0=BC=D1=8B=20=D0=B8?= =?UTF-8?q?=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=B0=20(?= =?UTF-8?q?=D0=BE=D0=B6=D0=B8=D0=B4=D0=B4=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5)?= =?UTF-8?q?=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_matrix.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 1f4640b4..0d7ae7f3 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -255,3 +255,61 @@ TEST(TestMatrixLib, matrix_sub_matrix_with_exception) { // Assert EXPECT_THROW(matrix1 - matrix2, std::logic_error); } +TEST(TestMatrixLib, matrix_add_assign_matrix) { + // Arrange + size_t M = 2, N = 3; + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[6] = { 8, 2, 3, 0, 5, 7 }; + + Matrix matrix1(data1, M, N); + Matrix matrix2(data2, M, N); + // Act + matrix1 += matrix2; + // Assert + EXPECT_EQ(matrix1[0][0], data1[0] + data2[0]); + EXPECT_EQ(matrix1[0][1], data1[1] + data2[1]); + EXPECT_EQ(matrix1[0][2], data1[2] + data2[2]); + EXPECT_EQ(matrix1[1][0], data1[3] + data2[3]); + EXPECT_EQ(matrix1[1][1], data1[4] + data2[4]); + EXPECT_EQ(matrix1[1][2], data1[5] + data2[5]); +} +TEST(TestMatrixLib, matrix_add_assign_matrix_with_exception) { + // Arrange & Act + size_t M1 = 2, N1 = 3; + size_t M2 = 2, N2 = 2; + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[4] = { 8, 2, 3, 0 }; + Matrix matrix1(data1, M1, N1); + Matrix matrix2(data2, M2, N2); + // Assert + EXPECT_THROW(matrix1 += matrix2, std::logic_error); +} +TEST(TestMatrixLib, matrix_sub_assign_matrix) { + // Arrange + size_t M = 2, N = 3; + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[6] = { 8, 2, 3, 0, 5, 7 }; + + Matrix matrix1(data1, M, N); + Matrix matrix2(data2, M, N); + // Act + matrix1 -= matrix2; + // Assert + EXPECT_EQ(matrix1[0][0], data1[0] - data2[0]); + EXPECT_EQ(matrix1[0][1], data1[1] - data2[1]); + EXPECT_EQ(matrix1[0][2], data1[2] - data2[2]); + EXPECT_EQ(matrix1[1][0], data1[3] - data2[3]); + EXPECT_EQ(matrix1[1][1], data1[4] - data2[4]); + EXPECT_EQ(matrix1[1][2], data1[5] - data2[5]); +} +TEST(TestMatrixLib, matrix_sub_assign_matrix_with_exception) { + // Arrange & Act + size_t M1 = 2, N1 = 3; + size_t M2 = 2, N2 = 2; + int data1[6] = { 2, 2, 10, 4, 5, 7 }; + int data2[4] = { 8, 2, 3, 0 }; + Matrix matrix1(data1, M1, N1); + Matrix matrix2(data2, M2, N2); + // Assert + EXPECT_THROW(matrix1 -= matrix2, std::logic_error); +} From 950db46804c7c2e08364b6d982fb06906e045734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 18:55:37 +0300 Subject: [PATCH 034/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20*=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=86=20=D0=98=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=82=D1=80=D0=B0=D0=BD=D1=81=D0=BF=D0=BE=D0=BD=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index a8eb107e..7eed3722 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -6,6 +6,15 @@ class Matrix : public MathVector> { protected: size_t _M; size_t _N; + Matrix transpose() const { + Matrix result(_N, _M); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + result[j][i] = (*this)[i][j]; + } + } + return result; + } public: Matrix(); Matrix(size_t M, size_t N); @@ -29,9 +38,10 @@ class Matrix : public MathVector> { Matrix operator - (const Matrix& other_matrix) const; Matrix operator * (const Matrix& other_matrix) const; + Matrix operator * (const MathVector& vector) const; + Matrix& operator += (const Matrix& other_matrix); Matrix& operator -= (const Matrix& other_matrix); - Matrix& operator *= (const Matrix& other_matrix); friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { for (size_t i = 0; i < matrix.getM(); ++i) { for (size_t j = 0; j < matrix.getN(); ++j) { @@ -187,9 +197,32 @@ Matrix Matrix::operator - (const Matrix& other_matrix) const { } template Matrix Matrix::operator * (const Matrix& other_matrix) const { - Matrix matrix; - return matrix; + if (_N != other_matrix.getM()) { + throw std::logic_error("The sizes of the matrices are not compatible for this operation!"); + } + Matrix result (_M, other_matrix.getN()); + Matrix matrix_t = other_matrix.transpose(); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < matrix_t.getM(); ++j) { + result[i][j] = (*this)[i] * matrix_t[j]; + } + } + return result; } +template +Matrix Matrix::operator * (const MathVector& vector) const { + if (_N != vector.get_size()) { + + } +} + + + + + + + + template Matrix& Matrix::operator += (const Matrix& other_matrix) { if (_M != other_matrix.getM() || _N != other_matrix.getN()) { @@ -215,10 +248,6 @@ Matrix& Matrix::operator -= (const Matrix& other_matrix) { return *this; } template -Matrix& Matrix::operator *= (const Matrix& other_matrix) { - return *this; -} -template MathVector& Matrix::operator[](size_t index) { return MathVector>::operator[](index); } From 078ecc41fc0dc4a62bd36a6fa249c8c132985391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 18:56:42 +0300 Subject: [PATCH 035/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20*?= =?UTF-8?q?=20(=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=87=D0=B8=D1=81=D0=BB?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=B0=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B9=20?= =?UTF-8?q?=D1=81=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=D0=BC=20-=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D1=8B?= =?UTF-8?q?=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=20=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=BE=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=B8=D0=BC=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=83=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_matrix.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 0d7ae7f3..bf188359 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -313,3 +313,33 @@ TEST(TestMatrixLib, matrix_sub_assign_matrix_with_exception) { // Assert EXPECT_THROW(matrix1 -= matrix2, std::logic_error); } +TEST(TestMatrixLib, matrix_mult_matrix) { + // Arrange + size_t M1 = 2, N1 = 3, M2 = 3, N2 = 2; + + int data1[6] = { 2, 2, 1, 2, 1, 2 }; + int data2[6] = { 3, 2, 2, 1, 3, 2 }; + + Matrix matrix1(data1, M1, N1); + Matrix matrix2(data2, M2, N2); + Matrix result(M1, N2); + // Act + result = matrix1 * matrix2; + // Assert + EXPECT_EQ(result[0][0], 13); + EXPECT_EQ(result[0][1], 8); + EXPECT_EQ(result[1][0], 14); + EXPECT_EQ(result[1][1], 9); +} +TEST(TestMatrixLib, matrix_mult_matrix_with_throw) { + //Arrange & Act + size_t M1 = 2, N1 = 3, M2 = 2, N2 = 2; + + int data1[6] = { 2, 2, 1, 2, 1, 2 }; + int data2[6] = { 3, 2, 2, 1 }; + + Matrix matrix1(data1, M1, N1); + Matrix matrix2(data2, M2, N2); + //Assert + EXPECT_THROW(matrix1 * matrix2, std::logic_error); +} From 36ee6e5f7fb3886c3d4ff79c825fd41f66d3370f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 19:51:50 +0300 Subject: [PATCH 036/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 7eed3722..78f155c3 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -53,6 +53,7 @@ class Matrix : public MathVector> { } MathVector& operator[](size_t index); const MathVector& operator[](size_t index) const; + Matrix& operator=(const Matrix& other); }; template size_t Matrix::getM() const { @@ -198,7 +199,8 @@ Matrix Matrix::operator - (const Matrix& other_matrix) const { template Matrix Matrix::operator * (const Matrix& other_matrix) const { if (_N != other_matrix.getM()) { - throw std::logic_error("The sizes of the matrices are not compatible for this operation!"); + throw std::logic_error + ("The sizes of the matrices are not compatible for this operation!"); } Matrix result (_M, other_matrix.getN()); Matrix matrix_t = other_matrix.transpose(); @@ -212,17 +214,16 @@ Matrix Matrix::operator * (const Matrix& other_matrix) const { template Matrix Matrix::operator * (const MathVector& vector) const { if (_N != vector.get_size()) { + throw std::logic_error + ("Size of matrix aren't compatible with vector's size for this operation!"); + } + MathVector result(_M); + for (size_t i = 0; i < _M; ++i) { + result[i] = (*this)[i] * vector; } + return result; } - - - - - - - - template Matrix& Matrix::operator += (const Matrix& other_matrix) { if (_M != other_matrix.getM() || _N != other_matrix.getN()) { @@ -255,3 +256,10 @@ template const MathVector& Matrix::operator[](size_t index) const { return MathVector>::operator[](index); } +template +Matrix& Matrix::operator=(const Matrix& other) { + MathVector>::operator=(other); + _M = other._M; + _N = other._N; + return *this; +} From 1f8c6914ae0007f40d57e314c5f149ce1307b0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 19:53:02 +0300 Subject: [PATCH 037/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 927b79e5..0fc35d21 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -21,6 +21,7 @@ class MathVector : public Tvector { MathVector& operator += (const MathVector& vector); MathVector& operator -= (const MathVector& vector); + MathVector& operator=(const MathVector& other); friend std::ostream& operator<<(std::ostream& out, const MathVector& vector) { out << "["; for (size_t i = 0; i < vector.get_size(); ++i) { @@ -132,3 +133,10 @@ MathVector& MathVector::operator -= (const MathVector& vector) { } return *this; } +template +MathVector& MathVector::operator=(const MathVector& other) { + if (this != &other) { + Tvector::operator=(other); + } + return *this; +} From e02c7d2235623f1ee2a15b743d36590829a1c51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 20:16:06 +0300 Subject: [PATCH 038/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D1=81=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index 9a234df9..920f2a7c 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -260,3 +260,17 @@ TEST(TestMathVectorLib, mathvector_sub_assign_for_vectors_with_different_dimensi // Assert EXPECT_THROW(vector1 -= vector2, std::logic_error); } +TEST(TestMathVectorLib, mathvector_simple_assignment) { + // Arrange + MathVector vector1(2); + MathVector vector2(2); + vector1[0] = 1; + vector1[1] = 2; + vector2[0] = 3; + vector2[1] = 4; + //Act + vector1 = vector2; + // Assert + EXPECT_EQ(vector1[0], 3); + EXPECT_EQ(vector1[1], 4); +} From 187a5e83c4300c829520870eee39d67dc9604481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 20:17:54 +0300 Subject: [PATCH 039/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20*=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=86=D1=8B=20=D0=B8=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B0,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6=D0=B5=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D1=81=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 78f155c3..2d009273 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -16,6 +16,8 @@ class Matrix : public MathVector> { return result; } public: + using MathVector>::operator=; + Matrix(); Matrix(size_t M, size_t N); Matrix(T* data, size_t M, size_t N); @@ -38,7 +40,7 @@ class Matrix : public MathVector> { Matrix operator - (const Matrix& other_matrix) const; Matrix operator * (const Matrix& other_matrix) const; - Matrix operator * (const MathVector& vector) const; + MathVector operator * (const MathVector& vector) const; Matrix& operator += (const Matrix& other_matrix); Matrix& operator -= (const Matrix& other_matrix); @@ -212,7 +214,7 @@ Matrix Matrix::operator * (const Matrix& other_matrix) const { return result; } template -Matrix Matrix::operator * (const MathVector& vector) const { +MathVector Matrix::operator * (const MathVector& vector) const { if (_N != vector.get_size()) { throw std::logic_error ("Size of matrix aren't compatible with vector's size for this operation!"); From 7d5d91b2def609a7335d18cef66805ee898ffb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 20:19:43 +0300 Subject: [PATCH 040/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20*?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86?= =?UTF-8?q?=D1=8B=20=D0=B8=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0=20?= =?UTF-8?q?(+=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B9=20=D1=81=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC),?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=BE=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2?= =?UTF-8?q?=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_matrix.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index bf188359..a0482516 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -343,3 +343,47 @@ TEST(TestMatrixLib, matrix_mult_matrix_with_throw) { //Assert EXPECT_THROW(matrix1 * matrix2, std::logic_error); } +TEST(TestMatrixLib, matrix_mult_vector) { + // Arrange + size_t M = 2, N = 3, size = 3; + int data1[6] = { 2, 3, 1, 4, 1, 2 }; + int data2[3] = { 2, 2, 2 }; + Matrix matrix(data1, M, N); + MathVector vector(data2, size); + MathVector result(M); + // Act + result = matrix * vector; + // Assert + EXPECT_EQ(result[0], 12); + EXPECT_EQ(result[1], 14); +} +TEST(TestMatrixLib, matrix_mult_vector_with_exception) { + // Arrange & Act + size_t M = 2, N = 3, size = 5; + int data1[6] = { 2, 3, 1, 4, 1, 2 }; + int data2[5] = { 2, 2, 2, 6, 7 }; + Matrix matrix(data1, M, N); + MathVector vector(data2, size); + // Assert + EXPECT_THROW(matrix * vector, std::logic_error); +} +TEST(TestMatrixLib, matrix_simple_assignment) { + // Arrange + Matrix matrix1(2, 2); + Matrix matrix2(2, 2); + matrix1[0][0] = 1; + matrix1[0][1] = 2; + matrix1[1][0] = 3; + matrix1[1][1] = 4; + matrix2[0][0] = 5; + matrix2[0][1] = 6; + matrix2[1][0] = 7; + matrix2[1][1] = 8; + //Act + matrix1 = matrix2; + // Проверяем + EXPECT_EQ(matrix1[0][0], 5); + EXPECT_EQ(matrix1[0][1], 6); + EXPECT_EQ(matrix1[1][0], 7); + EXPECT_EQ(matrix1[1][1], 8); +} From a5333b3880c86f2e8e8c21ff6d9fa2ebb1407894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 30 Sep 2025 20:38:11 +0300 Subject: [PATCH 041/275] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D0=B8=D0=B8:=20=D1=81?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=B2?= =?UTF-8?q?=D1=8B=D1=87=D0=B8=D1=82=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B2=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D0=B0=20=D0=B8=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86=D1=8B=20(=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D1=87=D0=BD=D1=8B=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index e65391ee..bc6d33d8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -143,10 +143,8 @@ void printMainMenu() { std::cout << " 5. Subtract scalar from matrix\n"; std::cout << " 6. Multiply matrix by scalar\n"; std::cout << " 7. Division by scalar\n"; - std::cout << " 8. Add vector to matrix\n"; - std::cout << " 9. Subtract vector from matrix\n"; - std::cout << " 10. Multiply matrix by vector\n"; - std::cout << " 11. Exit\n"; + std::cout << " 8. Multiply matrix by vector\n"; + std::cout << " 9. Exit\n"; } int main() { @@ -268,32 +266,12 @@ int main() { std::cout << "Enter vector elements:\n"; system("pause"); - Matrix result = matrix1 + mathvector; + MathVector result = matrix1 * mathvector; std::cout << "Result:\n" << result; break; } case 9: { - size_t size; - std::cout << "Enter vector size:\n"; - std::cin >> size; - MathVector mathvector(size); - std::cout << "Enter vector elements:\n"; - system("pause"); - Matrix result = matrix1 - mathvector; - std::cout << "Result:\n" << result; - break; - } - case 10: { - size_t size; - std::cout << "Enter vector size:\n"; - std::cin >> size; - MathVector mathvector(size); - - std::cout << "Enter vector elements:\n"; - system("pause"); - MathVector result = matrix1 * mathvector; - std::cout << "Result:\n" << result; break; } default: { From db6f76f4392f7a83e52ed3f5dd38857259f07c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 1 Oct 2025 10:00:53 +0300 Subject: [PATCH 042/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6=D0=B5=20?= =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20=D0=BE=D0=B1=D1=8A=D1=8F?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=BE=D0=B2,=20=D0=BA=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=D0=B5=20=D0=BD=D0=B5=20=D1=80=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D1=83=D0=B5=D0=BC=D1=8B=20=D0=B2=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=82=D0=B5=D0=BA=D1=81=D1=82=D0=B5=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D1=80=D0=B8=D1=86,=20=D0=B2=20=D1=82=D0=BE=D0=BC=20?= =?UTF-8?q?=D1=87=D0=B8=D1=81=D0=BB=D0=B5=20=D0=B8=20=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D1=83=D0=B3=D0=BE=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20(=D0=B1?= =?UTF-8?q?=D1=8B=D0=BB=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BF=D0=BE=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 33 +++++---------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 452dd223..f7bb6fbe 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -19,20 +19,14 @@ class TriangleMatrix : public Matrix { TriangleMatrix& operator *= (T value); TriangleMatrix& operator /= (T value); - TriangleMatrix operator + (const MathVector& vector) const; - TriangleMatrix operator - (const MathVector& vector) const; MathVector operator * (const MathVector& vector) const; - TriangleMatrix& operator += (const MathVector& vector); - TriangleMatrix& operator -= (const MathVector& vector); - TriangleMatrix operator + (const TriangleMatrix& other_matrix) const; TriangleMatrix operator - (const TriangleMatrix& other_matrix) const; TriangleMatrix operator * (const TriangleMatrix& other_matrix) const; TriangleMatrix& operator += (const TriangleMatrix& other_matrix); TriangleMatrix& operator -= (const TriangleMatrix& other_matrix); - TriangleMatrix& operator *= (const TriangleMatrix& other_matrix); friend std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { for (size_t i = 0; i < matrix.getM(); ++i) { @@ -43,6 +37,7 @@ class TriangleMatrix : public Matrix { } return out; } + TriangleMatrix& operator= (const TriangleMatrix& other); }; template TriangleMatrix::TriangleMatrix() : Matrix() {} @@ -50,7 +45,6 @@ template TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} template TriangleMatrix::~TriangleMatrix() = default; - template TriangleMatrix TriangleMatrix::operator + (T value) const { TriangleMatrix matrix; @@ -90,31 +84,12 @@ TriangleMatrix& TriangleMatrix::operator /= (T value) { return *this; } -template -TriangleMatrix TriangleMatrix::operator + (const MathVector& vector) const { - TriangleMatrix matrix; - return matrix; -} -template -TriangleMatrix TriangleMatrix::operator - (const MathVector& vector) const { - TriangleMatrix matrix; - return matrix; -} template MathVector TriangleMatrix::operator * (const MathVector& vector) const { TriangleMatrix matrix; return matrix; } -template -TriangleMatrix& TriangleMatrix::operator += (const MathVector& vector) { - return *this; -} -template -TriangleMatrix& TriangleMatrix::operator -= (const MathVector& vector) { - return *this; -} - template TriangleMatrix TriangleMatrix::operator + (const TriangleMatrix& other_matrix) const { TriangleMatrix matrix; @@ -139,7 +114,11 @@ template TriangleMatrix& TriangleMatrix::operator -= (const TriangleMatrix& other_matrix) { return *this; } + template -TriangleMatrix& TriangleMatrix::operator *= (const TriangleMatrix& other_matrix) { +TriangleMatrix& TriangleMatrix::operator= (const TriangleMatrix& other) { + Matrix::operator=(other); + _M = other._M; + _N = other._N; return *this; } From b800f7d23429d54aca9b8960c60a6498e6a36d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 11:10:18 +0300 Subject: [PATCH 043/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Tri?= =?UTF-8?q?angle=20Matrix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 124 ++++++++------------------ 1 file changed, 35 insertions(+), 89 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index f7bb6fbe..daf425c7 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -1,14 +1,18 @@ #pragma once #include "../lib_matrix/matrix.h" template -class TriangleMatrix : public Matrix { -public: +class TriangleMatrix: public Matrix { +public: TriangleMatrix(); - TriangleMatrix(size_t M, size_t N); - TriangleMatrix(T* data, size_t M, size_t N); + TriangleMatrix(size_t size); + TriangleMatrix(T* data, size_t size); TriangleMatrix(const TriangleMatrix& other); ~TriangleMatrix(); + size_t getSize() const { + return this->_M; + } + TriangleMatrix operator + (T value) const; TriangleMatrix operator - (T value) const; TriangleMatrix operator * (T value) const; @@ -21,104 +25,46 @@ class TriangleMatrix : public Matrix { MathVector operator * (const MathVector& vector) const; - TriangleMatrix operator + (const TriangleMatrix& other_matrix) const; - TriangleMatrix operator - (const TriangleMatrix& other_matrix) const; - TriangleMatrix operator * (const TriangleMatrix& other_matrix) const; + TriangleMatrix operator + (const TriangleMatrix& other) const; + TriangleMatrix operator - (const TriangleMatrix& other) const; + TriangleMatrix operator * (const TriangleMatrix& other) const; - TriangleMatrix& operator += (const TriangleMatrix& other_matrix); - TriangleMatrix& operator -= (const TriangleMatrix& other_matrix); + TriangleMatrix& operator += (const TriangleMatrix& other); + TriangleMatrix& operator -= (const TriangleMatrix& other); + TriangleMatrix& operator= (const TriangleMatrix& other); friend std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { - for (size_t i = 0; i < matrix.getM(); ++i) { - for (size_t j = 0; j < matrix.getN(); ++j) { - out << matrix[i][j] << "\t"; + for (size_t i = 0; i < matrix._M; ++i) { + for (size_t j = 0; j < matrix._N; ++j) { + if (i <= j) { + out << matrix[i][j] << "\t"; + } + else { + out << "0\t"; + } } out << std::endl; } return out; } - TriangleMatrix& operator= (const TriangleMatrix& other); }; -template -TriangleMatrix::TriangleMatrix() : Matrix() {} -template -TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} -template -TriangleMatrix::~TriangleMatrix() = default; -template -TriangleMatrix TriangleMatrix::operator + (T value) const { - TriangleMatrix matrix; - return matrix; -} -template -TriangleMatrix TriangleMatrix::operator - (T value) const { - TriangleMatrix matrix; - return matrix; -} -template -TriangleMatrix TriangleMatrix::operator * (T value) const { - TriangleMatrix matrix; - return matrix; -} -template -TriangleMatrix TriangleMatrix::operator / (T value) const { - TriangleMatrix matrix; - return matrix; -} - -template -TriangleMatrix& TriangleMatrix::operator += (T value) { - return *this; -} -template -TriangleMatrix& TriangleMatrix::operator -= (T value) { - return *this; - -} -template -TriangleMatrix& TriangleMatrix::operator *= (T value) { - return *this; -} -template -TriangleMatrix& TriangleMatrix::operator /= (T value) { - return *this; +template +TriangleMatrix::TriangleMatrix(): Matrix() {} +template +TriangleMatrix::TriangleMatrix(size_t N): MathVector>(N) { + for (size_t i = 0; i < N; ++i) { + + } } +template +TriangleMatrix::TriangleMatrix(T* data, size_t size) { -template -MathVector TriangleMatrix::operator * (const MathVector& vector) const { - TriangleMatrix matrix; - return matrix; } -template -TriangleMatrix TriangleMatrix::operator + (const TriangleMatrix& other_matrix) const { - TriangleMatrix matrix; - return matrix; -} -template -TriangleMatrix TriangleMatrix::operator - (const TriangleMatrix& other_matrix) const { - TriangleMatrix matrix; - return matrix; -} -template -TriangleMatrix TriangleMatrix::operator * (const TriangleMatrix& other_matrix) const { - TriangleMatrix matrix; - return matrix; -} +template +TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) { -template -TriangleMatrix& TriangleMatrix::operator += (const TriangleMatrix& other_matrix) { - return *this; -} -template -TriangleMatrix& TriangleMatrix::operator -= (const TriangleMatrix& other_matrix) { - return *this; } -template -TriangleMatrix& TriangleMatrix::operator= (const TriangleMatrix& other) { - Matrix::operator=(other); - _M = other._M; - _N = other._N; - return *this; -} +template +TriangleMatrix::~TriangleMatrix() = default; From 6892ddbd6ed2943bae218622fa5372755a1349cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 11:36:04 +0300 Subject: [PATCH 044/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=5Fstart=5Findex=20=D0=B4=D0=BB=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D1=83=D0=B3=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D1=80=D0=B8=D1=86=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 0fc35d21..2c14afe1 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -2,7 +2,9 @@ #include #include "../lib_vector/vector.h" template -class MathVector : public Tvector { +class MathVector:public Tvector { +protected: + size_t _start_index = 0; public: MathVector(); MathVector(size_t size); @@ -118,7 +120,7 @@ MathVector& MathVector::operator += (const MathVector& vector) { if (this->get_size() != vector.get_size()) { throw std::logic_error("Vectors must have the same dimension"); } - for (size_t i = 0; i < this->get_size(); ++i) { + for (size_t i = 0; i < this->get_size(); ++i) { (*this)[i] += vector[i]; } return *this; From be6bdd48676af9b2ba5bc37f6207a20138e0ab50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 11:42:14 +0300 Subject: [PATCH 045/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=5Fstart=5Findex=20=D0=B8=20=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BD=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D1=85=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=B2,=20=D1=83=D1=87=D0=B8=D1=82=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D1=8E=D1=89=D0=B8=D0=B5=20=D0=BD=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D1=87=D0=B8=D0=B5=20=D1=8D=D1=82=D0=BE=D0=B3=D0=BE=20=D0=B8?= =?UTF-8?q?=D0=BD=D0=B4=D0=B5=D0=BA=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 2c14afe1..f78449d0 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -8,9 +8,16 @@ class MathVector:public Tvector { public: MathVector(); MathVector(size_t size); + MathVector(size_t size, size_t start_index); MathVector(T* data, size_t size); + MathVector(T* data, size_t size, size_t start_index); MathVector(const MathVector& other); virtual ~MathVector(); + + size_t get_start_index() const { + return _start_index; + } + MathVector operator * (T value) const; MathVector operator / (T value) const; @@ -41,8 +48,13 @@ MathVector::MathVector() : Tvector() {} template MathVector::MathVector(size_t size) : Tvector(size) {} template +MathVector::MathVector(size_t size, size_t start_index) : Tvector(size), _start_index(start_index) {} +template MathVector::MathVector(T* data, size_t size) : Tvector(data, size) {} template +MathVector::MathVector(T* data, size_t size, size_t start_index) : + Tvector(data, size), _start_index(start_index) {} +template MathVector::MathVector(const MathVector& other) : Tvector(other) {} template MathVector::~MathVector() = default; From 893fc1e82628c015eb657fb83068bba437935334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 11:49:38 +0300 Subject: [PATCH 046/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=81=D0=B5=D1=82=D1=82=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=81=D1=82=D0=B0=D1=80=D1=82=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=B8=D0=BD=D0=B4=D0=B5=D0=BA=D1=81=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20[]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index f78449d0..0cf042d6 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -17,6 +17,9 @@ class MathVector:public Tvector { size_t get_start_index() const { return _start_index; } + void set_start_index(size_t index) { + _start_index = index; + } MathVector operator * (T value) const; MathVector operator / (T value) const; @@ -42,6 +45,8 @@ class MathVector:public Tvector { out << "]"; return out; } + T& operator[](size_t index); + const T& operator[](size_t index) const; }; template MathVector::MathVector() : Tvector() {} @@ -55,7 +60,8 @@ template MathVector::MathVector(T* data, size_t size, size_t start_index) : Tvector(data, size), _start_index(start_index) {} template -MathVector::MathVector(const MathVector& other) : Tvector(other) {} +MathVector::MathVector(const MathVector& other) : + Tvector(other), _start_index(other._start_index) {} template MathVector::~MathVector() = default; template @@ -154,3 +160,17 @@ MathVector& MathVector::operator=(const MathVector& other) { } return *this; } +template +T& MathVector::operator[](size_t index) { + if (index < _start_index || index >= _start_index + this->get_size()) { + throw std::out_of_range("Index out of range"); + } + return Tvector::operator[](index - _start_index); +} +template +const T& MathVector::operator[](size_t index) const { + if (index < _start_index || index >= _start_index + this->get_size()) { + throw std::out_of_range("Index out of range"); + } + return Tvector::operator[](index - _start_index); +} From ef5d9179bb55a006946078b4727661187a2e4161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 12:09:33 +0300 Subject: [PATCH 047/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index 0cf042d6..d36d94aa 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -46,7 +46,6 @@ class MathVector:public Tvector { return out; } T& operator[](size_t index); - const T& operator[](size_t index) const; }; template MathVector::MathVector() : Tvector() {} @@ -163,14 +162,7 @@ MathVector& MathVector::operator=(const MathVector& other) { template T& MathVector::operator[](size_t index) { if (index < _start_index || index >= _start_index + this->get_size()) { - throw std::out_of_range("Index out of range"); - } - return Tvector::operator[](index - _start_index); -} -template -const T& MathVector::operator[](size_t index) const { - if (index < _start_index || index >= _start_index + this->get_size()) { - throw std::out_of_range("Index out of range"); + throw std::logic_error("Index out of range"); } return Tvector::operator[](index - _start_index); } From 8f85ca52acfb3f0756d6e9982bdb02f7563f171a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 12:49:39 +0300 Subject: [PATCH 048/275] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20[]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 2d009273..6fbeaaf6 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -16,8 +16,6 @@ class Matrix : public MathVector> { return result; } public: - using MathVector>::operator=; - Matrix(); Matrix(size_t M, size_t N); Matrix(T* data, size_t M, size_t N); @@ -53,8 +51,7 @@ class Matrix : public MathVector> { } return out; } - MathVector& operator[](size_t index); - const MathVector& operator[](size_t index) const; + Matrix& operator=(const Matrix& other); }; template @@ -251,14 +248,6 @@ Matrix& Matrix::operator -= (const Matrix& other_matrix) { return *this; } template -MathVector& Matrix::operator[](size_t index) { - return MathVector>::operator[](index); -} -template -const MathVector& Matrix::operator[](size_t index) const { - return MathVector>::operator[](index); -} -template Matrix& Matrix::operator=(const Matrix& other) { MathVector>::operator=(other); _M = other._M; From afae7512c777b055090050403ea14d155945acfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 12:58:30 +0300 Subject: [PATCH 049/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8?= =?UTF-8?q?=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20[],=20?= =?UTF-8?q?=D1=81=20=D1=83=D1=87=D1=91=D1=82=D0=BE=D0=BC=20=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D1=82=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=B8=D0=BD?= =?UTF-8?q?=D0=B4=D0=B5=D0=BA=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index 920f2a7c..c6580006 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -16,6 +16,16 @@ TEST(TestMathVectorLib, mathvector_constructor_with_size) { EXPECT_LE(vector.get_size(), vector.get_capacity()); EXPECT_EQ(vector.get_size(), size); } +TEST(TestMathVectorLib, mathvector_constructor_with_size_and_start_index) { + // Arrange & Act + size_t size = 10; + size_t start_index = 1; + MathVector vector(size, start_index); + // Assert + EXPECT_LE(vector.get_size(), vector.get_capacity()); + EXPECT_EQ(vector.get_size(), size); + EXPECT_EQ(vector.get_start_index(), start_index); +} TEST(TestMathVectorLib, mathvector_constructor_with_array_and_size) { // Arrange & Act int data[3] = { 1,2,3 }; @@ -28,6 +38,20 @@ TEST(TestMathVectorLib, mathvector_constructor_with_array_and_size) { EXPECT_EQ(vector[2], data[2]); EXPECT_LE(vector.get_size(), vector.get_capacity()); } +TEST(TestMathVectorLib, mathvector_constructor_with_array_and_size_and_start_index) { + // Arrange & Act + int data[3] = { 1,2,3 }; + size_t size = 3; + size_t start_index = 1; + MathVector vector(data, size, start_index); + // Assert + EXPECT_EQ(vector.get_start_index(), start_index); + EXPECT_EQ(vector.get_size(), size); + EXPECT_EQ(vector[1], data[0]); + EXPECT_EQ(vector[2], data[1]); + EXPECT_EQ(vector[3], data[2]); + EXPECT_LE(vector.get_size(), vector.get_capacity()); +} TEST(TestMathVectorLib, mathvector_copy_constructor) { // Arrange & Act int data[3] = { 1,2,3 }; @@ -35,6 +59,7 @@ TEST(TestMathVectorLib, mathvector_copy_constructor) { MathVector old_vector(data, size); MathVector new_vector(old_vector); // Assert + EXPECT_EQ(old_vector.get_start_index(), new_vector.get_start_index()); EXPECT_EQ(old_vector, new_vector); } TEST(TestMathVectorLib, mathvector_mult_on_value) { @@ -274,3 +299,33 @@ TEST(TestMathVectorLib, mathvector_simple_assignment) { EXPECT_EQ(vector1[0], 3); EXPECT_EQ(vector1[1], 4); } +TEST(TestMathVectorLib, mathvector_operator_square_brackets) { + // Arrange + size_t size = 5; + size_t start_index = 1; + MathVector vector1(size, start_index); + vector1[1] = 2; + vector1[2] = 3; + vector1[3] = 4; + vector1[4] = 5; + vector1[5] = 6; + //Act & Assert + EXPECT_EQ(vector1[1], 2); + EXPECT_EQ(vector1[2], 3); + EXPECT_EQ(vector1[5], 6); +} +TEST(TestMathVectorLib, mathvector_operator_square_brackets_with_exception) { + // Arrange + size_t size = 5; + size_t start_index = 2; + MathVector vector1(size, start_index); + vector1[2] = 3; + vector1[3] = 4; + vector1[4] = 5; + vector1[5] = 6; + vector1[6] = 7; + //Act & Assert + EXPECT_THROW(vector1[0], std::logic_error); + EXPECT_THROW(vector1[1], std::logic_error); + EXPECT_THROW(vector1[7], std::logic_error); +} From 8ff2e54b666273a17016bf372b1b20737580c595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 4 Oct 2025 13:09:31 +0300 Subject: [PATCH 050/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 35 +++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index daf425c7..180166fe 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -1,7 +1,8 @@ #pragma once #include "../lib_matrix/matrix.h" + template -class TriangleMatrix: public Matrix { +class TriangleMatrix : public Matrix { public: TriangleMatrix(); TriangleMatrix(size_t size); @@ -9,8 +10,8 @@ class TriangleMatrix: public Matrix { TriangleMatrix(const TriangleMatrix& other); ~TriangleMatrix(); - size_t getSize() const { - return this->_M; + size_t getSize() const { + return this->_M; } TriangleMatrix operator + (T value) const; @@ -34,8 +35,8 @@ class TriangleMatrix: public Matrix { TriangleMatrix& operator= (const TriangleMatrix& other); friend std::ostream& operator<<(std::ostream& out, const TriangleMatrix& matrix) { - for (size_t i = 0; i < matrix._M; ++i) { - for (size_t j = 0; j < matrix._N; ++j) { + for (size_t i = 0; i < matrix.getSize(); ++i) { + for (size_t j = 0; j < matrix.getSize(); ++j) { if (i <= j) { out << matrix[i][j] << "\t"; } @@ -49,22 +50,24 @@ class TriangleMatrix: public Matrix { } }; template -TriangleMatrix::TriangleMatrix(): Matrix() {} +TriangleMatrix::TriangleMatrix() : Matrix() {} template -TriangleMatrix::TriangleMatrix(size_t N): MathVector>(N) { - for (size_t i = 0; i < N; ++i) { - +TriangleMatrix::TriangleMatrix(size_t size) : Matrix(size, size) { + for (size_t i = 0; i < size; ++i) { + (*this)[i] = MathVector(size - i, i); } } template -TriangleMatrix::TriangleMatrix(T* data, size_t size) { - +TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { + size_t data_index = 0; + for (size_t i = 0; i < size; ++i) { + (*this)[i] = MathVector(size - i, i); + for (size_t j = i; j < size; ++j) { + (*this)[i][j] = data[data_index++]; + } + } } - template -TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) { - -} - +TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} template TriangleMatrix::~TriangleMatrix() = default; From 0a176c5f34f517b51790ad260e13443ce6fb3a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 5 Oct 2025 18:00:02 +0300 Subject: [PATCH 051/275] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8C=D1=8E=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D1=8B=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B,=20=D0=B4=D0=B5=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=B8=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20(=D1=80=D0=B5=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 180166fe..53d8b2bd 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -59,15 +59,19 @@ TriangleMatrix::TriangleMatrix(size_t size) : Matrix(size, size) { } template TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { - size_t data_index = 0; for (size_t i = 0; i < size; ++i) { - (*this)[i] = MathVector(size - i, i); - for (size_t j = i; j < size; ++j) { - (*this)[i][j] = data[data_index++]; - } + (*this)[i] = MathVector(data + i * size + i, size - i, i); + } } template TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} template TriangleMatrix::~TriangleMatrix() = default; +template +TriangleMatrix& TriangleMatrix::operator= (const TriangleMatrix& other) { + if (this != &other) { + Matrix::operator=(other); + } + return *this; +} From 3822b6e0f2048485cd351672461d9e835e99bd58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 5 Oct 2025 20:18:35 +0300 Subject: [PATCH 052/275] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D0=BE=D1=80=20=D1=81=20=D0=BC=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D0=B2=D0=BE=D0=BC=20=D0=B8=20=D1=80=D0=B0=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D1=80=D0=BE=D0=BC=20=D0=BF=D0=BE=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20(=D0=BF=D1=80=D0=B5=D0=B4=D1=8B=D0=B4=D1=83?= =?UTF-8?q?=D1=89=D0=B8=D0=B9=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=20=D0=B1=D1=8B=D0=BB=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=B5=D0=BD=20-=20=D0=BE=D0=BD=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=80=D0=BE=D1=88=D1=91=D0=BB=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82),=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=20+,=20-,=20*,=20/=20=D0=B4=D0=BB=D1=8F=20=D1=82=D1=80?= =?UTF-8?q?=D0=B5=D1=83=D0=B3=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=BC?= =?UTF-8?q?=D0=B0=D1=82=D1=80=D0=B8=D1=86=D1=8B=20=D0=B8=20=D0=B7=D0=BD?= =?UTF-8?q?=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 53 +++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 53d8b2bd..34b70596 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -57,13 +57,9 @@ TriangleMatrix::TriangleMatrix(size_t size) : Matrix(size, size) { (*this)[i] = MathVector(size - i, i); } } -template -TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { - for (size_t i = 0; i < size; ++i) { - (*this)[i] = MathVector(data + i * size + i, size - i, i); - - } -} +//template +//TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { +//} template TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} template @@ -75,3 +71,46 @@ TriangleMatrix& TriangleMatrix::operator= (const TriangleMatrix& other) } return *this; } +template +TriangleMatrix TriangleMatrix::operator + (T value) const { + TriangleMatrix result(this->getSize()); + for (size_t i = 0; i < result.getSize(); ++i) { + for (size_t j = i; j < result.getSize(); ++j) { + result[i][j] = (*this)[i][j] + value; + } + } + return result; +} +template +TriangleMatrix TriangleMatrix::operator - (T value) const { + TriangleMatrix result(this->getSize()); + for (size_t i = 0; i < result.getSize(); ++i) { + for (size_t j = i; j < result.getSize(); ++j) { + result[i][j] = (*this)[i][j] - value; + } + } + return result; +} +template +TriangleMatrix TriangleMatrix::operator * (T value) const { + TriangleMatrix result(this->getSize()); + for (size_t i = 0; i < result.getSize(); ++i) { + for (size_t j = i; j < result.getSize(); ++j) { + result[i][j] = (*this)[i][j] * value; + } + } + return result; +} +template +TriangleMatrix TriangleMatrix::operator / (T value) const { + if (value == 0) { + throw std::logic_error("Division by zero!"); + } + TriangleMatrix result(this->getSize()); + for (size_t i = 0; i < result.getSize(); ++i) { + for (size_t j = i; j < result.getSize(); ++j) { + result[i][j] = (*this)[i][j] / value; + } + } + return result; +} From 3b942ac08ae56b879cba59528eb2b1fba207abf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 5 Oct 2025 20:19:42 +0300 Subject: [PATCH 053/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=20(=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D1=81=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=BE?= =?UTF-8?q?=D0=BC=20=D0=B8=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=BE?= =?UTF-8?q?=D0=BC=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D0=BD=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=85=D0=BE=D0=B4=D0=B8=D1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_triangle_matrix.cpp | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/test_triangle_matrix.cpp diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp new file mode 100644 index 00000000..3999ba4a --- /dev/null +++ b/tests/test_triangle_matrix.cpp @@ -0,0 +1,41 @@ +//Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_triangle_matrix/triangle_matrix.h" +TEST(TestTriangleMatrixLib, tr_matrix_default_constructor) { + // Arrange & Act + TriangleMatrix matrix; + // Assert + EXPECT_EQ(0, matrix.getSize()); + EXPECT_TRUE(matrix.is_empty()); +} +TEST(TestTriangleMatrixLib, tr_matrix_constructor_with_size) { + // Arrange & Act + size_t size = 4; + TriangleMatrix matrix(size); + // Assert + EXPECT_EQ(size, matrix.getSize()); +} + +TEST(TestTriangleMatrixLib, tr_matrix_constructor_with_array_and_size) { + // Arrange & Act + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + TriangleMatrix matrix(data, size); + + // Assert + EXPECT_EQ(size, matrix.getSize()); + + EXPECT_EQ(matrix[0][0], 1); + EXPECT_EQ(matrix[0][1], 2); + EXPECT_EQ(matrix[0][2], 3); + EXPECT_EQ(matrix[1][1], 4); + EXPECT_EQ(matrix[1][2], 5); + EXPECT_EQ(matrix[2][2], 6); + + EXPECT_THROW(matrix[1][0], std::logic_error); + EXPECT_THROW(matrix[2][0], std::logic_error); + EXPECT_THROW(matrix[2][1], std::logic_error); +} + + From da15c1d03a3d530bee670fe7f18f23016403d79f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 5 Oct 2025 20:24:15 +0300 Subject: [PATCH 054/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20+=3D,=20-=3D,=20*=3D,=20/=3D=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D1=8B=20=D0=B8=20?= =?UTF-8?q?=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 34b70596..99228672 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -114,3 +114,42 @@ TriangleMatrix TriangleMatrix::operator / (T value) const { } return result; } +template +TriangleMatrix& TriangleMatrix::operator += (T value) { + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + (*this)[i][j] += value; + } + } + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator -= (T value) { + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + (*this)[i][j] -= value; + } + } + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator *= (T value) { + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + (*this)[i][j] *= value; + } + } + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator /= (T value) { + if (value == 0) { + throw std::logic_error("Division by zero!"); + } + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + (*this)[i][j] /= value; + } + } + return *this; +} From 2042a5c90cb80f597d52b6d71610bec0c997a362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 22:38:18 +0300 Subject: [PATCH 055/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 1256 +++++++++++++++++++++---------------------- 1 file changed, 628 insertions(+), 628 deletions(-) diff --git a/lib_vector/vector.h b/lib_vector/vector.h index ea1c7777..0dcdbfae 100644 --- a/lib_vector/vector.h +++ b/lib_vector/vector.h @@ -1,694 +1,694 @@ -// Copyright 2025 Zabytina Julia. All rights reserved. -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -template -class Tvector { -public: - enum State { - empty, busy, deleted - }; -private: - static constexpr size_t RESERVE_MEMORY = 15; - static constexpr size_t MAX_PERCENT_DELETED = 15; + // Copyright 2025 Zabytina Julia. All rights reserved. + #pragma once + #include + #include + #include + #include + #include + #include + #include + #include + #include + template + class Tvector { + public: + enum State { + empty, busy, deleted + }; + protected: + static constexpr size_t RESERVE_MEMORY = 15; + static constexpr size_t MAX_PERCENT_DELETED = 15; - size_t _size; - size_t _capacity; - T* _data; - State* _states; - size_t _deleted; + size_t _size; + size_t _capacity; + T* _data; + State* _states; + size_t _deleted; - size_t get_real_position(size_t busy_index) const noexcept; - void resize(size_t new_size); - void resize(size_t new_size, const T& value); - void shrink_to_fit(); - void reserve(size_t new_capacity); - void compact_storage(); - inline bool is_full() const noexcept { - return _size == _capacity; - } -public: - Tvector() noexcept; - Tvector(size_t size); - Tvector(T* data, size_t size); - Tvector(const Tvector& other_vector); - ~Tvector() noexcept; + size_t get_real_position(size_t busy_index) const noexcept; + void resize(size_t new_size); + void resize(size_t new_size, const T& value); + void shrink_to_fit(); + void reserve(size_t new_capacity); + void compact_storage(); + inline bool is_full() const noexcept { + return _size == _capacity; + } + public: + Tvector() noexcept; + Tvector(size_t size); + Tvector(T* data, size_t size); + Tvector(const Tvector& other_vector); + virtual ~Tvector() noexcept; - inline bool is_empty() const noexcept { - return _size == 0; - } - inline const T* get_data() const noexcept { - return _data; - } - inline T* get_data() noexcept { - return _data; - } - inline const State* get_states() const noexcept { - return _states; - } - inline State* get_states() noexcept { - return _states; - } - inline T& front(); - inline T& back(); - inline const T& front() const; - inline const T& back() const; - size_t get_size() const noexcept { + inline bool is_empty() const noexcept { + return _size == 0; + } + inline const T* get_data() const noexcept { + return _data; + } + inline T* get_data() noexcept { + return _data; + } + inline const State* get_states() const noexcept { + return _states; + } + inline State* get_states() noexcept { + return _states; + } + inline T& front(); + inline T& back(); + inline const T& front() const; + inline const T& back() const; + size_t get_size() const noexcept { + return _size; + } + size_t get_deleted() const noexcept { + return _deleted; + } + size_t get_capacity() const noexcept { + return _capacity; + } + inline T* begin() const noexcept { + return _data; + } + inline T* end() const noexcept { + return _data + _size; + } + bool operator==(const Tvector& vector) const; + bool operator!=(const Tvector& vector) const; + Tvector& operator=(const Tvector& vector); + inline const T& operator[](size_t index) const; + inline T& operator[](size_t index); + T& at(size_t index); + const T& at(size_t index) const; + void assign(const Tvector& vector); + void clear(); + void emplace(size_t index, const T& value); + void push_front(const T& value); + void insert(const T& value, size_t position); + void push_back(const T& value); + void pop_back(); + void erase(size_t position); + void pop_front(); + + template + friend std::ostream& operator<<(std::ostream&, const Tvector& vector); + template friend void shell_sort(Tvector& object) noexcept; + template friend void shuffle(Tvector& object) noexcept; + template friend size_t find_first_element(const Tvector& object, const U& value); + template friend size_t find_last_element(const Tvector& object, const U& value); + template friend size_t find_count_of_all_suitable_elements(const Tvector& object, const U& value); + }; + template + size_t Tvector::get_real_position(size_t busy_index) const noexcept { + size_t busy_count = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == busy) { + if (busy_count == busy_index) { + return i; + } + ++busy_count; + } + } return _size; } - size_t get_deleted() const noexcept { - return _deleted; - } - size_t get_capacity() const noexcept { - return _capacity; - } - inline T* begin() const noexcept { - return _data; - } - inline T* end() const noexcept { - return _data + _size; - } - bool operator==(const Tvector& vector) const; - bool operator!=(const Tvector& vector) const; - Tvector& operator=(const Tvector& vector); - inline const T& operator[](size_t index) const; - inline T& operator[](size_t index); - T& at(size_t index); - const T& at(size_t index) const; - void assign(const Tvector& vector); - void clear(); - void emplace(size_t index, const T& value); - void push_front(const T& value); - void insert(const T& value, size_t position); - void push_back(const T& value); - void pop_back(); - void erase(size_t position); - void pop_front(); + template + void Tvector::resize(size_t new_size) { + if (new_size == _size) { + return; + } + else if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) { + _deleted--; + } + _data[i].~T(); + _states[i] = State::empty; + } + _size = new_size; + } + else { + size_t new_capacity = new_size + RESERVE_MEMORY; + T* new_data = static_cast(::operator new(new_capacity * sizeof(T))); + State* new_states = new State[new_capacity]; + for (size_t i = new_size; i < new_capacity; ++i) { + new_states[i] = State::empty; + } + try { + for (size_t i = 0; i < _size; ++i) { + new (new_data + i) T(std::move(_data[i])); + new_states[i] = _states[i]; + } + for (size_t i = _size; i < new_size; ++i) { + new (new_data + i) T(); + new_states[i] = State::busy; + } + for (size_t i = 0; i < _size; ++i) { + _data[i].~T(); + } + ::operator delete(_data); + delete[] _states; - template - friend std::ostream& operator<<(std::ostream&, const Tvector& vector); - template friend void shell_sort(Tvector& object) noexcept; - template friend void shuffle(Tvector& object) noexcept; - template friend size_t find_first_element(const Tvector& object, const U& value); - template friend size_t find_last_element(const Tvector& object, const U& value); - template friend size_t find_count_of_all_suitable_elements(const Tvector& object, const U& value); -}; -template -size_t Tvector::get_real_position(size_t busy_index) const noexcept { - size_t busy_count = 0; - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == busy) { - if (busy_count == busy_index) { - return i; + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = new_size; + } + catch (...) { + for (size_t i = 0; i < new_size; ++i) { + new_data[i].~T(); + } + ::operator delete(new_data); + delete[] new_states; + throw; } - ++busy_count; } } - return _size; -} -template -void Tvector::resize(size_t new_size) { - if (new_size == _size) { - return; - } - else if (new_size < _size) { - for (size_t i = new_size; i < _size; ++i) { - if (_states[i] == State::deleted) { - _deleted--; + template + void Tvector::resize(size_t new_size, const T& value) { + if (new_size == _size) { + return; + } + else if (new_size < _size) { + for (size_t i = new_size; i < _size; ++i) { + if (_states[i] == State::deleted) { + _deleted--; + } + _data[i].~T(); + _states[i] = State::empty; + } + _size = new_size; + } + else { + size_t new_capacity = new_size + RESERVE_MEMORY; + T* new_data = static_cast(::operator new(new_capacity * sizeof(T))); + State* new_states = new State[new_capacity]; + for (size_t i = new_size; i < new_capacity; ++i) { + new_states[i] = State::empty; + } + try { + for (size_t i = 0; i < _size; ++i) { + new (new_data + i) T(std::move(_data[i])); + new_states[i] = _states[i]; + } + for (size_t i = _size; i < new_size; ++i) { + new (new_data + i) T(value); + new_states[i] = State::busy; + } + for (size_t i = 0; i < _size; ++i) { + _data[i].~T(); + } + ::operator delete(_data); + delete[] _states; + + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = new_size; + } + catch (...) { + for (size_t i = 0; i < new_size; ++i) { + new_data[i].~T(); + } + ::operator delete(new_data); + delete[] new_states; + throw; } - _data[i].~T(); - _states[i] = State::empty; } - _size = new_size; } - else { - size_t new_capacity = new_size + RESERVE_MEMORY; - T* new_data = static_cast(::operator new(new_capacity * sizeof(T))); - State* new_states = new State[new_capacity]; - for (size_t i = new_size; i < new_capacity; ++i) { - new_states[i] = State::empty; + template + void Tvector::shrink_to_fit() { + if (_size >= _capacity) { + return; } - try { + else if (_size == 0) { + delete[] _data; + delete[] _states; + _data = nullptr; + _states = nullptr; + _capacity = 0; + _deleted = 0; + } + else { + T* new_data = new T[_size]; + State* new_states = new State[_size]; for (size_t i = 0; i < _size; ++i) { - new (new_data + i) T(std::move(_data[i])); + new_data[i] = std::move(_data[i]); new_states[i] = _states[i]; } - for (size_t i = _size; i < new_size; ++i) { - new (new_data + i) T(); - new_states[i] = State::busy; - } - for (size_t i = 0; i < _size; ++i) { - _data[i].~T(); - } - ::operator delete(_data); + delete[] _data; delete[] _states; - _data = new_data; _states = new_states; - _capacity = new_capacity; - _size = new_size; - } - catch (...) { - for (size_t i = 0; i < new_size; ++i) { - new_data[i].~T(); - } - ::operator delete(new_data); - delete[] new_states; - throw; + _capacity = _size; } } -} -template -void Tvector::resize(size_t new_size, const T& value) { - if (new_size == _size) { - return; - } - else if (new_size < _size) { - for (size_t i = new_size; i < _size; ++i) { - if (_states[i] == State::deleted) { - _deleted--; - } - _data[i].~T(); - _states[i] = State::empty; + template + void Tvector::reserve(size_t new_capacity) { + if (new_capacity <= _capacity) { + return; } - _size = new_size; - } - else { - size_t new_capacity = new_size + RESERVE_MEMORY; - T* new_data = static_cast(::operator new(new_capacity * sizeof(T))); + T* new_data = new T[new_capacity]; State* new_states = new State[new_capacity]; - for (size_t i = new_size; i < new_capacity; ++i) { - new_states[i] = State::empty; + std::fill_n(new_states, new_capacity, State::empty); + for (size_t i = 0; i < _size; ++i) { + new_data[i] = std::move(_data[i]); + new_states[i] = _states[i]; } - try { - for (size_t i = 0; i < _size; ++i) { - new (new_data + i) T(std::move(_data[i])); - new_states[i] = _states[i]; + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = new_capacity; + } + template + void Tvector::compact_storage() { + size_t busy_count = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + busy_count++; } - for (size_t i = _size; i < new_size; ++i) { - new (new_data + i) T(value); - new_states[i] = State::busy; + } + std::cout << "compact_storage(): busy_count = " << busy_count << "\n"; + size_t new_capacity = busy_count + RESERVE_MEMORY; + T* new_data = new T[new_capacity]; + State* new_states = new State[new_capacity]; + size_t new_index = 0; + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + new_data[new_index] = std::move(_data[i]); + new_states[new_index] = State::busy; + new_index++; } - for (size_t i = 0; i < _size; ++i) { + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] != State::empty) { _data[i].~T(); } - ::operator delete(_data); - delete[] _states; + } + delete[] _data; + delete[] _states; + _data = new_data; + _states = new_states; + _capacity = new_capacity; + _size = busy_count; + _deleted = 0; + } + template + Tvector::Tvector() noexcept { + _size = 0; + _capacity = 0; + _data = nullptr; + _states = nullptr; + _deleted = 0; + } + template + Tvector::Tvector(size_t size) { + _size = size; + _capacity = size + RESERVE_MEMORY; + _data = new T[_capacity]; - _data = new_data; - _states = new_states; - _capacity = new_capacity; - _size = new_size; + try { + _states = new State[_capacity]; } - catch (...) { - for (size_t i = 0; i < new_size; ++i) { - new_data[i].~T(); - } - ::operator delete(new_data); - delete[] new_states; + catch (const std::bad_alloc&) { + delete[] _data; throw; } + _deleted = 0; + + for (size_t i = 0; i < _capacity; ++i) { + _states[i] = i < _size ? State::busy : State::empty; + } } -} -template -void Tvector::shrink_to_fit() { - if (_size >= _capacity) { - return; + template + Tvector::Tvector(T* data, size_t size) { + if (size > 0 && data == nullptr) { + throw std::invalid_argument("Null data pointer with non-zero size"); + } + _size = size; + _capacity = _size + RESERVE_MEMORY; + _data = new T[_capacity]; + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; + throw; + } + for (size_t i = 0; i < _capacity; ++i) { + if (i < _size) { + _data[i] = data[i]; + _states[i] = State::busy; + } + else { + _states[i] = State::empty; + } + } + _deleted = 0; } - else if (_size == 0) { - delete[] _data; - delete[] _states; + template + Tvector::Tvector(const Tvector& other_vector) { + _size = other_vector._size; + _capacity = other_vector._capacity; _data = nullptr; _states = nullptr; - _capacity = 0; - _deleted = 0; + try { + _data = new T[_capacity]; + } + catch (const std::bad_alloc&) { + throw; + } + try { + _states = new State[_capacity]; + } + catch (const std::bad_alloc&) { + delete[] _data; + throw; + } + _deleted = other_vector._deleted; + + for (size_t i = 0; i < other_vector._size; ++i) { + _data[i] = other_vector._data[i]; + _states[i] = other_vector._states[i]; + } + for (size_t i = other_vector._size; i < other_vector._capacity; ++i) { + _states[i] = State::empty; + } + } + template + Tvector::~Tvector() noexcept { + delete[] _data; + delete[] _states; + } + template + inline T& Tvector::front() { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[0]; + } + template + inline T& Tvector::back() { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[_size - 1]; + } + template + inline const T& Tvector::front() const { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[0]; + } + template + inline const T& Tvector::back() const { + if (_size == 0) { + throw std::out_of_range("Vector is empty"); + } + return _data[_size - 1]; } - else { - T* new_data = new T[_size]; - State* new_states = new State[_size]; + template + bool Tvector::operator==(const Tvector& vector) const { + if (this->_size != vector._size) + return false; + for (size_t i = 0; i < _size; ++i) { - new_data[i] = std::move(_data[i]); - new_states[i] = _states[i]; + if ((*this)[i] != vector[i]) + return false; + } + return true; + } + template + bool Tvector::operator!=(const Tvector& vector) const { + return !(*this == vector); + } + template + Tvector& Tvector::operator=(const Tvector& vector) { + if (this == &vector) { + return *this; + } + T* new_data = new T[vector._capacity]; + State* new_states = new State[vector._capacity]; + for (size_t i = 0; i < vector._size; ++i) { + new_data[i] = vector._data[i]; + new_states[i] = vector._states[i]; + } + for (size_t i = vector._size; i < vector._capacity; ++i) { + new_states[i] = empty; } delete[] _data; delete[] _states; _data = new_data; _states = new_states; - _capacity = _size; - } -} -template -void Tvector::reserve(size_t new_capacity) { - if (new_capacity <= _capacity) { - return; - } - T* new_data = new T[new_capacity]; - State* new_states = new State[new_capacity]; - std::fill_n(new_states, new_capacity, State::empty); - for (size_t i = 0; i < _size; ++i) { - new_data[i] = std::move(_data[i]); - new_states[i] = _states[i]; - } - delete[] _data; - delete[] _states; - _data = new_data; - _states = new_states; - _capacity = new_capacity; -} -template -void Tvector::compact_storage() { - size_t busy_count = 0; - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - busy_count++; - } - } - std::cout << "compact_storage(): busy_count = " << busy_count << "\n"; - size_t new_capacity = busy_count + RESERVE_MEMORY; - T* new_data = new T[new_capacity]; - State* new_states = new State[new_capacity]; - size_t new_index = 0; - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - new_data[new_index] = std::move(_data[i]); - new_states[new_index] = State::busy; - new_index++; - } - } - for (size_t i = 0; i < _size; ++i) { - if (_states[i] != State::empty) { - _data[i].~T(); - } - } - delete[] _data; - delete[] _states; - _data = new_data; - _states = new_states; - _capacity = new_capacity; - _size = busy_count; - _deleted = 0; -} -template -Tvector::Tvector() noexcept { - _size = 0; - _capacity = 0; - _data = nullptr; - _states = nullptr; - _deleted = 0; -} -template -Tvector::Tvector(size_t size) { - _size = size; - _capacity = size + RESERVE_MEMORY; - _data = new T[_capacity]; + _capacity = vector._capacity; + _size = vector._size; + _deleted = vector._deleted; - try { - _states = new State[_capacity]; - } - catch (const std::bad_alloc&) { - delete[] _data; - throw; + return *this; } - _deleted = 0; - - for (size_t i = 0; i < _capacity; ++i) { - _states[i] = i < _size ? State::busy : State::empty; - } -} -template -Tvector::Tvector(T* data, size_t size) { - if (size > 0 && data == nullptr) { - throw std::invalid_argument("Null data pointer with non-zero size"); - } - _size = size; - _capacity = _size + RESERVE_MEMORY; - _data = new T[_capacity]; - try { - _states = new State[_capacity]; + template + inline const T& Tvector::operator[](size_t index) const { + size_t real_index = get_real_position(index); + return _data[real_index]; } - catch (const std::bad_alloc&) { - delete[] _data; - throw; + template + inline T& Tvector::operator[](size_t index) { + size_t real_index = get_real_position(index); + return _data[real_index]; } - for (size_t i = 0; i < _capacity; ++i) { - if (i < _size) { - _data[i] = data[i]; - _states[i] = State::busy; + template + T& Tvector::at(size_t index) { + if (index >= _size) { + throw std::out_of_range("Index out of range"); } - else { - _states[i] = State::empty; + if (_states[index] != busy) { + throw std::logic_error("Element at this index is not available (deleted or empty)"); } + size_t real_index = get_real_position(index); + return _data[real_index]; + } + template + const T& Tvector::at(size_t index) const { + size_t real_index = get_real_position(index); + return const_cast(this)->at(real_index); } - _deleted = 0; -} -template -Tvector::Tvector(const Tvector& other_vector) { - _size = other_vector._size; - _capacity = other_vector._capacity; - _data = nullptr; - _states = nullptr; - try { + template + void Tvector::assign(const Tvector& vector) { + if (this == &vector) { + return; + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _data[i].~T(); + } + } + delete[] _data; + delete[] _states; + _capacity = vector._capacity; + _size = vector._size; + _deleted = vector._deleted; + _data = new T[_capacity]; + _states = new State[_capacity]; + for (size_t i = 0; i < _size; ++i) { + if (vector._states[i] == State::busy) { + new (&_data[i]) T(vector._data[i]); + } + _states[i] = vector._states[i]; + } + std::fill(_states + _size, _states + _capacity, State::empty); } - catch (const std::bad_alloc&) { - throw; + template + void Tvector::clear() { + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _states[i] = State::empty; + } + } + _size = 0; + _deleted = 0; } - try { - _states = new State[_capacity]; + template + void Tvector::emplace(size_t index, const T& value) { + if (index >= _size) { + throw std::out_of_range("Index out of range"); + } + size_t real_index = get_real_position(index); + if (_states[real_index] == State::deleted) { + --_deleted; + } + _data[real_index] = value; + _states[real_index] = State::busy; } - catch (const std::bad_alloc&) { - delete[] _data; - throw; + template + void Tvector::push_front(const T& value) { + if (_size >= _capacity) { + reserve(_capacity + RESERVE_MEMORY); + } + for (size_t i = _size; i > 0; --i) { + _data[i] = std::move(_data[i - 1]); + _states[i] = _states[i - 1]; + } + _data[0] = value; + _states[0] = State::busy; + _size++; + } + template + void Tvector::insert(const T& value, size_t position) { + if (_size == 0) { + resize(RESERVE_MEMORY); + } + if (position >= _size) { + throw std::out_of_range("Insert position out of range"); + } + size_t real_pos = get_real_position(position); + for (size_t i = _size; i > real_pos; --i) { + _data[i] = std::move(_data[i - 1]); + _states[i] = _states[i - 1]; + } + _data[real_pos] = value; + _states[real_pos] = State::busy; + _size++; + } + template + void Tvector::push_back(const T& value) { + if (_size >= _capacity) { + reserve(_capacity + RESERVE_MEMORY); + } + _data[_size] = value; + _states[_size] = State::busy; + _size++; + } + template + void Tvector::pop_back() { + if (_size == 0) { + throw std::out_of_range("Vector has size = 0"); + } + _states[_size - 1] = State::empty; + _size--; } - _deleted = other_vector._deleted; - - for (size_t i = 0; i < other_vector._size; ++i) { - _data[i] = other_vector._data[i]; - _states[i] = other_vector._states[i]; - } - for (size_t i = other_vector._size; i < other_vector._capacity; ++i) { - _states[i] = State::empty; - } -} -template -Tvector::~Tvector() noexcept { - delete[] _data; - delete[] _states; -} -template -inline T& Tvector::front() { - if (_size == 0) { - throw std::out_of_range("Vector is empty"); - } - return _data[0]; -} -template -inline T& Tvector::back() { - if (_size == 0) { - throw std::out_of_range("Vector is empty"); - } - return _data[_size - 1]; -} -template -inline const T& Tvector::front() const { - if (_size == 0) { - throw std::out_of_range("Vector is empty"); - } - return _data[0]; -} -template -inline const T& Tvector::back() const { - if (_size == 0) { - throw std::out_of_range("Vector is empty"); - } - return _data[_size - 1]; -} -template -bool Tvector::operator==(const Tvector& vector) const { - if (this->_size != vector._size) - return false; - - for (size_t i = 0; i < _size; ++i) { - if ((*this)[i] != vector[i]) - return false; + template + void Tvector::erase(size_t position) { + if (position >= _size) { + throw std::out_of_range("Invalid position"); + } + size_t real_pos = get_real_position(position); + _states[real_pos] = State::deleted; + _deleted++; + if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { + compact_storage(); + } } - return true; -} -template -bool Tvector::operator!=(const Tvector& vector) const { - return !(*this == vector); -} -template -Tvector& Tvector::operator=(const Tvector& vector) { - if (this == &vector) { - return *this; + template + void Tvector::pop_front() { + if (_size == 0) { + throw std::out_of_range("Vector has size = 0"); + } + for (size_t i = 0; i < _size; ++i) { + if (_states[i] == State::busy) { + _states[i] = State::deleted; + _deleted++; + if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { + compact_storage(); + } + return; + } + } } - T* new_data = new T[vector._capacity]; - State* new_states = new State[vector._capacity]; - for (size_t i = 0; i < vector._size; ++i) { - new_data[i] = vector._data[i]; - new_states[i] = vector._states[i]; - } - for (size_t i = vector._size; i < vector._capacity; ++i) { - new_states[i] = empty; - } - delete[] _data; - delete[] _states; - _data = new_data; - _states = new_states; - _capacity = vector._capacity; - _size = vector._size; - _deleted = vector._deleted; - return *this; -} -template -inline const T& Tvector::operator[](size_t index) const { - size_t real_index = get_real_position(index); - return _data[real_index]; -} -template -inline T& Tvector::operator[](size_t index) { - size_t real_index = get_real_position(index); - return _data[real_index]; -} -template -T& Tvector::at(size_t index) { - if (index >= _size) { - throw std::out_of_range("Index out of range"); - } - if (_states[index] != busy) { - throw std::logic_error("Element at this index is not available (deleted or empty)"); - } - size_t real_index = get_real_position(index); - return _data[real_index]; -} -template -const T& Tvector::at(size_t index) const { - size_t real_index = get_real_position(index); - return const_cast(this)->at(real_index); -} -template -void Tvector::assign(const Tvector& vector) { - if (this == &vector) { - return; - } - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - _data[i].~T(); - } - } - delete[] _data; - delete[] _states; - _capacity = vector._capacity; - _size = vector._size; - _deleted = vector._deleted; - - _data = new T[_capacity]; - _states = new State[_capacity]; - for (size_t i = 0; i < _size; ++i) { - if (vector._states[i] == State::busy) { - new (&_data[i]) T(vector._data[i]); - } - _states[i] = vector._states[i]; - } - std::fill(_states + _size, _states + _capacity, State::empty); -} -template -void Tvector::clear() { - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - _states[i] = State::empty; + template + std::ostream& operator<<(std::ostream& out, const Tvector& vector) { + out << "["; + bool first = true; + for (size_t i = 0; i < vector._size; ++i) { + if (vector._states[i] == Tvector::State::busy) { + if (!first) out << ", "; + out << vector._data[i]; + first = false; + } } + out << "]"; + return out; } - _size = 0; - _deleted = 0; -} -template -void Tvector::emplace(size_t index, const T& value) { - if (index >= _size) { - throw std::out_of_range("Index out of range"); - } - size_t real_index = get_real_position(index); - if (_states[real_index] == State::deleted) { - --_deleted; - } - _data[real_index] = value; - _states[real_index] = State::busy; -} -template -void Tvector::push_front(const T& value) { - if (_size >= _capacity) { - reserve(_capacity + RESERVE_MEMORY); - } - for (size_t i = _size; i > 0; --i) { - _data[i] = std::move(_data[i - 1]); - _states[i] = _states[i - 1]; - } - _data[0] = value; - _states[0] = State::busy; - _size++; -} -template -void Tvector::insert(const T& value, size_t position) { - if (_size == 0) { - resize(RESERVE_MEMORY); - } - if (position >= _size) { - throw std::out_of_range("Insert position out of range"); - } - size_t real_pos = get_real_position(position); - for (size_t i = _size; i > real_pos; --i) { - _data[i] = std::move(_data[i - 1]); - _states[i] = _states[i - 1]; - } - _data[real_pos] = value; - _states[real_pos] = State::busy; - _size++; -} -template -void Tvector::push_back(const T& value) { - if (_size >= _capacity) { - reserve(_capacity + RESERVE_MEMORY); - } - _data[_size] = value; - _states[_size] = State::busy; - _size++; -} -template -void Tvector::pop_back() { - if (_size == 0) { - throw std::out_of_range("Vector has size = 0"); - } - _states[_size - 1] = State::empty; - _size--; -} -template -void Tvector::erase(size_t position) { - if (position >= _size) { - throw std::out_of_range("Invalid position"); - } - size_t real_pos = get_real_position(position); - _states[real_pos] = State::deleted; - _deleted++; - if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { - compact_storage(); - } -} -template -void Tvector::pop_front() { - if (_size == 0) { - throw std::out_of_range("Vector has size = 0"); - } - for (size_t i = 0; i < _size; ++i) { - if (_states[i] == State::busy) { - _states[i] = State::deleted; - _deleted++; - if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { - compact_storage(); + template + void shell_sort(Tvector& object) noexcept { + if (object._size < 2 || object._data == nullptr + || object._states == nullptr) { + return; + } + size_t h = 1; + while (h < object._size / 3) { + h = 3 * h + 1; + } + while (h >= 1) { + for (size_t i = h; i < object._size; ++i) { + for (size_t j = i; j >= h; j -= h) { + size_t previous = j - h; + if (object._data[j] < object._data[previous]) { + std::swap(object._data[j], object._data[previous]); + std::swap(object._states[j], object._states[previous]); + } + else { + break; + } + } } + h /= 3; + } + } + template + void shuffle(Tvector& object) noexcept { + if (object._size < 2 || object._data == nullptr || object._states == nullptr) { return; } + std::random_device rd; + std::mt19937 gen(rd()); + for (size_t i = object._size - 1; i > 0; --i) { + std::uniform_int_distribution dist(0, i); + size_t j = dist(gen); + std::swap(object._data[i], object._data[j]); + std::swap(object._states[i], object._states[j]); + } } -} - -template -std::ostream& operator<<(std::ostream& out, const Tvector& vector) { - out << "["; - bool first = true; - for (size_t i = 0; i < vector._size; ++i) { - if (vector._states[i] == Tvector::State::busy) { - if (!first) out << ", "; - out << vector._data[i]; - first = false; - } - } - out << "]"; - return out; -} -template -void shell_sort(Tvector& object) noexcept { - if (object._size < 2 || object._data == nullptr - || object._states == nullptr) { - return; - } - size_t h = 1; - while (h < object._size / 3) { - h = 3 * h + 1; - } - while (h >= 1) { - for (size_t i = h; i < object._size; ++i) { - for (size_t j = i; j >= h; j -= h) { - size_t previous = j - h; - if (object._data[j] < object._data[previous]) { - std::swap(object._data[j], object._data[previous]); - std::swap(object._states[j], object._states[previous]); - } - else { - break; - } + template + size_t find_first_element(const Tvector& object, const U& value) { + size_t result = 0; + for (size_t i = 0; i < object._size; i++) { + if (object._states[i] == object.State::deleted) { + continue; + } + else if (object._data[i] == value && object._states[i] == object.State::busy) { + return result + 1; + } + result++; + } + return 0; + } + template + size_t find_last_element(const Tvector& object, const U& value) { + size_t last_pos = 0; + size_t current_pos = 0; + for (size_t i = 0; i < object._size; i++) { + if (object._states[i] == object.State::deleted) { + continue; + } + current_pos++; + if (object._data[i] == value && object._states[i] == object.State::busy) { + last_pos = current_pos; } } - h /= 3; - } -} -template -void shuffle(Tvector& object) noexcept { - if (object._size < 2 || object._data == nullptr || object._states == nullptr) { - return; - } - std::random_device rd; - std::mt19937 gen(rd()); - for (size_t i = object._size - 1; i > 0; --i) { - std::uniform_int_distribution dist(0, i); - size_t j = dist(gen); - std::swap(object._data[i], object._data[j]); - std::swap(object._states[i], object._states[j]); - } -} -template -size_t find_first_element(const Tvector& object, const U& value) { - size_t result = 0; - for (size_t i = 0; i < object._size; i++) { - if (object._states[i] == object.State::deleted) { - continue; - } - else if (object._data[i] == value && object._states[i] == object.State::busy) { - return result + 1; - } - result++; - } - return 0; -} -template -size_t find_last_element(const Tvector& object, const U& value) { - size_t last_pos = 0; - size_t current_pos = 0; - for (size_t i = 0; i < object._size; i++) { - if (object._states[i] == object.State::deleted) { - continue; - } - current_pos++; - if (object._data[i] == value && object._states[i] == object.State::busy) { - last_pos = current_pos; - } - } - return last_pos; -} -template -size_t find_count_of_all_suitable_elements(const Tvector& object, const U& value) { - size_t count = 0; - for (size_t i = 0; i < object._size; ++i) { - if (object._data[i] == value && object._states[i] == object.State::busy) { - count++; - } - } - return count; -} + return last_pos; + } + template + size_t find_count_of_all_suitable_elements(const Tvector& object, const U& value) { + size_t count = 0; + for (size_t i = 0; i < object._size; ++i) { + if (object._data[i] == value && object._states[i] == object.State::busy) { + count++; + } + } + return count; + } From 0d800ec19dc1706d6b8428652491cdaaebae33e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 22:41:22 +0300 Subject: [PATCH 056/275] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Mat?= =?UTF-8?q?hVector=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D1=82=D1=80=D0=B5=D1=83=D0=B3?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=BC=D0=B0=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D1=86=D1=8B:=20=D0=B2=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=3D=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=5Fstart=5Findex,=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20[]=20-=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=B1=D1=80=D0=BE=D1=81=D0=B0=D0=B5=D1=82=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B9,?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20at()=20-=20?= =?UTF-8?q?=D0=B1=D1=80=D0=BE=D1=81=D0=B0=D0=B5=D1=82=20=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index d36d94aa..103425da 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -21,6 +21,9 @@ class MathVector:public Tvector { _start_index = index; } + T& at(size_t index); + const T& at(size_t index) const; + MathVector operator * (T value) const; MathVector operator / (T value) const; @@ -37,7 +40,7 @@ class MathVector:public Tvector { friend std::ostream& operator<<(std::ostream& out, const MathVector& vector) { out << "["; for (size_t i = 0; i < vector.get_size(); ++i) { - out << vector[i]; + out << vector.Tvector::operator[](i); if (i < vector.get_size() - 1) { out << ", "; } @@ -46,6 +49,7 @@ class MathVector:public Tvector { return out; } T& operator[](size_t index); + const T& operator[](size_t index) const; }; template MathVector::MathVector() : Tvector() {} @@ -156,13 +160,29 @@ template MathVector& MathVector::operator=(const MathVector& other) { if (this != &other) { Tvector::operator=(other); + _start_index = other._start_index; } return *this; } template T& MathVector::operator[](size_t index) { + return Tvector::operator[](index - _start_index); +} +template +const T& MathVector::operator[](size_t index) const { + return Tvector::operator[](index - _start_index); +} +template +T& MathVector::at(size_t index) { + if (index < _start_index || index >= _start_index + this->get_size()) { + throw std::logic_error("MathVector index out of range"); + } + return Tvector::operator[](index - _start_index); +} +template +const T& MathVector::at(size_t index) const { if (index < _start_index || index >= _start_index + this->get_size()) { - throw std::logic_error("Index out of range"); + throw std::std::logic_error("MathVector index out of range"); } return Tvector::operator[](index - _start_index); } From 02f0378c3572222f78a06258544dacb3eee24643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 22:43:33 +0300 Subject: [PATCH 057/275] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D1=91?= =?UTF-8?q?=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20< { friend std::ostream& operator<<(std::ostream& out, const MathVector& vector) { out << "["; for (size_t i = 0; i < vector.get_size(); ++i) { - out << vector.Tvector::operator[](i); + out << vector[i]; if (i < vector.get_size() - 1) { out << ", "; } From 5f678fbc057eede8be713683b896ada8d20bbc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 23:07:28 +0300 Subject: [PATCH 058/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20[]?= =?UTF-8?q?=20=D0=B8=20at()=20(=D0=B2=20=D1=82=D0=BE=D0=BC=20=D1=87=D0=B8?= =?UTF-8?q?=D1=81=D0=BB=D0=B5,=20=D0=BD=D0=B0=20=D1=81=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D0=B9=20=D1=81=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_math_vector.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/test_math_vector.cpp b/tests/test_math_vector.cpp index c6580006..bff4c8e9 100644 --- a/tests/test_math_vector.cpp +++ b/tests/test_math_vector.cpp @@ -312,9 +312,11 @@ TEST(TestMathVectorLib, mathvector_operator_square_brackets) { //Act & Assert EXPECT_EQ(vector1[1], 2); EXPECT_EQ(vector1[2], 3); + EXPECT_EQ(vector1[3], 4); + EXPECT_EQ(vector1[4], 5); EXPECT_EQ(vector1[5], 6); } -TEST(TestMathVectorLib, mathvector_operator_square_brackets_with_exception) { +TEST(TestMathVectorLib, mathvector_at) { // Arrange size_t size = 5; size_t start_index = 2; @@ -325,7 +327,24 @@ TEST(TestMathVectorLib, mathvector_operator_square_brackets_with_exception) { vector1[5] = 6; vector1[6] = 7; //Act & Assert - EXPECT_THROW(vector1[0], std::logic_error); - EXPECT_THROW(vector1[1], std::logic_error); - EXPECT_THROW(vector1[7], std::logic_error); + EXPECT_EQ(vector1.at(2), 3); + EXPECT_EQ(vector1.at(3), 4); + EXPECT_EQ(vector1.at(4), 5); + EXPECT_EQ(vector1.at(5), 6); + EXPECT_EQ(vector1.at(6), 7); +} +TEST(TestMathVectorLib, mathvector_at_with_exception) { + // Arrange + size_t size = 5; + size_t start_index = 2; + MathVector vector1(size, start_index); + vector1[2] = 3; + vector1[3] = 4; + vector1[4] = 5; + vector1[5] = 6; + vector1[6] = 7; + //Act & Assert + EXPECT_THROW(vector1.at(0), std::logic_error); + EXPECT_THROW(vector1.at(1), std::logic_error); + EXPECT_THROW(vector1.at(7), std::logic_error); } From eb7bca6b1980ddcf0f8668e689649b3a0fce5162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 23:10:13 +0300 Subject: [PATCH 059/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index ad786875..a2c39fb7 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -1,6 +1,7 @@ // Copyright 2025 Julia Zabytina #include #include "../lib_vector/vector.h" +/* TEST(TestVectorLib, vector_default_constructor) { // Arrange & Act Tvector vector; @@ -517,3 +518,5 @@ TEST(TestVectorLib, find_count_of_suitable_elements) { size_t expected_result = 4; EXPECT_EQ(expected_result, actual_result); } + +*/ From 21c4b06c48efff4efe28605835495930a4fa948c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 23:13:05 +0300 Subject: [PATCH 060/275] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D1=91?= =?UTF-8?q?=D0=BD=20=D1=82=D0=B5=D1=81=D1=82=20find=5Fcount=5Fof=5Fsuitabl?= =?UTF-8?q?e=5Felements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index a2c39fb7..e003e79e 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -1,7 +1,6 @@ // Copyright 2025 Julia Zabytina #include #include "../lib_vector/vector.h" -/* TEST(TestVectorLib, vector_default_constructor) { // Arrange & Act Tvector vector; @@ -509,8 +508,8 @@ TEST(TestVectorLib, find_last) { } TEST(TestVectorLib, find_count_of_suitable_elements) { // Arrange - int data[7] = { 1 ,3, 3, 4, 5, 3, 3 }; - size_t size = 7; + int data[8] = { 1 ,3, 3, 4, 5, 3, 3, 7 }; + size_t size = 8; Tvector vector(data, size); // Act size_t actual_result = find_count_of_all_suitable_elements(vector, 3); @@ -518,5 +517,3 @@ TEST(TestVectorLib, find_count_of_suitable_elements) { size_t expected_result = 4; EXPECT_EQ(expected_result, actual_result); } - -*/ From 15e7707d8a7953ba6565452356dc262b9fb50250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 23:31:31 +0300 Subject: [PATCH 061/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=20=D0=BA=D0=BE=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_triangle_matrix.cpp | 43 ++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index 3999ba4a..7eff65c7 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -16,26 +16,39 @@ TEST(TestTriangleMatrixLib, tr_matrix_constructor_with_size) { // Assert EXPECT_EQ(size, matrix.getSize()); } - TEST(TestTriangleMatrixLib, tr_matrix_constructor_with_array_and_size) { // Arrange & Act size_t size = 3; int data[6] = { 1, 2, 3, 4, 5, 6 }; TriangleMatrix matrix(data, size); - // Assert EXPECT_EQ(size, matrix.getSize()); - - EXPECT_EQ(matrix[0][0], 1); - EXPECT_EQ(matrix[0][1], 2); - EXPECT_EQ(matrix[0][2], 3); - EXPECT_EQ(matrix[1][1], 4); - EXPECT_EQ(matrix[1][2], 5); - EXPECT_EQ(matrix[2][2], 6); - - EXPECT_THROW(matrix[1][0], std::logic_error); - EXPECT_THROW(matrix[2][0], std::logic_error); - EXPECT_THROW(matrix[2][1], std::logic_error); + EXPECT_EQ(matrix.at(0).at(0), 1); + EXPECT_EQ(matrix.at(0).at(1), 2); + EXPECT_EQ(matrix.at(0).at(2), 3); + EXPECT_EQ(matrix.at(1).at(1), 4); + EXPECT_EQ(matrix.at(1).at(2), 5); + EXPECT_EQ(matrix.at(2).at(2), 6); + EXPECT_THROW(matrix.at(1).at(0), std::logic_error); + EXPECT_THROW(matrix.at(2).at(0), std::logic_error); + EXPECT_THROW(matrix.at(2).at(1), std::logic_error); +} +TEST(TestTriangleMatrixLib, tr_matrix_copy_constructor) { + // Arrange & Act + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + TriangleMatrix matrix1(data, size); + TriangleMatrix matrix2(matrix1); + // Assert + EXPECT_EQ(matrix1, matrix2); +} +TEST(TestTriangleMatrixLib, tr_matrix_operator_assignment) { + // Arrange & Act + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + TriangleMatrix matrix1(data, size); + TriangleMatrix matrix2; + matrix2 = matrix1; + // Assert + EXPECT_EQ(matrix1, matrix2); } - - From 8d943a0b17f10386173ba728ff88fb4ecd79ca5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 23:48:20 +0300 Subject: [PATCH 062/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20+,-,*,/,=20+=3D,=20-=3D,=20*=3D,=20/=3D=20=D1=81=20=D1=82?= =?UTF-8?q?=D1=80=D0=B5=D1=83=D0=B3=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D0=B5=D0=B9=20=D0=B8=20?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=D0=BB=D1=8F=D1=80=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_triangle_matrix.cpp | 137 +++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index 7eff65c7..0845af87 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -52,3 +52,140 @@ TEST(TestTriangleMatrixLib, tr_matrix_operator_assignment) { // Assert EXPECT_EQ(matrix1, matrix2); } +TEST(TestTriangleMatrixLib, tr_matrix_add_value) { + // Arrange + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + size_t value = 10; + TriangleMatrix matrix(data, size); + TriangleMatrix result(size); + //Act + result = matrix + value; + // Assert + EXPECT_EQ(result.at(0).at(0), 11); + EXPECT_EQ(result.at(0).at(1), 12); + EXPECT_EQ(result.at(0).at(2), 13); + EXPECT_EQ(result.at(1).at(1), 14); + EXPECT_EQ(result.at(1).at(2), 15); + EXPECT_EQ(result.at(2).at(2), 16); +} +TEST(TestTriangleMatrixLib, tr_matrix_sub_value) { + // Arrange + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + size_t value = 1; + TriangleMatrix matrix(data, size); + TriangleMatrix result(size); + //Act + result = matrix - value; + // Assert + EXPECT_EQ(result.at(0).at(0), 0); + EXPECT_EQ(result.at(0).at(1), 1); + EXPECT_EQ(result.at(0).at(2), 2); + EXPECT_EQ(result.at(1).at(1), 3); + EXPECT_EQ(result.at(1).at(2), 4); + EXPECT_EQ(result.at(2).at(2), 5); +} +TEST(TestTriangleMatrixLib, tr_matrix_mult_value) { + // Arrange + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + size_t value = 10; + TriangleMatrix matrix(data, size); + TriangleMatrix result(size); + //Act + result = matrix * value; + // Assert + EXPECT_EQ(result.at(0).at(0), 10); + EXPECT_EQ(result.at(0).at(1), 20); + EXPECT_EQ(result.at(0).at(2), 30); + EXPECT_EQ(result.at(1).at(1), 40); + EXPECT_EQ(result.at(1).at(2), 50); + EXPECT_EQ(result.at(2).at(2), 60); +} +TEST(TestTriangleMatrixLib, tr_matrix_div_value) { + // Arrange + size_t size = 3; + int data[6] = { 2, 2, 4, 4, 8, 6 }; + size_t value = 2; + TriangleMatrix matrix(data, size); + TriangleMatrix result(size); + //Act + result = matrix / value; + // Assert + EXPECT_EQ(result.at(0).at(0), 1); + EXPECT_EQ(result.at(0).at(1), 1); + EXPECT_EQ(result.at(0).at(2), 2); + EXPECT_EQ(result.at(1).at(1), 2); + EXPECT_EQ(result.at(1).at(2), 4); + EXPECT_EQ(result.at(2).at(2), 3); +} +TEST(TestTriangleMatrixLib, tr_matrix_div_value_with_exception) { + // Arrange & Act + size_t size = 3; + int data[6] = { 2, 2, 4, 4, 8, 6 }; + size_t value = 0; + TriangleMatrix matrix(data, size); + // Assert + EXPECT_THROW(matrix / value, std::logic_error); +} +TEST(TestTriangleMatrixLib, tr_matrix_add_assign_value) { + // Arrange & Act + size_t size = 3; + int data[6] = { 2, 2, 4, 4, 8, 6 }; + size_t value = 2; + TriangleMatrix matrix(data, size); + matrix += value; + // Assert + EXPECT_EQ(matrix.at(0).at(0), 4); + EXPECT_EQ(matrix.at(0).at(1), 4); + EXPECT_EQ(matrix.at(0).at(2), 6); + EXPECT_EQ(matrix.at(1).at(1), 6); + EXPECT_EQ(matrix.at(1).at(2), 10); + EXPECT_EQ(matrix.at(2).at(2), 8); +} +TEST(TestTriangleMatrixLib, tr_matrix_sub_assign_value) { + // Arrange & Act + size_t size = 3; + int data[6] = { 2, 2, 4, 4, 8, 6 }; + size_t value = 2; + TriangleMatrix matrix(data, size); + matrix -= value; + // Assert + EXPECT_EQ(matrix.at(0).at(0), 0); + EXPECT_EQ(matrix.at(0).at(1), 0); + EXPECT_EQ(matrix.at(0).at(2), 2); + EXPECT_EQ(matrix.at(1).at(1), 2); + EXPECT_EQ(matrix.at(1).at(2), 6); + EXPECT_EQ(matrix.at(2).at(2), 4); +} +TEST(TestTriangleMatrixLib, tr_matrix_mult_assign_value) { + // Arrange & Act + size_t size = 3; + int data[6] = { 1, 2, 3, 4, 5, 6 }; + size_t value = 10; + TriangleMatrix matrix(data, size); + matrix *= value; + // Assert + EXPECT_EQ(matrix.at(0).at(0), 10); + EXPECT_EQ(matrix.at(0).at(1), 20); + EXPECT_EQ(matrix.at(0).at(2), 30); + EXPECT_EQ(matrix.at(1).at(1), 40); + EXPECT_EQ(matrix.at(1).at(2), 50); + EXPECT_EQ(matrix.at(2).at(2), 60); +} +TEST(TestTriangleMatrixLib, tr_matrix_div_assign_value) { + // Arrange & Act + size_t size = 3; + int data[6] = { 2, 2, 4, 4, 8, 6 }; + size_t value = 2; + TriangleMatrix matrix(data, size); + matrix /= value; + // Assert + EXPECT_EQ(matrix.at(0).at(0), 1); + EXPECT_EQ(matrix.at(0).at(1), 1); + EXPECT_EQ(matrix.at(0).at(2), 2); + EXPECT_EQ(matrix.at(1).at(1), 2); + EXPECT_EQ(matrix.at(1).at(2), 4); + EXPECT_EQ(matrix.at(2).at(2), 3); +} From f6170cc8c06dc78a1ab3b3a21c95d610ce1433b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 6 Oct 2025 23:52:20 +0300 Subject: [PATCH 063/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 99228672..304a2067 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -10,6 +10,7 @@ class TriangleMatrix : public Matrix { TriangleMatrix(const TriangleMatrix& other); ~TriangleMatrix(); + size_t getSize() const { return this->_M; } @@ -57,9 +58,21 @@ TriangleMatrix::TriangleMatrix(size_t size) : Matrix(size, size) { (*this)[i] = MathVector(size - i, i); } } -//template -//TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { -//} +template +TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { + if (size > 0 && data == nullptr) { + throw std::invalid_argument("Null data pointer with non-zero size"); + } + size_t data_index = 0; + for (size_t i = 0; i < size; ++i) { + // Создаем MathVector для строки i + (*this)[i] = MathVector(size - i, i); + // Заполняем элементы от i до size-1 + for (size_t j = i; j < size; ++j) { + (*this)[i][j] = data[data_index++]; + } + } +} template TriangleMatrix::TriangleMatrix(const TriangleMatrix& other) : Matrix(other) {} template From 6fa8ee3581edbada3428aad22c363f00aeffd510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 00:11:40 +0300 Subject: [PATCH 064/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20*=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86=D1=8B=20=D0=B8=20=D0=B2=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B0,=20+,=20-,=20+=3D,=20-=3D=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=B2=D1=83=D1=85=20=D0=BC=D0=B0=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 66 +++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index 304a2067..c4f1caa7 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -60,14 +60,9 @@ TriangleMatrix::TriangleMatrix(size_t size) : Matrix(size, size) { } template TriangleMatrix::TriangleMatrix(T* data, size_t size) : Matrix(size, size) { - if (size > 0 && data == nullptr) { - throw std::invalid_argument("Null data pointer with non-zero size"); - } size_t data_index = 0; for (size_t i = 0; i < size; ++i) { - // Создаем MathVector для строки i (*this)[i] = MathVector(size - i, i); - // Заполняем элементы от i до size-1 for (size_t j = i; j < size; ++j) { (*this)[i][j] = data[data_index++]; } @@ -166,3 +161,64 @@ TriangleMatrix& TriangleMatrix::operator /= (T value) { } return *this; } +template +MathVector TriangleMatrix::operator * (const MathVector& vector) const { + if (this->getSize() != vector.getSize()()) { + throw std::logic_error("Matrix columns must equal vector size"); + } + MathVector result(this->getSize()); + for (size_t i = 0; i < this->getSize(); ++i) { + result[i] = (*this)[i] * vector; + } + return result; +} +template +TriangleMatrix TriangleMatrix::operator + (const TriangleMatrix& other) const { + if (this->getSize() != other.getSize()()) { + throw std::logic_error("Matrices must have the same size"); + } + TriangleMatrix result(this->getSize()); + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j){ + result[i][j] = (*this)[i][j] + other[i][j]; + } + } + return result; +} +template +TriangleMatrix TriangleMatrix::operator - (const TriangleMatrix& other) const { + if (this->getSize() != other.getSize()()) { + throw std::logic_error("Matrices must have the same size"); + } + TriangleMatrix result(this->getSize()); + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j){ + result[i][j] = (*this)[i][j] - other[i][j]; + } + } + return result; +} +template +TriangleMatrix& TriangleMatrix::operator += (const TriangleMatrix& other) { + if (this->getSize() != other.getSize()()) { + throw std::logic_error("Matrices must have the same size"); + } + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + (*this)[i][j] += other[i][j]; + } + } + return *this; +} +template +TriangleMatrix& TriangleMatrix::operator -= (const TriangleMatrix& other) { + if (this->getSize() != other.getSize()()) { + throw std::logic_error("Matrices must have the same size"); + } + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + (*this)[i][j] -= other[i][j]; + } + } + return *this; +} From fd581a3432d5a511e704b48c57f4e37f3460e2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 00:15:46 +0300 Subject: [PATCH 065/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?=20=D1=81=D0=B8=D0=BD=D1=82=D0=B0=D0=BA=D1=81=D0=B8=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index c4f1caa7..db72ee1a 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -161,9 +161,10 @@ TriangleMatrix& TriangleMatrix::operator /= (T value) { } return *this; } + template MathVector TriangleMatrix::operator * (const MathVector& vector) const { - if (this->getSize() != vector.getSize()()) { + if (this->getSize() != vector.get_size() ) { throw std::logic_error("Matrix columns must equal vector size"); } MathVector result(this->getSize()); @@ -174,7 +175,7 @@ MathVector TriangleMatrix::operator * (const MathVector& vector) const } template TriangleMatrix TriangleMatrix::operator + (const TriangleMatrix& other) const { - if (this->getSize() != other.getSize()()) { + if (this->getSize() != other.getSize()) { throw std::logic_error("Matrices must have the same size"); } TriangleMatrix result(this->getSize()); @@ -187,7 +188,7 @@ TriangleMatrix TriangleMatrix::operator + (const TriangleMatrix& other) } template TriangleMatrix TriangleMatrix::operator - (const TriangleMatrix& other) const { - if (this->getSize() != other.getSize()()) { + if (this->getSize() != other.getSize()) { throw std::logic_error("Matrices must have the same size"); } TriangleMatrix result(this->getSize()); @@ -200,7 +201,7 @@ TriangleMatrix TriangleMatrix::operator - (const TriangleMatrix& other) } template TriangleMatrix& TriangleMatrix::operator += (const TriangleMatrix& other) { - if (this->getSize() != other.getSize()()) { + if (this->getSize() != other.getSize()) { throw std::logic_error("Matrices must have the same size"); } for (size_t i = 0; i < this->getSize(); ++i) { @@ -212,7 +213,7 @@ TriangleMatrix& TriangleMatrix::operator += (const TriangleMatrix& othe } template TriangleMatrix& TriangleMatrix::operator -= (const TriangleMatrix& other) { - if (this->getSize() != other.getSize()()) { + if (this->getSize() != other.getSize()) { throw std::logic_error("Matrices must have the same size"); } for (size_t i = 0; i < this->getSize(); ++i) { From 23ff54a82dae65b077493d3d2027001ddcf6d00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 00:25:15 +0300 Subject: [PATCH 066/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20main=20=D1=81=20=D0=BA=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE=D1=80=D0=BE=D0=BC=20-=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=B7=D0=B0=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86=20=D0=B8=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0,=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=20switch,=20=D1=83=D0=B1=D1=80=D0=B0=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B3=D0=BB=D1=83=D1=88=D0=BA=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=D0=B2=D0=BE=D0=B4=D0=B5=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 347 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 236 insertions(+), 111 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index bc6d33d8..3e651e30 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -147,144 +147,269 @@ void printMainMenu() { std::cout << " 9. Exit\n"; } +template +void fillMatrix(Matrix& matrix) { + for (size_t i = 0; i < matrix.getM(); ++i) { + for (size_t j = 0; j < matrix.getN(); ++j) { + T value; + std::cout << "Element [" << i << "][" << j << "]: "; + std::cin >> value; + matrix[i][j] = value; + } + } +} + +template +void fillTriangleMatrix(TriangleMatrix& matrix) { + for (size_t i = 0; i < matrix.getSize(); ++i) { + for (size_t j = i; j < matrix.getSize(); ++j) { + T value; + std::cout << "Element [" << i << "][" << j << "]: "; + std::cin >> value; + matrix[i][j] = value; + } + } +} + +template +void fillVector(MathVector& vector) { + for (size_t i = 0; i < vector.get_size(); ++i) { + T value; + std::cout << "Element [" << i << "]: "; + std::cin >> value; + vector[i] = value; + } +} + int main() { setlocale(LC_ALL, "Russian"); int user_choice; + do { printMainMenu(); std::cout << "Your choice: "; std::cin >> user_choice; - if (user_choice == 11) { + if (user_choice == 9) { break; } + int matrix_type; std::cout << "Choose matrix type:\n"; std::cout << "1. Regular matrix\n"; std::cout << "2. Triangular matrix\n"; std::cin >> matrix_type; - size_t M, N; - std::cout << "Enter dimensions of matrix 1 (M - rows, N - columns)\n"; - std::cout << "M = "; - std::cin >> M; - std::cout << "N = "; - std::cin >> N; - - Matrix matrix1(M, N); - - std::cout << "Enter elements of matrix 1:\n"; - system("pause"); + if (matrix_type == 1) { + // Regular Matrix operations + size_t M, N; + std::cout << "Enter dimensions of matrix 1 (M - rows, N - columns)\n"; + std::cout << "M = "; + std::cin >> M; + std::cout << "N = "; + std::cin >> N; - switch (user_choice) { - case 1: { - size_t K, L; - std::cout << "Enter dimensions of matrix 2 (K - rows, L - columns)\n"; - std::cout << "K = "; - std::cin >> K; - std::cout << "L = "; - std::cin >> L; - Matrix matrix2(K, L); + Matrix matrix1(M, N); + std::cout << "Enter elements of matrix 1:\n"; + fillMatrix(matrix1); - std::cout << "Enter elements of matrix 2:\n"; - system("pause"); - Matrix result = matrix1 + matrix2; - std::cout << "Result:\n" << result; - break; + switch (user_choice) { + case 1: { + std::cout << "Enter dimensions of matrix 2 (must be " << M << " x " << N << ")\n"; + Matrix matrix2(M, N); + std::cout << "Enter elements of matrix 2:\n"; + fillMatrix(matrix2); + Matrix result = matrix1 + matrix2; + std::cout << "Result:\n" << result; + break; + } + case 2: { + std::cout << "Enter dimensions of matrix 2 (must be " << M << " x " << N << ")\n"; + Matrix matrix2(M, N); + std::cout << "Enter elements of matrix 2:\n"; + fillMatrix(matrix2); + Matrix result = matrix1 - matrix2; + std::cout << "Result:\n" << result; + break; + } + case 3: { + size_t K; + std::cout << "Enter number of columns for matrix 2 (must be " << N << " rows)\n"; + std::cout << "K = "; + std::cin >> K; + Matrix matrix2(N, K); + std::cout << "Enter elements of matrix 2:\n"; + fillMatrix(matrix2); + Matrix result = matrix1 * matrix2; + std::cout << "Result:\n" << result; + break; + } + case 4: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + Matrix result = matrix1 + scalar; + std::cout << "Result:\n" << result; + break; + } + case 5: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + Matrix result = matrix1 - scalar; + std::cout << "Result:\n" << result; + break; + } + case 6: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + Matrix result = matrix1 * scalar; + std::cout << "Result:\n" << result; + break; + } + case 7: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + try { + Matrix result = matrix1 / scalar; + std::cout << "Result:\n" << result; + } + catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } + break; + } + case 8: { + size_t size; + std::cout << "Enter vector size (must be " << N << "): "; + std::cin >> size; + if (size != N) { + std::cout << "Error: Vector size must match matrix columns!\n"; + break; + } + MathVector mathvector(size); + std::cout << "Enter vector elements:\n"; + fillVector(mathvector); + try { + MathVector result = matrix1 * mathvector; + std::cout << "Result:\n" << result; + } + catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } + break; + } + default: { + std::cout << "Invalid input! Please try again\n"; + break; + } + } } - case 2: { - size_t K, L; - std::cout << "Enter dimensions of matrix 2 (K - rows, L - columns)\n"; - std::cout << "K = "; - std::cin >> K; - std::cout << "L = "; - std::cin >> L; - Matrix matrix2(K, L); - - std::cout << "Enter elements of matrix 2:\n"; - system("pause"); - Matrix result = matrix1 - matrix2; - std::cout << "Result:\n" << result; - break; - } - case 3: { - size_t K, L; - std::cout << "Enter dimensions of matrix 2 (K - rows, L - columns)\n"; - std::cout << "K = "; - std::cin >> K; - std::cout << "L = "; - std::cin >> L; - Matrix matrix2(K, L); - - std::cout << "Enter elements of matrix 2:\n"; - system("pause"); - Matrix result = matrix1 * matrix2; - std::cout << "Result:\n" << result; - break; - } - case 4: { - int scalar; - std::cout << "Enter scalar:\n"; - std::cin >> scalar; - system("pause"); - Matrix result = matrix1 + scalar; - std::cout << "Result:\n" << result; - break; - } - case 5: { - int scalar; - std::cout << "Enter scalar:\n"; - std::cin >> scalar; - system("pause"); - Matrix result = matrix1 - scalar; - std::cout << "Result:\n" << result; - break; - } - case 6: { - int scalar; - std::cout << "Enter scalar:\n"; - std::cin >> scalar; - system("pause"); - Matrix result = matrix1 * scalar; - std::cout << "Result:\n" << result; - break; - } - case 7: { - int scalar; - std::cout << "Enter scalar:\n"; - std::cin >> scalar; - system("pause"); - Matrix result = matrix1 / scalar; - std::cout << "Result:\n" << result; - break; - } - case 8: { + else if (matrix_type == 2) { + // Triangular Matrix operations size_t size; - std::cout << "Enter vector size:\n"; + std::cout << "Enter size of triangular matrix: "; std::cin >> size; - MathVector mathvector(size); - std::cout << "Enter vector elements:\n"; - system("pause"); - MathVector result = matrix1 * mathvector; - std::cout << "Result:\n" << result; - break; - } - case 9: { + TriangleMatrix matrix1(size); + std::cout << "Enter elements of triangular matrix (only upper triangle):\n"; + fillTriangleMatrix(matrix1); - break; - } - default: { - std::cout << "Invalid input! Please try again\n"; - break; + switch (user_choice) { + case 1: { + TriangleMatrix matrix2(size); + std::cout << "Enter elements of second triangular matrix:\n"; + fillTriangleMatrix(matrix2); + try { + TriangleMatrix result = matrix1 + matrix2; + std::cout << "Result:\n" << result; + } + catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } + break; + } + case 2: { + TriangleMatrix matrix2(size); + std::cout << "Enter elements of second triangular matrix:\n"; + fillTriangleMatrix(matrix2); + try { + TriangleMatrix result = matrix1 - matrix2; + std::cout << "Result:\n" << result; + } + catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } + break; + } + case 3: { + std::cout << "Matrix multiplication for triangular matrices not implemented in this example\n"; + break; + } + case 4: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + TriangleMatrix result = matrix1 + scalar; + std::cout << "Result:\n" << result; + break; + } + case 5: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + TriangleMatrix result = matrix1 - scalar; + std::cout << "Result:\n" << result; + break; + } + case 6: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + TriangleMatrix result = matrix1 * scalar; + std::cout << "Result:\n" << result; + break; + } + case 7: { + int scalar; + std::cout << "Enter scalar: "; + std::cin >> scalar; + try { + TriangleMatrix result = matrix1 / scalar; + std::cout << "Result:\n" << result; + } + catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } + break; + } + case 8: { + MathVector mathvector(size); + std::cout << "Enter vector elements:\n"; + fillVector(mathvector); + try { + MathVector result = matrix1 * mathvector; + std::cout << "Result:\n" << result; + } + catch (const std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } + break; + } + default: { + std::cout << "Invalid input! Please try again\n"; + break; + } + } } - } - std::cout << "\n Press Enter to continue..."; + std::cout << "\nPress Enter to continue..."; std::cin.ignore(); std::cin.get(); - } while (true); + system("cls"); + } while (true); return 0; } #endif - From f16c315e954f6fdb4c97dcb8583b839b99d61bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 00:50:58 +0300 Subject: [PATCH 067/275] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D1=86=D1=8B=20=D0=B8=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index db72ee1a..c0568ec0 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -164,13 +164,24 @@ TriangleMatrix& TriangleMatrix::operator /= (T value) { template MathVector TriangleMatrix::operator * (const MathVector& vector) const { - if (this->getSize() != vector.get_size() ) { + if (this->getSize() != vector.get_size()) { throw std::logic_error("Matrix columns must equal vector size"); } + MathVector result(this->getSize()); + for (size_t i = 0; i < this->getSize(); ++i) { - result[i] = (*this)[i] * vector; + T sum = T(); // Инициализируем нулем типа T + + // Ручное вычисление скалярного произведения + // Для треугольной матрицы: элементы ниже диагонали = 0 + for (size_t j = i; j < this->getSize(); ++j) { + sum += (*this)[i][j] * vector[j]; + } + + result[i] = sum; } + return result; } template @@ -223,3 +234,11 @@ TriangleMatrix& TriangleMatrix::operator -= (const TriangleMatrix& othe } return *this; } +template +TriangleMatrix TriangleMatrix::operator * (const TriangleMatrix& other) const { + if (this->getSize() != other.getSize()) { + throw std::logic_error("Matrices must have the same size"); + } + TriangleMatrix result(this->getSize()); + +} From ac1d6ecd7d53cf03b5a674668183e1eb32884c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 00:52:18 +0300 Subject: [PATCH 068/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20+,=20-,=20+=3D,=20-=3D,=20*=20=D0=B2=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B8=20=D1=81=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_triangle_matrix.cpp | 163 +++++++++++++++++++++++++++++++-- 1 file changed, 154 insertions(+), 9 deletions(-) diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index 0845af87..f650a943 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -56,7 +56,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_add_value) { // Arrange size_t size = 3; int data[6] = { 1, 2, 3, 4, 5, 6 }; - size_t value = 10; + int value = 10; TriangleMatrix matrix(data, size); TriangleMatrix result(size); //Act @@ -73,7 +73,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_sub_value) { // Arrange size_t size = 3; int data[6] = { 1, 2, 3, 4, 5, 6 }; - size_t value = 1; + int value = 1; TriangleMatrix matrix(data, size); TriangleMatrix result(size); //Act @@ -90,7 +90,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_mult_value) { // Arrange size_t size = 3; int data[6] = { 1, 2, 3, 4, 5, 6 }; - size_t value = 10; + int value = 10; TriangleMatrix matrix(data, size); TriangleMatrix result(size); //Act @@ -107,7 +107,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_div_value) { // Arrange size_t size = 3; int data[6] = { 2, 2, 4, 4, 8, 6 }; - size_t value = 2; + int value = 2; TriangleMatrix matrix(data, size); TriangleMatrix result(size); //Act @@ -131,9 +131,9 @@ TEST(TestTriangleMatrixLib, tr_matrix_div_value_with_exception) { } TEST(TestTriangleMatrixLib, tr_matrix_add_assign_value) { // Arrange & Act - size_t size = 3; + int size = 3; int data[6] = { 2, 2, 4, 4, 8, 6 }; - size_t value = 2; + int value = 2; TriangleMatrix matrix(data, size); matrix += value; // Assert @@ -148,7 +148,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_sub_assign_value) { // Arrange & Act size_t size = 3; int data[6] = { 2, 2, 4, 4, 8, 6 }; - size_t value = 2; + int value = 2; TriangleMatrix matrix(data, size); matrix -= value; // Assert @@ -163,7 +163,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_mult_assign_value) { // Arrange & Act size_t size = 3; int data[6] = { 1, 2, 3, 4, 5, 6 }; - size_t value = 10; + int value = 10; TriangleMatrix matrix(data, size); matrix *= value; // Assert @@ -178,7 +178,7 @@ TEST(TestTriangleMatrixLib, tr_matrix_div_assign_value) { // Arrange & Act size_t size = 3; int data[6] = { 2, 2, 4, 4, 8, 6 }; - size_t value = 2; + int value = 2; TriangleMatrix matrix(data, size); matrix /= value; // Assert @@ -189,3 +189,148 @@ TEST(TestTriangleMatrixLib, tr_matrix_div_assign_value) { EXPECT_EQ(matrix.at(1).at(2), 4); EXPECT_EQ(matrix.at(2).at(2), 3); } +TEST(TestTriangleMatrixLib, tr_matrix_add_tr_matrix) { + // Arrange + size_t size = 3; + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[6] = { 1, 1, 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, size); + TriangleMatrix matrix2(data2, size); + TriangleMatrix result(size); + //Act + result = matrix1 + matrix2; + // Assert + EXPECT_EQ(result.at(0).at(0), 3); + EXPECT_EQ(result.at(0).at(1), 3); + EXPECT_EQ(result.at(0).at(2), 5); + EXPECT_EQ(result.at(1).at(1), 5); + EXPECT_EQ(result.at(1).at(2), 9); + EXPECT_EQ(result.at(2).at(2), 7); +} +TEST(TestTriangleMatrixLib, tr_matrix_sub_tr_matrix) { + // Arrange + size_t size = 3; + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[6] = { 1, 1, 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, size); + TriangleMatrix matrix2(data2, size); + TriangleMatrix result(size); + //Act + result = matrix1 - matrix2; + // Assert + EXPECT_EQ(result.at(0).at(0), 1); + EXPECT_EQ(result.at(0).at(1), 1); + EXPECT_EQ(result.at(0).at(2), 3); + EXPECT_EQ(result.at(1).at(1), 3); + EXPECT_EQ(result.at(1).at(2), 7); + EXPECT_EQ(result.at(2).at(2), 5); +} +TEST(TestTriangleMatrixLib, tr_matrix_add_assig_tr_matrix) { + // Arrange & Act + size_t size = 3; + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[6] = { 1, 1, 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, size); + TriangleMatrix matrix2(data2, size); + matrix1 += matrix2; + // Assert + EXPECT_EQ(matrix1.at(0).at(0), 3); + EXPECT_EQ(matrix1.at(0).at(1), 3); + EXPECT_EQ(matrix1.at(0).at(2), 5); + EXPECT_EQ(matrix1.at(1).at(1), 5); + EXPECT_EQ(matrix1.at(1).at(2), 9); + EXPECT_EQ(matrix1.at(2).at(2), 7); +} +TEST(TestTriangleMatrixLib, tr_matrix_sub_assig_tr_matrix) { + // Arrange & Act + size_t size = 3; + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[6] = { 1, 1, 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, size); + TriangleMatrix matrix2(data2, size); + matrix1 -= matrix2; + // Assert + EXPECT_EQ(matrix1.at(0).at(0), 1); + EXPECT_EQ(matrix1.at(0).at(1), 1); + EXPECT_EQ(matrix1.at(0).at(2), 3); + EXPECT_EQ(matrix1.at(1).at(1), 3); + EXPECT_EQ(matrix1.at(1).at(2), 7); + EXPECT_EQ(matrix1.at(2).at(2), 5); +} +TEST(TestTriangleMatrixLib, tr_matrix_add_tr_matrix_with_exception) { + // Arrange & Act + + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[4] = { 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, 3); + TriangleMatrix matrix2(data2, 2); + + // Assert + EXPECT_THROW(matrix1 + matrix2, std::logic_error); +} +TEST(TestTriangleMatrixLib, tr_matrix_sub_tr_matrix_with_exception) { + // Arrange & Act + + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[4] = { 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, 3); + TriangleMatrix matrix2(data2, 2); + + // Assert + EXPECT_THROW(matrix1 - matrix2, std::logic_error); +} +TEST(TestTriangleMatrixLib, tr_matrix_add_assign_tr_matrix_with_exception) { + // Arrange & Act + + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[4] = { 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, 3); + TriangleMatrix matrix2(data2, 2); + + // Assert + EXPECT_THROW(matrix1 += matrix2, std::logic_error); +} +TEST(TestTriangleMatrixLib, tr_matrix_sub_assign_tr_matrix_with_exception) { + // Arrange & Act + + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[4] = { 1, 1, 1, 1 }; + + TriangleMatrix matrix1(data1, 3); + TriangleMatrix matrix2(data2, 2); + + // Assert + EXPECT_THROW(matrix1 -= matrix2, std::logic_error); +} +TEST(TestTriangleMatrixLib, tr_matrix_mult_vector) { + // Arrange + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[3] = { 1, 1, 1 }; + + TriangleMatrix matrix(data1, 3); + MathVector vector(data2, 3); + MathVector result(3); + //Act + result = matrix * vector; + // Assert + EXPECT_EQ(result.at(0), 8); + EXPECT_EQ(result.at(1), 12); + EXPECT_EQ(result.at(2), 6); +} +TEST(TestTriangleMatrixLib, tr_matrix_mult_vector_with_exception) { + // Arrange & Act + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + TriangleMatrix matrix(data1, 3); + MathVector vector(data2, 10); + MathVector result(3); + // Assert + EXPECT_THROW(matrix * vector, std::logic_error); +} From 224cdde464d5cd2f26f7b9a98e60d37fc8a109ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 01:08:57 +0300 Subject: [PATCH 069/275] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20*=20=D0=B4=D0=B2=D1=83=D1=85=20=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D1=83=D0=B3=D0=BE=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D1=80=D0=B8=D1=86=20(+=20=D1=81=D0=BB=D1=83=D1=87=D0=B0?= =?UTF-8?q?=D0=B9=20=D1=81=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=D0=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_triangle_matrix.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_triangle_matrix.cpp b/tests/test_triangle_matrix.cpp index f650a943..560a8c4b 100644 --- a/tests/test_triangle_matrix.cpp +++ b/tests/test_triangle_matrix.cpp @@ -334,3 +334,31 @@ TEST(TestTriangleMatrixLib, tr_matrix_mult_vector_with_exception) { // Assert EXPECT_THROW(matrix * vector, std::logic_error); } +TEST(TestTriangleMatrixLib, tr_matrix_mult_tr_matrix) { + // Arrange + size_t size = 3; + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[6] = { 1, 1, 1, 1, 1, 1 }; + TriangleMatrix matrix1(data1, size); + TriangleMatrix matrix2(data2, size); + TriangleMatrix result(size); + //Act + result = matrix1 * matrix2; + // Assert + EXPECT_EQ(result.at(0).at(0), 2); + EXPECT_EQ(result.at(0).at(1), 4); + EXPECT_EQ(result.at(0).at(2), 8); + EXPECT_EQ(result.at(1).at(1), 4); + EXPECT_EQ(result.at(1).at(2), 12); + EXPECT_EQ(result.at(2).at(2), 6); +} +TEST(TestTriangleMatrixLib, tr_matrix_mult_tr_matrix_with_exception) { + // Arrange & Act + int data1[6] = { 2, 2, 4, 4, 8, 6 }; + int data2[8] = { 1, 1, 1, 1, 1, 1, 1, 1 }; + TriangleMatrix matrix1(data1, 3); + TriangleMatrix matrix2(data2, 4); + TriangleMatrix result(3); + // Assert + EXPECT_THROW(matrix1 * matrix2, std::logic_error); +} From 032e29715a2cd0ddc8a6bbb16ccc7cbd3b39a83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 01:15:15 +0300 Subject: [PATCH 070/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20*?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_triangle_matrix/triangle_matrix.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib_triangle_matrix/triangle_matrix.h b/lib_triangle_matrix/triangle_matrix.h index c0568ec0..d4a05bb3 100644 --- a/lib_triangle_matrix/triangle_matrix.h +++ b/lib_triangle_matrix/triangle_matrix.h @@ -161,27 +161,19 @@ TriangleMatrix& TriangleMatrix::operator /= (T value) { } return *this; } - template MathVector TriangleMatrix::operator * (const MathVector& vector) const { if (this->getSize() != vector.get_size()) { throw std::logic_error("Matrix columns must equal vector size"); } - MathVector result(this->getSize()); - for (size_t i = 0; i < this->getSize(); ++i) { - T sum = T(); // Инициализируем нулем типа T - - // Ручное вычисление скалярного произведения - // Для треугольной матрицы: элементы ниже диагонали = 0 + T sum = T(); for (size_t j = i; j < this->getSize(); ++j) { sum += (*this)[i][j] * vector[j]; } - result[i] = sum; } - return result; } template @@ -240,5 +232,14 @@ TriangleMatrix TriangleMatrix::operator * (const TriangleMatrix& other) throw std::logic_error("Matrices must have the same size"); } TriangleMatrix result(this->getSize()); - + for (size_t i = 0; i < this->getSize(); ++i) { + for (size_t j = i; j < this->getSize(); ++j) { + T sum = T(); + for (size_t k = i; k <= j; ++k) { + sum += (*this)[i][k] * other[k][j]; + } + result[i][j] = sum; + } + } + return result; } From 12a4eae71ec7b966a4a3a0e0ab447273f1112dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 18:15:03 +0300 Subject: [PATCH 071/275] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B8=D0=B7=20=D0=A2=D0=9A:=20=D0=BF=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=20=D0=BB=D0=BE=D0=BA=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BC=D0=B8=D0=BD=D0=B8=D0=BC=D1=83=D0=BC=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib_algoritm/algoritm.h diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h new file mode 100644 index 00000000..8f69bcb9 --- /dev/null +++ b/lib_algoritm/algoritm.h @@ -0,0 +1,55 @@ +#pragma once +#include "../lib_matrix/matrix.h" +#include +#include +template +std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { + T min_val = matrix[x][y]; + size_t min_x = x; + size_t min_y = y; + if (x > 0 && matrix[x - 1][y] < min_val) { + min_val = matrix[x - 1][y]; + min_x = x - 1; + min_y = y; + } + if (x < matrix.getM() - 1 && matrix[x + 1][y] < min_val) { + min_val = matrix[x + 1][y]; + min_x = x + 1; + min_y = y; + } + if (y > 0 && matrix[x][y - 1] < min_val) { + min_val = matrix[x][y - 1]; + min_x = x; + min_y = y - 1; + } + if (y < matrix.getN() - 1 && matrix[x][y + 1] < min_val) { + min_val = matrix[x][y + 1]; + min_x = x; + min_y = y + 1; + } + return { min_x, min_y }; +} +size_t getRandomIndex(size_t max) { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dist(0, max - 1); + return dist(gen); +} +template +T find_local_minimum(const Matrix& matrix) { + size_t x = getRandomIndex(matrix.getM()); + size_t y = getRandomIndex(matrix.getN()); + + while (true) { + std::pair min_coords = find_min_neighbor_coords(matrix, x, y); + size_t new_x = min_coords.first; + size_t new_y = min_coords.second; + + if (new_x == x && new_y == y) { + return matrix[x][y]; + } + + x = new_x; + y = new_y; + } +} From dfeff048ace158e9fef4fb2a7232623f5e248585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 7 Oct 2025 18:53:05 +0300 Subject: [PATCH 072/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/test_algoritm.cpp diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp new file mode 100644 index 00000000..ac26dbf0 --- /dev/null +++ b/tests/test_algoritm.cpp @@ -0,0 +1,68 @@ +#include "../lib_algoritm/algoritm.h" +#include +#include +TEST(TestAlgoritmLib, test_for_one_local_min) { + int data[9] = { 1, 20, 30, + 40, 50, 60 , + 70, 80, 90 }; + Matrix matrix(data, 3, 3); + int result = find_local_minimum(matrix); + EXPECT_EQ(result, 1); +} +TEST(TestAlgoritmLib, test_for_many_local_min) { + int data[9] = { 1, 4, 3, + 8, 10, 6 , + 7, 9, 2 }; + Matrix matrix(data, 3, 3); + int result = find_local_minimum(matrix); + EXPECT_TRUE(result == 1 || result == 3 || result == 7 || result == 2); +} +TEST(TestAlgoritmLib, test_top_left_corner_minimum) { + int data[9] = { 1, 3, 4, + 2, 5, 6, + 7, 8, 9 }; + Matrix matrix(data, 3, 3); + int result = find_local_minimum(matrix); + + EXPECT_TRUE(result == 1); +} +TEST(TestAlgoritmLib, test_bottom_right_corner_minimum) { + int data[9] = { 9, 8, 7, + 6, 5, 4, + 3, 2, 1 }; + Matrix matrix(data, 3, 3); + int result = find_local_minimum(matrix); + EXPECT_EQ(result, 1); +} +TEST(TestAlgoritmLib, test_center_minimum) { + int data[9] = { 9, 8, 7, + 6, 1, 5, + 4, 2, 3 }; + Matrix matrix(data, 3, 3); + int result = find_local_minimum(matrix); + EXPECT_EQ(result, 1); +} +TEST(TestAlgoritmLib, test_single_element) { + int data[1] = { 5 }; + Matrix matrix(data, 1, 1); + int result = find_local_minimum(matrix); + EXPECT_EQ(result, 5); +} +TEST(TestAlgoritmLib, test_2x2_matrix) { + int data[4] = { 4, 3, + 1, 2 }; + Matrix matrix(data, 2, 2); + int result = find_local_minimum(matrix); + EXPECT_TRUE(result == 3 || result == 1); +} +TEST(TestAlgoritmLib, test_5x5_matrix) { + int data[25] = { 10, 11, 12, 13, 14, + 15, 16, 1, 17, 18, + 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, + 29, 30, 31, 32, 33 }; + Matrix matrix(data, 5, 5); + int result = find_local_minimum(matrix); + EXPECT_TRUE(result == 1 || result == 10 || result == 14 || result == 19 + || result == 23 || result == 29 || result == 33); +} From c1ab29cce511f76912292b9a8d8825dec941b04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 10 Oct 2025 16:56:48 +0300 Subject: [PATCH 073/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lib_stack/stack.h diff --git a/lib_stack/stack.h b/lib_stack/stack.h new file mode 100644 index 00000000..9fd08547 --- /dev/null +++ b/lib_stack/stack.h @@ -0,0 +1,16 @@ +#pragma once +#include "../lib_vector/vector.h" +template +class Stack { +private: + Tvector _data; +public: + Stack(size_t size); + Stack(const Stack& other); + void push(int value); + void pop(); + inline int top() const; + inline bool is_empty() const noexcept; + inline bool is_full() const noexcept; + void clear(); noexcept; +}; From 2b70b8dfc7c8dc2b37d272562d8fcec3ef85127b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 11:52:18 +0300 Subject: [PATCH 074/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B:=202=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D0=B0=20(=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D1=83=20=D0=B8=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F),=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20getData(),=20?= =?UTF-8?q?push(),=20pop(),=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6=D0=B5=20?= =?UTF-8?q?=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B=20=3D=3D?= =?UTF-8?q?=20=D0=B8=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.h | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index 9fd08547..1246a9e3 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -6,11 +6,50 @@ class Stack { Tvector _data; public: Stack(size_t size); - Stack(const Stack& other); - void push(int value); + Stack(const Stack& other); + const Tvector& getData() const; + Stack& operator=(const Stack& other); + bool operator ==(const Stack& other) const; + void push(T value); void pop(); - inline int top() const; + inline T top() const; inline bool is_empty() const noexcept; inline bool is_full() const noexcept; - void clear(); noexcept; + void clear() noexcept; }; +template +Stack::Stack(size_t size): _data(size) {} +template +Stack::Stack(const Stack& other): _data(other.getData()) {} +template +const Tvector& Stack::getData() const { + return _data; +} +template +Stack& Stack::operator=(const Stack& other) { + if (this != &other) { + _data = other.getData(); + } + return *this; +} + +template + +bool Stack::operator ==(const Stack& other) const { + return _data == other.getData(); +} + +template +void Stack::push(T value) { + if (is_full()) { + throw std::logic_error("You can't push the element, because stack is full"); + } + _data.push_back(value); +} +template +void Stack::pop() { + if (is_empty()) { + throw std::logic_error("You can't pop the element, because stack is empty"); + } + _data.pop_back(); +} From 63f1ec18e9a74d15ebfd3811faa9381a8b1d6172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 12:29:57 +0300 Subject: [PATCH 075/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=B2=D1=81=D0=B5=20=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=B4=D1=8B:=20top(),=20is=5Fempty(),=20is=5Ffull(?= =?UTF-8?q?)=20=D0=B8=20=D1=82.=D0=B4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.h | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index 1246a9e3..33496a88 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -7,7 +7,9 @@ class Stack { public: Stack(size_t size); Stack(const Stack& other); + const Tvector& getData() const; + Stack& operator=(const Stack& other); bool operator ==(const Stack& other) const; void push(T value); @@ -18,7 +20,8 @@ class Stack { void clear() noexcept; }; template -Stack::Stack(size_t size): _data(size) {} +Stack::Stack(size_t size): _data(size) { +} template Stack::Stack(const Stack& other): _data(other.getData()) {} template @@ -32,13 +35,19 @@ Stack& Stack::operator=(const Stack& other) { } return *this; } - template - -bool Stack::operator ==(const Stack& other) const { +bool Stack::operator ==(const Stack& other) const { return _data == other.getData(); } +template +inline T Stack::top() const { + if (is_empty()) { + throw std::logic_error("Stack is empty, you can't get top element's index"); + } + return _data.back(); +} + template void Stack::push(T value) { if (is_full()) { @@ -53,3 +62,18 @@ void Stack::pop() { } _data.pop_back(); } + +template + +inline bool Stack::is_empty() const noexcept { + return _data.is_empty(); +} +template + +inline bool Stack::is_full() const noexcept { + return _data.is_full(); +} +template +void Stack::clear() noexcept { + _data.clear(); +} From e6d23ac597ac8eab9df39be8ccff03144e1ab008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 12:30:49 +0300 Subject: [PATCH 076/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20?= =?UTF-8?q?push()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_stack.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/test_stack.cpp diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp new file mode 100644 index 00000000..6295757d --- /dev/null +++ b/tests/test_stack.cpp @@ -0,0 +1,31 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_stack/stack.h" + +TEST(TestStackLib, stack_constructor_with_size) { + // Arrange & Act + size_t size = 10; + Stack stack(size); + // Assert + EXPECT_EQ(stack.getData().get_size(), size); + EXPECT_EQ(stack.getData().get_capacity(), size+15); +} +TEST(TestStackLib, stack_copy_constructor) { + // Arrange & Act + size_t size = 10; + Stack stack1(size); + Stack stack2(stack1); + // Assert + EXPECT_TRUE(stack1 == stack2); +} +TEST(TestStackLib, stack_push_element) { + // Arrange + size_t size = 10; + int element = 100; + Stack stack(size); + // Act + stack.push(element); + // Assert + EXPECT_EQ(stack.top(), element); +} From c4a2bbb95d7fc4588f690d6ab921eb7d0a845c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 12:32:48 +0300 Subject: [PATCH 077/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index 33496a88..51ce369a 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -7,9 +7,7 @@ class Stack { public: Stack(size_t size); Stack(const Stack& other); - const Tvector& getData() const; - Stack& operator=(const Stack& other); bool operator ==(const Stack& other) const; void push(T value); @@ -39,7 +37,6 @@ template bool Stack::operator ==(const Stack& other) const { return _data == other.getData(); } - template inline T Stack::top() const { if (is_empty()) { @@ -47,7 +44,6 @@ inline T Stack::top() const { } return _data.back(); } - template void Stack::push(T value) { if (is_full()) { @@ -62,14 +58,11 @@ void Stack::pop() { } _data.pop_back(); } - template - inline bool Stack::is_empty() const noexcept { return _data.is_empty(); } template - inline bool Stack::is_full() const noexcept { return _data.is_full(); } From 90d03ae7774d519b983f68965e57b3bfb782be3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 12:42:40 +0300 Subject: [PATCH 078/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib_vector/vector.h b/lib_vector/vector.h index 0dcdbfae..70a2233b 100644 --- a/lib_vector/vector.h +++ b/lib_vector/vector.h @@ -24,23 +24,21 @@ T* _data; State* _states; size_t _deleted; - size_t get_real_position(size_t busy_index) const noexcept; void resize(size_t new_size); void resize(size_t new_size, const T& value); void shrink_to_fit(); void reserve(size_t new_capacity); void compact_storage(); - inline bool is_full() const noexcept { - return _size == _capacity; - } public: Tvector() noexcept; Tvector(size_t size); Tvector(T* data, size_t size); Tvector(const Tvector& other_vector); virtual ~Tvector() noexcept; - + inline bool is_full() const noexcept { + return _size == _capacity; + } inline bool is_empty() const noexcept { return _size == 0; } From b86a0c03725f186605b7f1c5035178c06b80d93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 13:32:29 +0300 Subject: [PATCH 079/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20push(),=20pop(),=20is=5Ffull(),=20is=5Fempty(),=20clea?= =?UTF-8?q?r()=20+=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B8=20=D1=81=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_stack.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp index 6295757d..704c51e7 100644 --- a/tests/test_stack.cpp +++ b/tests/test_stack.cpp @@ -29,3 +29,77 @@ TEST(TestStackLib, stack_push_element) { // Assert EXPECT_EQ(stack.top(), element); } + + + + + + +TEST(TestStackLib, stack_push_element_with_exception) { + // Arrange + size_t size = 0; + int element = 100; + Stack stack(size); + // Act + for (size_t i = 0; i < 15; ++i) { + stack.push(element); + } + // Assert + EXPECT_THROW(stack.push(element), std::logic_error); +} +TEST(TestStackLib, stack_pop_element_with_exception) { + // Arrange & Act + size_t size = 0; + Stack stack(size); + // Assert + EXPECT_THROW(stack.pop(), std::logic_error); +} +TEST(TestStackLib, stack_pop_element) { + // Arrange + size_t size = 10; + int element1 = 100; + int element2 = 200; + int element3 = 300; + + Stack stack(size); + // Act + stack.push(element1); + stack.push(element2); + stack.push(element3); + stack.pop(); + // Assert + EXPECT_EQ(stack.top(), element2); +} +TEST(TestStackLib, stack_is_full) { + // Arrange + size_t size = 0; + int element = 300; + Stack stack(size); + // Act + for (size_t i = 0; i < 15; ++i) { + stack.push(element); + } + // Assert + EXPECT_TRUE(stack.is_full()); +} +TEST(TestStackLib, stack_is_empty) { + // Arrange + size_t size = 0; + Stack stack(size); + // Assert + EXPECT_TRUE(stack.is_empty()); +} +TEST(TestStackLib, stack_clear) { + // Arrange + size_t size = 0; + int element = 300; + + Stack stack(size); + // Act + for (size_t i = 0; i < 15; ++i) { + stack.push(element); + } + stack.clear(); + // Assert + EXPECT_TRUE(stack.is_empty()); +} From 8caa1ca6c6145c0b27b90391a9ac49e228167639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 13:43:41 +0300 Subject: [PATCH 080/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20top()=20+=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5,=20=D0=BD=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D1=8B=20=3D=3D=20=D0=B8=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_stack.cpp | 61 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp index 704c51e7..e06249db 100644 --- a/tests/test_stack.cpp +++ b/tests/test_stack.cpp @@ -29,12 +29,6 @@ TEST(TestStackLib, stack_push_element) { // Assert EXPECT_EQ(stack.top(), element); } - - - - - - TEST(TestStackLib, stack_push_element_with_exception) { // Arrange size_t size = 0; @@ -60,7 +54,6 @@ TEST(TestStackLib, stack_pop_element) { int element1 = 100; int element2 = 200; int element3 = 300; - Stack stack(size); // Act stack.push(element1); @@ -86,7 +79,7 @@ TEST(TestStackLib, stack_is_empty) { // Arrange size_t size = 0; Stack stack(size); - // Assert + // Assert & Act EXPECT_TRUE(stack.is_empty()); } TEST(TestStackLib, stack_clear) { @@ -103,3 +96,55 @@ TEST(TestStackLib, stack_clear) { // Assert EXPECT_TRUE(stack.is_empty()); } +TEST(TestStackLib, stack_top) { + // Arrange + size_t size = 0; + int common_element = 100; + int last_element = 999; + Stack stack(size); + // Act + for (size_t i = 0; i < 14; ++i) { + stack.push(common_element); + } + stack.push(last_element); + // Assert + EXPECT_EQ(stack.top(), last_element); +} +TEST(TestStackLib, stack_top_with_exception) { + // Arrange & Act + size_t size = 0; + Stack stack(size); + // Assert + EXPECT_THROW(stack.top(), std::logic_error); +} +TEST(TestStackLib, stack_operator_compare_expect_true) { + // Arrange & Act + size_t size = 10; + Stack stack(size); + Stack stack_other(size); + // Assert + EXPECT_TRUE(stack == stack_other); +} +TEST(TestStackLib, stack_operator_compare_expect_false) { + // Arrange & Act + size_t size1 = 10; + size_t size2 = 20; + Stack stack(size1); + Stack stack_other(size2); + // Assert + EXPECT_FALSE(stack == stack_other); +} +TEST(TestStackLib, stack_operator_assign) { + //Arrange + size_t size1 = 10; + size_t size2 = 20; + Stack stack(size1); + Stack stack_other(size2); + //Act + stack_other.push(100); + stack_other.push(200); + stack = stack_other; + //Assert + EXPECT_TRUE(stack == stack_other); + EXPECT_EQ(stack.top(), 200); +} From aff94bdc14760d6e167db976bb0ea33f0e8deaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 13:49:03 +0300 Subject: [PATCH 081/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20!?= =?UTF-8?q?=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_stack.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_stack.cpp b/tests/test_stack.cpp index e06249db..f67cbdde 100644 --- a/tests/test_stack.cpp +++ b/tests/test_stack.cpp @@ -148,3 +148,20 @@ TEST(TestStackLib, stack_operator_assign) { EXPECT_TRUE(stack == stack_other); EXPECT_EQ(stack.top(), 200); } +TEST(TestStackLib, stack_operator_not_compare_expect_true) { + // Arrange & Act + size_t size = 10; + Stack stack(size); + Stack stack_other(size); + // Assert + EXPECT_FALSE(stack != stack_other); +} +TEST(TestStackLib, stack_operator_not_compare_expect_false) { + // Arrange & Act + size_t size1 = 10; + size_t size2 = 20; + Stack stack(size1); + Stack stack_other(size2); + // Assert + EXPECT_TRUE(stack != stack_other); +} From 84177c1c820ed7950ae15d42d41537dc498fa2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 12 Oct 2025 13:49:43 +0300 Subject: [PATCH 082/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20!=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index 51ce369a..b70bd318 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -10,6 +10,7 @@ class Stack { const Tvector& getData() const; Stack& operator=(const Stack& other); bool operator ==(const Stack& other) const; + bool operator !=(const Stack& other) const; void push(T value); void pop(); inline T top() const; @@ -38,6 +39,10 @@ bool Stack::operator ==(const Stack& other) const { return _data == other.getData(); } template +bool Stack::operator !=(const Stack& other) const { + return !(_data == other.getData()); +} +template inline T Stack::top() const { if (is_empty()) { throw std::logic_error("Stack is empty, you can't get top element's index"); From 901810f4485dedd315d9a2c4e828902cf248046c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 21 Oct 2025 10:15:13 +0300 Subject: [PATCH 083/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20check=5Fbrackets=20=D0=98=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BD=D0=B5=D1=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 24 ++++++++++++++++++++++ tests/test_algoritm.cpp | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 8f69bcb9..8fb206bd 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -1,5 +1,6 @@ #pragma once #include "../lib_matrix/matrix.h" +#include "../lib_stack/stack.h" #include #include template @@ -53,3 +54,26 @@ T find_local_minimum(const Matrix& matrix) { y = new_y; } } +bool check_brackets(std::string str) { + Stack stack(str.length()); + stack.clear(); + for (char c : str) { + if (c == '(' || c == '[' || c == '{') { + stack.push(c); + } + else if (c == ')' || c == ']' || c == '}') { + if (stack.is_empty()) { + return false; + } + char top = stack.top(); + stack.pop(); + + if ((c == ')' && top != '(') || + (c == ']' && top != '[') || + (c == '}' && top != '{')) { + return false; + } + } + } + return stack.is_empty(); +} diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index ac26dbf0..7e5dd841 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -66,3 +66,48 @@ TEST(TestAlgoritmLib, test_5x5_matrix) { EXPECT_TRUE(result == 1 || result == 10 || result == 14 || result == 19 || result == 23 || result == 29 || result == 33); } +TEST(TestAlgorithmLib, test_check_brackets_false1) { + std::string string = ")"; + EXPECT_EQ(check_brackets(string), false); + +} +TEST(TestAlgorithmLib, test_check_brackets_false2) { + std::string string = "["; + EXPECT_EQ(check_brackets(string), false); +} +TEST(TestAlgorithmLib, test_check_brackets_false3) { + std::string string = "())"; + EXPECT_EQ(check_brackets(string), false); +} +TEST(TestAlgorithmLib, test_check_brackets_false4) { + std::string string = "({)"; + EXPECT_EQ(check_brackets(string), false); +} +TEST(TestAlgorithmLib, test_check_brackets_false5) { + std::string string = "({})]"; + EXPECT_EQ(check_brackets(string), false); +} +TEST(TestAlgorithmLib, test_check_brackets_true1) { + std::string string = "()"; + EXPECT_EQ(check_brackets(string), true); +} +TEST(TestAlgorithmLib, test_check_brackets_true2) { + std::string string = "{}"; + EXPECT_EQ(check_brackets(string), true); +} +TEST(TestAlgorithmLib, test_check_brackets_true3) { + std::string string = "[()]"; + EXPECT_EQ(check_brackets(string), true); +} +TEST(TestAlgorithmLib, test_check_brackets_true4) { + std::string string = "{[()]}"; + EXPECT_EQ(check_brackets(string), true); +} +TEST(TestAlgorithmLib, test_check_brackets_true5) { + std::string string = "()[]{}"; + EXPECT_EQ(check_brackets(string), true); +} +TEST(TestAlgorithmLib, test_check_brackets_true6) { + std::string string = ""; + EXPECT_EQ(check_brackets(string), true); +} From d4c6c473eb3d8392398517f12e4ba07354b127a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 11:57:07 +0300 Subject: [PATCH 084/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20read=5Fexpression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 8fb206bd..1be5f29d 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -3,6 +3,9 @@ #include "../lib_stack/stack.h" #include #include +#include +#include +#include template std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { T min_val = matrix[x][y]; @@ -77,3 +80,74 @@ bool check_brackets(std::string str) { } return stack.is_empty(); } +void read_expression(std::string expression) { + std::string brackets; + enum State { + EXPECT_OPERAND, + EXPECT_OPERATOR, + EXPECT_OPERAND_OR_UNARY + }; + State current_state = EXPECT_OPERAND; + bool has_operand = false; + for (size_t i = 0; i < expression.length(); ++i) { + char c = expression[i]; + if (std::isspace(c)) { + continue; + } + // обработка скобок + if (c == '(' || c == '{' || c == '[' || c == ')' || c == '}' || c == ']') { + brackets += c; + if (c == '(' || c == '{' || c == '[') { + if (current_state != EXPECT_OPERAND && current_state != EXPECT_OPERAND_OR_UNARY) { + throw std::invalid_argument("Operand expected before opening bracket"); + } + current_state = EXPECT_OPERAND; + } + else { // закрывающая скобка + if (current_state == EXPECT_OPERAND) { + throw std::invalid_argument("Empty brackets"); + } + current_state = EXPECT_OPERATOR; + } + } + // обработка операторов + else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') { + if ((c == '+' || c == '-') && + (current_state == EXPECT_OPERAND || current_state == EXPECT_OPERAND_OR_UNARY)) { + // унарный оператор - ожидаем операнд + current_state = EXPECT_OPERAND; + continue; + } + if (current_state != EXPECT_OPERATOR) { + throw std::invalid_argument("Operator '" + std::string(1, c) + "' in wrong position"); + } + current_state = EXPECT_OPERAND_OR_UNARY; + } + // обработка операндов + else if (std::isalnum(c)) { + if (current_state != EXPECT_OPERAND && current_state != EXPECT_OPERAND_OR_UNARY) { + throw std::invalid_argument("Operator expected before operand"); + } + has_operand = true; + // пропускаем операнд + while (i < expression.length() && std::isalnum(expression[i])) { + i++; + } + i--; + current_state = EXPECT_OPERATOR; + } + else { + throw std::invalid_argument("Invalid character '" + std::string(1, c) + "' in expression"); + } + } + // проверка скобок + if (!check_brackets(brackets)) { + throw std::invalid_argument("Unbalanced brackets"); + } + if (current_state == EXPECT_OPERAND || current_state == EXPECT_OPERAND_OR_UNARY) { + throw std::invalid_argument("Missing operand at the end of expression"); + } + if (!has_operand) { + throw std::invalid_argument("No operands in expression"); + } +} From ceb73302388022fea5a3dd893a06ea381b74a292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 12:06:10 +0300 Subject: [PATCH 085/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20read=5Fex?= =?UTF-8?q?pression:=20=D1=82=D0=B5=D1=81=D1=82=20=D0=BD=D0=B0=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20(=D0=BF=D1=83=D1=81=D1=82?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=81=D0=BA=D0=BE=D0=B1=D0=BA=D0=B8,=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=D0=B2=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=B2=D0=B5=D1=80=D0=BD=D0=BE=D0=B9=20=D0=BF=D0=BE=D0=B7?= =?UTF-8?q?=D0=B8=D1=86=D0=B8=D0=B8=20-=203=20=D1=81=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 7e5dd841..42b24cfe 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -111,3 +111,19 @@ TEST(TestAlgorithmLib, test_check_brackets_true6) { std::string string = ""; EXPECT_EQ(check_brackets(string), true); } +TEST(TestAlgorithmLib, test_read_expression_empty_brackets) { + std::string expression = "a + b - ()"; + EXPECT_THROW(expression, std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position1) { + std::string expression = "a + * b"; + EXPECT_THROW(expression, std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position2) { + std::string expression = "+ a * b"; + EXPECT_THROW(expression, std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position3) { + std::string expression = "(* a)"; + EXPECT_THROW(expression, std::invalid_argument); +} From 4164490cbbab43eca2d73840f804a1bd1157dfc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 12:10:31 +0300 Subject: [PATCH 086/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 42b24cfe..47df2619 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -111,19 +111,20 @@ TEST(TestAlgorithmLib, test_check_brackets_true6) { std::string string = ""; EXPECT_EQ(check_brackets(string), true); } + TEST(TestAlgorithmLib, test_read_expression_empty_brackets) { std::string expression = "a + b - ()"; - EXPECT_THROW(expression, std::invalid_argument); + EXPECT_THROW(read_expression(expression), std::invalid_argument); } TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position1) { std::string expression = "a + * b"; - EXPECT_THROW(expression, std::invalid_argument); + EXPECT_THROW(read_expression(expression), std::invalid_argument); } TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position2) { std::string expression = "+ a * b"; - EXPECT_THROW(expression, std::invalid_argument); + EXPECT_THROW(read_expression(expression), std::invalid_argument); } TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position3) { std::string expression = "(* a)"; - EXPECT_THROW(expression, std::invalid_argument); + EXPECT_THROW(read_expression(expression), std::invalid_argument); } From 84057160b8c8540c12b16136e7ba527e3c7ea00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 12:25:15 +0300 Subject: [PATCH 087/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20read=5Fex?= =?UTF-8?q?pression,=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8:=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=BF=D1=83=D1=89=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80,=20=D0=B2=D0=B2=D0=B5=D0=B4?= =?UTF-8?q?=D1=91=D0=BD=20=D0=BD=D0=B5=D0=B4=D0=BE=D0=BF=D1=83=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BC=D1=8B=D0=B9=20=D1=81=D0=B8=D0=BC=D0=B2=D0=BE=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 47df2619..9b0f6eca 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -128,3 +128,22 @@ TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position3) { std::string expression = "(* a)"; EXPECT_THROW(read_expression(expression), std::invalid_argument); } +TEST(TestAlgorithmLib, test_read_expression_operands_without_operator1) { + std::string expression = "(a b)"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_operands_without_operator2) { + std::string expression = "a b"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_operands_without_operator3) { + std::string expression = "a + b c"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_invalid_character) { + std::string expression = "(a@b)"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} + + + From 7959c94021ca80100e53cb70558c7a8b0a96cc74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 12:37:19 +0300 Subject: [PATCH 088/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20read=5Fex?= =?UTF-8?q?pression,=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8:=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B9=D0=B4=D0=B5=D0=BD=D1=8B=20=D0=BD=D0=B5=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BA=D0=BE=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?,=20=D0=BF=D1=80=D0=BE=D1=83=D1=89=D0=B5=D0=BD=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D0=BD=D0=B4=20=D0=B2=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=86=D0=B5,=20=D0=BD=D0=B5=D1=82=20=D0=BE=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D0=BE=D0=B2=20=D0=B2=20=D0=B2=D1=8B=D1=80?= =?UTF-8?q?=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B8,=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=BF=D1=83=D1=89=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B4=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=20=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=B1=D0=BA=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 9b0f6eca..7f1e3f0f 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -121,10 +121,6 @@ TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position1) { EXPECT_THROW(read_expression(expression), std::invalid_argument); } TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position2) { - std::string expression = "+ a * b"; - EXPECT_THROW(read_expression(expression), std::invalid_argument); -} -TEST(TestAlgorithmLib, test_read_expression_operator_in_wrong_position3) { std::string expression = "(* a)"; EXPECT_THROW(read_expression(expression), std::invalid_argument); } @@ -146,4 +142,20 @@ TEST(TestAlgorithmLib, test_read_expression_invalid_character) { } +TEST(TestAlgorithmLib, test_read_expression_unbalanced_brackets) { + std::string expression = "(a-b)*c + (d + f)))"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_missing_operand_at_the_end) { + std::string expression = "a + b - "; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_no_operands_in_expression) { + std::string expression = "+ -"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} +TEST(TestAlgorithmLib, test_read_expression_no_operand_before_opening_bracket) { + std::string expression = "a ( a + b)"; + EXPECT_THROW(read_expression(expression), std::invalid_argument); +} From ee0f7cccefb6249607caa45b27ddac662bdd098e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 14:58:50 +0300 Subject: [PATCH 089/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=81=D0=BF=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_list/List.cpp diff --git a/lib_list/List.cpp b/lib_list/List.cpp new file mode 100644 index 00000000..ad08685d --- /dev/null +++ b/lib_list/List.cpp @@ -0,0 +1 @@ +#include "List.h" From 38a8a7ca82bd1660a025a08801ec7db83089fc75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 14:59:36 +0300 Subject: [PATCH 090/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Node=20=D0=B8?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20List?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib_list/List.h diff --git a/lib_list/List.h b/lib_list/List.h new file mode 100644 index 00000000..41103977 --- /dev/null +++ b/lib_list/List.h @@ -0,0 +1,18 @@ +#pragma once +template +struct Node { + T value; + Node* next; + Node(T value_, Node* next_ = nullptr) { + value = value_; + next = next_; + } +}; +template +class List { +private: + Node* _head; + Node* _tail; +public: + +}; From fe935b2b7e4908a46ca1e70e8c84f0d65673b2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 15:13:12 +0300 Subject: [PATCH 091/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Lis?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 41103977..a5ba8042 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -3,10 +3,7 @@ template struct Node { T value; Node* next; - Node(T value_, Node* next_ = nullptr) { - value = value_; - next = next_; - } + Node(T value_, Node* next_ = nullptr) : value(value_), next(next_) {} }; template class List { @@ -14,5 +11,20 @@ class List { Node* _head; Node* _tail; public: + List(); + ~List(); + List(const List& other_list); + bool operator==(const List& other) const; + List& operator=(const List& other); + + bool is_empty() const noexcept; + void push_back(const T& value) noexcept; + void push_front(const T& value) noexcept; + void insert(size_t position, const T& value); + void insert(Node* node, const T& value); + void pop_front() noexcept; + void pop_back() noexcept; + void erase(size_t position); + void erase(Node* node); }; From aaf6e49c16369e5c347f0597be2ba4a3508b9bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 4 Nov 2025 19:12:33 +0300 Subject: [PATCH 092/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib_list/List.h b/lib_list/List.h index a5ba8042..431bc41c 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -28,3 +28,25 @@ class List { void erase(size_t position); void erase(Node* node); }; +template +List::List() { + _head = nullptr; + _tail = nullptr; +} +template +List::~List() { + while (_head != nullptr) { + Node* temporary = _head; + _head = _head->next; + delete temporary; + } +} +template +List::List(const List& other_list) : _head(nullptr), _tail(nullptr) { + Node* current = other_list._head; + while (current != nullptr) { + this->push_back(current->value); + current = current->next; + } +} + From 6531627b2d9b0d1c8c78e3197197d90028e42bdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 08:59:34 +0300 Subject: [PATCH 093/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/test_list.cpp diff --git a/tests/test_list.cpp b/tests/test_list.cpp new file mode 100644 index 00000000..04400937 --- /dev/null +++ b/tests/test_list.cpp @@ -0,0 +1,24 @@ +// Copyright 2025 Zabytina Julia + +#include +#include "../lib_list/List.h" +TEST(TestListLib, list_default_constructor) { + // Arrange & Act + List list; + // Assert + EXPECT_EQ(list.get_head(), nullptr); + EXPECT_EQ(list.get_tail(), nullptr); +} +TEST(TestListLib, list_copy_constructor) { + // Arrange & Act + List list1; + list1.push_back(10); + list1.push_back(20); + List list2(list1); + // Assert + EXPECT_NE(list1.get_head(), list2.get_head()); + EXPECT_NE(list1.get_tail(), list2.get_tail()); + + EXPECT_EQ(list1.get_head()->value, list2.get_head()->value); + EXPECT_EQ(list1.get_tail()->value, list2.get_tail()->value); +} From 7d884e59663f372e59de69ce05b2827bf117f195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 09:11:18 +0300 Subject: [PATCH 094/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B:=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D1=8B=20=D0=BD=D0=B0=20=5Fhead,=20=5Ftail,=20is=5Fempty(),=20p?= =?UTF-8?q?ush=5Fback(),=20push=5Ffront()=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 431bc41c..a5913544 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -15,6 +15,9 @@ class List { ~List(); List(const List& other_list); + Node* get_head() const noexcept; + Node* get_tail() const noexcept; + bool operator==(const List& other) const; List& operator=(const List& other); @@ -29,10 +32,7 @@ class List { void erase(Node* node); }; template -List::List() { - _head = nullptr; - _tail = nullptr; -} +List::List() : _head(nullptr), _tail(nullptr) {} template List::~List() { while (_head != nullptr) { @@ -50,3 +50,39 @@ List::List(const List& other_list) : _head(nullptr), _tail(nullptr) { } } + + +template +Node* List::get_head() const noexcept { + return _head; +} +template +Node* List::get_tail() const noexcept { + return _tail; +} + +template +bool List::is_empty() const noexcept { + return _head == nullptr && _tail == nullptr; +} +template +void List::push_front(const T& value) noexcept { + Node* node = new Node(value); + if (is_empty()) { + _head = node; + _tail = node; + } + node->next = _head; + _head = node; +} + +template +void List::push_back(const T& value) noexcept{ + Node* node = new Node(value); + if (is_empty()) { + _head = node; + _tail = node; + } + _tail->next = node; + _tail = node; +} From cd6e1437d075c06af8834e9697c6a6d90eee5ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 16:49:31 +0300 Subject: [PATCH 095/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20pop?= =?UTF-8?q?=5Ffront(),=20pop=5Fback()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index a5913544..41e417ed 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -1,4 +1,5 @@ #pragma once +#include template struct Node { T value; @@ -26,8 +27,8 @@ class List { void push_front(const T& value) noexcept; void insert(size_t position, const T& value); void insert(Node* node, const T& value); - void pop_front() noexcept; - void pop_back() noexcept; + void pop_front(); + void pop_back(); void erase(size_t position); void erase(Node* node); }; @@ -49,9 +50,6 @@ List::List(const List& other_list) : _head(nullptr), _tail(nullptr) { current = current->next; } } - - - template Node* List::get_head() const noexcept { return _head; @@ -60,7 +58,6 @@ template Node* List::get_tail() const noexcept { return _tail; } - template bool List::is_empty() const noexcept { return _head == nullptr && _tail == nullptr; @@ -75,7 +72,6 @@ void List::push_front(const T& value) noexcept { node->next = _head; _head = node; } - template void List::push_back(const T& value) noexcept{ Node* node = new Node(value); @@ -86,3 +82,38 @@ void List::push_back(const T& value) noexcept{ _tail->next = node; _tail = node; } +template +void List::pop_front() { + if (is_empty()) { + throw std::logic_error("Can't pop the first element at empty list!"); + } + if (_head == _tail) { + delete _head; + _head = nullptr; + _tail = nullptr; + return; + } + Node* temporary = _head; + _head = _head->next; + delete temporary; +} +template +void List::pop_back() { + if (is_empty()) { + throw std::logic_error("Can't pop the last element at empty list!"); + } + if (_head == _tail) { + delete _head; + _tail = nullptr; + _head = nullptr; + return; + } + Node* current = _head; + while (current->next != _tail) { + current = current->next; + } + Node* temporary = current->next; + _tail = current; + delete temporary; + +} From 86ba8f3334c889c612569fcf2fa9026c29e75a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 17:08:38 +0300 Subject: [PATCH 096/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=81=D1=87=D1=91=D1=82=D1=87=D0=B8=D0=BA=20=5F?= =?UTF-8?q?count=5Felements,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=81=D1=87=D1=91=D1=82=D1=87=D0=B8=D0=BA=20=D0=B2=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D1=83=D1=8E=D1=89=D0=B8=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D1=8B=20push,=20pop,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=5Fcount=5Felements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 41e417ed..efc08e52 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -11,11 +11,13 @@ class List { private: Node* _head; Node* _tail; + size_t _count_elements; public: List(); ~List(); List(const List& other_list); + size_t get_size() const noexcept; Node* get_head() const noexcept; Node* get_tail() const noexcept; @@ -33,7 +35,7 @@ class List { void erase(Node* node); }; template -List::List() : _head(nullptr), _tail(nullptr) {} +List::List() : _head(nullptr), _tail(nullptr), _count_elements(0) {} template List::~List() { while (_head != nullptr) { @@ -43,7 +45,7 @@ List::~List() { } } template -List::List(const List& other_list) : _head(nullptr), _tail(nullptr) { +List::List(const List& other_list) : _head(nullptr), _tail(nullptr), _count_elements(0) { Node* current = other_list._head; while (current != nullptr) { this->push_back(current->value); @@ -51,6 +53,10 @@ List::List(const List& other_list) : _head(nullptr), _tail(nullptr) { } } template +size_t List::get_size() const noexcept { + return _count_elements; +} +template Node* List::get_head() const noexcept { return _head; } @@ -68,9 +74,12 @@ void List::push_front(const T& value) noexcept { if (is_empty()) { _head = node; _tail = node; + _count_elements++; + return; } node->next = _head; _head = node; + _count_elements++; } template void List::push_back(const T& value) noexcept{ @@ -78,9 +87,12 @@ void List::push_back(const T& value) noexcept{ if (is_empty()) { _head = node; _tail = node; + _count_elements++; + return; } _tail->next = node; _tail = node; + _count_elements++; } template void List::pop_front() { @@ -91,11 +103,13 @@ void List::pop_front() { delete _head; _head = nullptr; _tail = nullptr; + _count_elements--; return; } Node* temporary = _head; _head = _head->next; delete temporary; + _count_elements--; } template void List::pop_back() { @@ -106,6 +120,7 @@ void List::pop_back() { delete _head; _tail = nullptr; _head = nullptr; + _count_elements--; return; } Node* current = _head; @@ -113,7 +128,8 @@ void List::pop_back() { current = current->next; } Node* temporary = current->next; - _tail = current; + _tail = current; + _tail->next = nullptr; delete temporary; - + _count_elements--; } From 361a98843f7728d1ba8fb28b0265112394234e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 18:57:08 +0300 Subject: [PATCH 097/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20is=5Fempty(),=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20pop()=20=D0=98=20push()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 04400937..b1e44ef0 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -8,6 +8,7 @@ TEST(TestListLib, list_default_constructor) { // Assert EXPECT_EQ(list.get_head(), nullptr); EXPECT_EQ(list.get_tail(), nullptr); + EXPECT_EQ(list.get_size(), 0); } TEST(TestListLib, list_copy_constructor) { // Arrange & Act @@ -21,4 +22,131 @@ TEST(TestListLib, list_copy_constructor) { EXPECT_EQ(list1.get_head()->value, list2.get_head()->value); EXPECT_EQ(list1.get_tail()->value, list2.get_tail()->value); + EXPECT_EQ(list1.get_size(), list2.get_size()); +} +TEST(TestListLib, list_is_empty_false) { + // Arrange & Act + List list; + list.push_back(10); + list.push_back(10); + // Assert + EXPECT_FALSE(list.is_empty()); +} +TEST(TestListLib, list_is_empty_true) { + // Arrange & Act + List list; + // Assert + EXPECT_TRUE(list.is_empty()); +} +TEST(TestListLib, push_front_with_empty_list) { + // Arrange & Act + List list; + list.push_front(10); + // Assert + EXPECT_EQ(list.get_size(), 1); + EXPECT_EQ(list.get_head(), list.get_tail()); +} +TEST(TestListLib, push_front_with_several_elements_list) { + // Arrange & Act + List list; + list.push_front(100); + list.push_front(200); + list.push_front(300); + list.push_front(400); + list.push_front(500); + // Assert + EXPECT_EQ(list.get_head()->value, 500); + EXPECT_EQ(list.get_size(), 5); +} +TEST(TestListLib, push_back_with_empty_list) { + // Arrange & Act + List list; + list.push_back(30); + // Assert + EXPECT_EQ(list.get_size(), 1); + EXPECT_EQ(list.get_head(), list.get_tail()); +} +TEST(TestListLib, push_back_with_several_elements_list) { + // Arrange & Act + List list; + list.push_back(300); + list.push_back(400); + list.push_back(500); + list.push_back(600); + list.push_back(700); + list.push_back(800); + list.push_back(900); + // Assert + EXPECT_EQ(list.get_tail()->value, 900); + EXPECT_EQ(list.get_size(), 7); +} + + + + +TEST(TestListLib, pop_back_with_empty_list_exception) { + // Arrange & Act + List list; + // Assert + ASSERT_THROW(list.pop_back(), std::logic_error); +} +TEST(TestListLib, pop_back_with_one_element_list) { + // Arrange & Act + List list; + list.push_back(10); + list.pop_back(); + // Assert + EXPECT_EQ(list.get_head(), nullptr); + EXPECT_EQ(list.get_tail(), nullptr); + EXPECT_EQ(list.get_size(), 0); +} +TEST(TestListLib, pop_back_with_several_elements_list) { + // Arrange & Act + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + list.pop_back(); + list.pop_back(); + // Assert + EXPECT_EQ(list.get_tail()->value, 30); + EXPECT_EQ(list.get_size(), 3); +} + + +TEST(TestListLib, pop_front_with_empty_list_exception) { + // Arrange & Act + List list; + // Assert + ASSERT_THROW(list.pop_front(), std::logic_error); +} +TEST(TestListLib, pop_front_with_one_element_list) { + // Arrange & Act + List list; + list.push_back(10); + list.pop_front(); + // Assert + EXPECT_EQ(list.get_head(), nullptr); + EXPECT_EQ(list.get_tail(), nullptr); + EXPECT_EQ(list.get_size(), 0); +} +TEST(TestListLib, pop_front_with_several_elements_list) { + // Arrange & Act + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + list.push_back(60); + list.push_back(70); + + list.pop_front(); + list.pop_front(); + list.pop_front(); + // Assert + EXPECT_EQ(list.get_head()->value, 40); + EXPECT_EQ(list.get_size(), 4); } From 5b1b7c566cd96eca5615704124b5c65b094d2c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 20:12:51 +0300 Subject: [PATCH 098/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D0=B0=D1=80=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20=3D,=20=3D=3D,=20!=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib_list/List.h b/lib_list/List.h index efc08e52..313d010a 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -22,6 +22,7 @@ class List { Node* get_tail() const noexcept; bool operator==(const List& other) const; + bool operator!=(const List& other) const; List& operator=(const List& other); bool is_empty() const noexcept; @@ -133,3 +134,49 @@ void List::pop_back() { delete temporary; _count_elements--; } +template +bool List::operator==(const List& other) const { + if (this->get_size() != other.get_size()) { + return false; + } + Node* current_this = this->get_head(); + Node* current_other = other.get_head(); + while (current_this != nullptr && current_other != nullptr) { + if (current_this->value != current_other->value) { + return false; + } + current_this = current_this->next; + current_other = current_other->next; + } + return true; +} +template +bool List::operator!=(const List& other) const { + return !(*this == other); +} +template +List& List::operator=(const List& other) { + if (this != &other) { + while (_head != nullptr) { + Node* temporary = _head; + _head = _head->next; + delete temporary; + } + _tail = nullptr; + _count_elements = 0; + Node* current = other._head; + while (current != nullptr) { + push_back(current->value); + current = current->next; + } + } + return *this; +} +template +void List::insert(size_t position, const T& value) { + +} +template +void List::insert(Node* node, const T& value) { + +} From 8de088c389395ef01d8a7604a18c81bc99f727ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 5 Nov 2025 20:21:12 +0300 Subject: [PATCH 099/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20=3D=3D,=20!=3D,=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/test_list.cpp b/tests/test_list.cpp index b1e44ef0..da46ad03 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -150,3 +150,77 @@ TEST(TestListLib, pop_front_with_several_elements_list) { EXPECT_EQ(list.get_head()->value, 40); EXPECT_EQ(list.get_size(), 4); } +TEST(TestListLib, list_operator_equal_true) { + // Arrange & Act + List list1; + List list2; + list1.push_back(10); + list1.push_back(20); + list1.push_back(30); + + list2.push_back(10); + list2.push_back(20); + list2.push_back(30); + // Assert + EXPECT_TRUE(list1 == list2); +} +TEST(TestListLib, list_operator_equal_false) { + // Arrange & Act + List list1; + List list2; + list1.push_back(30); + list1.push_back(40); + list1.push_back(50); + list1.push_back(100); + + list2.push_back(30); + list2.push_back(40); + list2.push_back(50); + // Assert + EXPECT_FALSE(list1 == list2); +} +TEST(TestListLib, list_operator_not_equal_true) { + // Arrange & Act + List list1; + List list2; + list1.push_back(70); + list1.push_back(80); + list1.push_back(90); + list1.push_back(100); + + list2.push_back(10); + list2.push_back(20); + list2.push_back(30); + // Assert + EXPECT_TRUE(list1 != list2); +} +TEST(TestListLib, list_operator_not_equal_false) { + // Arrange & Act + List list1; + List list2; + list1.push_back(80); + list1.push_back(80); + list1.push_back(80); + + list2.push_back(80); + list2.push_back(80); + list2.push_back(80); + // Assert + EXPECT_FALSE(list1 != list2); +} +TEST(TestListLib, list_operator_assign) { + // Arrange & Act + List list1; + List list2; + list1.push_back(100); + list1.push_back(800); + list1.push_back(900); + list1.push_back(600); + + list2.push_back(10); + list2.push_back(20); + list2.push_back(30); + list2 = list1; + // Assert + EXPECT_TRUE(list1 == list2); +} From 908c493d4b599bca341fbc4c353f456a01cecdca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 6 Nov 2025 09:55:01 +0300 Subject: [PATCH 100/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20insert(),=20erase()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 100 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 12 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 313d010a..ae2a4977 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -72,7 +72,7 @@ bool List::is_empty() const noexcept { template void List::push_front(const T& value) noexcept { Node* node = new Node(value); - if (is_empty()) { + if (this->is_empty()) { _head = node; _tail = node; _count_elements++; @@ -83,9 +83,9 @@ void List::push_front(const T& value) noexcept { _count_elements++; } template -void List::push_back(const T& value) noexcept{ +void List::push_back(const T& value) noexcept { Node* node = new Node(value); - if (is_empty()) { + if (this->is_empty()) { _head = node; _tail = node; _count_elements++; @@ -97,7 +97,7 @@ void List::push_back(const T& value) noexcept{ } template void List::pop_front() { - if (is_empty()) { + if (this->is_empty()) { throw std::logic_error("Can't pop the first element at empty list!"); } if (_head == _tail) { @@ -114,7 +114,7 @@ void List::pop_front() { } template void List::pop_back() { - if (is_empty()) { + if (this->is_empty()) { throw std::logic_error("Can't pop the last element at empty list!"); } if (_head == _tail) { @@ -129,17 +129,17 @@ void List::pop_back() { current = current->next; } Node* temporary = current->next; - _tail = current; + _tail = current; _tail->next = nullptr; delete temporary; - _count_elements--; + _count_elements--; } template bool List::operator==(const List& other) const { if (this->get_size() != other.get_size()) { return false; } - Node* current_this = this->get_head(); + Node* current_this = _head; Node* current_other = other.get_head(); while (current_this != nullptr && current_other != nullptr) { if (current_this->value != current_other->value) { @@ -162,11 +162,10 @@ List& List::operator=(const List& other) { _head = _head->next; delete temporary; } - _tail = nullptr; _count_elements = 0; Node* current = other._head; while (current != nullptr) { - push_back(current->value); + this->push_back(current->value); current = current->next; } } @@ -174,9 +173,86 @@ List& List::operator=(const List& other) { } template void List::insert(size_t position, const T& value) { - + if (position == 0) { + this->push_front(value); + return; + } + if (position == _count_elements - 1) { + this->push_back(value); + return; + } + Node* current = _head; + size_t current_position = 0; + while (current != nullptr && current_position != position-1) { + current = current->next; + current_position++; + } + if (current == nullptr) { + throw std::invalid_argument("Uncorrect position"); + } + this->insert(current, value); } template void List::insert(Node* node, const T& value) { - + if (node == nullptr || this->is_empty()) { + throw std::logic_error("The transmitted node or/and the list can't be empty!"); + } + Node* new_node = new Node(value); + new_node->next = node->next; + node->next = new_node; + if (node == _tail) { + _tail = new_node; + } + _count_elements++; +} +template +void List::erase(size_t position) { + if (position == 0) { + this->pop_front(); + return; + } + if (position == _count_elements - 1) { + this->pop_back(); + return; + } + Node* current = _head; + size_t current_position = 0; + while (current != nullptr) { + if (current_position == position-1) { + break; + } + current = current->next; + current_position++; + } + if (current == nullptr) { + throw std::invalid_argument("Uncorrect position"); + } + Node* temporary = current->next; + current->next = temporary->next; + delete temporary; + _count_elements--; +} +template +void List::erase(Node* node) { + if (node == nullptr || this->is_empty()) { + throw std::logic_error("The transmitted node or/and the list can't be empty!"); + } + if (node == _head) { + this->pop_front(); + return; + } + if (node == _tail) { + this->pop_back(); + return; + } + Node* current = _head; + while (current != nullptr && current->next != node) { + current = current->next; + } + if (current == nullptr) { + throw std::invalid_argument("Uncorrect position"); + } + current->next = node->next; + delete node; + _count_elements--; } From 24476f1019590814f27330d5b6e134898d38e3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 6 Nov 2025 10:28:56 +0300 Subject: [PATCH 101/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20insert()=20-=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B8=20+?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/tests/test_list.cpp b/tests/test_list.cpp index da46ad03..f6c83232 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -80,10 +80,6 @@ TEST(TestListLib, push_back_with_several_elements_list) { EXPECT_EQ(list.get_tail()->value, 900); EXPECT_EQ(list.get_size(), 7); } - - - - TEST(TestListLib, pop_back_with_empty_list_exception) { // Arrange & Act List list; @@ -224,3 +220,102 @@ TEST(TestListLib, list_operator_assign) { // Assert EXPECT_TRUE(list1 == list2); } +TEST(TestListLib, list_node_insert) { + // Arrange + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + // Act + Node* node = list.get_head()->next; + list.insert(node, 500); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 10); + EXPECT_EQ(list.get_head()->next->value, 20); + EXPECT_EQ(list.get_head()->next->next->value, 500); + EXPECT_EQ(list.get_head()->next->next->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 40); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 50); +} +TEST(TestListLib, list_node_insert_with_exception) { + // Arrange + List list; + Node* node = list.get_head(); + // Act & Assert + ASSERT_THROW(list.insert(node, 500), std::logic_error); +} +TEST(TestListLib, list_position_insert_in_begin) { + // Arrange + List list; + list.push_back(60); + list.push_back(70); + list.push_back(80); + list.push_back(90); + list.push_back(20); + size_t position = 0; + // Act + list.insert(position, 700); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 700); + EXPECT_EQ(list.get_head()->next->value, 60); + EXPECT_EQ(list.get_head()->next->next->value, 70); + EXPECT_EQ(list.get_head()->next->next->next->value, 80); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 90); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 20); +} +TEST(TestListLib, list_position_insert_in_end) { + // Arrange + List list; + list.push_back(30); + list.push_back(30); + list.push_back(30); + list.push_back(40); + list.push_back(50); + size_t position = 4; + // Act + list.insert(position, 900); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 30); + EXPECT_EQ(list.get_head()->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->next->value, 40); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 50); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 900); +} +TEST(TestListLib, list_position_insert) { + // Arrange + List list; + list.push_back(30); + list.push_back(80); + list.push_back(30); + list.push_back(40); + list.push_back(50); + size_t position = 2; + // Act + list.insert(position, 250); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 30); + EXPECT_EQ(list.get_head()->next->value, 80); + EXPECT_EQ(list.get_head()->next->next->value, 250); + EXPECT_EQ(list.get_head()->next->next->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 40); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 50); +} +TEST(TestListLib, list_position_insert_with_exception) { + // Arrange + List list; + list.push_back(300); + list.push_back(850); + list.push_back(320); + list.push_back(40); + list.push_back(90); + size_t position = 10; + // Act & Assert + ASSERT_THROW(list.insert(position, 500), std::invalid_argument); +} From c461d79dfec7bf4e770f87a39f2c36fca33c85b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 6 Nov 2025 10:44:39 +0300 Subject: [PATCH 102/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=D1=81=D0=B5=20=D1=81=D0=BB=D1=83=D1=87=D0=B0?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BB=D1=8F=20erase()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/tests/test_list.cpp b/tests/test_list.cpp index f6c83232..5e6b6ec0 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -110,8 +110,6 @@ TEST(TestListLib, pop_back_with_several_elements_list) { EXPECT_EQ(list.get_tail()->value, 30); EXPECT_EQ(list.get_size(), 3); } - - TEST(TestListLib, pop_front_with_empty_list_exception) { // Arrange & Act List list; @@ -319,3 +317,126 @@ TEST(TestListLib, list_position_insert_with_exception) { // Act & Assert ASSERT_THROW(list.insert(position, 500), std::invalid_argument); } + + + + + + + + + + + + + +TEST(TestListLib, list_position_erase_in_begin) { + // Arrange + List list; + list.push_back(60); + list.push_back(70); + list.push_back(80); + list.push_back(90); + list.push_back(20); + size_t position = 0; + // Act + list.erase(position); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 70); + EXPECT_EQ(list.get_head()->next->value, 80); + EXPECT_EQ(list.get_head()->next->next->value, 90); + EXPECT_EQ(list.get_head()->next->next->next->value, 20); +} +TEST(TestListLib, list_erase_erase_in_end) { + // Arrange + List list; + list.push_back(680); + list.push_back(70); + list.push_back(80); + list.push_back(920); + list.push_back(20); + size_t position = 4; + // Act + list.erase(position); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 680); + EXPECT_EQ(list.get_head()->next->value, 70); + EXPECT_EQ(list.get_head()->next->next->value, 80); + EXPECT_EQ(list.get_head()->next->next->next->value, 920); +} +TEST(TestListLib, list_position_erase) { + // Arrange + List list; + list.push_back(320); + list.push_back(840); + list.push_back(30); + list.push_back(40); + list.push_back(550); + size_t position = 3; + // Act + list.erase(position); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 320); + EXPECT_EQ(list.get_head()->next->value, 840); + EXPECT_EQ(list.get_head()->next->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->next->value, 550); +} +TEST(TestListLib, list_position_erase_with_exception1) { + // Arrange + List list; + size_t position = 2; + // Act & Assert + ASSERT_THROW(list.erase(position), std::logic_error); +} +TEST(TestListLib, list_position_erase_with_exception2) { + // Arrange + List list; + list.push_back(32000); + list.push_back(8220); + list.push_back(320); + list.push_back(40); + list.push_back(50); + size_t position = 10; + // Act & Assert + ASSERT_THROW(list.erase(position), std::invalid_argument); +} +TEST(TestListLib, list_node_erase) { + // Arrange + List list; + list.push_back(3270); + list.push_back(840); + list.push_back(30); + list.push_back(40); + list.push_back(55770); + Node* node = list.get_head()->next; + // Act + list.erase(node); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 3270); + EXPECT_EQ(list.get_head()->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->value, 40); + EXPECT_EQ(list.get_head()->next->next->next->value, 55770); +} +TEST(TestListLib, list_node_erase_with_exception1) { + // Arrange + List list; + Node* node = list.get_head(); + // Act & Assert + ASSERT_THROW(list.erase(node), std::logic_error); +} +TEST(TestListLib, list_node_erase_with_exception2) { + // Arrange + List list; + list.push_back(32000); + list.push_back(8220); + list.push_back(32100); + list.push_back(10); + list.push_back(10); + Node* node = new Node(2300); + // Act & Assert + ASSERT_THROW(list.erase(node), std::invalid_argument); +} From 0214b4690f5b6a98f9b576cc5109ad0f8a70fa6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 16:07:13 +0300 Subject: [PATCH 103/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=84=D0=B0=D0=B9=D0=BB=20queue.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_queue/queue.cpp diff --git a/lib_queue/queue.cpp b/lib_queue/queue.cpp new file mode 100644 index 00000000..462f7c08 --- /dev/null +++ b/lib_queue/queue.cpp @@ -0,0 +1 @@ +#include "queue.h" \ No newline at end of file From d491a4ae4008c3f274c7e68c2d00eb3b1f1c52af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 16:07:51 +0300 Subject: [PATCH 104/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=9E=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib_queue/queue.h diff --git a/lib_queue/queue.h b/lib_queue/queue.h new file mode 100644 index 00000000..533806fb --- /dev/null +++ b/lib_queue/queue.h @@ -0,0 +1,25 @@ +#pragma once +template +class Queue { +private: + T* _data; + size_t _size; + size_t _head; + size_t _tail; + size_t _count; +public: + Queue(size_t size); + Queue(const Queue& other); + ~Queue(); + + inline T head() const noexcept; + inline T tail() const noexcept; + bool is_empty() const noexcept; + void is_full() const noexcept; + size_t size() const noexcept; + + void push(int value); + void pop(); + + void clear() noexcept; +}; From 2ba50af0e8d403b34c52ac757b053cea70828ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 16:12:46 +0300 Subject: [PATCH 105/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index 533806fb..71abc1ba 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -8,17 +8,18 @@ class Queue { size_t _tail; size_t _count; public: - Queue(size_t size); + explicit Queue(size_t size); Queue(const Queue& other); + Queue& operator=(const Queue& other); ~Queue(); inline T head() const noexcept; inline T tail() const noexcept; bool is_empty() const noexcept; - void is_full() const noexcept; + bool is_full() const noexcept; size_t size() const noexcept; - void push(int value); + void push(T value); void pop(); void clear() noexcept; From 14180fe46c39f6e6e2d4cd3ebc04bf0c99fcb8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 16:26:06 +0300 Subject: [PATCH 106/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82?= =?UTF-8?q?=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=B4?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index 71abc1ba..acb1fcd0 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -12,15 +12,28 @@ class Queue { Queue(const Queue& other); Queue& operator=(const Queue& other); ~Queue(); - inline T head() const noexcept; inline T tail() const noexcept; bool is_empty() const noexcept; bool is_full() const noexcept; size_t size() const noexcept; - void push(T value); void pop(); - void clear() noexcept; }; +template +Queue::Queue(size_t size): _size(size), _head(0), _tail(0), _count(0) { + _data = new T[size]; +} +template +Queue::Queue(const Queue& other): _size(other._size), _head(other._head), _tail(other._tail), _count(0) { + _data = new T[other._size]; + for (size_t i = 0; i < _size; ++i) { + _data[i] = other._data[i]; + } +} +template +Queue::~Queue() { + delete _data; + _data = nullptr; +} From d84a72438a62ad7c558d07baf723bba14200ec7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 16:42:54 +0300 Subject: [PATCH 107/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/test_queue.cpp diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp new file mode 100644 index 00000000..e570ce35 --- /dev/null +++ b/tests/test_queue.cpp @@ -0,0 +1,30 @@ +// Copyright 2025 Zabytina Julia +#include +#include "../lib_queue/queue.h" +TEST(TestQueueLib, queue_constructor_with_size) { + // Arrange & Act + size_t size = 10; + Queue queue(size); + // Assert + EXPECT_EQ(queue.size(), size); + EXPECT_EQ(queue.count(), 0); + EXPECT_TRUE(queue.is_empty()); +} +TEST(TestQueueLib, queue_copy_constructor) { + // Arrange + size_t size = 5; + Queue queue1(size); + queue1.push(10); + queue1.push(20); + queue1.push(30); + // Act + Queue queue2(queue1); + // Assert + EXPECT_EQ(queue1.size(), queue2.size()); + EXPECT_EQ(queue1.count(), queue2.count()); + while (!queue1.is_empty()) { + EXPECT_EQ(queue1.head(), queue2.head()); + queue1.pop(); + queue2.pop(); + } +} From bc8bd39a65186cf3e82e17818237e8b89e351c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 17:07:19 +0300 Subject: [PATCH 108/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20is?= =?UTF-8?q?=5Ffull(),=20is=5Fempty(),=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6?= =?UTF-8?q?=D0=B5=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20head,tail,=20size,=20count?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.h | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index acb1fcd0..c9be2264 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -1,4 +1,5 @@ #pragma once +#include template class Queue { private: @@ -10,13 +11,13 @@ class Queue { public: explicit Queue(size_t size); Queue(const Queue& other); - Queue& operator=(const Queue& other); ~Queue(); inline T head() const noexcept; inline T tail() const noexcept; + size_t size() const noexcept; + size_t count() const noexcept; bool is_empty() const noexcept; bool is_full() const noexcept; - size_t size() const noexcept; void push(T value); void pop(); void clear() noexcept; @@ -26,7 +27,7 @@ Queue::Queue(size_t size): _size(size), _head(0), _tail(0), _count(0) { _data = new T[size]; } template -Queue::Queue(const Queue& other): _size(other._size), _head(other._head), _tail(other._tail), _count(0) { +Queue::Queue(const Queue& other): _size(other._size), _head(other._head), _tail(other._tail), _count(other._count) { _data = new T[other._size]; for (size_t i = 0; i < _size; ++i) { _data[i] = other._data[i]; @@ -34,6 +35,30 @@ Queue::Queue(const Queue& other): _size(other._size), _head(other._head), } template Queue::~Queue() { - delete _data; + delete[] _data; _data = nullptr; } +template +inline T Queue::head() const noexcept { + return _data[_head]; +} +template +inline T Queue::tail() const noexcept { + return _data[(_tail+ _size-1) % _size]; +} +template +size_t Queue::size() const noexcept { + return _size; +} +template +size_t Queue::count() const noexcept { + return _count; +} +template +bool Queue::is_empty() const noexcept { + return _count == 0; +} +template +bool Queue::is_full() const noexcept { + return _count == _size; +} From 96b11d3552968f1f5744c2d05e95b8611a1ffa66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 18:46:44 +0300 Subject: [PATCH 109/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20is=5Fempty(),=20is=5Ffull()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp index e570ce35..568a976a 100644 --- a/tests/test_queue.cpp +++ b/tests/test_queue.cpp @@ -28,3 +28,39 @@ TEST(TestQueueLib, queue_copy_constructor) { queue2.pop(); } } +TEST(TestQueueLib, queue_is_empty_true) { + // Arrange & Act + Queue queue(10); + // Assert + EXPECT_TRUE(queue.is_empty()); +} +TEST(TestQueueLib, queue_is_empty_false) { + // Arrange + size_t size = 3; + Queue queue(size); + // Act + queue.push(10); + // Assert + EXPECT_FALSE(queue.is_empty()); +} +TEST(TestQueueLib, queue_is_full_true) { + // Arrange + size_t size = 3; + Queue queue(size); + // Act + queue.push(30); + queue.push(240); + queue.push(190); + // Assert + EXPECT_TRUE(queue.is_full()); +} +TEST(TestQueueLib, queue_is_full_false) { + // Arrange + size_t size = 5; + Queue queue(size); + // Act + queue.push(10); + queue.push(30); + // Assert + EXPECT_FALSE(queue.is_full()); +} From d79dd27013604d024157e6e0f069f4dc1793d7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 19:09:39 +0300 Subject: [PATCH 110/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20head(),=20tail()=20+=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F,=20=D1=82=D0=B5=D1=81=D1=82=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B8=D1=87=D0=B5=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D0=B5=20=D0=BF=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp index 568a976a..59275b73 100644 --- a/tests/test_queue.cpp +++ b/tests/test_queue.cpp @@ -64,3 +64,52 @@ TEST(TestQueueLib, queue_is_full_false) { // Assert EXPECT_FALSE(queue.is_full()); } +TEST(TestQueueLib, queue_get_head) { + // Arrange + size_t size = 6; + Queue queue(size); + // Act + queue.push(10); + queue.push(20); + queue.push(30); + // Assert + EXPECT_EQ(queue.head(), 10); +} +TEST(TestQueueLib, queue_get_tail) { + // Arrange + size_t size = 6; + Queue queue(size); + // Act + queue.push(70); + queue.push(230); + queue.push(50); + // Assert + EXPECT_EQ(queue.tail(), 50); +} +TEST(TestQueueLib, queue_empty_get_head_exception) { + // Arrange & Act + size_t size = 5; + Queue queue(size); + // Assert + ASSERT_THROW(queue.head(), std::runtime_error); +} +TEST(TestQueueLib, queue_empty_get_tail_exception) { + // Arrange & Act + size_t size = 9; + Queue queue(size); + // Assert + ASSERT_THROW(queue.tail(), std::runtime_error); +} +TEST(TestQueueLib, queue_circular_behavior) { + // Arrange + Queue queue(3); + // Act + queue.push(1); + queue.push(2); + queue.push(3); + queue.pop(); + queue.push(4); + // Assert + EXPECT_EQ(queue.head(), 2); + EXPECT_EQ(queue.tail(), 4); +} \ No newline at end of file From 67b0f7b022af889c9da58a0142532ba6ed4f9630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 19:17:47 +0300 Subject: [PATCH 111/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20push()=20=D0=B8=20pop()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.h | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index c9be2264..eaa5ef73 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -40,11 +40,17 @@ Queue::~Queue() { } template inline T Queue::head() const noexcept { + if (this->is_empty()) { + throw std::runtime_error("Can't get head: queue is empty"); + } return _data[_head]; } template inline T Queue::tail() const noexcept { - return _data[(_tail+ _size-1) % _size]; + if (this->is_empty()) { + throw std::runtime_error("Can't get tail: queue is empty"); + } + return _data[(_tail + _size - 1) % _size]; } template size_t Queue::size() const noexcept { @@ -62,3 +68,24 @@ template bool Queue::is_full() const noexcept { return _count == _size; } +template +void Queue::push(T value) { + if (this->is_full()) { + throw std::logic_error("The queue is full, you can't push the element"); + } + _data[_tail] = value; + _tail = (_tail + 1) % _size; + _count++; +} +template +void Queue::pop() { + if (this->is_empty()) { + throw std::logic_error("The queue is empty, you can't pop the element"); + } + _head = (_head + 1) % _size; + _count--; +} +template +void Queue::clear() noexcept { + +} \ No newline at end of file From c855a18a327c4b6adb8d7e7b0a3a405fae516282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 19:35:15 +0300 Subject: [PATCH 112/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20push()=20=D0=B8=20pop()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp index 59275b73..e50b3c7f 100644 --- a/tests/test_queue.cpp +++ b/tests/test_queue.cpp @@ -112,4 +112,30 @@ TEST(TestQueueLib, queue_circular_behavior) { // Assert EXPECT_EQ(queue.head(), 2); EXPECT_EQ(queue.tail(), 4); -} \ No newline at end of file +} +TEST(TestQueueLib, queue_push) { + // Arrange + Queue queue(5); + // Act + queue.push(22); + queue.push(44); + queue.push(77); + // Assert + EXPECT_EQ(queue.head(), 22); + EXPECT_EQ(queue.tail(), 77); + EXPECT_EQ(queue.count(), 3); +} +TEST(TestQueueLib, queue_pop) { + // Arrange + Queue queue(5); + // Act + queue.push(88); + queue.push(54); + queue.push(27); + queue.pop(); + queue.pop(); + // Assert + EXPECT_EQ(queue.head(), queue.tail()); + EXPECT_EQ(queue.head(), 27); + EXPECT_EQ(queue.count(), 1); +} From 385c44700ac4fc30b74adb9ccb2f4780ce57184e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 19:47:10 +0300 Subject: [PATCH 113/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20clear()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/queue.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib_queue/queue.h b/lib_queue/queue.h index eaa5ef73..f88f6b11 100644 --- a/lib_queue/queue.h +++ b/lib_queue/queue.h @@ -12,8 +12,8 @@ class Queue { explicit Queue(size_t size); Queue(const Queue& other); ~Queue(); - inline T head() const noexcept; - inline T tail() const noexcept; + inline T head() const; + inline T tail() const; size_t size() const noexcept; size_t count() const noexcept; bool is_empty() const noexcept; @@ -39,14 +39,14 @@ Queue::~Queue() { _data = nullptr; } template -inline T Queue::head() const noexcept { +inline T Queue::head() const { if (this->is_empty()) { throw std::runtime_error("Can't get head: queue is empty"); } return _data[_head]; } template -inline T Queue::tail() const noexcept { +inline T Queue::tail() const { if (this->is_empty()) { throw std::runtime_error("Can't get tail: queue is empty"); } @@ -86,6 +86,8 @@ void Queue::pop() { _count--; } template -void Queue::clear() noexcept { - -} \ No newline at end of file +void Queue::clear() noexcept { + _head = 0; + _tail = 0; + _count = 0; +} From 113c52bfa62d60e6da54dcbf3c20fe37254c74da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 7 Nov 2025 19:54:08 +0300 Subject: [PATCH 114/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20push(),=20pop()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/test_queue.cpp b/tests/test_queue.cpp index e50b3c7f..0254f45b 100644 --- a/tests/test_queue.cpp +++ b/tests/test_queue.cpp @@ -127,7 +127,7 @@ TEST(TestQueueLib, queue_push) { } TEST(TestQueueLib, queue_pop) { // Arrange - Queue queue(5); + Queue queue(8); // Act queue.push(88); queue.push(54); @@ -139,3 +139,35 @@ TEST(TestQueueLib, queue_pop) { EXPECT_EQ(queue.head(), 27); EXPECT_EQ(queue.count(), 1); } +TEST(TestQueueLib, queue_push_exception) { + // Arrange + Queue queue(3); + // Act + queue.push(20); + queue.push(11); + queue.push(37); + // Assert + ASSERT_THROW(queue.push(10), std::logic_error); +} +TEST(TestQueueLib, queue_pop_exception) { + // Arrange & Act + Queue queue(10); + // Assert + ASSERT_THROW(queue.pop(), std::logic_error); +} +TEST(TestQueueLib, queue_clear) { + // Arrange + Queue queue(10); + queue.push(88); + queue.push(44); + queue.push(57); + queue.push(25); + queue.push(41); + queue.push(20); + queue.push(3); + queue.push(9); + // Act + queue.clear(); + // Assert + EXPECT_EQ(queue.count(), 0); +} From 0fdc854e15709f9cc7dd76555c221194430b4720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 14:06:16 +0300 Subject: [PATCH 115/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Ite?= =?UTF-8?q?rator=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=20List?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib_list/List.h b/lib_list/List.h index ae2a4977..d1a3abb6 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -13,6 +13,24 @@ class List { Node* _tail; size_t _count_elements; public: + class Iterator { + private: + Node* _current; + public: + Iterator(); + Iterator(Node* node); + Iterator(const Iterator& other); + + Iterator& operator =(const Iterator& it); + Iterator& operator ++(); + Iterator& operator ++(int); + Iterator& operator +=(const Iterator& it); + bool operator == (const Iterator& it); + bool operator != (const Iterator& it); + T& operator*(); + }; + Iterator begin(); + Iterator end(); List(); ~List(); List(const List& other_list); @@ -36,6 +54,14 @@ class List { void erase(Node* node); }; template +typename List::Iterator List::begin() { + return Iterator(_head); +} +template +typename List::Iterator List::end() { + return Iterator(nullptr); +} +template List::List() : _head(nullptr), _tail(nullptr), _count_elements(0) {} template List::~List() { From 8ecf6172c6194277e55c74d0494c312051ad1ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 20:30:30 +0300 Subject: [PATCH 116/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=BE=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=3D=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=98=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 54 +++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index d1a3abb6..b1ce7ea9 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -17,24 +17,32 @@ class List { private: Node* _current; public: - Iterator(); - Iterator(Node* node); - Iterator(const Iterator& other); + Iterator() : _current(nullptr) {} + Iterator(Node* node): _current(node) {} + Iterator(const Iterator& other): _current(other._current) {} + Iterator& operator=(const Iterator& other) { + _current = other._current; + return *this; + } + Iterator& operator+=(size_t n) {} + Iterator& operator++() {} + Iterator operator++(int) {} + + bool operator==(const Iterator& it) const {} + bool operator!=(const Iterator& it) const {} - Iterator& operator =(const Iterator& it); - Iterator& operator ++(); - Iterator& operator ++(int); - Iterator& operator +=(const Iterator& it); - bool operator == (const Iterator& it); - bool operator != (const Iterator& it); - T& operator*(); + T& operator*() {} + const T& operator*() const {} }; - Iterator begin(); - Iterator end(); + Iterator begin() { + return Iterator(_head); + } + Iterator end() { + return Iterator(nullptr); + } List(); ~List(); List(const List& other_list); - size_t get_size() const noexcept; Node* get_head() const noexcept; Node* get_tail() const noexcept; @@ -54,15 +62,8 @@ class List { void erase(Node* node); }; template -typename List::Iterator List::begin() { - return Iterator(_head); -} -template -typename List::Iterator List::end() { - return Iterator(nullptr); -} -template List::List() : _head(nullptr), _tail(nullptr), _count_elements(0) {} + template List::~List() { while (_head != nullptr) { @@ -72,7 +73,8 @@ List::~List() { } } template -List::List(const List& other_list) : _head(nullptr), _tail(nullptr), _count_elements(0) { +List::List(const List& other_list) : _head(nullptr), _tail(nullptr), +_count_elements(0) { Node* current = other_list._head; while (current != nullptr) { this->push_back(current->value); @@ -80,8 +82,8 @@ List::List(const List& other_list) : _head(nullptr), _tail(nullptr), _coun } } template -size_t List::get_size() const noexcept { - return _count_elements; +size_t List::get_size() const noexcept { + return _count_elements; } template Node* List::get_head() const noexcept { @@ -209,7 +211,7 @@ void List::insert(size_t position, const T& value) { } Node* current = _head; size_t current_position = 0; - while (current != nullptr && current_position != position-1) { + while (current != nullptr && current_position != position - 1) { current = current->next; current_position++; } @@ -244,7 +246,7 @@ void List::erase(size_t position) { Node* current = _head; size_t current_position = 0; while (current != nullptr) { - if (current_position == position-1) { + if (current_position == position - 1) { break; } current = current->next; From 0ec4175b0a108378640d5d72c812adc164be17ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 20:37:14 +0300 Subject: [PATCH 117/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20=3D=3D=20=D0=B8=20!=3D=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=98=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index b1ce7ea9..1a4a57d7 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -28,12 +28,16 @@ class List { Iterator& operator++() {} Iterator operator++(int) {} - bool operator==(const Iterator& it) const {} - bool operator!=(const Iterator& it) const {} - + bool operator==(const Iterator& it) const { + return this->_current == it._current; + } + bool operator!=(const Iterator& it) const { + return !(*this == it); + } T& operator*() {} const T& operator*() const {} }; + Iterator begin() { return Iterator(_head); } From e73d065f353a97905e1566c21ffe999ed46f6258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 20:47:07 +0300 Subject: [PATCH 118/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20+=3D=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=20=D0=98=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib_list/List.h b/lib_list/List.h index 1a4a57d7..6395b6ad 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -24,7 +24,12 @@ class List { _current = other._current; return *this; } - Iterator& operator+=(size_t n) {} + Iterator& operator+=(size_t n) { + for (size_t i = 0; i < n && _current != nullptr; ++i) { + _current = _current->next; + } + return *this; + } Iterator& operator++() {} Iterator operator++(int) {} From bc8b4576825a396c7db3e95722178d33d931bde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 20:59:52 +0300 Subject: [PATCH 119/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20*=20=D0=B8=20++=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=98=D1=82=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 6395b6ad..ef27ce1f 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -30,19 +30,28 @@ class List { } return *this; } - Iterator& operator++() {} - Iterator operator++(int) {} - + Iterator& operator++() { + if (_current != nullptr) { + _current = _current->next; + } + return *this; + } + Iterator operator++(int) { + + } bool operator==(const Iterator& it) const { return this->_current == it._current; } bool operator!=(const Iterator& it) const { return !(*this == it); } - T& operator*() {} - const T& operator*() const {} + T& operator*() { + return _current->value; + } + const T& operator*() const { + return _current->value; + } }; - Iterator begin() { return Iterator(_head); } From 1fa2561d1674e0fac20225fba2b2eb2e51c09b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 21:31:10 +0300 Subject: [PATCH 120/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B0=20++=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=98=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib_list/List.h b/lib_list/List.h index ef27ce1f..2261a63a 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -37,7 +37,11 @@ class List { return *this; } Iterator operator++(int) { - + Iterator it = *this; + if (_current != nullptr) { + _current = _current->next; + } + return it; } bool operator==(const Iterator& it) const { return this->_current == it._current; From d7a5bb6282be6d33ad401dd157d8cebb25b3a4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 21:56:32 +0300 Subject: [PATCH 121/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=20List=20=D0=BF=D1=80=D0=B8=20=D0=BF=D0=BE=D0=BC?= =?UTF-8?q?=D0=BE=D1=89=D0=B8=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20Iterator,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20const-=D0=B2=D0=B5=D1=80=D1=81=D0=B8?= =?UTF-8?q?=D0=B8=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20begin()?= =?UTF-8?q?=20=D0=B8=20end()=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20List?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 2261a63a..289e8cb3 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -50,9 +50,15 @@ class List { return !(*this == it); } T& operator*() { + if (_current == nullptr) { + throw std::runtime_error("Dereferencing end iterator"); + } return _current->value; } const T& operator*() const { + if (_current == nullptr) { + throw std::runtime_error("Dereferencing end iterator"); + } return _current->value; } }; @@ -62,6 +68,12 @@ class List { Iterator end() { return Iterator(nullptr); } + Iterator begin() const { + return Iterator(_head); + } + Iterator end() const { + return Iterator(nullptr); + } List(); ~List(); List(const List& other_list); @@ -85,7 +97,6 @@ class List { }; template List::List() : _head(nullptr), _tail(nullptr), _count_elements(0) {} - template List::~List() { while (_head != nullptr) { @@ -97,10 +108,8 @@ List::~List() { template List::List(const List& other_list) : _head(nullptr), _tail(nullptr), _count_elements(0) { - Node* current = other_list._head; - while (current != nullptr) { - this->push_back(current->value); - current = current->next; + for (auto it = other_list.begin(); it != other_list.end(); ++it) { + this->push_back(*it); } } template From ede8f44bba6043c33f9141b4df36c4c1e71295ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 22:25:45 +0300 Subject: [PATCH 122/275] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=20List?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=20=D1=82=D0=B0=D0=BA,=20=D1=87=D1=82=D0=BE=20=D0=B2=20=D0=BD?= =?UTF-8?q?=D0=B8=D0=B7=D0=BA=D0=BE=D1=83=D1=80=D0=BE=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=B2=D1=8B=D1=85=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=D1=85?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D1=8E=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=83=D0=BA=D0=B0=D0=B7=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=20(=D1=8D=D1=84=D1=84=D0=B5=D0=BA=D1=82=D0=B8=D0=B2?= =?UTF-8?q?=D0=BD=D0=BE),=20=D0=B0=20=D0=B2=D1=8B=D1=81=D0=BE=D0=BA=D0=BE?= =?UTF-8?q?=D1=83=D1=80=D0=BE=D0=B2=D0=BD=D0=B5=D0=B2=D1=8B=D1=85=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D1=8F=D1=85=20-=20=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=D0=B0=D1=80=D1=82=D0=BE=D1=80=D1=8B=20(=D1=87?= =?UTF-8?q?=D0=B8=D1=82=D0=B0=D0=B5=D0=BC=D0=BE=D1=81=D1=82=D1=8C=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=20=D0=BB=D1=83=D1=87=D1=88=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index 289e8cb3..d4a735d5 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -68,7 +68,7 @@ class List { Iterator end() { return Iterator(nullptr); } - Iterator begin() const { + Iterator begin() const { return Iterator(_head); } Iterator end() const { @@ -198,14 +198,14 @@ bool List::operator==(const List& other) const { if (this->get_size() != other.get_size()) { return false; } - Node* current_this = _head; - Node* current_other = other.get_head(); - while (current_this != nullptr && current_other != nullptr) { - if (current_this->value != current_other->value) { + auto it_this = this->begin(); + auto it_other = other.begin(); + while (it_this != this->end() && it_other != other.end()) { + if (*it_this != *it_other) { return false; } - current_this = current_this->next; - current_other = current_other->next; + ++it_this; + ++it_other; } return true; } @@ -222,10 +222,8 @@ List& List::operator=(const List& other) { delete temporary; } _count_elements = 0; - Node* current = other._head; - while (current != nullptr) { - this->push_back(current->value); - current = current->next; + for (auto it = other.begin(); it != other.end(); ++it) { + this->push_back(*it); } } return *this; From 2d917a574160c7ecddc91419c16906c47c5995d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 10 Nov 2025 22:53:43 +0300 Subject: [PATCH 123/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Lis?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 5e6b6ec0..208044c3 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -440,3 +440,48 @@ TEST(TestListLib, list_node_erase_with_exception2) { // Act & Assert ASSERT_THROW(list.erase(node), std::invalid_argument); } +TEST(TestListLib, list_iterator_read) { + // Arrange + List list; + list.push_back(80); + list.push_back(320); + list.push_back(107); + // Act + auto it = list.begin(); + it += 2; + // Assert + EXPECT_EQ(107, *it); +} +TEST(TestListLib, list_iterator_write) { + // Arrange + List list; + list.push_back(50); + list.push_back(20); + list.push_back(7); + list.push_back(1); + list.push_back(300); + // Act + auto it = list.begin(); + it += 3; + *it = 900; + // Assert + EXPECT_EQ(900, *it); +} +TEST(TestListLib, list_iterator_empty_list) { + // Arrange + List list; + // Act & Assert + EXPECT_EQ(list.begin(), list.end()); + + auto it = list.begin(); + ++it; + EXPECT_EQ(it, list.end()); + + auto end_it = list.end(); + ++end_it; + EXPECT_EQ(end_it, list.end()); + + auto it2 = list.begin(); + it2 += 5; + EXPECT_EQ(it2, list.end()); +} From cab1e0d4c20994544e57927adff1cb7e42f84e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 11 Nov 2025 09:58:26 +0300 Subject: [PATCH 124/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D1=86=D0=B8=D0=BA=D0=BB=D0=B0=20=D0=B2=20=D0=BB=D0=B8=D0=BD?= =?UTF-8?q?=D0=B5=D0=B9=D0=BD=D0=BE=D0=BC=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 1be5f29d..cda2f96e 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -1,6 +1,7 @@ #pragma once #include "../lib_matrix/matrix.h" #include "../lib_stack/stack.h" +#include "../lib_list/List.h" #include #include #include @@ -151,3 +152,23 @@ void read_expression(std::string expression) { throw std::invalid_argument("No operands in expression"); } } +template +bool is_looped(const List& list) { + auto it1_fast = list.begin(); + auto it2_slow = list.begin(); + while (it1_fast != list.end()) { + if (list.is_empty()) { + return false; + } + it2_slow++; + it1_fast++; + if (it1_fast != list.end()) { + it1_fast++; + } + if (it1_fast == it2_slow) { + return true; + } + } + return false; +} + From 431001992fc8d820a08636828ae4589fa7a26093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 11 Nov 2025 09:59:42 +0300 Subject: [PATCH 125/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B2=D1=83=D1=8E=20=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=81=D0=B8=D1=8E=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D1=86=D0=B8?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=20=D0=B2=20=D0=BB=D0=B8=D0=BD=D0=B5=D0=B9?= =?UTF-8?q?=D0=BD=D0=BE=D0=BC=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5=20(tr?= =?UTF-8?q?ue,=20false=20=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D1=81=20=D0=BF=D1=83=D1=81=D1=82=D1=8B=D0=BC=20=D1=81=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BE=D0=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 7f1e3f0f..68876047 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -158,4 +158,31 @@ TEST(TestAlgorithmLib, test_read_expression_no_operand_before_opening_bracket) { std::string expression = "a ( a + b)"; EXPECT_THROW(read_expression(expression), std::invalid_argument); } - +TEST(TestAlgorithmLib, test_find_loop_in_list_1_false) { + // Arrange & Act + List list; + list.push_back(90); + list.push_back(30); + list.push_back(100); + // Assert + EXPECT_FALSE(is_looped(list)); +} +TEST(TestAlgorithmLib, test_find_loop_in_list_1_true) { + // Arrange & Act + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(30); + list.get_tail()->next = list.get_head(); + // Assert + EXPECT_TRUE(is_looped(list)); + list.get_tail()->next = nullptr; +} +TEST(TestAlgorithmLib, test_find_loop_in_list_1_with_empty_list) { + // Arrange & Act + List list; + // Assert + EXPECT_FALSE(is_looped(list)); +} From ad3740dc336785b1ceb63a70c8249398e1ed0405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 11 Nov 2025 10:39:18 +0300 Subject: [PATCH 126/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B0=20=D0=B2=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index cda2f96e..d6179570 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -153,13 +153,13 @@ void read_expression(std::string expression) { } } template -bool is_looped(const List& list) { +bool is_looped1(const List& list) { + if (list.is_empty()) { + return false; + } auto it1_fast = list.begin(); auto it2_slow = list.begin(); while (it1_fast != list.end()) { - if (list.is_empty()) { - return false; - } it2_slow++; it1_fast++; if (it1_fast != list.end()) { @@ -171,4 +171,32 @@ bool is_looped(const List& list) { } return false; } +template +bool is_looped2(List& list) { + if (list.is_empty()) { + return false; + } + Node* original_head = list.get_head(); + Node* current = list.get_head(); + Node* prev = nullptr; + while (current != nullptr) { + Node* next_temp = current->next; + current->next = prev; + prev = current; + current = next_temp; + } + bool has_cycle = (prev == original_head); + + current = prev; + prev = nullptr; + while (current != nullptr) { + Node* next_temp = current->next; + current->next = prev; + prev = current; + current = next_temp; + + } + return has_cycle; +} + \ No newline at end of file From 51f48248f11c293f304c7ba2f58c118b0d02cc4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 11 Nov 2025 10:47:38 +0300 Subject: [PATCH 127/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=D1=82=D0=BE=D1=80=D1=83=D1=8E=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B0=20=D0=B2=20=D1=81=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 68876047..46f6dd74 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -165,7 +165,7 @@ TEST(TestAlgorithmLib, test_find_loop_in_list_1_false) { list.push_back(30); list.push_back(100); // Assert - EXPECT_FALSE(is_looped(list)); + EXPECT_FALSE(is_looped1(list)); } TEST(TestAlgorithmLib, test_find_loop_in_list_1_true) { // Arrange & Act @@ -177,12 +177,43 @@ TEST(TestAlgorithmLib, test_find_loop_in_list_1_true) { list.push_back(30); list.get_tail()->next = list.get_head(); // Assert - EXPECT_TRUE(is_looped(list)); + EXPECT_TRUE(is_looped1(list)); list.get_tail()->next = nullptr; } TEST(TestAlgorithmLib, test_find_loop_in_list_1_with_empty_list) { // Arrange & Act List list; // Assert - EXPECT_FALSE(is_looped(list)); + EXPECT_FALSE(is_looped1(list)); } + +TEST(TestAlgorithmLib, test_find_loop_in_list_2_false) { + // Arrange & Act + List list; + list.push_back(8); + list.push_back(5); + list.push_back(100); + list.push_back(1); + // Assert + EXPECT_FALSE(is_looped2(list)); +} +TEST(TestAlgorithmLib, test_find_loop_in_list_2_true) { + // Arrange & Act + List list; + list.push_back(103); + list.push_back(2); + list.push_back(304); + list.push_back(70); + list.get_tail()->next = list.get_head(); + // Assert + EXPECT_TRUE(is_looped2(list)); + list.get_tail()->next = nullptr; +} + +TEST(TestAlgorithmLib, test_find_loop_in_list_2_with_empty_list) { + // Arrange & Act + List list; + // Assert + EXPECT_FALSE(is_looped2(list)); +} + From 468fb462eb5199ae37a9f918751560ed3fa43e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 13 Nov 2025 09:25:31 +0300 Subject: [PATCH 128/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=B2=D1=83=D0=BD=D0=B0=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_doubly_linked_list/doubly_linked_list.cpp diff --git a/lib_doubly_linked_list/doubly_linked_list.cpp b/lib_doubly_linked_list/doubly_linked_list.cpp new file mode 100644 index 00000000..e96b2162 --- /dev/null +++ b/lib_doubly_linked_list/doubly_linked_list.cpp @@ -0,0 +1 @@ +#include "doubly_linked_list.h" From 5b7b5dc9c36bccf5cb1048e489b74ec6c6f8801c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 13 Nov 2025 09:26:39 +0300 Subject: [PATCH 129/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B2=D1=83=D0=BD=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D0=B0,=20=D1=80=D0=B5=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=B8=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=20=D0=B4=D0=BB=D1=8F=20=D1=8D=D1=82?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D1=81=20=D1=81=D0=BE=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D1=83=D1=8E=D1=89=D0=B8=D0=BC=D0=B8=20=D0=B4=D0=BE=D0=BF?= =?UTF-8?q?.=20=D0=BE=D0=BF=D0=B5=D0=B0=D1=80=D1=82=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=20--,=20-=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 117 ++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 lib_doubly_linked_list/doubly_linked_list.h diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h new file mode 100644 index 00000000..2c250344 --- /dev/null +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -0,0 +1,117 @@ +#pragma once +#include +template +struct Node { + T value; + Node* next; + Node* previous; + Node(T value_, Node* next_ = nullptr, Node* previous_ = nullptr) : value(value_), next(next_), previous(previous_) {} +}; +template +class DoublyLinkedList { +private: + Node* _head; + Node* _tail; + size_t _count_elements; +public: + class Iterator { + private: + Node* _current; + public: + Iterator() : _current(nullptr) {} + Iterator(Node* node) : _current(node) {} + Iterator(const Iterator& other) : _current(other._current) {} + Iterator& operator=(const Iterator& other) { + _current = other._current; + return *this; + } + Iterator& operator+=(size_t n) { + for (size_t i = 0; i < n && _current != nullptr; ++i) { + _current = _current->next; + } + return *this; + } + Iterator& operator-=(size_t n) { + for (size_t i = 0; i < n && _current != nullptr; --i) { + _current = _current->previous; + } + return *this; + } + Iterator& operator++() { + if (_current != nullptr) { + _current = _current->next; + } + return *this; + } + Iterator operator++(int) { + Iterator it = *this; + if (_current != nullptr) { + _current = _current->next; + } + return it; + } + Iterator& operator--() { + if (_current != nullptr) { + _current = _current->previous; + } + return *this; + } + Iterator operator--(int) { + Iterator it = *this; + if (_current != nullptr) { + _current = _current->previous; + } + return it; + } + bool operator==(const Iterator& it) const { + return this->_current == it._current; + } + bool operator!=(const Iterator& it) const { + return !(*this == it); + } + T& operator*() { + if (_current == nullptr) { + throw std::runtime_error("Dereferencing end iterator"); + } + return _current->value; + } + const T& operator*() const { + if (_current == nullptr) { + throw std::runtime_error("Dereferencing end iterator"); + } + return _current->value; + } + }; + Iterator begin() { + return Iterator(_head); + } + Iterator end() { + return Iterator(nullptr); + } + Iterator begin() const { + return Iterator(_head); + } + Iterator end() const { + return Iterator(nullptr); + } + DoublyLinkedList(); + ~DoublyLinkedList(); + DoublyLinkedList(const DoublyLinkedList& other_list); + size_t get_size() const noexcept; + Node* get_head() const noexcept; + Node* get_tail() const noexcept; + + bool operator==(const DoublyLinkedList& other) const; + bool operator!=(const DoublyLinkedList& other) const; + DoublyLinkedList& operator=(const DoublyLinkedList& other); + + bool is_empty() const noexcept; + void push_back(const T& value) noexcept; + void push_front(const T& value) noexcept; + void insert(size_t position, const T& value); + void insert(Node* node, const T& value); + void pop_front(); + void pop_back(); + void erase(size_t position); + void erase(Node* node); +}; \ No newline at end of file From c5e38661fcc0d47f3652c82901535686313b333d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 13 Nov 2025 09:45:44 +0300 Subject: [PATCH 130/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D1=8B,=20=D0=B4=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=20=D0=B8=20=D0=B3=D0=B5?= =?UTF-8?q?=D1=82=D1=82=D0=B5=D1=80=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=B4?= =?UTF-8?q?=D0=B2=D1=83=D1=81=D0=B2=D1=8F=D0=B7=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 30 ++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index 2c250344..587dff92 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -114,4 +114,32 @@ class DoublyLinkedList { void pop_back(); void erase(size_t position); void erase(Node* node); -}; \ No newline at end of file +}; +template +DoublyLinkedList::DoublyLinkedList(): _head(nullptr), _tail(nullptr), _count_elements(0) {} +template +DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList& other_list): _head(nullptr), _tail(nullptr), _count_elements(nullptr) { + for (auto it = other_list.begin(); it != other_list.end(); ++it) { + this->push_back(*it); + } +} +template +DoublyLinkedList::~DoublyLinkedList() { + while (_head != nullptr) { + Node* temporary = _head; + _head = _head->next; + delete temporary; + } +} +template +size_t DoublyLinkedList::get_size() const noexcept { + return _count_elements; +} +template +Node* DoublyLinkedList::get_head() const noexcept { + return _head; +} +template +Node* DoublyLinkedList::get_tail() const noexcept { + return _tail; +} From 7f9a4d0dcbb40c95a092288fd8b9a96965d18e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 13 Nov 2025 10:32:37 +0300 Subject: [PATCH 131/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=20=3D=3D,=20!=3D,=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 39 +++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index 587dff92..4a727f0a 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -32,7 +32,7 @@ class DoublyLinkedList { return *this; } Iterator& operator-=(size_t n) { - for (size_t i = 0; i < n && _current != nullptr; --i) { + for (size_t i = 0; i < n && _current != nullptr; ++i) { _current = _current->previous; } return *this; @@ -118,7 +118,7 @@ class DoublyLinkedList { template DoublyLinkedList::DoublyLinkedList(): _head(nullptr), _tail(nullptr), _count_elements(0) {} template -DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList& other_list): _head(nullptr), _tail(nullptr), _count_elements(nullptr) { +DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList& other_list): _head(nullptr), _tail(nullptr), _count_elements(0) { for (auto it = other_list.begin(); it != other_list.end(); ++it) { this->push_back(*it); } @@ -143,3 +143,38 @@ template Node* DoublyLinkedList::get_tail() const noexcept { return _tail; } +template +bool DoublyLinkedList::operator==(const DoublyLinkedList& other) const { + if (this->get_size() != other.get_size()) { + return false; + } + auto it_this = this->begin(); + auto it_other = other.begin(); + while (it_this != this->end() && it_other != other.end()) { + if (*it_this != *it_other) { + return false; + } + ++it_this; + ++it_other; + } + return true; +} +template +bool DoublyLinkedList::operator!=(const DoublyLinkedList& other) const { + return !(*this == other); +} +template +DoublyLinkedList& DoublyLinkedList::operator=(const DoublyLinkedList& other) { + if (this != &other) { + while (_head != nullptr) { + Node* temporary = _head; + _head = _head->next; + delete temporary; + } + _count_elements = 0; + for (auto it = other.begin(); it != other.end(); ++it) { + this->push_back(*it); + } + } + return *this; +} From 62eb8a130ede95d240186526aa9396fcc659d995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 18:37:40 +0300 Subject: [PATCH 132/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B,=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20is?= =?UTF-8?q?=5Fempty()=20=D0=98=20push=5Fback()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/test_doubly_linked_list.cpp diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp new file mode 100644 index 00000000..c491dec7 --- /dev/null +++ b/tests/test_doubly_linked_list.cpp @@ -0,0 +1,60 @@ +#include +#include "../lib_doubly_linked_list/doubly_linked_list.h" +TEST(TestDoublyLinkedListLib, doubly_linked_list_default_constructor) { + // Arrange & Act + DoublyLinkedList list; + // Assert + EXPECT_EQ(list.get_head(), nullptr); + EXPECT_EQ(list.get_tail(), nullptr); + EXPECT_EQ(list.get_size(), 0); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_copy_constructor) { + // Arrange & Act + DoublyLinkedList list1; + list1.push_back(10); + list1.push_back(20); + DoublyLinkedList list2(list1); + // Assert + EXPECT_NE(list1.get_head(), list2.get_head()); + EXPECT_NE(list1.get_tail(), list2.get_tail()); + + EXPECT_EQ(list1.get_head()->value, list2.get_head()->value); + EXPECT_EQ(list1.get_tail()->value, list2.get_tail()->value); + EXPECT_EQ(list1.get_size(), list2.get_size()); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_is_empty_false) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(10); + list.push_back(10); + // Assert + EXPECT_FALSE(list.is_empty()); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_is_empty_true) { + // Arrange & Act + DoublyLinkedList list; + // Assert + EXPECT_TRUE(list.is_empty()); +} +TEST(TestDoublyLinkedListLib, push_back_with_empty_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(30); + // Assert + EXPECT_EQ(list.get_size(), 1); + EXPECT_EQ(list.get_head(), list.get_tail()); +} +TEST(TestDoublyLinkedListLib, push_back_with_several_elements_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(300); + list.push_back(400); + list.push_back(500); + list.push_back(600); + list.push_back(700); + list.push_back(800); + list.push_back(900); + // Assert + EXPECT_EQ(list.get_tail()->value, 900); + EXPECT_EQ(list.get_size(), 7); +} From aaf7a669e77845eaff2c483a0162371a38dbd0b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 18:45:53 +0300 Subject: [PATCH 133/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20!=3D,=20=3D=3D,=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 86 ++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp index c491dec7..f95ef39e 100644 --- a/tests/test_doubly_linked_list.cpp +++ b/tests/test_doubly_linked_list.cpp @@ -39,7 +39,7 @@ TEST(TestDoublyLinkedListLib, doubly_linked_list_is_empty_true) { TEST(TestDoublyLinkedListLib, push_back_with_empty_doubly_linked_list) { // Arrange & Act DoublyLinkedList list; - list.push_back(30); + list.push_back(19); // Assert EXPECT_EQ(list.get_size(), 1); EXPECT_EQ(list.get_head(), list.get_tail()); @@ -48,13 +48,87 @@ TEST(TestDoublyLinkedListLib, push_back_with_several_elements_doubly_linked_list // Arrange & Act DoublyLinkedList list; list.push_back(300); - list.push_back(400); + list.push_back(0); list.push_back(500); - list.push_back(600); - list.push_back(700); + list.push_back(3); + list.push_back(5); list.push_back(800); - list.push_back(900); + list.push_back(2); // Assert - EXPECT_EQ(list.get_tail()->value, 900); + EXPECT_EQ(list.get_tail()->value, 2); EXPECT_EQ(list.get_size(), 7); } +TEST(TestDoublyLinkedListLib, doubly_linked_list_operator_equal_true) { + // Arrange & Act + DoublyLinkedList list1; + DoublyLinkedList list2; + list1.push_back(20); + list1.push_back(20); + list1.push_back(20); + + list2.push_back(20); + list2.push_back(20); + list2.push_back(20); + // Assert + EXPECT_TRUE(list1 == list2); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_operator_equal_false) { + // Arrange & Act + DoublyLinkedList list1; + DoublyLinkedList list2; + list1.push_back(29); + list1.push_back(40); + list1.push_back(22); + list1.push_back(100); + + list2.push_back(30); + list2.push_back(88); + list2.push_back(66); + // Assert + EXPECT_FALSE(list1 == list2); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_operator_not_equal_true) { + // Arrange & Act + DoublyLinkedList list1; + DoublyLinkedList list2; + list1.push_back(70); + list1.push_back(80); + list1.push_back(90); + list1.push_back(700); + + list2.push_back(1); + list2.push_back(2); + list2.push_back(3); + // Assert + EXPECT_TRUE(list1 != list2); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_operator_not_equal_false) { + // Arrange & Act + DoublyLinkedList list1; + DoublyLinkedList list2; + list1.push_back(70); + list1.push_back(70); + list1.push_back(70); + + list2.push_back(70); + list2.push_back(70); + list2.push_back(70); + // Assert + EXPECT_FALSE(list1 != list2); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_operator_assign) { + // Arrange & Act + DoublyLinkedList list1; + DoublyLinkedList list2; + list1.push_back(30); + list1.push_back(800); + list1.push_back(70); + list1.push_back(600); + + list2.push_back(30); + list2.push_back(20); + list2.push_back(30); + list2 = list1; + // Assert + EXPECT_TRUE(list1 == list2); +} From 94872bab26fa5cb4ea9efd40e42b7ff83df81a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 18:47:04 +0300 Subject: [PATCH 134/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20is=5Fempty()=20=D0=B8=20push=5Fback()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index 4a727f0a..bc4f05a5 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -178,3 +178,20 @@ DoublyLinkedList& DoublyLinkedList::operator=(const DoublyLinkedList& o } return *this; } +template +bool DoublyLinkedList::is_empty() const noexcept { + return _head == nullptr && _tail == nullptr; +} +template +void DoublyLinkedList::push_back(const T& value) noexcept { + Node* node = new Node(value); + if (this->is_empty()) { + _head = node; + _tail = node; + _count_elements++; + return; + } + _tail->next = node; + _tail = node; + _count_elements++; +} \ No newline at end of file From 29e64639ee6343a94368efc049ea38283846599a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 19:37:25 +0300 Subject: [PATCH 135/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20push=20front()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 26 +++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index bc4f05a5..9f3e24d5 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -188,10 +188,26 @@ void DoublyLinkedList::push_back(const T& value) noexcept { if (this->is_empty()) { _head = node; _tail = node; - _count_elements++; - return; } - _tail->next = node; - _tail = node; + else { + node->previous = _tail; + _tail->next = node; + _tail = node; + } _count_elements++; -} \ No newline at end of file +} +template +void DoublyLinkedList::push_front(const T& value) noexcept { + Node* node = new Node(value); + if (this->is_empty()) { + _head = node; + _tail = node; + } + else { + _head->previous = node; + node->next = _head; + _head = node; + } + _count_elements++; +} + \ No newline at end of file From 41dc6e71fdf5382ab5f6691d005950ed73823296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 20:16:05 +0300 Subject: [PATCH 136/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20push=5Ffront(),=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D1=83=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B5=D0=B9=20=D0=BC?= =?UTF-8?q?=D0=B5=D0=B6=D0=B4=D1=83=20=D1=83=D0=B7=D0=BB=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?=20=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20?= =?UTF-8?q?=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=BD=D1=8B=D1=85=20=D1=83?= =?UTF-8?q?=D0=B7=D0=BB=D0=BE=D0=B2=20(=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?push=5Fback(),=20push=5Ffront())?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp index f95ef39e..8d2f5bc2 100644 --- a/tests/test_doubly_linked_list.cpp +++ b/tests/test_doubly_linked_list.cpp @@ -132,3 +132,72 @@ TEST(TestDoublyLinkedListLib, doubly_linked_list_operator_assign) { // Assert EXPECT_TRUE(list1 == list2); } +TEST(TestDoublyLinkedListLib, push_front_with_empty_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_front(10); + // Assert + EXPECT_EQ(list.get_size(), 1); + EXPECT_EQ(list.get_head(), list.get_tail()); +} +TEST(TestDoublyLinkedListLib, push_front_with_several_elements_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_front(100); + list.push_front(550); + list.push_front(39); + list.push_front(400); + list.push_front(30); + // Assert + EXPECT_EQ(list.get_head()->value, 30); + EXPECT_EQ(list.get_size(), 5); +} +TEST(TestDoublyLinkedListLib, push_back_check_сonnections_between_nodes_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + // Assert + EXPECT_EQ(list.get_head()->value, 10); + EXPECT_EQ(list.get_head()->next->value, 20); + EXPECT_EQ(list.get_head()->next->next->value, 30); + + EXPECT_EQ(list.get_tail()->value, 30); + EXPECT_EQ(list.get_tail()->previous->value, 20); + EXPECT_EQ(list.get_tail()->previous->previous->value, 10); +} +TEST(TestDoublyLinkedListLib, push_back_check_boundary_conditions_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + // Assert + EXPECT_EQ(list.get_head()->previous, nullptr); + EXPECT_EQ(list.get_tail()->next, nullptr); +} +TEST(TestDoublyLinkedListLib, push_front_check_сonnections_between_nodes_doubly_linked_list) { + DoublyLinkedList list; + list.push_front(10); + list.push_front(20); + list.push_front(30); + + EXPECT_EQ(list.get_head()->value, 30); + EXPECT_EQ(list.get_head()->next->value, 20); + EXPECT_EQ(list.get_head()->next->next->value, 10); + + EXPECT_EQ(list.get_tail()->value, 10); + EXPECT_EQ(list.get_tail()->previous->value, 20); + EXPECT_EQ(list.get_tail()->previous->previous->value, 30); +} +TEST(TestDoublyLinkedListLib, push_front_check_boundary_conditions_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_front(10); + list.push_front(20); + list.push_front(30); + // Assert + EXPECT_EQ(list.get_head()->previous, nullptr); + EXPECT_EQ(list.get_tail()->next, nullptr); +} From 1c5d6bf39d02125bb4120d2297ab102d966aab98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 20:57:33 +0300 Subject: [PATCH 137/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20insert(),=20pop=5Ffront(),=20pop=5Fback()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 78 ++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index 9f3e24d5..bcae8538 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -189,7 +189,7 @@ void DoublyLinkedList::push_back(const T& value) noexcept { _head = node; _tail = node; } - else { + else { node->previous = _tail; _tail->next = node; _tail = node; @@ -210,4 +210,78 @@ void DoublyLinkedList::push_front(const T& value) noexcept { } _count_elements++; } - \ No newline at end of file +template +void DoublyLinkedList::insert(size_t position, const T& value) { + if (position == 0) { + this->push_front(value); + return; + } + if (position == _count_elements - 1) { + this->push_back(value); + return; + } + Node* current = _head; + size_t current_position = 0; + while (current != nullptr && current_position != position - 1) { + current = current->next; + current_position++; + } + if (current == nullptr) { + throw std::invalid_argument("Uncorrect position"); + } + this->insert(current, value); +} +template +void DoublyLinkedList::insert(Node* node, const T& value) { + if (node == nullptr || this->is_empty()) { + throw std::logic_error("The transmitted node or/and the list can't be empty!"); + } + Node* new_node = new Node(value); + new_node->previous = node; + new_node->next = node->next; + + node->next = new_node; + if (new_node->next != nullptr) { + new_node->next->previous = new_node; + } + if (node == _tail) { + _tail = new_node; + } + _count_elements++; +} +template +void DoublyLinkedList::pop_front() { + if (this->is_empty()) { + throw std::logic_error("Can't pop the first element at empty list!"); + } + if (_head == _tail) { + delete _head; + _head = nullptr; + _tail = nullptr; + _count_elements--; + return; + } + Node* temporary = _head; + _head = _head->next; + _head->previous = nullptr; + delete temporary; + _count_elements--; +} +template +void DoublyLinkedList::pop_back() { + if (this->is_empty()) { + throw std::logic_error("Can't pop the last element at empty list!"); + } + if (_head == _tail) { + delete _head; + _tail = nullptr; + _head = nullptr; + _count_elements--; + return; + } + Node* temporary = _tail; + _tail = _tail->previous; + _tail->next = nullptr; + delete temporary; + _count_elements--; +} From d6cb7673ee5d3cc031b8496806e63de56aa86c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 21:02:49 +0300 Subject: [PATCH 138/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20pop=5Ffront(),=20pop=5Fback()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp index 8d2f5bc2..4479c069 100644 --- a/tests/test_doubly_linked_list.cpp +++ b/tests/test_doubly_linked_list.cpp @@ -201,3 +201,67 @@ TEST(TestDoublyLinkedListLib, push_front_check_boundary_conditions_doubly_linked EXPECT_EQ(list.get_head()->previous, nullptr); EXPECT_EQ(list.get_tail()->next, nullptr); } +TEST(TestDoublyLinkedListLib, pop_back_with_empty_doubly_linked_list_exception) { + // Arrange & Act + DoublyLinkedList list; + // Assert + ASSERT_THROW(list.pop_back(), std::logic_error); +} +TEST(TestDoublyLinkedListLib, pop_back_with_one_doubly_linked_list_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(30); + list.pop_back(); + // Assert + EXPECT_EQ(list.get_head(), nullptr); + EXPECT_EQ(list.get_tail(), nullptr); + EXPECT_EQ(list.get_size(), 0); +} +TEST(TestDoublyLinkedListLib, pop_back_with_several_elements_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(40); + list.push_back(44); + list.push_back(43); + list.push_back(1); + list.push_back(5); + list.pop_back(); + list.pop_back(); + // Assert + EXPECT_EQ(list.get_tail()->value, 43); + EXPECT_EQ(list.get_size(), 3); +} +TEST(TestDoublyLinkedListLib, pop_front_with_empty_doubly_linked_list_exception) { + // Arrange & Act + DoublyLinkedList list; + // Assert + ASSERT_THROW(list.pop_front(), std::logic_error); +} +TEST(TestDoublyLinkedListLib, pop_front_with_one_element_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(79); + list.pop_front(); + // Assert + EXPECT_EQ(list.get_head(), nullptr); + EXPECT_EQ(list.get_tail(), nullptr); + EXPECT_EQ(list.get_size(), 0); +} +TEST(TestDoublyLinkedListLib, pop_front_with_several_elements_doubly_linked_list) { + // Arrange & Act + DoublyLinkedList list; + list.push_back(1110); + list.push_back(20); + list.push_back(52); + list.push_back(4); + list.push_back(5220); + list.push_back(50); + list.push_back(50); + + list.pop_front(); + list.pop_front(); + list.pop_front(); + // Assert + EXPECT_EQ(list.get_head()->value, 4); + EXPECT_EQ(list.get_size(), 4); +} From 7e5db33742bbab89669756d0bc6a23e914186c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 15 Nov 2025 21:15:44 +0300 Subject: [PATCH 139/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B5=D1=89=D1=91=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B=20=D0=BD=D0=B0=20insert()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp index 4479c069..c50e9ecc 100644 --- a/tests/test_doubly_linked_list.cpp +++ b/tests/test_doubly_linked_list.cpp @@ -265,3 +265,82 @@ TEST(TestDoublyLinkedListLib, pop_front_with_several_elements_doubly_linked_list EXPECT_EQ(list.get_head()->value, 4); EXPECT_EQ(list.get_size(), 4); } +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_insert_in_end) { + // Arrange + DoublyLinkedList list; + list.push_back(320); + list.push_back(22); + list.push_back(1000); + list.push_back(42); + list.push_back(520); + size_t position = 4; + // Act + list.insert(position, 900); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 320); + EXPECT_EQ(list.get_head()->next->value, 22); + EXPECT_EQ(list.get_head()->next->next->value, 1000); + EXPECT_EQ(list.get_head()->next->next->next->value, 42); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 520); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 900); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_insert) { + // Arrange + DoublyLinkedList list; + list.push_back(30); + list.push_back(4); + list.push_back(30); + list.push_back(2); + list.push_back(3); + size_t position = 2; + // Act + list.insert(position, 250); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 30); + EXPECT_EQ(list.get_head()->next->value, 4); + EXPECT_EQ(list.get_head()->next->next->value, 250); + EXPECT_EQ(list.get_head()->next->next->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 2); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 3); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_insert_with_exception) { + // Arrange + DoublyLinkedList list; + list.push_back(1); + list.push_back(50); + list.push_back(30); + list.push_back(40); + list.push_back(1); + size_t position = 10; + // Act & Assert + ASSERT_THROW(list.insert(position, 500), std::invalid_argument); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_node_insert) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(2); + list.push_back(30); + list.push_back(77); + list.push_back(50); + // Act + Node* node = list.get_head()->next; + list.insert(node, 700); + // Assert + EXPECT_EQ(list.get_size(), 6); + EXPECT_EQ(list.get_head()->value, 10); + EXPECT_EQ(list.get_head()->next->value, 2); + EXPECT_EQ(list.get_head()->next->next->value, 700); + EXPECT_EQ(list.get_head()->next->next->next->value, 30); + EXPECT_EQ(list.get_head()->next->next->next->next->value, 77); + EXPECT_EQ(list.get_head()->next->next->next->next->next->value, 50); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_node_insert_with_exception) { + // Arrange + DoublyLinkedList list; + Node* node = list.get_head(); + // Act & Assert + ASSERT_THROW(list.insert(node, 1), std::logic_error); +} From de2ef9b155491e67344aa0e3853f5b7070143eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 12:12:37 +0300 Subject: [PATCH 140/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=B2=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=D1=85=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_list.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 208044c3..4ea32003 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -317,19 +317,6 @@ TEST(TestListLib, list_position_insert_with_exception) { // Act & Assert ASSERT_THROW(list.insert(position, 500), std::invalid_argument); } - - - - - - - - - - - - - TEST(TestListLib, list_position_erase_in_begin) { // Arrange List list; @@ -348,7 +335,7 @@ TEST(TestListLib, list_position_erase_in_begin) { EXPECT_EQ(list.get_head()->next->next->value, 90); EXPECT_EQ(list.get_head()->next->next->next->value, 20); } -TEST(TestListLib, list_erase_erase_in_end) { +TEST(TestListLib, list_position_erase_in_end) { // Arrange List list; list.push_back(680); From 193c30ad05b65124c1da1e679751aa433149e457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 12:29:59 +0300 Subject: [PATCH 141/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20era?= =?UTF-8?q?se()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index bcae8538..f7c9d130 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -285,3 +285,51 @@ void DoublyLinkedList::pop_back() { delete temporary; _count_elements--; } +template +void DoublyLinkedList::erase(size_t position) { + if (position == 0) { + this->pop_front(); + return; + } + if (position == _count_elements - 1) { + this->pop_back(); + return; + } + Node* current = _head; + size_t current_position = 0; + while (current != nullptr) { + if (current_position == position) { + break; + } + current = current->next; + current_position++; + } + if (current == nullptr) { + throw std::invalid_argument("Uncorrect position"); + } + + Node* temporary = current; + current->previous->next = current->next; + current->next->previous = current->previous; + delete temporary; + _count_elements--; +} +template +void DoublyLinkedList::erase(Node* node) { + if (node == nullptr || this->is_empty()) { + throw std::logic_error("The transmitted node or/and the list can't be empty!"); + } + if (node == _head) { + this->pop_front(); + return; + } + if (node == _tail) { + this->pop_back(); + return; + } // 10 20 <- 30 -> 40 50 + Node* temporary = node; + temporary->previous->next = temporary->next; + temporary->next->previous = temporary->previous; + delete temporary; + _count_elements--; +} From d718596a79d483f8d31fdb7bd9825100ee177bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 12:30:51 +0300 Subject: [PATCH 142/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20erase()=20=D0=B8=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=B4=D0=B2=D1=83=D1=81=D0=B2=D1=8F=D0=B7?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 147 ++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp index c50e9ecc..b6d3111f 100644 --- a/tests/test_doubly_linked_list.cpp +++ b/tests/test_doubly_linked_list.cpp @@ -344,3 +344,150 @@ TEST(TestDoublyLinkedListLib, doubly_linked_list_node_insert_with_exception) { // Act & Assert ASSERT_THROW(list.insert(node, 1), std::logic_error); } + + +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_erase_in_begin) { + // Arrange + DoublyLinkedList list; + list.push_back(3); + list.push_back(7); + list.push_back(830); + list.push_back(1); + list.push_back(20); + size_t position = 0; + // Act + list.erase(position); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 7); + EXPECT_EQ(list.get_head()->next->value, 830); + EXPECT_EQ(list.get_head()->next->next->value, 1); + EXPECT_EQ(list.get_head()->next->next->next->value, 20); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_erase_in_end) { + // Arrange + DoublyLinkedList list; + list.push_back(60); + list.push_back(2); + list.push_back(80); + list.push_back(90); + list.push_back(20); + size_t position = 4; + // Act + list.erase(position); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 60); + EXPECT_EQ(list.get_head()->next->value, 2); + EXPECT_EQ(list.get_head()->next->next->value, 80); + EXPECT_EQ(list.get_head()->next->next->next->value, 90); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_erase) { + // Arrange + DoublyLinkedList list; + list.push_back(320); + list.push_back(4); + list.push_back(0); + list.push_back(40); + list.push_back(570); + size_t position = 3; + // Act + list.erase(position); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 320); + EXPECT_EQ(list.get_head()->next->value, 4); + EXPECT_EQ(list.get_head()->next->next->value, 0); + EXPECT_EQ(list.get_head()->next->next->next->value, 570); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_erase_with_exception1) { + // Arrange + DoublyLinkedList list; + size_t position = 100; + // Act & Assert + ASSERT_THROW(list.erase(position), std::invalid_argument); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_position_erase_with_exception2) { + // Arrange + DoublyLinkedList list; + list.push_back(32000); + list.push_back(8220); + list.push_back(320); + list.push_back(40); + list.push_back(50); + size_t position = 20; + // Act & Assert + ASSERT_THROW(list.erase(position), std::invalid_argument); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_node_erase) { + // Arrange + DoublyLinkedList list; + list.push_back(1); + list.push_back(840007); + list.push_back(3); + list.push_back(40); + list.push_back(55770); + Node* node = list.get_head()->next; + // Act + list.erase(node); + // Assert + EXPECT_EQ(list.get_size(), 4); + EXPECT_EQ(list.get_head()->value, 1); + EXPECT_EQ(list.get_head()->next->value, 3); + EXPECT_EQ(list.get_head()->next->next->value, 40); + EXPECT_EQ(list.get_head()->next->next->next->value, 55770); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_node_erase_with_exception) { + // Arrange + DoublyLinkedList list; + Node* node = list.get_head(); + // Act & Assert + ASSERT_THROW(list.erase(node), std::logic_error); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_read) { + // Arrange + DoublyLinkedList list; + list.push_back(1); + list.push_back(3); + list.push_back(17); + // Act + auto it = list.begin(); + it += 2; + // Assert + EXPECT_EQ(17, *it); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_write) { + // Arrange + DoublyLinkedList list; + list.push_back(50); + list.push_back(1); + list.push_back(79876); + list.push_back(12); + list.push_back(300); + // Act + auto it = list.begin(); + it += 3; + // Assert + EXPECT_EQ(12, *it); + *it = 900; + EXPECT_EQ(900, *it); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_empty_list) { + // Arrange + DoublyLinkedList list; + // Act & Assert + EXPECT_EQ(list.begin(), list.end()); + + auto it = list.begin(); + ++it; + EXPECT_EQ(it, list.end()); + + auto end_it = list.end(); + ++end_it; + EXPECT_EQ(end_it, list.end()); + + auto it2 = list.begin(); + it2 += 5; + EXPECT_EQ(it2, list.end()); +} From c738a25c6e2359f5db38f9702a1ae26345230212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 13:22:12 +0300 Subject: [PATCH 143/275] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8?= =?UTF-8?q?=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20=D0=BF=D1=80=D0=B8=D1=81?= =?UTF-8?q?=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=20ins?= =?UTF-8?q?ert()=20=D0=BF=D0=BE=20=D0=BF=D0=BE=D0=B7=D0=B8=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 44 ++++++++++----------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index f7c9d130..88250ce2 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -103,7 +103,7 @@ class DoublyLinkedList { bool operator==(const DoublyLinkedList& other) const; bool operator!=(const DoublyLinkedList& other) const; - DoublyLinkedList& operator=(const DoublyLinkedList& other); + DoublyLinkedList& operator=(const DoublyLinkedList other); bool is_empty() const noexcept; void push_back(const T& value) noexcept; @@ -164,18 +164,10 @@ bool DoublyLinkedList::operator!=(const DoublyLinkedList& other) const { return !(*this == other); } template -DoublyLinkedList& DoublyLinkedList::operator=(const DoublyLinkedList& other) { - if (this != &other) { - while (_head != nullptr) { - Node* temporary = _head; - _head = _head->next; - delete temporary; - } - _count_elements = 0; - for (auto it = other.begin(); it != other.end(); ++it) { - this->push_back(*it); - } - } +DoublyLinkedList& DoublyLinkedList::operator=(DoublyLinkedList other) { + std::swap(_head, other._head); + std::swap(_tail, other._tail); + std::swap(_count_elements, other._count_elements); return *this; } template @@ -213,23 +205,27 @@ void DoublyLinkedList::push_front(const T& value) noexcept { template void DoublyLinkedList::insert(size_t position, const T& value) { if (position == 0) { - this->push_front(value); + push_front(value); return; } - if (position == _count_elements - 1) { - this->push_back(value); + if (position >= _count_elements) { + push_back(value); return; } - Node* current = _head; - size_t current_position = 0; - while (current != nullptr && current_position != position - 1) { - current = current->next; - current_position++; + Node* current; + if (position <= _count_elements / 2) { + current = _head; + for (size_t i = 0; i < position - 1; ++i) { + current = current->next; + } } - if (current == nullptr) { - throw std::invalid_argument("Uncorrect position"); + else { + current = _tail; + for (size_t i = _count_elements - 1; i > position - 1; --i) { + current = current->previous; + } } - this->insert(current, value); + insert(current, value); } template void DoublyLinkedList::insert(Node* node, const T& value) { From 889f2669b2bbf59ac64aae493c836755dfc26809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 13:25:09 +0300 Subject: [PATCH 144/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_doubly_linked_list/doubly_linked_list.h | 26 +++++++++------------ 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/lib_doubly_linked_list/doubly_linked_list.h b/lib_doubly_linked_list/doubly_linked_list.h index 88250ce2..e6a0361e 100644 --- a/lib_doubly_linked_list/doubly_linked_list.h +++ b/lib_doubly_linked_list/doubly_linked_list.h @@ -205,27 +205,23 @@ void DoublyLinkedList::push_front(const T& value) noexcept { template void DoublyLinkedList::insert(size_t position, const T& value) { if (position == 0) { - push_front(value); + this->push_front(value); return; } - if (position >= _count_elements) { - push_back(value); + if (position == _count_elements - 1) { + this->push_back(value); return; } - Node* current; - if (position <= _count_elements / 2) { - current = _head; - for (size_t i = 0; i < position - 1; ++i) { - current = current->next; - } + Node* current = _head; + size_t current_position = 0; + while (current != nullptr && current_position != position - 1) { + current = current->next; + current_position++; } - else { - current = _tail; - for (size_t i = _count_elements - 1; i > position - 1; --i) { - current = current->previous; - } + if (current == nullptr) { + throw std::invalid_argument("Uncorrect position"); } - insert(current, value); + this->insert(current, value); } template void DoublyLinkedList::insert(Node* node, const T& value) { From b8d7ecd26f4feea69752ec25f1ccf6d2693b68aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 13:42:19 +0300 Subject: [PATCH 145/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20Tvector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/lib_vector/vector.h b/lib_vector/vector.h index 70a2233b..11b9b36d 100644 --- a/lib_vector/vector.h +++ b/lib_vector/vector.h @@ -31,6 +31,74 @@ void reserve(size_t new_capacity); void compact_storage(); public: + class Iterator { + private: + T* _current; + public: + Iterator() : _current(nullptr) {} + Iterator(T* ptr) : _current(ptr) {} + Iterator(const Iterator& other) : _current(other._current) {} + Iterator& operator=(const Iterator& other) { + _current = other._current; + return *this; + } + Iterator& operator+=(size_t n) { + if (_current != nullptr) { + _current += n; + } + return *this; + } + Iterator& operator-=(size_t n) { + if (_current != nullptr) { + _current -= n; + } + return *this; + } + Iterator& operator--() { + if (_current != nullptr) { + _current --; + } + return *this; + } + Iterator operator--(int) { + Iterator it = *this; + if (_current != nullptr) { + _current --; + } + return it; + } + Iterator& operator++() { + if (_current != nullptr) { + _current ++; + } + return *this; + } + Iterator operator++(int) { + Iterator it = *this; + if (_current != nullptr) { + _current ++; + } + return it; + } + bool operator==(const Iterator& it) const { + return this->_current == it._current; + } + bool operator!=(const Iterator& it) const { + return !(*this == it); + } + T& operator*() { + if (_current == nullptr) { + throw std::runtime_error("Dereferencing nullptr"); + } + return *_current; + } + const T& operator*() const { + if (_current == nullptr) { + throw std::runtime_error("Dereferencing nullptr"); + } + return *_current; + } + }; Tvector() noexcept; Tvector(size_t size); Tvector(T* data, size_t size); From aeefd17b0a9a6e7d8cdece418551dca9a72ce9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 13:47:13 +0300 Subject: [PATCH 146/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D1=8B=20=D0=BD=D0=B0=20begin()=20=D0=B8=20end()=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib_vector/vector.h b/lib_vector/vector.h index 11b9b36d..12fda0df 100644 --- a/lib_vector/vector.h +++ b/lib_vector/vector.h @@ -96,7 +96,6 @@ if (_current == nullptr) { throw std::runtime_error("Dereferencing nullptr"); } - return *_current; } }; Tvector() noexcept; @@ -135,11 +134,11 @@ size_t get_capacity() const noexcept { return _capacity; } - inline T* begin() const noexcept { - return _data; + inline Iterator* begin() const noexcept { + return Iterator(_head); } - inline T* end() const noexcept { - return _data + _size; + inline Iterator* end() const noexcept { + return Iterator(nullptr); } bool operator==(const Tvector& vector) const; bool operator!=(const Tvector& vector) const; From 832e1c7af1aab0568ce84763e5e146209ec62215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 13:51:24 +0300 Subject: [PATCH 147/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib_vector/vector.h b/lib_vector/vector.h index 12fda0df..e0592a31 100644 --- a/lib_vector/vector.h +++ b/lib_vector/vector.h @@ -96,6 +96,7 @@ if (_current == nullptr) { throw std::runtime_error("Dereferencing nullptr"); } + return *_current; } }; Tvector() noexcept; @@ -134,11 +135,11 @@ size_t get_capacity() const noexcept { return _capacity; } - inline Iterator* begin() const noexcept { - return Iterator(_head); + inline Iterator begin() const noexcept { + return Iterator(_data); } - inline Iterator* end() const noexcept { - return Iterator(nullptr); + inline Iterator end() const noexcept { + return Iterator(_data + _size); } bool operator==(const Tvector& vector) const; bool operator!=(const Tvector& vector) const; From 76f5fbda763475cc2d76175e25845866ece843e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 16 Nov 2025 13:58:33 +0300 Subject: [PATCH 148/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 70 +++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index e003e79e..b374b308 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -119,28 +119,6 @@ TEST(TestVectorLib, get_capacity) { size_t expected_result = capacity; EXPECT_EQ(expected_result, actual_result); } -TEST(TestVectorLib, get_begin) { - // Arrange - int data[3] = { 1,2,3 }; - size_t size = 3; - Tvector vector(data, size); - // Act - int* actual_result = vector.begin(); - // Assert - int* expected_result = &(vector.get_data()[0]); - EXPECT_EQ(expected_result, actual_result); -} -TEST(TestVectorLib, get_end) { - // Arrange - int data[3] = { 1,2,3 }; - size_t size = 3; - Tvector vector(data, size); - // Act - int* actual_result = vector.end(); - // Assert - int* expected_result = &(vector.get_data()[0]) + size; - EXPECT_EQ(expected_result, actual_result); -} TEST(TestVectorLib, comparison_operator_true) { // Arrange int data[3] = { 1,2,3 }; @@ -517,3 +495,51 @@ TEST(TestVectorLib, find_count_of_suitable_elements) { size_t expected_result = 4; EXPECT_EQ(expected_result, actual_result); } +TEST(TestVectorLib, vector_iterator_read) { + // Arrange + Tvector vector; + vector.push_back(3); + vector.push_back(400); + vector.push_back(109); + vector.push_back(1); + vector.push_back(2); + // Act + auto it = vector.begin(); + it += 3; + // Assert + EXPECT_EQ(1, *it); +} +TEST(TestVectorLib, vector_iterator_write) { + // Arrange + Tvector vector; + vector.push_back(1); + vector.push_back(34); + vector.push_back(17); + vector.push_back(12); + vector.push_back(3); + // Act + auto it = vector.begin(); + it += 3; + // Assert + EXPECT_EQ(12, *it); + *it = 900; + EXPECT_EQ(900, *it); +} +TEST(TestVectorLib, vector_iterator_empty_list) { + // Arrange + Tvector vector; + // Act & Assert + EXPECT_EQ(vector.begin(), vector.end()); + + auto it = vector.begin(); + ++it; + EXPECT_EQ(it, vector.end()); + + auto end_it = vector.end(); + ++end_it; + EXPECT_EQ(end_it, vector.end()); + + auto it2 = vector.begin(); + it2 += 7; + EXPECT_EQ(it2, vector.end()); +} From 22f2cfdf5bd784cb01f2984f6dfc9ae0058b794d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 17 Nov 2025 08:36:27 +0300 Subject: [PATCH 149/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=203-=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D1=8F,=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=81=20=D1=86=D0=B8=D0=BA=D0=BB=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=B2=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5=20(=D0=BF=D0=BE?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=20=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D1=86=D0=B8=D0=BA=D0=BB=D0=B0)=20-=20=D0=B7=D0=B0=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=B7=20=D0=A2=D0=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index d6179570..862fe988 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -169,7 +169,7 @@ bool is_looped1(const List& list) { return true; } } - return false; + return false; } template bool is_looped2(List& list) { @@ -199,4 +199,29 @@ bool is_looped2(List& list) { } return has_cycle; } - \ No newline at end of file +template +Node* find_loop_start(List& list) { + if (list.is_empty()) { + return nullptr; + } + Node* slow = list.get_head(); + Node* fast = list.get_head(); + + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + + if (slow == fast) { + break; + } + } + if (fast == nullptr || fast->next == nullptr) { + return nullptr; + } + slow = list.get_head(); + while (slow != fast) { + slow = slow->next; + fast = fast->next; + } + return slow; +} From 006cc4253574f7a65cc8d827dde4866e52a99f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 17 Nov 2025 12:39:46 +0300 Subject: [PATCH 150/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=203-=D1=8E=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E?= =?UTF-8?q?=20=D0=BF=D0=BE=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B5=20=D1=81?= =?UTF-8?q?=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B0=D0=BC=D0=B8=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D0=B5=20(=D0=BF=D0=BE=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=20=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=B0=20=D1=86=D0=B8?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 862fe988..790e8644 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -204,24 +204,24 @@ Node* find_loop_start(List& list) { if (list.is_empty()) { return nullptr; } - Node* slow = list.get_head(); - Node* fast = list.get_head(); + Node* it2_slow = list.get_head(); + Node* it1_fast = list.get_head(); - while (fast != nullptr && fast->next != nullptr) { - slow = slow->next; - fast = fast->next->next; + while (it1_fast != nullptr && it1_fast->next != nullptr) { + it2_slow = it2_slow->next; + it1_fast = it1_fast->next->next; - if (slow == fast) { + if (it2_slow == it1_fast) { break; } } - if (fast == nullptr || fast->next == nullptr) { + if (it1_fast == nullptr || it1_fast->next == nullptr) { return nullptr; } - slow = list.get_head(); - while (slow != fast) { - slow = slow->next; - fast = fast->next; + it2_slow = list.get_head(); + while (it2_slow != it1_fast) { + it2_slow = it2_slow->next; + it1_fast = it1_fast->next; } - return slow; + return it2_slow; } From e0006bf0cc46a8a6aca9be88451f3c80590b7d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 17 Nov 2025 12:45:40 +0300 Subject: [PATCH 151/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5=D1=81=D1=82=D0=B2=D0=B0,=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BA=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D1=83=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.cpp | 1 + lib_dsu/dsu.h | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 lib_dsu/dsu.cpp create mode 100644 lib_dsu/dsu.h diff --git a/lib_dsu/dsu.cpp b/lib_dsu/dsu.cpp new file mode 100644 index 00000000..13c02283 --- /dev/null +++ b/lib_dsu/dsu.cpp @@ -0,0 +1 @@ +#include "dsu.h" diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h new file mode 100644 index 00000000..f8b70139 --- /dev/null +++ b/lib_dsu/dsu.h @@ -0,0 +1,12 @@ +#pragma once +class DSU { +private: + int* _parent; + size_t _size; +public: + DSU(size_t size); + DSU(const DSU& other); + ~DSU(); + void dsu_union(int x, int y); + int dsu_find(int x); +}; \ No newline at end of file From 504d429869ed82b5c7cb7dc808bf15d292b5df4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 09:58:55 +0300 Subject: [PATCH 152/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=81=D1=82=D0=B5=D0=BA,=20=D1=80=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack_on_list/stack_on_list.h | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lib_stack_on_list/stack_on_list.h diff --git a/lib_stack_on_list/stack_on_list.h b/lib_stack_on_list/stack_on_list.h new file mode 100644 index 00000000..be8ac20d --- /dev/null +++ b/lib_stack_on_list/stack_on_list.h @@ -0,0 +1,68 @@ +#pragma once +#include "../lib_list/List.h" +#include + +template +class StackOnList { +private: + List _list; +public: + StackOnList() = default; + StackOnList(const StackOnList& other): _list(other._list){} + ~StackOnList() = default; + StackOnList& operator=(const StackOnList& other); + bool operator ==(const StackOnList& other) const; + bool operator !=(const StackOnList& other) const; + void push(T value); + void pop(); + inline T top() const; + inline bool is_empty() const noexcept; + inline bool is_full() const noexcept; + void clear() noexcept; + size_t size() const noexcept { + return _list.get_size(); + } +}; +template +StackOnList& StackOnList::operator=(const StackOnList& other) { + if (this != &other) { + this->_list = other._list; + } + return *this; +} +template +bool StackOnList::operator==(const StackOnList& other) const { + return this->_list == other._list; +} +template +bool StackOnList::operator !=(const StackOnList& other) const { + return !(this->_list == other._list); +} +template +void StackOnList::push(T value) { + _list.push_front(value); +} +template +void StackOnList::pop() { + if (_list.is_empty()) { + throw std::logic_error("Can't pop from empty stack"); + } + _list.pop_front(); +} +template +inline T StackOnList::top() const { + if (_list.is_empty()) { + throw std::logic_error("Stack is empty, you can't get top element's index"); + } + return *(_list.begin()); +} +template +inline bool StackOnList::is_empty() const noexcept { + return _list.is_empty(); +} +template +void StackOnList::clear() noexcept { + while (!_list.is_empty()) { + _list.pop_front(); + } +} From 06f1591dcfe2de3949f2562db3d9861a0eb737c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 09:59:45 +0300 Subject: [PATCH 153/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack_on_list/stack_on_list.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_stack_on_list/stack_on_list.cpp diff --git a/lib_stack_on_list/stack_on_list.cpp b/lib_stack_on_list/stack_on_list.cpp new file mode 100644 index 00000000..4c67494b --- /dev/null +++ b/lib_stack_on_list/stack_on_list.cpp @@ -0,0 +1 @@ +#include "stack_on_list.h" From 5e6f6c195a2ea9494f56d286e4aca2ea84b6298a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:14:02 +0300 Subject: [PATCH 154/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack_on_list/stack_on_list.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib_stack_on_list/stack_on_list.h b/lib_stack_on_list/stack_on_list.h index be8ac20d..01473e4e 100644 --- a/lib_stack_on_list/stack_on_list.h +++ b/lib_stack_on_list/stack_on_list.h @@ -19,11 +19,13 @@ class StackOnList { inline bool is_empty() const noexcept; inline bool is_full() const noexcept; void clear() noexcept; - size_t size() const noexcept { - return _list.get_size(); - } + size_t size() const noexcept; }; template +size_t StackOnList::size() const noexcept { + return _list.get_size(); +} +template StackOnList& StackOnList::operator=(const StackOnList& other) { if (this != &other) { this->_list = other._list; From cb067509673dcde3aa2d1f91cf33455e589e1d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:18:40 +0300 Subject: [PATCH 155/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BD=D0=B5=D0=B3=D0=BE?= =?UTF-8?q?=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20(=D0=B4=D0=BB=D1=8F=20=D0=B1?= =?UTF-8?q?=D0=BE=D0=BB=D0=B5=D0=B5=20=D1=8D=D1=84=D1=84=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D0=B2=D0=BD=D0=BE=D0=B9=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B8=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib_list/List.h b/lib_list/List.h index d4a735d5..eed0d2cf 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -77,9 +77,27 @@ class List { List(); ~List(); List(const List& other_list); - size_t get_size() const noexcept; - Node* get_head() const noexcept; - Node* get_tail() const noexcept; + size_t get_size() const noexcept { + return _count_elements; + } + Node* get_head() const noexcept { + return _head; + } + Node* get_tail() const noexcept { + return _tail; + } + T& get_last_element() { + if (_tail == nullptr) { + throw std::runtime_error("List is empty"); + } + return _tail->value; + } + const T& get_last_element() const { + if (_tail == nullptr) { + throw std::runtime_error("List is empty"); + } + return _tail->value; + } bool operator==(const List& other) const; bool operator!=(const List& other) const; @@ -113,18 +131,6 @@ _count_elements(0) { } } template -size_t List::get_size() const noexcept { - return _count_elements; -} -template -Node* List::get_head() const noexcept { - return _head; -} -template -Node* List::get_tail() const noexcept { - return _tail; -} -template bool List::is_empty() const noexcept { return _head == nullptr && _tail == nullptr; } From 7d92299b8355dc135912b11173410a9a1871cd9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:21:55 +0300 Subject: [PATCH 156/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack_on_list/stack_on_list.h | 1 - 1 file changed, 1 deletion(-) diff --git a/lib_stack_on_list/stack_on_list.h b/lib_stack_on_list/stack_on_list.h index 01473e4e..472948ce 100644 --- a/lib_stack_on_list/stack_on_list.h +++ b/lib_stack_on_list/stack_on_list.h @@ -17,7 +17,6 @@ class StackOnList { void pop(); inline T top() const; inline bool is_empty() const noexcept; - inline bool is_full() const noexcept; void clear() noexcept; size_t size() const noexcept; }; From 843f4e4189e261382ec23241cf79dbb49a4b08a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:25:39 +0300 Subject: [PATCH 157/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=B8?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib_queue_on_list/queue_on_list.h diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h new file mode 100644 index 00000000..5f6f4439 --- /dev/null +++ b/lib_queue_on_list/queue_on_list.h @@ -0,0 +1,51 @@ +#pragma once +#include "../lib_list/List.h" +template +class QueueOnList { +private: + List _list; +public: + QueueOnList() = default; + QueueOnList(const QueueOnList& other): _list(other._list){} + ~QueueOnList() = default; + inline T head() const; + inline T tail() const; + size_t size() const noexcept; + bool is_empty() const noexcept; + void push(T value); + void pop(); + void clear() noexcept; +}; +template +T QueueOnList::head() const { + return *(_list.begin()); +} +template +T QueueOnList::tail() const { + return _list.get_last_element(); +} +template +size_t QueueOnList::size() const noexcept { + return _list.get_size(); +} +template +bool QueueOnList::is_empty() const noexcept { + return _list.is_empty(); +} +template +void QueueOnList::push(T value) { + _list.push_back(value); +} +template +void QueueOnList::pop() { + if (_list.is_empty()) { + throw std::logic_error("Can't pop from empty stack"); + } + _list.pop_front(); +} +template +void QueueOnList::clear() noexcept { + while (!_list.is_empty()) { + _list.pop_front(); + } +} From 655cb445f510a917a5758456763eb0ac14187dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:27:43 +0300 Subject: [PATCH 158/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_queue_on_list/queue_on_list.cpp diff --git a/lib_queue_on_list/queue_on_list.cpp b/lib_queue_on_list/queue_on_list.cpp new file mode 100644 index 00000000..69794cd2 --- /dev/null +++ b/lib_queue_on_list/queue_on_list.cpp @@ -0,0 +1 @@ +#include "queue_on_list.h" From fbbb5d7e81a285bbdde48445c014736fa249bd1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:31:27 +0300 Subject: [PATCH 159/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B0=20=D0=BF=D1=83=D1=81=D1=82=D0=BE=D1=82?= =?UTF-8?q?=D1=83=20=D0=B2=20get=5Fhead()=20=D0=B8=20get=5Ftail()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h index 5f6f4439..9e52c05a 100644 --- a/lib_queue_on_list/queue_on_list.h +++ b/lib_queue_on_list/queue_on_list.h @@ -18,10 +18,16 @@ class QueueOnList { }; template T QueueOnList::head() const { + if (_list.is_empty()) { + throw std::runtime_error("Can't get head: queue is empty"); + } return *(_list.begin()); } template T QueueOnList::tail() const { + if (this->is_empty()) { + throw std::runtime_error("Can't get tail: queue is empty"); + } return _list.get_last_element(); } template From 7a962a6aca25c409b11fca371289e48eaa37369e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 10:32:27 +0300 Subject: [PATCH 160/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h index 9e52c05a..e55d35bc 100644 --- a/lib_queue_on_list/queue_on_list.h +++ b/lib_queue_on_list/queue_on_list.h @@ -45,7 +45,7 @@ void QueueOnList::push(T value) { template void QueueOnList::pop() { if (_list.is_empty()) { - throw std::logic_error("Can't pop from empty stack"); + throw std::logic_error("Can't pop from empty queue"); } _list.pop_front(); } From 1a52afa67d11960ab3f1ac6185d298ede3fdefa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 19:42:27 +0300 Subject: [PATCH 161/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h index e55d35bc..c02154c6 100644 --- a/lib_queue_on_list/queue_on_list.h +++ b/lib_queue_on_list/queue_on_list.h @@ -4,8 +4,9 @@ template class QueueOnList { private: List _list; + size_t _size; public: - QueueOnList() = default; + QueueOnList(size_t size = 0) : _size(size) {}; QueueOnList(const QueueOnList& other): _list(other._list){} ~QueueOnList() = default; inline T head() const; From 61d8226cc51fc4858357f6923ce5d8646ba2b37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 20:10:49 +0300 Subject: [PATCH 162/275] =?UTF-8?q?=D0=9E=D1=87=D0=B5=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D1=8C=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0:=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BE=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BC=D0=BE=D0=B6=D0=B5=D1=82=20=D0=B1=D1=8B=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BE=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D0=B2=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=20(=D0=BF=D0=BE=20=D0=B6=D0=B5=D0=BB=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8E=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8F)=20=D0=B8=D0=BB=D0=B8=20=D1=81=20?= =?UTF-8?q?=D0=B1=D0=BE=D0=BB=D0=B5=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=87?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5=20=D0=BD=D0=B5=D0=BE=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h index c02154c6..ca120fea 100644 --- a/lib_queue_on_list/queue_on_list.h +++ b/lib_queue_on_list/queue_on_list.h @@ -4,10 +4,11 @@ template class QueueOnList { private: List _list; - size_t _size; + size_t _max_size; + bool _has_max_size = false; public: - QueueOnList(size_t size = 0) : _size(size) {}; - QueueOnList(const QueueOnList& other): _list(other._list){} + QueueOnList(size_t size = 0); + QueueOnList(const QueueOnList& other); ~QueueOnList() = default; inline T head() const; inline T tail() const; @@ -15,9 +16,23 @@ class QueueOnList { bool is_empty() const noexcept; void push(T value); void pop(); + inline bool is_full() const noexcept; void clear() noexcept; }; template +QueueOnList::QueueOnList(size_t size) { + _max_size = size; + if (_max_size != 0) { + _has_max_size = true; + } +} +template +QueueOnList::QueueOnList(const QueueOnList& other) { + _list = other._list; + _max_size = other._max_size; + _has_max_size = other._has_max_size; +} +template T QueueOnList::head() const { if (_list.is_empty()) { throw std::runtime_error("Can't get head: queue is empty"); @@ -41,6 +56,9 @@ bool QueueOnList::is_empty() const noexcept { } template void QueueOnList::push(T value) { + if (is_full()) { + throw std::logic_error("Can't push element: queue is full"); + } _list.push_back(value); } template @@ -51,6 +69,13 @@ void QueueOnList::pop() { _list.pop_front(); } template +bool QueueOnList::is_full() const noexcept { + if (_has_max_size == false) { + return false; + } + return _max_size == _list.get_size(); +} +template void QueueOnList::clear() noexcept { while (!_list.is_empty()) { _list.pop_front(); From b2260bc18fae3a8516e12d1637dd0c772bf70542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 20:16:38 +0300 Subject: [PATCH 163/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h index ca120fea..27263906 100644 --- a/lib_queue_on_list/queue_on_list.h +++ b/lib_queue_on_list/queue_on_list.h @@ -13,6 +13,8 @@ class QueueOnList { inline T head() const; inline T tail() const; size_t size() const noexcept; + size_t max_size() const noexcept; + bool has_max_size() const noexcept; bool is_empty() const noexcept; void push(T value); void pop(); @@ -51,6 +53,14 @@ size_t QueueOnList::size() const noexcept { return _list.get_size(); } template +size_t QueueOnList::max_size() const noexcept { + return _max_size; +} +template +bool QueueOnList::has_max_size() const noexcept { + return _has_max_size; +} +template bool QueueOnList::is_empty() const noexcept { return _list.is_empty(); } From fc6a87c91d8bd1d885e34f971e5c7dbbe7457452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 20:36:02 +0300 Subject: [PATCH 164/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue_on_list.cpp | 183 +++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 tests/test_queue_on_list.cpp diff --git a/tests/test_queue_on_list.cpp b/tests/test_queue_on_list.cpp new file mode 100644 index 00000000..5657e2fe --- /dev/null +++ b/tests/test_queue_on_list.cpp @@ -0,0 +1,183 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_queue_on_list/queue_on_list.h" + +TEST(TestQueueOnListLib, queue_constructor_with_size) { + // Arrange & Act + size_t max_size = 10; + QueueOnList queue(max_size); + // Assert + EXPECT_EQ(queue.max_size(), max_size); + EXPECT_EQ(queue.size(), 0); + EXPECT_TRUE(queue.is_empty()); +} +TEST(TestQueueOnListLib, queue_constructor_with_size_default) { + // Arrange & Act + QueueOnList queue; + // Assert + EXPECT_EQ(queue.size(), 0); + EXPECT_FALSE(queue.has_max_size()); + EXPECT_TRUE(queue.is_empty()); +} +TEST(TestQueueOnListLib, queue_copy_constructor) { + // Arrange + size_t max_size = 5; + QueueOnList queue1(max_size); + queue1.push(5); + queue1.push(6); + queue1.push(7); + // Act + QueueOnList queue2(queue1); + // Assert + EXPECT_EQ(queue1.max_size(), queue2.max_size()); + EXPECT_EQ(queue1.size(), queue2.size()); + while (!queue1.is_empty()) { + EXPECT_EQ(queue1.head(), queue2.head()); + queue1.pop(); + queue2.pop(); + } +} +TEST(TestQueueOnListLib, queue_is_empty_true) { + // Arrange & Act + QueueOnList queue; + // Assert + EXPECT_TRUE(queue.is_empty()); +} +TEST(TestQueueOnListLib, queue_is_empty_false) { + // Arrange + size_t max_size = 3; + QueueOnList queue(max_size); + // Act + queue.push(5); + // Assert + EXPECT_FALSE(queue.is_empty()); +} +TEST(TestQueueOnListLib, queue_is_full_true) { + // Arrange + size_t max_size = 3; + QueueOnList queue(max_size); + // Act + queue.push(30); + queue.push(22); + queue.push(12); + // Assert + EXPECT_TRUE(queue.is_full()); +} +TEST(TestQueueOnListLib, queue_is_full_false) { + // Arrange + size_t max_size = 5; + QueueOnList queue(max_size); + // Act + queue.push(1); + queue.push(3); + // Assert + EXPECT_FALSE(queue.is_full()); +} +TEST(TestQueueOnListLib, queue_get_head) { + // Arrange + size_t max_size = 6; + QueueOnList queue(max_size); + // Act + queue.push(10); + queue.push(23); + queue.push(30); + // Assert + EXPECT_EQ(queue.head(), 10); +} +TEST(TestQueueOnListLib, queue_get_tail) { + // Arrange + size_t max_size = 6; + QueueOnList queue(max_size); + // Act + queue.push(70); + queue.push(5); + queue.push(50); + // Assert + EXPECT_EQ(queue.tail(), 50); +} +TEST(TestQueueOnListLib, queue_empty_get_head_exception) { + // Arrange & Act + QueueOnList queue; + // Assert + ASSERT_THROW(queue.head(), std::runtime_error); +} +TEST(TestQueueOnListLib, queue_empty_get_tail_exception) { + // Arrange & Act + size_t max_size = 10; + QueueOnList queue(max_size); + // Assert + ASSERT_THROW(queue.tail(), std::runtime_error); +} +TEST(TestQueueOnListLib, queue_circular_behavior) { + // Arrange + QueueOnList queue(3); + // Act + queue.push(1); + queue.push(2); + queue.push(3); + queue.pop(); + queue.push(4); + // Assert + EXPECT_EQ(queue.head(), 2); + EXPECT_EQ(queue.tail(), 4); +} +TEST(TestQueueOnListLib, queue_push) { + // Arrange + QueueOnList queue(5); + // Act + queue.push(22); + queue.push(55); + queue.push(77); + // Assert + EXPECT_EQ(queue.head(), 22); + EXPECT_EQ(queue.tail(), 77); + EXPECT_EQ(queue.size(), 3); +} +TEST(TestQueueOnListLib, queue_pop) { + // Arrange + QueueOnList queue(8); + // Act + queue.push(22); + queue.push(34); + queue.push(27); + queue.pop(); + queue.pop(); + // Assert + EXPECT_EQ(queue.head(), queue.tail()); + EXPECT_EQ(queue.head(), 27); + EXPECT_EQ(queue.size(), 1); +} +TEST(TestQueueOnListLib, queue_push_exception) { + // Arrange + QueueOnList queue(3); + // Act + queue.push(20); + queue.push(10); + queue.push(30); + // Assert + ASSERT_THROW(queue.push(9), std::logic_error); +} +TEST(TestQueueOnListLib, queue_pop_exception) { + // Arrange & Act + QueueOnList queue(10); + // Assert + ASSERT_THROW(queue.pop(), std::logic_error); +} +TEST(TestQueueOnListLib, queue_clear) { + // Arrange + QueueOnList queue(10); + queue.push(2); + queue.push(76); + queue.push(34); + queue.push(25); + queue.push(1); + queue.push(3); + queue.push(4); + queue.push(5); + // Act + queue.clear(); + // Assert + EXPECT_TRUE(queue.is_empty()); + EXPECT_EQ(queue.max_size(), 10); +} From 23448029caa2130fbcca753964926a376b4dc838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 20:38:06 +0300 Subject: [PATCH 165/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue_on_list.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/test_queue_on_list.cpp b/tests/test_queue_on_list.cpp index 5657e2fe..3f6fc375 100644 --- a/tests/test_queue_on_list.cpp +++ b/tests/test_queue_on_list.cpp @@ -109,19 +109,6 @@ TEST(TestQueueOnListLib, queue_empty_get_tail_exception) { // Assert ASSERT_THROW(queue.tail(), std::runtime_error); } -TEST(TestQueueOnListLib, queue_circular_behavior) { - // Arrange - QueueOnList queue(3); - // Act - queue.push(1); - queue.push(2); - queue.push(3); - queue.pop(); - queue.push(4); - // Assert - EXPECT_EQ(queue.head(), 2); - EXPECT_EQ(queue.tail(), 4); -} TEST(TestQueueOnListLib, queue_push) { // Arrange QueueOnList queue(5); From 760cc26af4a60dc4afb2568dfd87d358f3b3ce75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 20:59:46 +0300 Subject: [PATCH 166/275] =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BA=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5=20=D1=82=D0=B0=D0=BA?= =?UTF-8?q?=D0=B6=D0=B5=20=D0=BC=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D1=86=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD,=20=D0=BA=D0=B0=D0=BA=20=D0=B8?= =?UTF-8?q?=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8C=20-=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=81=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=BE=D0=BC=20=D0=B8=20=D0=B1?= =?UTF-8?q?=D0=B5=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack_on_list/stack_on_list.h | 46 ++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/lib_stack_on_list/stack_on_list.h b/lib_stack_on_list/stack_on_list.h index 472948ce..c945c182 100644 --- a/lib_stack_on_list/stack_on_list.h +++ b/lib_stack_on_list/stack_on_list.h @@ -6,9 +6,11 @@ template class StackOnList { private: List _list; + size_t _max_size; + bool _has_max_size = false; public: - StackOnList() = default; - StackOnList(const StackOnList& other): _list(other._list){} + StackOnList(size_t size = 0); + StackOnList(const StackOnList& other); ~StackOnList() = default; StackOnList& operator=(const StackOnList& other); bool operator ==(const StackOnList& other) const; @@ -17,17 +19,43 @@ class StackOnList { void pop(); inline T top() const; inline bool is_empty() const noexcept; + inline bool is_full() const noexcept; void clear() noexcept; size_t size() const noexcept; + size_t max_size() const noexcept; + bool has_max_size() const noexcept; }; template +StackOnList::StackOnList(size_t size) { + _max_size = size; + if (_max_size != 0) { + _has_max_size = true; + } +} +template +StackOnList::StackOnList(const StackOnList& other) { + _list = other._list; + _max_size = other._max_size; + _has_max_size = other._has_max_size; +} +template size_t StackOnList::size() const noexcept { return _list.get_size(); } template +size_t StackOnList::max_size() const noexcept { + return _max_size; +} +template +bool StackOnList::has_max_size() const noexcept { + return _has_max_size; +} +template StackOnList& StackOnList::operator=(const StackOnList& other) { if (this != &other) { - this->_list = other._list; + _list = other._list; + _max_size = other._max_size; + _has_max_size = other._has_max_size; } return *this; } @@ -41,6 +69,9 @@ bool StackOnList::operator !=(const StackOnList& other) const { } template void StackOnList::push(T value) { + if (is_full()) { + throw std::logic_error("Can't push the element: stack is full"); + } _list.push_front(value); } template @@ -53,7 +84,7 @@ void StackOnList::pop() { template inline T StackOnList::top() const { if (_list.is_empty()) { - throw std::logic_error("Stack is empty, you can't get top element's index"); + throw std::logic_error("Stack is empty, you can't get top element"); } return *(_list.begin()); } @@ -62,6 +93,13 @@ inline bool StackOnList::is_empty() const noexcept { return _list.is_empty(); } template +bool StackOnList::is_full() const noexcept { + if (_has_max_size == false) { + return false; + } + return _max_size == _list.get_size(); +} +template void StackOnList::clear() noexcept { while (!_list.is_empty()) { _list.pop_front(); From 1d4a18cfe0e708b2924f76d5b6aa8c1375909701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 21:02:07 +0300 Subject: [PATCH 167/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/queue_on_list.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib_queue_on_list/queue_on_list.h b/lib_queue_on_list/queue_on_list.h index 27263906..704b8e33 100644 --- a/lib_queue_on_list/queue_on_list.h +++ b/lib_queue_on_list/queue_on_list.h @@ -10,6 +10,9 @@ class QueueOnList { QueueOnList(size_t size = 0); QueueOnList(const QueueOnList& other); ~QueueOnList() = default; + QueueOnList& operator=(const QueueOnList& other); + bool operator ==(const QueueOnList& other) const; + bool operator !=(const QueueOnList& other) const; inline T head() const; inline T tail() const; size_t size() const noexcept; @@ -91,3 +94,20 @@ void QueueOnList::clear() noexcept { _list.pop_front(); } } +template +QueueOnList& QueueOnList::operator=(const QueueOnList& other) { + if (this != &other) { + _list = other._list; + _max_size = other._max_size; + _has_max_size = other._has_max_size; + } + return *this; +} +template +bool QueueOnList::operator ==(const QueueOnList& other) const { + return this->_list == other._list; +} +template +bool QueueOnList::operator !=(const QueueOnList& other) const { + return !(this->_list == other._list); +} From 15c86c22ac0564ff9be27b16981bf7f89b5f8154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 21:08:49 +0300 Subject: [PATCH 168/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=20=3D=3D,=20!=3D=20=D0=B8=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue_on_list.cpp | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/test_queue_on_list.cpp b/tests/test_queue_on_list.cpp index 3f6fc375..7c7d0efb 100644 --- a/tests/test_queue_on_list.cpp +++ b/tests/test_queue_on_list.cpp @@ -168,3 +168,65 @@ TEST(TestQueueOnListLib, queue_clear) { EXPECT_TRUE(queue.is_empty()); EXPECT_EQ(queue.max_size(), 10); } +TEST(TestQueueOnListLib, queue_operator_assign) { + // Arrange + QueueOnList queue1(10); + queue1.push(2); + queue1.push(3); + queue1.push(4); + QueueOnList queue2; + // Act + queue1 = queue2; + // Assert + EXPECT_TRUE(queue1 == queue2); +} +TEST(TestQueueOnListLib, queue_operator_equal_true) { + // Arrange & Act + QueueOnList queue1; + queue1.push(2); + queue1.push(3); + queue1.push(4); + QueueOnList queue2; + queue2.push(2); + queue2.push(3); + queue2.push(4); + // Assert + EXPECT_TRUE(queue1 == queue2); +} +TEST(TestQueueOnListLib, queue_operator_equal_false) { + // Arrange & Act + QueueOnList queue1; + queue1.push(2); + queue1.push(3); + queue1.push(4); + QueueOnList queue2; + queue2.push(2); + queue2.push(4); + queue2.push(4); + // Assert + EXPECT_FALSE(queue1 == queue2); +} +TEST(TestQueueOnListLib, queue_operator_not_equal_true) { + // Arrange & Act + QueueOnList queue1; + queue1.push(2); + queue1.push(3); + queue1.push(4); + QueueOnList queue2; + queue2.push(10); + // Assert + EXPECT_TRUE(queue1 != queue2); +} +TEST(TestQueueOnListLib, queue_operator_not_equal_false) { + // Arrange & Act + QueueOnList queue1; + queue1.push(2); + queue1.push(2); + queue1.push(2); + QueueOnList queue2; + queue2.push(2); + queue2.push(2); + queue2.push(2); + // Assert + EXPECT_FALSE(queue1 != queue2); +} From 4120ac7b1b0da23f5d029512562bf2512583cc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 21:12:37 +0300 Subject: [PATCH 169/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue_on_list.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/test_queue_on_list.cpp b/tests/test_queue_on_list.cpp index 7c7d0efb..8756b2eb 100644 --- a/tests/test_queue_on_list.cpp +++ b/tests/test_queue_on_list.cpp @@ -174,10 +174,16 @@ TEST(TestQueueOnListLib, queue_operator_assign) { queue1.push(2); queue1.push(3); queue1.push(4); - QueueOnList queue2; + QueueOnList queue2(5); + queue2.push(10); + queue2.push(20); // Act queue1 = queue2; - // Assert + // Assert + EXPECT_EQ(queue1.max_size(), 5); + EXPECT_EQ(queue1.size(), 2); + EXPECT_EQ(queue1.head(), 10); + EXPECT_EQ(queue1.tail(), 20); EXPECT_TRUE(queue1 == queue2); } TEST(TestQueueOnListLib, queue_operator_equal_true) { From 11fb8b183ec63a41f54a9d3c1ef2ab5429b29ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 21:52:45 +0300 Subject: [PATCH 170/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_queue_on_list.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_queue_on_list.cpp b/tests/test_queue_on_list.cpp index 8756b2eb..34d10bbc 100644 --- a/tests/test_queue_on_list.cpp +++ b/tests/test_queue_on_list.cpp @@ -181,9 +181,7 @@ TEST(TestQueueOnListLib, queue_operator_assign) { queue1 = queue2; // Assert EXPECT_EQ(queue1.max_size(), 5); - EXPECT_EQ(queue1.size(), 2); - EXPECT_EQ(queue1.head(), 10); - EXPECT_EQ(queue1.tail(), 20); + EXPECT_EQ(queue1.size(), queue2.size()); EXPECT_TRUE(queue1 == queue2); } TEST(TestQueueOnListLib, queue_operator_equal_true) { From eb1204e913afd77d09f6cbc1f1064d27f898b6ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 18 Nov 2025 21:57:03 +0300 Subject: [PATCH 171/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=81=D1=82=D0=B5=D0=BA=D0=B0=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_stack_on_list.cpp | 174 +++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 tests/test_stack_on_list.cpp diff --git a/tests/test_stack_on_list.cpp b/tests/test_stack_on_list.cpp new file mode 100644 index 00000000..d7c4ed4d --- /dev/null +++ b/tests/test_stack_on_list.cpp @@ -0,0 +1,174 @@ +// Copyright 2025 Julia Zabytina +#include +#include "../lib_stack_on_list/stack_on_list.h" + +TEST(TestStackOnListLib, stack_constructor_with_size_default) { + // Arrange & Act + StackOnList stack; + // Assert + EXPECT_EQ(stack.size(), 0); + EXPECT_FALSE(stack.has_max_size()); + EXPECT_TRUE(stack.is_empty()); +} +TEST(TestStackOnListLib, stack_constructor_with_size) { + // Arrange & Act + size_t max_size = 10; + StackOnList stack(max_size); + // Assert + EXPECT_EQ(stack.max_size(), max_size); + EXPECT_EQ(stack.size(), 0); + EXPECT_TRUE(stack.is_empty()); +} +TEST(TestStackOnListLib, stack_copy_constructor) { + // Arrange + size_t max_size = 5; + StackOnList stack1(max_size); + stack1.push(5); + stack1.push(6); + stack1.push(7); + // Act + StackOnList stack2(stack1); + // Assert + EXPECT_EQ(stack1.max_size(), stack2.max_size()); + EXPECT_EQ(stack1.size(), stack2.size()); + while (!stack1.is_empty()) { + EXPECT_EQ(stack1.top(), stack2.top()); + stack1.pop(); + stack2.pop(); + } +} +TEST(TestStackOnListLib, stack_is_empty_true) { + // Arrange & Act + StackOnList stack; + // Assert + EXPECT_TRUE(stack.is_empty()); +} +TEST(TestStackOnListLib, stack_is_empty_false) { + // Arrange + size_t max_size = 5; + StackOnList stack(max_size); + // Act + stack.push(10); + // Assert + EXPECT_FALSE(stack.is_empty()); +} +TEST(TestStackOnListLib, stack_is_full_true) { + // Arrange + size_t max_size = 3; + StackOnList stack(max_size); + // Act + stack.push(4); + stack.push(3); + stack.push(7); + // Assert + EXPECT_TRUE(stack.is_full()); +} +TEST(TestStackOnListLib, stack_is_full_false) { + // Arrange + size_t max_size = 7; + StackOnList stack(max_size); + // Act + stack.push(1); + stack.push(9); + // Assert + EXPECT_FALSE(stack.is_full()); +} +TEST(TestStackOnListLib, stack_push_element) { + // Arrange + int element = 55; + StackOnList stack; + // Act + stack.push(element); + // Assert + EXPECT_EQ(stack.top(), element); +} +TEST(TestStackOnListLib, stack_push_element_with_exception) { + // Arrange + size_t max_size = 1; + int element = 100; + StackOnList stack(max_size); + // Act + stack.push(element); + // Assert + EXPECT_THROW(stack.push(200), std::logic_error); +} +TEST(TestStackOnListLib, stack_pop_element_with_exception) { + // Arrange & Act + StackOnList stack; + // Assert + EXPECT_THROW(stack.pop(), std::logic_error); +} +TEST(TestStackOnListLib, stack_pop_element) { + // Arrange + int element1 = 100; + int element2 = 200; + int element3 = 300; + StackOnList stack; + // Act + stack.push(element1); + stack.push(element2); + stack.push(element3); + stack.pop(); + // Assert + EXPECT_EQ(stack.size(), 2); + EXPECT_EQ(stack.top(), element2); +} +TEST(TestStackOnListLib, stack_clear) { + // Arrange + int element = 300; + StackOnList stack; + // Act + for (size_t i = 0; i < 15; ++i) { + stack.push(element); + } + stack.clear(); + // Assert + EXPECT_TRUE(stack.is_empty()); +} +TEST(TestStackOnListLib, stack_operator_equall_expect_true) { + // Arrange & Act + StackOnList stack; + StackOnList stack_other; + // Assert + EXPECT_TRUE(stack == stack_other); +} +TEST(TestStackOnListLib, stack_operator_equall_expect_false) { + // Arrange & Act + StackOnList stack; + stack.push(10); + StackOnList stack_other; + stack_other.push(20); + stack_other.push(25); + // Assert + EXPECT_FALSE(stack == stack_other); +} +TEST(TestStackOnListLib, stack_operator_assign) { + //Arrange + StackOnList stack; + StackOnList stack_other; + //Act + stack_other.push(100); + stack_other.push(200); + stack = stack_other; + //Assert + EXPECT_TRUE(stack == stack_other); +} +TEST(TestStackOnListLib, stack_operator_not_equal_expect_true) { + // Arrange & Act + StackOnList stack; + stack.push(100); + StackOnList stack_other; + stack_other.push(20); + stack_other.push(35); + // Assert + EXPECT_TRUE(stack != stack_other); +} +TEST(TestStackOnListLib, stack_operator_not_equal_expect_false) { + // Arrange & Act + StackOnList stack; + stack.push(20); + StackOnList stack_other; + stack_other.push(20); + // Assert + EXPECT_FALSE(stack != stack_other); +} From b32892c27996e824dda0db0e2dc76c5de74eb83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 10:45:48 +0300 Subject: [PATCH 172/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 46f6dd74..5535253f 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -217,3 +217,39 @@ TEST(TestAlgorithmLib, test_find_loop_in_list_2_with_empty_list) { EXPECT_FALSE(is_looped2(list)); } +TEST(TestAlgorithmLib, test_find_loop_start_in_list) { + // Arrange & Act + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(50); + + list.push_back(70); + list.get_tail()->next = list.get_head(); + // Assert + EXPECT_EQ(find_loop_start(list), list.get_head()); + list.get_tail()->next = nullptr; +} +TEST(TestAlgorithmLib, test_find_loop_start_no_loop) { + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + EXPECT_EQ(find_loop_start(list), nullptr); +} +TEST(TestAlgorithmLib, test_find_loop_start_in_middle) { + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + + list.get_tail()->next = list.get_head()->next->next; + + EXPECT_EQ(find_loop_start(list), list.get_head()->next->next); + + list.get_tail()->next = nullptr; +} From 52db1225a7841f68341e6b5082f8c899a44abee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 11:17:40 +0300 Subject: [PATCH 173/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20DSU?= =?UTF-8?q?=20=D0=B8=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h index f8b70139..1ba84505 100644 --- a/lib_dsu/dsu.h +++ b/lib_dsu/dsu.h @@ -2,6 +2,7 @@ class DSU { private: int* _parent; + int* _rank; size_t _size; public: DSU(size_t size); @@ -9,4 +10,28 @@ class DSU { ~DSU(); void dsu_union(int x, int y); int dsu_find(int x); -}; \ No newline at end of file +}; +DSU::DSU(size_t size) { + _size = size; + _parent = new int[_size]; + _rank = new int[_size]; + for (size_t i = 0; i < _size; i++) { + _parent[i] = i; + _rank[i] = 0; + } +} +DSU::DSU(const DSU& other) { + _size = other._size; + _parent = new int[_size]; + _rank = new int[_size]; + for (size_t i = 0; i < _size; i++) { + this->_parent[i] = other._parent[i]; + this->_rank[i] = other._rank[i]; + } +} +DSU::~DSU() { + delete[] _parent; + delete[] _rank; + _parent = nullptr; + _rank = nullptr; +} From 69e75f7b8c22607d27115298ce135b8dd433abd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 11:24:18 +0300 Subject: [PATCH 174/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B3=D0=B5=D1=82=D1=82=D0=B5=D1=80=D1=8B?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h index 1ba84505..a418a041 100644 --- a/lib_dsu/dsu.h +++ b/lib_dsu/dsu.h @@ -8,6 +8,9 @@ class DSU { DSU(size_t size); DSU(const DSU& other); ~DSU(); + size_t size() const noexcept; + int* parent() const noexcept; + int* rank() const noexcept; void dsu_union(int x, int y); int dsu_find(int x); }; @@ -35,3 +38,12 @@ DSU::~DSU() { _parent = nullptr; _rank = nullptr; } +size_t DSU::size() const noexcept { + return _size; +} +int* DSU::parent() const noexcept { + return _parent; +} +int* DSU::rank() const noexcept { + return _rank; +} From 8a33184d812250756bb209e4948fa96c11f68ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 11:36:40 +0300 Subject: [PATCH 175/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dsu.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_dsu.cpp diff --git a/tests/test_dsu.cpp b/tests/test_dsu.cpp new file mode 100644 index 00000000..522f3849 --- /dev/null +++ b/tests/test_dsu.cpp @@ -0,0 +1,28 @@ +// Copyright 2025 Julia Zabytina +#include +#include "../lib_dsu/dsu.h" + +TEST(TestDSULib, dsu_constructor_with_size) { + // Arrange & Act + size_t size = 6; + DSU dsu(size); + // Assert + EXPECT_EQ(dsu.size(), size); + for (size_t i = 0; i < dsu.size(); i++) { + EXPECT_EQ(dsu.parent()[i], i); + EXPECT_EQ(dsu.rank()[i], 0); + } +} +TEST(TestDSULib, dsu_copy_constructor) { + // Arrange + size_t size = 10; + DSU dsu1(size); + // Act + DSU dsu2(dsu1); + // Assert + EXPECT_EQ(dsu1.size(), dsu2.size()); + for (size_t i = 0; i < dsu2.size(); i++) { + EXPECT_EQ(dsu1.parent()[i], dsu2.parent()[i]); + EXPECT_EQ(dsu1.rank()[i], dsu2.rank()[i]); + } +} From 1ad6e93cf6d6bde72d6ea49e2534c2a35ecfb978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 14:03:57 +0300 Subject: [PATCH 176/275] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20DSU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h index a418a041..17f6be11 100644 --- a/lib_dsu/dsu.h +++ b/lib_dsu/dsu.h @@ -1,4 +1,5 @@ #pragma once +#include class DSU { private: int* _parent; @@ -13,6 +14,8 @@ class DSU { int* rank() const noexcept; void dsu_union(int x, int y); int dsu_find(int x); + int dsu_find_recursive(int x); + DSU& operator=(const DSU& other); }; DSU::DSU(size_t size) { _size = size; @@ -47,3 +50,44 @@ int* DSU::parent() const noexcept { int* DSU::rank() const noexcept { return _rank; } +void DSU::dsu_union(int x, int y) { + if (x == y) { + return; + } + if (x >= _size || y >= _size || x < 0 || y < 0) { + throw std::logic_error("Uncorrect arguments"); + } + int parent_x = dsu_find_recursive(x); + int parent_y = dsu_find_recursive(y); + if (_rank[parent_x] < _rank[parent_y]) { + _parent[parent_x] = parent_y; + } else if (_rank[parent_x] > _rank[parent_y]) { + _parent[parent_y] = parent_x; + } else { + _parent[parent_y] = parent_x; + _rank[parent_x]++; + } +} +int DSU::dsu_find_recursive(int x) { + if (x >= _size || x < 0) { + throw std::logic_error("Uncorrect argument"); + } + if (_parent[x] == x) { + return x; + } + return _parent[x] = dsu_find_recursive(_parent[x]); +} +DSU& DSU::operator=(const DSU& other) { + if (this != &other) { + delete[] _parent; + delete[] _rank; + _size = other._size; + _parent = new int[_size]; + _rank = new int[_size]; + for (size_t i = 0; i < _size; i++) { + this->_parent[i] = other._parent[i]; + this->_rank[i] = other._rank[i]; + } + } + return *this; +} From c9b3304de51e40705e86cafe14bbf617992bf058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 18:23:42 +0300 Subject: [PATCH 177/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.h | 1 - 1 file changed, 1 deletion(-) diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h index 17f6be11..b74c6066 100644 --- a/lib_dsu/dsu.h +++ b/lib_dsu/dsu.h @@ -13,7 +13,6 @@ class DSU { int* parent() const noexcept; int* rank() const noexcept; void dsu_union(int x, int y); - int dsu_find(int x); int dsu_find_recursive(int x); DSU& operator=(const DSU& other); }; From f58ffafeee65daa95c2cff641f6e6127700104e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 18:26:40 +0300 Subject: [PATCH 178/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20DSU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dsu.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test_dsu.cpp b/tests/test_dsu.cpp index 522f3849..99633db0 100644 --- a/tests/test_dsu.cpp +++ b/tests/test_dsu.cpp @@ -26,3 +26,51 @@ TEST(TestDSULib, dsu_copy_constructor) { EXPECT_EQ(dsu1.rank()[i], dsu2.rank()[i]); } } +TEST(TestDSULib, dsu_union) { + // Arrange & Act + size_t size = 4; + DSU dsu(size); + dsu.dsu_union(1, 2); + dsu.dsu_union(1, 3); + // Assert + EXPECT_EQ(dsu.size(), size); + EXPECT_EQ(dsu.rank()[1], 1); + EXPECT_EQ(dsu.parent()[2], 1); + EXPECT_EQ(dsu.parent()[3], 1); +} +TEST(TestDSULib, dsu_union_with_exception) { + // Arrange & Act + size_t size = 5; + DSU dsu(size); + // Assert + ASSERT_THROW(dsu.dsu_union(-1, 2), std::logic_error); +} +TEST(TestDSULib, dsu_find_recursive) { + // Arrange & Act + size_t size = 7; + DSU dsu(size); + dsu.dsu_union(5, 4); + dsu.dsu_union(5, 6); + // Assert + EXPECT_EQ(dsu.dsu_find_recursive(6), 5); + EXPECT_EQ(dsu.dsu_find_recursive(4), 5); +} +TEST(TestDSULib, dsu_find_recursive_with_exception) { + // Arrange & Act + size_t size = 3; + DSU dsu(size); + // Assert + ASSERT_THROW(dsu.dsu_find_recursive(5), std::logic_error); +} +TEST(TestDSULib, dsu_operator_assign) { + // Arrange & Act + size_t size = 3; + DSU dsu1(size); + DSU dsu2 = dsu1; + // Assert + EXPECT_EQ(dsu1.size(), dsu2.size()); + for (size_t i = 0; i < size; i++) { + EXPECT_EQ(dsu1.parent()[i], dsu2.parent()[i]); + EXPECT_EQ(dsu1.rank()[i], dsu2.rank()[i]); + } +} From 563e4a0e0bb42b3b8958d3663588834bbe82316a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 18:34:18 +0300 Subject: [PATCH 179/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h index b74c6066..eac2f92a 100644 --- a/lib_dsu/dsu.h +++ b/lib_dsu/dsu.h @@ -58,6 +58,9 @@ void DSU::dsu_union(int x, int y) { } int parent_x = dsu_find_recursive(x); int parent_y = dsu_find_recursive(y); + if (parent_x == parent_y) { + return; + } if (_rank[parent_x] < _rank[parent_y]) { _parent[parent_x] = parent_y; } else if (_rank[parent_x] > _rank[parent_y]) { From f3a472173303e26e40122488fb028b8f0f62ef3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 23 Nov 2025 18:38:08 +0300 Subject: [PATCH 180/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_dsu.cpp | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/tests/test_dsu.cpp b/tests/test_dsu.cpp index 99633db0..d14114d6 100644 --- a/tests/test_dsu.cpp +++ b/tests/test_dsu.cpp @@ -26,17 +26,25 @@ TEST(TestDSULib, dsu_copy_constructor) { EXPECT_EQ(dsu1.rank()[i], dsu2.rank()[i]); } } -TEST(TestDSULib, dsu_union) { - // Arrange & Act - size_t size = 4; +TEST(TestDSULib, dsu_union1) { + // Arran & Act + size_t size = 5; DSU dsu(size); dsu.dsu_union(1, 2); - dsu.dsu_union(1, 3); - // Assert - EXPECT_EQ(dsu.size(), size); - EXPECT_EQ(dsu.rank()[1], 1); - EXPECT_EQ(dsu.parent()[2], 1); - EXPECT_EQ(dsu.parent()[3], 1); + dsu.dsu_union(4, 3); + EXPECT_EQ(dsu.dsu_find_recursive(1), dsu.dsu_find_recursive(2)); + EXPECT_EQ(dsu.dsu_find_recursive(4), dsu.dsu_find_recursive(3)); +} +TEST(TestDSULib, dsu_union2) { + DSU dsu(5); + dsu.dsu_union(0, 1); + dsu.dsu_union(1, 2); + EXPECT_EQ(dsu.dsu_find_recursive(0), dsu.dsu_find_recursive(2)); +} +TEST(TestDSULib, dsu_union_same_element) { + DSU dsu(3); + dsu.dsu_union(1, 1); + EXPECT_EQ(dsu.dsu_find_recursive(1), 1); } TEST(TestDSULib, dsu_union_with_exception) { // Arrange & Act @@ -55,6 +63,19 @@ TEST(TestDSULib, dsu_find_recursive) { EXPECT_EQ(dsu.dsu_find_recursive(6), 5); EXPECT_EQ(dsu.dsu_find_recursive(4), 5); } +TEST(TestDSULib, dsu_find_path_compression) { + // Arrange + DSU dsu(5); + for (int i = 0; i < 4; i++) { + dsu.dsu_union(i, i + 1); + } + // Act + int root = dsu.dsu_find_recursive(4); + // Assert + for (int i = 0; i < 5; i++) { + EXPECT_EQ(dsu.parent()[i], root); + } +} TEST(TestDSULib, dsu_find_recursive_with_exception) { // Arrange & Act size_t size = 3; From d260df60b35d94ec696cf902c9c59599c28cc0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 13:57:45 +0300 Subject: [PATCH 181/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20get=5Fcount=5Fof=5Fislands()=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D1=81=D1=87=D1=91=D1=82=D0=B0=20=D0=BE=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B2=20=D0=B2=20=D0=BC=D0=B0=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D1=86=D0=B5=20=D0=B8=D0=B7=20=D0=B5=D0=B4=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D1=86=20=D0=B8=20=D0=BD=D1=83=D0=BB=D0=B5=D0=B9,=20?= =?UTF-8?q?=D1=81=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D1=91=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BC=D0=BD=D0=BE=D0=B6=D0=B5=D1=81=D1=82=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 790e8644..8adca10d 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -2,11 +2,13 @@ #include "../lib_matrix/matrix.h" #include "../lib_stack/stack.h" #include "../lib_list/List.h" +#include "../lib_dsu/dsu.h" #include #include #include #include #include +#include template std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { T min_val = matrix[x][y]; @@ -225,3 +227,50 @@ Node* find_loop_start(List& list) { } return it2_slow; } +size_t get_count_of_islands(Matrix& matrix) { + if (matrix.is_empty()) { + return 0; + } + size_t N = matrix.getN(); + size_t M = matrix.getM(); + DSU islands(M*N); + + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + if (matrix[i][j] == 1) { + if (i == 0 && j == 0) { // верхний левый угол + + } + else if (i == 0) { //верхняя строка + if (matrix[i][j - 1] == 1) { + islands.dsu_union(i * N + j, i * N + j - 1); + } + } + else if (j == 0) { //левый столбец + if (matrix[i - 1][j] == 1) { + islands.dsu_union(i * N + j, (i - 1) * N + j); + } + } + else { + if (matrix[i - 1][j] == 1) { + islands.dsu_union(i * N + j, (i - 1) * N + j); + //соединяем этот элемент с [i][j] union + } + if (matrix[i][j - 1] == 1) { + islands.dsu_union(i * N + j, i * N + j - 1); + //соединяем этот элемент с [i][j] union + } + } + } + } + } + std::unordered_set unique_roots; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + if (matrix[i][j] == 1) { + unique_roots.insert( islands.dsu_find_recursive(i * N + j)); + } + } + } + return unique_roots.size(); +} From 2c26cdd4edb105b75e7f98a1bfdaa7ed1cd7be1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 15:18:53 +0300 Subject: [PATCH 182/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20DSU=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.h | 77 --------------------------------------------------- 1 file changed, 77 deletions(-) diff --git a/lib_dsu/dsu.h b/lib_dsu/dsu.h index eac2f92a..9331fa80 100644 --- a/lib_dsu/dsu.h +++ b/lib_dsu/dsu.h @@ -16,80 +16,3 @@ class DSU { int dsu_find_recursive(int x); DSU& operator=(const DSU& other); }; -DSU::DSU(size_t size) { - _size = size; - _parent = new int[_size]; - _rank = new int[_size]; - for (size_t i = 0; i < _size; i++) { - _parent[i] = i; - _rank[i] = 0; - } -} -DSU::DSU(const DSU& other) { - _size = other._size; - _parent = new int[_size]; - _rank = new int[_size]; - for (size_t i = 0; i < _size; i++) { - this->_parent[i] = other._parent[i]; - this->_rank[i] = other._rank[i]; - } -} -DSU::~DSU() { - delete[] _parent; - delete[] _rank; - _parent = nullptr; - _rank = nullptr; -} -size_t DSU::size() const noexcept { - return _size; -} -int* DSU::parent() const noexcept { - return _parent; -} -int* DSU::rank() const noexcept { - return _rank; -} -void DSU::dsu_union(int x, int y) { - if (x == y) { - return; - } - if (x >= _size || y >= _size || x < 0 || y < 0) { - throw std::logic_error("Uncorrect arguments"); - } - int parent_x = dsu_find_recursive(x); - int parent_y = dsu_find_recursive(y); - if (parent_x == parent_y) { - return; - } - if (_rank[parent_x] < _rank[parent_y]) { - _parent[parent_x] = parent_y; - } else if (_rank[parent_x] > _rank[parent_y]) { - _parent[parent_y] = parent_x; - } else { - _parent[parent_y] = parent_x; - _rank[parent_x]++; - } -} -int DSU::dsu_find_recursive(int x) { - if (x >= _size || x < 0) { - throw std::logic_error("Uncorrect argument"); - } - if (_parent[x] == x) { - return x; - } - return _parent[x] = dsu_find_recursive(_parent[x]); -} -DSU& DSU::operator=(const DSU& other) { - if (this != &other) { - delete[] _parent; - delete[] _rank; - _size = other._size; - _parent = new int[_size]; - _rank = new int[_size]; - for (size_t i = 0; i < _size; i++) { - this->_parent[i] = other._parent[i]; - this->_rank[i] = other._rank[i]; - } - } - return *this; -} From ce5203f5fb3f0f1498e30e0022ec67abaa7f381d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 15:23:05 +0300 Subject: [PATCH 183/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20DSU=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/dsu.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/lib_dsu/dsu.cpp b/lib_dsu/dsu.cpp index 13c02283..208653ad 100644 --- a/lib_dsu/dsu.cpp +++ b/lib_dsu/dsu.cpp @@ -1 +1,80 @@ #include "dsu.h" +DSU::DSU(size_t size) { + _size = size; + _parent = new int[_size]; + _rank = new int[_size]; + for (size_t i = 0; i < _size; i++) { + _parent[i] = i; + _rank[i] = 0; + } +} +DSU::DSU(const DSU& other) { + _size = other._size; + _parent = new int[_size]; + _rank = new int[_size]; + for (size_t i = 0; i < _size; i++) { + this->_parent[i] = other._parent[i]; + this->_rank[i] = other._rank[i]; + } +} +DSU::~DSU() { + delete[] _parent; + delete[] _rank; + _parent = nullptr; + _rank = nullptr; +} +size_t DSU::size() const noexcept { + return _size; +} +int* DSU::parent() const noexcept { + return _parent; +} +int* DSU::rank() const noexcept { + return _rank; +} +void DSU::dsu_union(int x, int y) { + if (x == y) { + return; + } + if (x >= _size || y >= _size || x < 0 || y < 0) { + throw std::logic_error("Uncorrect arguments"); + } + int parent_x = dsu_find_recursive(x); + int parent_y = dsu_find_recursive(y); + if (parent_x == parent_y) { + return; + } + if (_rank[parent_x] < _rank[parent_y]) { + _parent[parent_x] = parent_y; + } + else if (_rank[parent_x] > _rank[parent_y]) { + _parent[parent_y] = parent_x; + } + else { + _parent[parent_y] = parent_x; + _rank[parent_x]++; + } +} +int DSU::dsu_find_recursive(int x) { + if (x >= _size || x < 0) { + throw std::logic_error("Uncorrect argument"); + } + if (_parent[x] == x) { + return x; + } + return _parent[x] = dsu_find_recursive(_parent[x]); +} +DSU& DSU::operator=(const DSU& other) { + if (this != &other) { + delete[] _parent; + delete[] _rank; + _size = other._size; + _parent = new int[_size]; + _rank = new int[_size]; + for (size_t i = 0; i < _size; i++) { + this->_parent[i] = other._parent[i]; + this->_rank[i] = other._rank[i]; + } + } + return *this; +} From 11a57b0b6d556d2ba7216402c6f154d7e22a6434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 20:24:52 +0300 Subject: [PATCH 184/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B2=D0=B0=20=D0=BE=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D0=B2=20=D1=81=20DSU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_algoritm.cpp | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/test_algoritm.cpp b/tests/test_algoritm.cpp index 5535253f..6356cb3a 100644 --- a/tests/test_algoritm.cpp +++ b/tests/test_algoritm.cpp @@ -253,3 +253,70 @@ TEST(TestAlgorithmLib, test_find_loop_start_in_middle) { list.get_tail()->next = nullptr; } +TEST(TestAlgorithmLib, test_dsu_find_count_of_islands1) { + // Arrange & Act + int data[] = { 1, 1, + 0, 0 }; + Matrix matrix(data, 2, 2); + // Assert + EXPECT_EQ(1, get_count_of_islands(matrix)); +} +TEST(TestAlgorithmLib, test_dsu_find_count_of_islands2_empty_matrix) { + // Arrange & Act + Matrix matrix(10, 10); + // Assert + EXPECT_EQ(0, get_count_of_islands(matrix)); +} +TEST(TestAlgorithmLib, test_dsu_find_count_of_islands3) { + // Arrange & Act + int data[] = {1, 1, + 0, 1, + 1, 1, + 0, 1}; + Matrix matrix(data, 4, 2); + // Assert + EXPECT_EQ(1, get_count_of_islands(matrix)); +} +TEST(TestAlgorithmLib, test_dsu_find_count_of_islands5) { + // Arrange & Act + int data[] = { 0,0, + 0, 0, + 0, 0, + 0, 0}; + Matrix matrix(data, 4, 2); + // Assert + EXPECT_EQ(0, get_count_of_islands(matrix)); +} +TEST(TestAlgorithmLib, test_dsu_find_count_of_islands6_large_matrix) { + // Arrange & Act + int data[] = { + 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, + 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 1, 1, 1, 1, 0, 0 + }; + + Matrix matrix(data, 10, 10); + + // Assert + EXPECT_EQ(6, get_count_of_islands(matrix)); +} +TEST(TestAlgorithmLib, test_dsu_find_count_of_islands7_diagonal) { + // Arrange & Act + int data[] = { + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + }; + Matrix matrix(data, 4, 4); + + // Assert + EXPECT_EQ(1, get_count_of_islands(matrix)); +} From d558f8e18954dcfcc1593821e6104654a028ba10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 20:30:01 +0300 Subject: [PATCH 185/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=B4=D1=81=D1=87=D1=91=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=80=D0=BE=D0=B2=D0=BE=D0=B2=20=D1=81=20?= =?UTF-8?q?=D0=92=3DDSU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 42 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 8adca10d..bc6c3cf7 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -233,42 +233,38 @@ size_t get_count_of_islands(Matrix& matrix) { } size_t N = matrix.getN(); size_t M = matrix.getM(); - DSU islands(M*N); + DSU islands(M * N); + + // Все 8 направлений + int directions[8][2] = { + {-1, -1}, {-1, 0}, {-1, 1}, // верхние + {0, -1}, {0, 1}, // левый, правый + {1, -1}, {1, 0}, {1, 1} // нижние + }; for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { if (matrix[i][j] == 1) { - if (i == 0 && j == 0) { // верхний левый угол - - } - else if (i == 0) { //верхняя строка - if (matrix[i][j - 1] == 1) { - islands.dsu_union(i * N + j, i * N + j - 1); - } - } - else if (j == 0) { //левый столбец - if (matrix[i - 1][j] == 1) { - islands.dsu_union(i * N + j, (i - 1) * N + j); - } - } - else { - if (matrix[i - 1][j] == 1) { - islands.dsu_union(i * N + j, (i - 1) * N + j); - //соединяем этот элемент с [i][j] union - } - if (matrix[i][j - 1] == 1) { - islands.dsu_union(i * N + j, i * N + j - 1); - //соединяем этот элемент с [i][j] union + // Проверяем всех 8 соседей + for (auto& dir : directions) { + int ni = i + dir[0]; + int nj = j + dir[1]; + + if (ni >= 0 && ni < M && nj >= 0 && nj < N) { + if (matrix[ni][nj] == 1) { + islands.dsu_union(i * N + j, ni * N + nj); + } } } } } } + std::unordered_set unique_roots; for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { if (matrix[i][j] == 1) { - unique_roots.insert( islands.dsu_find_recursive(i * N + j)); + unique_roots.insert(islands.dsu_find_recursive(i * N + j)); } } } From da05fc790a8456160d5d2fcd79b8477210fa91a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 21:48:11 +0300 Subject: [PATCH 186/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F,=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B0=20<<=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B7=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_math_vector/math_vector.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib_math_vector/math_vector.h b/lib_math_vector/math_vector.h index c5a3630a..fa1671ac 100644 --- a/lib_math_vector/math_vector.h +++ b/lib_math_vector/math_vector.h @@ -1,6 +1,7 @@ #pragma once #include #include "../lib_vector/vector.h" + template class MathVector:public Tvector { protected: @@ -37,21 +38,23 @@ class MathVector:public Tvector { MathVector& operator += (const MathVector& vector); MathVector& operator -= (const MathVector& vector); MathVector& operator=(const MathVector& other); - friend std::ostream& operator<<(std::ostream& out, const MathVector& vector) { - out << "["; - for (size_t i = 0; i < vector.get_size(); ++i) { - out << vector[i]; - if (i < vector.get_size() - 1) { - out << ", "; - } - } - out << "]"; - return out; - } + friend std::ostream& operator<< (std::ostream& out, const MathVector& vector); T& operator[](size_t index); const T& operator[](size_t index) const; }; template +std::ostream& operator<<(std::ostream& out, const MathVector& vector) { + out << "["; + for (size_t i = 0; i < vector.get_size(); ++i) { + out << vector[i]; + if (i < vector.get_size() - 1) { + out << ", "; + } + } + out << "]"; + return out; +} +template MathVector::MathVector() : Tvector() {} template MathVector::MathVector(size_t size) : Tvector(size) {} @@ -166,23 +169,23 @@ MathVector& MathVector::operator=(const MathVector& other) { } template T& MathVector::operator[](size_t index) { - return Tvector::operator[](index - _start_index); + return this->Tvector::operator[](index - _start_index); } template const T& MathVector::operator[](size_t index) const { - return Tvector::operator[](index - _start_index); + return this->Tvector::operator[](index - _start_index); } template T& MathVector::at(size_t index) { if (index < _start_index || index >= _start_index + this->get_size()) { throw std::logic_error("MathVector index out of range"); } - return Tvector::operator[](index - _start_index); + return this->Tvector::operator[](index - _start_index); } template const T& MathVector::at(size_t index) const { if (index < _start_index || index >= _start_index + this->get_size()) { throw std::std::logic_error("MathVector index out of range"); } - return Tvector::operator[](index - _start_index); + return this->Tvector::operator[](index - _start_index); } From 56691d8bcba96aa77ecdabdef5d4797220b2dab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 24 Nov 2025 22:23:52 +0300 Subject: [PATCH 187/275] =?UTF-8?q?=D0=A3=D0=BF=D1=80=D0=BE=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5=D1=82=D0=B8?= =?UTF-8?q?=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D0=BE=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=BE=D0=B2,=20=D0=B2=D1=8B=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20<<=20=D0=B7=D0=B0=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/matrix.h | 114 ++++++++------------------------------------ 1 file changed, 19 insertions(+), 95 deletions(-) diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h index 6fbeaaf6..da3585ff 100644 --- a/lib_matrix/matrix.h +++ b/lib_matrix/matrix.h @@ -24,13 +24,9 @@ class Matrix : public MathVector> { size_t getM() const; size_t getN() const; - Matrix operator + (T value) const; - Matrix operator - (T value) const; Matrix operator * (T value) const; Matrix operator / (T value) const; - Matrix& operator += (T value); - Matrix& operator -= (T value); Matrix& operator *= (T value); Matrix& operator /= (T value); @@ -42,19 +38,20 @@ class Matrix : public MathVector> { Matrix& operator += (const Matrix& other_matrix); Matrix& operator -= (const Matrix& other_matrix); - friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { - for (size_t i = 0; i < matrix.getM(); ++i) { - for (size_t j = 0; j < matrix.getN(); ++j) { - out << matrix[i][j] << "\t"; - } - out << std::endl; - } - return out; - } - + friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix); Matrix& operator=(const Matrix& other); }; template +std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { + for (size_t i = 0; i < matrix.getM(); ++i) { + for (size_t j = 0; j < matrix.getN(); ++j) { + out << matrix[i][j] << "\t"; + } + out << std::endl; + } + return out; +} +template size_t Matrix::getM() const { return _M; } @@ -86,86 +83,25 @@ Matrix::Matrix(const Matrix& other) : MathVector>(other), _M(other._M), _N(other._N) {} template Matrix::~Matrix() = default; - -template -Matrix Matrix::operator + (T value) const { - Matrix matrix(_M, _N); - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] + value; - } - } - return matrix; -} -template -Matrix Matrix::operator - (T value) const { - Matrix matrix(_M, _N); - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] - value; - } - } - return matrix; -} template Matrix Matrix::operator * (T value) const { - Matrix matrix(_M, _N); - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] * value; - } - } - return matrix; + return Matrix(*this) *= value; } template Matrix Matrix::operator / (T value) const { - if (value == 0) { - throw std::logic_error("Division by zero!"); - } - Matrix matrix(_M, _N); - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - matrix[i][j] = (*this)[i][j] / value; - } - } - return matrix; -} -template -Matrix& Matrix::operator += (T value) { - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - (*this)[i][j] = (*this)[i][j] + value; - } - } - return *this; -} -template -Matrix& Matrix::operator -= (T value) { - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - (*this)[i][j] = (*this)[i][j] - value; - } - } - return *this; + return Matrix(*this) /= value; } template Matrix& Matrix::operator *= (T value) { for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - (*this)[i][j] = (*this)[i][j] * value; - } + (*this)[i] *= value; } return *this; } template Matrix& Matrix::operator /= (T value) { - if (value == 0) { - throw std::logic_error("Division by zero!"); - } for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - (*this)[i][j] = (*this)[i][j] / value; - } + (*this)[i] /= value; } return *this; } @@ -176,9 +112,7 @@ Matrix Matrix::operator + (const Matrix& other_matrix) const { } Matrix result(_M, _N); for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - result[i][j] = (*this)[i][j] + other_matrix[i][j]; - } + result[i] = (*this)[i] + other_matrix[i]; } return result; } @@ -189,9 +123,7 @@ Matrix Matrix::operator - (const Matrix& other_matrix) const { } Matrix result(_M, _N); for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - result[i][j] = (*this)[i][j] - other_matrix[i][j]; - } + result[i] = (*this)[i] - other_matrix[i]; } return result; } @@ -228,11 +160,7 @@ Matrix& Matrix::operator += (const Matrix& other_matrix) { if (_M != other_matrix.getM() || _N != other_matrix.getN()) { throw std::logic_error("The matrices have different sizes!"); } - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - (*this)[i][j] += other_matrix[i][j]; - } - } + *this = *this + other_matrix; return *this; } template @@ -240,11 +168,7 @@ Matrix& Matrix::operator -= (const Matrix& other_matrix) { if (_M != other_matrix.getM() || _N != other_matrix.getN()) { throw std::logic_error("The matrices have different sizes!"); } - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - (*this)[i][j] -= other_matrix[i][j]; - } - } + *this = *this - other_matrix; return *this; } template From 4801c897f36c67ba41350533799639adda8fe9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 09:19:51 +0300 Subject: [PATCH 188/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20Cmake=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ed05fc1..dc516c92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,33 @@ include(cmake/function.cmake) # подхватываем функции, # Рё для создания исполняемого проекта РІ отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt РёР· подкаталога СЃ именем lib_easy_example + +add_subdirectory(lib_vector) +add_subdirectory(lib_math_vector) +add_subdirectory(lib_matrix) +add_subdirectory(lib_triangle_matrix) + +add_subdirectory(lib_list) +add_subdirectory(lib_stack) +add_subdirectory(lib_queue) +add_subdirectory(lib_queue_on_list) +add_subdirectory(lib_stack_on_list) + +add_subdirectory(lib_point) +add_subdirectory(lib_point3d) +add_subdirectory(lib_circle) +add_subdirectory(lib_sphere) + + +add_subdirectory(lib_dsu) + +add_subdirectory(lib_doubly_linked_list) + +add_subdirectory(lib_algoritm) + + +add_subdirectory(lib_easy_example) + add_subdirectory(main) # подключаем дополнительный CMakeLists.txt РёР· подкаталога СЃ именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) From dca1fd7c0ec86a7e0f940da7f086ad4d00e015ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:00:49 +0300 Subject: [PATCH 189/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20cmake=20list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc516c92..f6cbd7d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ add_subdirectory(lib_doubly_linked_list) add_subdirectory(lib_algoritm) -add_subdirectory(lib_easy_example) + add_subdirectory(main) # подключаем дополнительный CMakeLists.txt РёР· подкаталога СЃ именем main From efec949a1d5cd7668ff75579bb5ecf1240dc95c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:09:37 +0300 Subject: [PATCH 190/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20cmake=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack_on_list/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_stack_on_list/CMakeLists.txt diff --git a/lib_stack_on_list/CMakeLists.txt b/lib_stack_on_list/CMakeLists.txt new file mode 100644 index 00000000..5e9177c1 --- /dev/null +++ b/lib_stack_on_list/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(StackOnList) \ No newline at end of file From 73adbf585d9de68cfce9735dcd842c96c23f0257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:11:19 +0300 Subject: [PATCH 191/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20cmake=20=D0=B4=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_stack/CMakeLists.txt diff --git a/lib_stack/CMakeLists.txt b/lib_stack/CMakeLists.txt new file mode 100644 index 00000000..b6bedb01 --- /dev/null +++ b/lib_stack/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Stack) \ No newline at end of file From 0532c7723e011a4954d762d45a1af31a21a39f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:15:47 +0300 Subject: [PATCH 192/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_stack/stack.cpp diff --git a/lib_stack/stack.cpp b/lib_stack/stack.cpp new file mode 100644 index 00000000..3d38ae2e --- /dev/null +++ b/lib_stack/stack.cpp @@ -0,0 +1 @@ +#include "stack.h" \ No newline at end of file From 3ab042fb3883f20a8928f04c0cd6dcaf4b711079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:18:58 +0300 Subject: [PATCH 193/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B8=20cmake?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue_on_list/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_queue_on_list/CMakeLists.txt diff --git a/lib_queue_on_list/CMakeLists.txt b/lib_queue_on_list/CMakeLists.txt new file mode 100644 index 00000000..fcef8d62 --- /dev/null +++ b/lib_queue_on_list/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(QueueOnList) \ No newline at end of file From 7922de90fef7406ccf71d4f9472e5d952f5d4796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:21:01 +0300 Subject: [PATCH 194/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B8=20cmake?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_queue/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_queue/CMakeLists.txt diff --git a/lib_queue/CMakeLists.txt b/lib_queue/CMakeLists.txt new file mode 100644 index 00000000..f0846b24 --- /dev/null +++ b/lib_queue/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Queue) \ No newline at end of file From f8a596f52fdae5e1d6cb55c308c89b24112932d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:24:23 +0300 Subject: [PATCH 195/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B8=20cmake?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/CMakeLists.txt | 2 ++ lib_matrix/matrix.cpp | 1 + 2 files changed, 3 insertions(+) create mode 100644 lib_matrix/CMakeLists.txt create mode 100644 lib_matrix/matrix.cpp diff --git a/lib_matrix/CMakeLists.txt b/lib_matrix/CMakeLists.txt new file mode 100644 index 00000000..db5e1759 --- /dev/null +++ b/lib_matrix/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(Matrix) +add_depend(Matrix MathVector lib_math_vector) \ No newline at end of file diff --git a/lib_matrix/matrix.cpp b/lib_matrix/matrix.cpp new file mode 100644 index 00000000..1a1f343c --- /dev/null +++ b/lib_matrix/matrix.cpp @@ -0,0 +1 @@ +#include "matrix.h" From 0649d68091c9ac7ea45dce797c5cf1fce279957c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 25 Nov 2025 14:29:10 +0300 Subject: [PATCH 196/275] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/CMakeLists.txt | 2 - lib_matrix/matrix.cpp | 1 - lib_matrix/matrix.h | 180 -------------------------------------- 3 files changed, 183 deletions(-) delete mode 100644 lib_matrix/CMakeLists.txt delete mode 100644 lib_matrix/matrix.cpp delete mode 100644 lib_matrix/matrix.h diff --git a/lib_matrix/CMakeLists.txt b/lib_matrix/CMakeLists.txt deleted file mode 100644 index db5e1759..00000000 --- a/lib_matrix/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -create_project_lib(Matrix) -add_depend(Matrix MathVector lib_math_vector) \ No newline at end of file diff --git a/lib_matrix/matrix.cpp b/lib_matrix/matrix.cpp deleted file mode 100644 index 1a1f343c..00000000 --- a/lib_matrix/matrix.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "matrix.h" diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h deleted file mode 100644 index da3585ff..00000000 --- a/lib_matrix/matrix.h +++ /dev/null @@ -1,180 +0,0 @@ -#pragma once -#include -#include "../lib_math_vector/math_vector.h" -template -class Matrix : public MathVector> { -protected: - size_t _M; - size_t _N; - Matrix transpose() const { - Matrix result(_N, _M); - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < _N; ++j) { - result[j][i] = (*this)[i][j]; - } - } - return result; - } -public: - Matrix(); - Matrix(size_t M, size_t N); - Matrix(T* data, size_t M, size_t N); - Matrix(const Matrix& other); - virtual ~Matrix(); - size_t getM() const; - size_t getN() const; - - Matrix operator * (T value) const; - Matrix operator / (T value) const; - - Matrix& operator *= (T value); - Matrix& operator /= (T value); - - Matrix operator + (const Matrix& other_matrix) const; - Matrix operator - (const Matrix& other_matrix) const; - Matrix operator * (const Matrix& other_matrix) const; - - MathVector operator * (const MathVector& vector) const; - - Matrix& operator += (const Matrix& other_matrix); - Matrix& operator -= (const Matrix& other_matrix); - friend std::ostream& operator<< (std::ostream& out, const Matrix& matrix); - Matrix& operator=(const Matrix& other); -}; -template -std::ostream& operator<< (std::ostream& out, const Matrix& matrix) { - for (size_t i = 0; i < matrix.getM(); ++i) { - for (size_t j = 0; j < matrix.getN(); ++j) { - out << matrix[i][j] << "\t"; - } - out << std::endl; - } - return out; -} -template -size_t Matrix::getM() const { - return _M; -} -template -size_t Matrix::getN() const { - return _N; -} - -template -Matrix::Matrix() : MathVector>(), _M(0), _N(0) {} -template -Matrix::Matrix(size_t M, size_t N) : MathVector>(M) { - _M = M; - _N = N; - for (size_t i = 0; i < _M; ++i) { - (*this)[i] = MathVector(_N); - } -} -template -Matrix::Matrix(T* data, size_t M, size_t N) : MathVector>(M) { - _M = M; - _N = N; - for (size_t i = 0; i < _M; ++i) { - (*this)[i] = MathVector(data + i * _N, _N); - } -} -template -Matrix::Matrix(const Matrix& other) : - MathVector>(other), _M(other._M), _N(other._N) {} -template -Matrix::~Matrix() = default; -template -Matrix Matrix::operator * (T value) const { - return Matrix(*this) *= value; -} -template -Matrix Matrix::operator / (T value) const { - return Matrix(*this) /= value; -} -template -Matrix& Matrix::operator *= (T value) { - for (size_t i = 0; i < _M; ++i) { - (*this)[i] *= value; - } - return *this; -} -template -Matrix& Matrix::operator /= (T value) { - for (size_t i = 0; i < _M; ++i) { - (*this)[i] /= value; - } - return *this; -} -template -Matrix Matrix::operator + (const Matrix& other_matrix) const { - if (_M != other_matrix.getM() || _N != other_matrix.getN()) { - throw std::logic_error("The matrices have different sizes!"); - } - Matrix result(_M, _N); - for (size_t i = 0; i < _M; ++i) { - result[i] = (*this)[i] + other_matrix[i]; - } - return result; -} -template -Matrix Matrix::operator - (const Matrix& other_matrix) const { - if (_M != other_matrix.getM() || _N != other_matrix.getN()) { - throw std::logic_error("The matrices have different sizes!"); - } - Matrix result(_M, _N); - for (size_t i = 0; i < _M; ++i) { - result[i] = (*this)[i] - other_matrix[i]; - } - return result; -} -template -Matrix Matrix::operator * (const Matrix& other_matrix) const { - if (_N != other_matrix.getM()) { - throw std::logic_error - ("The sizes of the matrices are not compatible for this operation!"); - } - Matrix result (_M, other_matrix.getN()); - Matrix matrix_t = other_matrix.transpose(); - for (size_t i = 0; i < _M; ++i) { - for (size_t j = 0; j < matrix_t.getM(); ++j) { - result[i][j] = (*this)[i] * matrix_t[j]; - } - } - return result; -} -template -MathVector Matrix::operator * (const MathVector& vector) const { - if (_N != vector.get_size()) { - throw std::logic_error - ("Size of matrix aren't compatible with vector's size for this operation!"); - } - MathVector result(_M); - for (size_t i = 0; i < _M; ++i) { - result[i] = (*this)[i] * vector; - - } - return result; -} -template -Matrix& Matrix::operator += (const Matrix& other_matrix) { - if (_M != other_matrix.getM() || _N != other_matrix.getN()) { - throw std::logic_error("The matrices have different sizes!"); - } - *this = *this + other_matrix; - return *this; -} -template -Matrix& Matrix::operator -= (const Matrix& other_matrix) { - if (_M != other_matrix.getM() || _N != other_matrix.getN()) { - throw std::logic_error("The matrices have different sizes!"); - } - *this = *this - other_matrix; - return *this; -} -template -Matrix& Matrix::operator=(const Matrix& other) { - MathVector>::operator=(other); - _M = other._M; - _N = other._N; - return *this; -} From 3cf58e3691f0cf14c907140e774a2d5a762838b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 30 Nov 2025 09:07:17 +0300 Subject: [PATCH 197/275] =?UTF-8?q?Cmake=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f6cbd7d8..8216aa6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,23 +15,12 @@ include(cmake/function.cmake) # подхватываем функции, add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt РёР· подкаталога СЃ именем lib_easy_example -add_subdirectory(lib_vector) -add_subdirectory(lib_math_vector) -add_subdirectory(lib_matrix) -add_subdirectory(lib_triangle_matrix) - add_subdirectory(lib_list) add_subdirectory(lib_stack) add_subdirectory(lib_queue) add_subdirectory(lib_queue_on_list) add_subdirectory(lib_stack_on_list) -add_subdirectory(lib_point) -add_subdirectory(lib_point3d) -add_subdirectory(lib_circle) -add_subdirectory(lib_sphere) - - add_subdirectory(lib_dsu) add_subdirectory(lib_doubly_linked_list) From dadd4be5c38a90f2cf7a4a8647286dfe1613d7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 30 Nov 2025 10:48:38 +0300 Subject: [PATCH 198/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=20=D1=81=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=D0=BC=D0=B8=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5=D1=82=D0=B8?= =?UTF-8?q?=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D0=B2=D1=8B=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/CMakeLists.txt | 1 + lib_calculator_app/expression.cpp | 0 lib_calculator_app/expression.h | 9 ++++++++ lib_calculator_app/functions.cpp | 0 lib_calculator_app/functions.h | 0 lib_calculator_app/lexem.cpp | 1 + lib_calculator_app/lexem.h | 37 +++++++++++++++++++++++++++++++ lib_calculator_app/main.cpp | 37 +++++++++++++++++++++++++++++++ lib_calculator_app/parser.cpp | 0 lib_calculator_app/parser.h | 5 +++++ 10 files changed, 90 insertions(+) create mode 100644 lib_calculator_app/CMakeLists.txt create mode 100644 lib_calculator_app/expression.cpp create mode 100644 lib_calculator_app/expression.h create mode 100644 lib_calculator_app/functions.cpp create mode 100644 lib_calculator_app/functions.h create mode 100644 lib_calculator_app/lexem.cpp create mode 100644 lib_calculator_app/lexem.h create mode 100644 lib_calculator_app/main.cpp create mode 100644 lib_calculator_app/parser.cpp create mode 100644 lib_calculator_app/parser.h diff --git a/lib_calculator_app/CMakeLists.txt b/lib_calculator_app/CMakeLists.txt new file mode 100644 index 00000000..de8e9e76 --- /dev/null +++ b/lib_calculator_app/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(CalculatorApp) \ No newline at end of file diff --git a/lib_calculator_app/expression.cpp b/lib_calculator_app/expression.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h new file mode 100644 index 00000000..3d37d2a4 --- /dev/null +++ b/lib_calculator_app/expression.h @@ -0,0 +1,9 @@ +#include "../lib_vector/vector.h" +#include "lexem.h" +class Expression { +private: + List _lexems; +public: + Expression(std::string expression); + Expression(const List& list); +}; \ No newline at end of file diff --git a/lib_calculator_app/functions.cpp b/lib_calculator_app/functions.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_calculator_app/functions.h b/lib_calculator_app/functions.h new file mode 100644 index 00000000..e69de29b diff --git a/lib_calculator_app/lexem.cpp b/lib_calculator_app/lexem.cpp new file mode 100644 index 00000000..c16bc0a9 --- /dev/null +++ b/lib_calculator_app/lexem.cpp @@ -0,0 +1 @@ +#include "lexem.h" diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h new file mode 100644 index 00000000..e0ebd6ee --- /dev/null +++ b/lib_calculator_app/lexem.h @@ -0,0 +1,37 @@ +#include "../lib_list/List.h" +enum TypeLexem { + Constant, + Variable, + OpenBrecket, + ClosedBrecket, + Function, + Operator, + UnOperator, + OpenedAbs, + ClosedAbs }; +struct Lexem { + std::string name; + TypeLexem type; + double value; + int priority; + double (*function)(double); + Lexem(std::string _name, TypeLexem _type, double _value = DBL_MAX, int _priority = -1, double(*_function)(double) = nullptr); + Lexem(const Lexem& other); + + std::string getName() const { + return name; + } + TypeLexem getType() const { + return type; + } + double getValue() const { + return value; + } + int getPriority() const { + return priority; + } + double (*getFunction() const)(double) { + return function; + } + +}; diff --git a/lib_calculator_app/main.cpp b/lib_calculator_app/main.cpp new file mode 100644 index 00000000..6e442f5f --- /dev/null +++ b/lib_calculator_app/main.cpp @@ -0,0 +1,37 @@ +//#include "expression.h" +//#include "functions.h" +//#include "lexem.h" +//#include "parser.h" +//#include "../lib_vector/vector.h" +//#include +//// В main.cpp +//void printExpressionsTable(const Tvector& expressions) { +// std::cout << "+-----+-----------------------------------------------+-----------------------------+" << std::endl; +// std::cout << "| ID | EXPRESSION | VARIABLES VALUES |" << std::endl; +// std::cout << "+-----+-----------------------------------------------+-----------------------------+" << std::endl; +// +// for (int i = 0; i < expressions.size(); ++i) { +// if (expressions.exists(i)) { +// const Expression& expr = expressions[i]; +// std::cout << "| " << std::setw(3) << expr.getId() << " | " +// << std::setw(45) << std::left << expr.getOriginalExpression() << std::right << " | " +// << std::setw(27) << formatVariables(expr) << " |" << std::endl; +// } +// } +// +// std::cout << "+-----+-----------------------------------------------+-----------------------------+" << std::endl; +//} +//std::string formatVariables(const Expression& expr) { +// auto vars = expr.getVariables(); +// std::string result; +// for (const auto& [name, value] : vars) { +// if (!result.empty()) result += ", "; +// if (value == DBL_MAX) { +// result += name + " = ?"; +// } +// else { +// result += name + " = " + std::to_string(value); +// } +// } +// return result; +//} diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp new file mode 100644 index 00000000..e69de29b diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h new file mode 100644 index 00000000..017b1496 --- /dev/null +++ b/lib_calculator_app/parser.h @@ -0,0 +1,5 @@ +#include "lexem.h" +#include "../lib_list/List.h" +namespace Parser { + List parse(std::string expression); +} From 17e359739eb3793ecc4b75f880718d1282361a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 30 Nov 2025 11:12:36 +0300 Subject: [PATCH 199/275] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D1=80=D0=B0=20=D0=9B=D0=B5=D0=BA=D1=81=D0=B5=D0=BC?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/lexem.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h index e0ebd6ee..e2ad6a44 100644 --- a/lib_calculator_app/lexem.h +++ b/lib_calculator_app/lexem.h @@ -1,9 +1,10 @@ #include "../lib_list/List.h" +#include enum TypeLexem { Constant, Variable, - OpenBrecket, - ClosedBrecket, + OpenBracket, + ClosedBracket, Function, Operator, UnOperator, @@ -15,8 +16,18 @@ struct Lexem { double value; int priority; double (*function)(double); - Lexem(std::string _name, TypeLexem _type, double _value = DBL_MAX, int _priority = -1, double(*_function)(double) = nullptr); - Lexem(const Lexem& other); + + Lexem(std::string _name, TypeLexem _type, double _value = DBL_MAX, + int _priority = -1, double(*_function)(double) = nullptr): + name(_name), type(_type), value(_value), + priority(_priority), function(_function){}; + + Lexem(const Lexem& other): name(other.name), type(other.type), + value(other.value), priority(other.priority), + function(other.function){} + + Lexem(double _value): name(std::to_string(_value)), type(Constant), + value(_value), priority(-1), function(nullptr) {} std::string getName() const { return name; @@ -33,5 +44,4 @@ struct Lexem { double (*getFunction() const)(double) { return function; } - }; From 8c35e55a125b5813c18b6318903663362a41ce64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 30 Nov 2025 20:56:50 +0300 Subject: [PATCH 200/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=20=D0=B8=20=D0=B2=D1=81=D0=BF=D0=BE=D0=BC=D0=BE=D0=B3?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D1=85=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index 017b1496..f5922e02 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -1,5 +1,19 @@ -#include "lexem.h" +#pragma once #include "../lib_list/List.h" -namespace Parser { +#include "lexem.h" +namespace Parser { List parse(std::string expression); + double getSin(double value); + double getCos(double value); + double getTg(double value); + double getAbs(double value); + bool isOpenBracket(char c); + bool isCloseBracket(char c); + bool isDigit(char c); + bool isLetter(char c); + bool isVariableChar(char c); + bool isValidVariableName(const std::string& name); + bool isFunction(const std::string& name); + bool isBinaryOperator(char c); + bool isUnaryOperator(char c, const Lexem* prevLexem); } From a62e3195bc017bddbf2d20f89fee9f0da150db60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 6 Dec 2025 14:10:45 +0300 Subject: [PATCH 201/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20cmake=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_algoritm/CMakeLists.txt diff --git a/lib_algoritm/CMakeLists.txt b/lib_algoritm/CMakeLists.txt new file mode 100644 index 00000000..b0cc27ac --- /dev/null +++ b/lib_algoritm/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Algoritm) \ No newline at end of file From 6afdd048ae3f251b6a8eae2e66312b8345c918cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 6 Dec 2025 14:11:22 +0300 Subject: [PATCH 202/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20cmake=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_dsu/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib_dsu/CMakeLists.txt diff --git a/lib_dsu/CMakeLists.txt b/lib_dsu/CMakeLists.txt new file mode 100644 index 00000000..424d2417 --- /dev/null +++ b/lib_dsu/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(DSU) \ No newline at end of file From 986206aa2a6bb5211f290d651ae5148a66170913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 6 Dec 2025 14:11:59 +0300 Subject: [PATCH 203/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20cmake=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8216aa6c..61be80d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ add_subdirectory(lib_stack_on_list) add_subdirectory(lib_dsu) add_subdirectory(lib_doubly_linked_list) - +add_subdirectory(lib_calculator_app) add_subdirectory(lib_algoritm) From a76f55e7e35047ca86470eb0a3449b9a019d32df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 6 Dec 2025 15:12:45 +0300 Subject: [PATCH 204/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B5=D0=BE=D0=B1=D1=85=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=D1=8B=D1=85=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D1=81=D0=B5=D1=80=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index f5922e02..603b9a82 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -1,6 +1,10 @@ #pragma once #include "../lib_list/List.h" #include "lexem.h" +#include +#include +#include +#include namespace Parser { List parse(std::string expression); double getSin(double value); From 94d38965e371152746918a2b39841488026d90a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 6 Dec 2025 15:13:26 +0300 Subject: [PATCH 205/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=87=D0=B0=D1=81=D1=82=D0=B8=D1=87=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B0=D1=81=D1=80=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index e69de29b..ecdfee5c 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -0,0 +1,57 @@ +#include "parser.h" +List Parser::parse(std::string expression) { + +} +double Parser::getSin(double value) { + return sin(value); +} +double Parser::getCos(double value) { + return cos(value); +} +double Parser::getTg(double value) { + return tan(value); +} +double Parser::getAbs(double value) { + return fabs(value); +} +bool Parser::isOpenBracket(char c) { + return c == '(' || c == '{' || c == '['; +} +bool Parser::isCloseBracket(char c) { + return c == ')' || c == '}' || c == ']'; +} +bool Parser::isDigit(char c) { + return c >= '0' && c <= '9'; +} +bool Parser::isLetter(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} +bool Parser::isVariableChar(char c) { + return isDigit(c) || isLetter(c) || c == '_'; +} +bool Parser::isValidVariableName(const std::string& name) { + if (name.empty()) { + return false; + } + if (!isLetter(name[0]) && name[0] != '_') { + return false; + } + for (char c : name) { + if (!isVariableChar(c)) { + return false; + } + } + if (isFunction(name)) { + return false; + } + return true; +} +bool Parser::isFunction(const std::string& name) { + +} +bool Parser::isBinaryOperator(char c) { + +} +bool Parser::isUnaryOperator(char c, const Lexem* prevLexem) { + +} \ No newline at end of file From 39c540fc8aab89239a54c50588b59d401c540519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 09:29:35 +0300 Subject: [PATCH 206/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/lexem.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h index e2ad6a44..1e572a43 100644 --- a/lib_calculator_app/lexem.h +++ b/lib_calculator_app/lexem.h @@ -2,14 +2,14 @@ #include enum TypeLexem { Constant, - Variable, - OpenBracket, - ClosedBracket, + OpenBracket, + CloseBracket, + Variable, Function, Operator, UnOperator, - OpenedAbs, - ClosedAbs }; + OpenAbs, + CloseAbs }; struct Lexem { std::string name; TypeLexem type; From 7b1ff5945c4f07e92b9144baba3c9fcf638aa6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 09:30:22 +0300 Subject: [PATCH 207/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=B2?= =?UTF-8?q?=D1=81=D0=BF=D0=BE=D0=BC=D0=BE=D0=B3=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B8=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80=20(?= =?UTF-8?q?=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B9=20=D1=81=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D0=BB=D0=B5=D0=BC=20=D0=BF=D0=BE=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B5?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 188 ++++++++++++++++++++++++++++++++-- 1 file changed, 182 insertions(+), 6 deletions(-) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index ecdfee5c..c9d45616 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -1,13 +1,133 @@ -#include "parser.h" +п»ї#include "parser.h" List Parser::parse(std::string expression) { + if (expression.empty()) { + throw std::logic_error("Expression is empty"); + } + List lexems; + bool lastWasOperatorOrBracketOrFunction = true; + size_t i = 0; + while (i < expression.length()) { + char c = expression[i]; + if (c == ' ') { + i++; + continue; + } + if (isDigit(c)) { + std::string number = ""; + number += c; + i++; + while (i < expression.length() && + (isDigit(expression[i]) || expression[i] == '.')) { + number += expression[i]; + i++; + } + try { + double constant = std::stod(number); + Lexem lexem(constant); + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = false; + } + catch (std::exception& ex) { + size_t errorPos = i - number.length(); + std::string errorMsg = formatError(expression, errorPos, + "Invalid number format: '" + number + "'"); + throw std::logic_error(errorMsg); + } + + } else if (isOpenBracket(c)) { + Lexem lexem(std::string(1, c), TypeLexem::OpenBracket); + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = true; + i++; + } else if (isCloseBracket(c)) { + Lexem lexem(std::string(1, c), TypeLexem::CloseBracket); + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = false; + i++; + } else if (isBinaryOperator(c)) { + bool isUnary = isUnaryOperator(c, lastWasOperatorOrBracketOrFunction); + if (isUnary && c != '+') { + Lexem lexem1(0.0); + Lexem lexem2("-", TypeLexem::Operator, DBL_MAX, 1); + lexems.push_back(lexem1); + lexems.push_back(lexem2); + lastWasOperatorOrBracketOrFunction = true; + } else { + int priority = getOperatorPriority(c); + Lexem lexem(std::string(1, c), TypeLexem::Operator, DBL_MAX, priority); + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = true; + } + i++; // функции, переменные Рё модуль + } else if (isLetter(c) || c == '_') { //это или функция или переменная + std::string word = ""; + word += c; + i++; + while (i < expression.length() && isVariableChar(expression[i])) { + word += expression[i]; //sin + i++; + } if (isFunction(word)) { + if ( i >= expression.length() ||!isOpenBracket(expression[i])){ + size_t errorPos = i - word.length(); + std::string errorMsg = formatError(expression, errorPos, + "Invalid variable/function format: '" + word + "'"); + throw std::logic_error(errorMsg); + } + double (*funcPtr)(double) = getFunctionByName(word); + if (funcPtr == nullptr) { + size_t errorPos = i - word.length(); + std::string errorMsg = formatError(expression, errorPos, + "Invalid variable/function format: '" + word + "'"); + throw std::logic_error(errorMsg); + } + Lexem lexem(word, TypeLexem::Function, DBL_MAX, 4, funcPtr); + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = true; + + } else if (isValidVariableName(word)) { + Lexem lexem(word, TypeLexem::Variable); + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = false; + + } else { + size_t errorPos = i - word.length(); + std::string errorMsg = formatError(expression, errorPos, + "Invalid variable/function format: '" + word + "'"); + throw std::logic_error(errorMsg); + } + } else if (c == '|') { //модуль + Lexem lexem(std::string(1,c), TypeLexem::OpenAbs); + + lexems.push_back(lexem); + lastWasOperatorOrBracketOrFunction = false; + i++; + } + else { + size_t errorPos = i; + std::string errorMsg = formatError(expression, errorPos, + "Unknown character: '" + std::string(1, c) + "'"); + throw std::logic_error(errorMsg); + } + + } // цикл while i < expr. lenght + + if (lastWasOperatorOrBracketOrFunction && !(lexems.get_size() == 0) ) { + size_t errorPos = expression.length() - 1; + std::string errorMsg = formatError(expression, errorPos, + "Expression cannot end with operator or function"); + throw std::logic_error(errorMsg); + } + return lexems; } double Parser::getSin(double value) { return sin(value); } + double Parser::getCos(double value) { return cos(value); } + double Parser::getTg(double value) { return tan(value); } @@ -47,11 +167,67 @@ bool Parser::isValidVariableName(const std::string& name) { return true; } bool Parser::isFunction(const std::string& name) { - + const std::string functions[] = { + "sin", "cos", "tg" + }; + for (const auto& function : functions) { + if (name == function) { + return true; + } + } + return false; } bool Parser::isBinaryOperator(char c) { - + return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'; +} +bool Parser::isUnaryOperator(char c, bool lastWasOperatorOrBracket) { + if (c != '-') { + return false; + } + return lastWasOperatorOrBracket; +} +int Parser::getOperatorPriority(char c) { + switch (c) { + case '^': + return 3; + break; + case '*': case '/': + return 2; + break; + case '+': case '-': + return 1; + break; + default: + return 0; + } +} +std::string Parser::formatError(const std::string& expression, + size_t position, + const std::string& message) { + std::stringstream ss; + ss << "\nВыражение: " << expression << "\n"; + ss << "Позиция " << position + 1 << ": "; + for (size_t i = 0; i < position && i < expression.length(); i++) { + if (expression[i] == '\t') { + ss << "\t"; + } + else { + ss << " "; + } + } + ss << "^\n"; + ss << "Ошибка: " << message; + return ss.str(); +} +double (*Parser::getFunctionByName(const std::string& name))(double) { + if (name == "sin") { + return Parser::getSin; + } + if (name == "cos") { + return Parser::getCos; + } + if (name == "tg") { + return Parser::getTg; + } + return nullptr; } -bool Parser::isUnaryOperator(char c, const Lexem* prevLexem) { - -} \ No newline at end of file From e2cf17706ec78b7f512cd56ee609b4ee6ab34d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 09:30:57 +0300 Subject: [PATCH 208/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index 603b9a82..b219329a 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -3,9 +3,12 @@ #include "lexem.h" #include #include -#include +#include "parser.h" #include -namespace Parser { +#include +#include +#include +namespace Parser { List parse(std::string expression); double getSin(double value); double getCos(double value); @@ -19,5 +22,8 @@ namespace Parser { bool isValidVariableName(const std::string& name); bool isFunction(const std::string& name); bool isBinaryOperator(char c); - bool isUnaryOperator(char c, const Lexem* prevLexem); + bool isUnaryOperator(char c, bool lastWasOperatorOrBracket); + int getOperatorPriority(char c); + std::string formatError(const std::string& expression, size_t position, const std::string& message); + double (*getFunctionByName(const std::string& name))(double); } From 99947460f3c3d6fef5ea12ebdee72d620257cf8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 16:09:26 +0300 Subject: [PATCH 209/275] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/functions.cpp | 0 lib_calculator_app/functions.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 lib_calculator_app/functions.cpp delete mode 100644 lib_calculator_app/functions.h diff --git a/lib_calculator_app/functions.cpp b/lib_calculator_app/functions.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/lib_calculator_app/functions.h b/lib_calculator_app/functions.h deleted file mode 100644 index e69de29b..00000000 From 4b0af434172137da2daa7605b4fefd65e4abf21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 16:50:53 +0300 Subject: [PATCH 210/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Exp?= =?UTF-8?q?ression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.h | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index 3d37d2a4..79d34926 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -1,9 +1,44 @@ -#include "../lib_vector/vector.h" +#pragma once +#include "../lib_list/List.h" #include "lexem.h" +#include +#include class Expression { private: + std::string _expression; + size_t _expression_id; + std::map _variables_map; List _lexems; + List _polish_record; + void build_polish_notation(); public: - Expression(std::string expression); - Expression(const List& list); -}; \ No newline at end of file + Expression(size_t id, std::string expression); + Expression(size_t id, const List& list); + // Геттеры + size_t get_id() const { + return _expression_id; + } + const std::string& get_expression() const { + return _expression; + } + const List& get_lexems() const { + return _lexems; + } + const List& get_polish_record() const { + return _polish_record; + } + const std::map& get_variables() const { + return _variables_map; + } + void set_variable(const std::string& name, double value); + double get_variable(const std::string& name) const; + bool has_variable(const std::string& name) const; + void clear_variables() { _variables_map.clear(); } + + double calculate(); + + void set_variables(); + void validate_variables() const; + void print_variables() const; + void extract_variables(); +}; From f8a32eb3fb0687ac921023400ec2266453762b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 19:17:13 +0300 Subject: [PATCH 211/275] =?UTF-8?q?=D0=9E=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20Expression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index 79d34926..d7da8e1f 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -1,8 +1,11 @@ #pragma once #include "../lib_list/List.h" #include "lexem.h" +#include "parser.h" #include +#include #include +#include class Expression { private: std::string _expression; @@ -14,26 +17,28 @@ class Expression { public: Expression(size_t id, std::string expression); Expression(size_t id, const List& list); - // Геттеры - size_t get_id() const { - return _expression_id; - } + const std::string& get_expression() const { return _expression; } + size_t get_id() const { + return _expression_id; + } + const std::map& get_variables() const { + return _variables_map; + } const List& get_lexems() const { return _lexems; } const List& get_polish_record() const { return _polish_record; } - const std::map& get_variables() const { - return _variables_map; - } void set_variable(const std::string& name, double value); double get_variable(const std::string& name) const; bool has_variable(const std::string& name) const; - void clear_variables() { _variables_map.clear(); } + void clear_variables() { + _variables_map.clear(); + } double calculate(); From f0e05aed7ee85981e20c980f0b7478f70ce42fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 9 Dec 2025 19:17:58 +0300 Subject: [PATCH 212/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82?= =?UTF-8?q?=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib_calculator_app/expression.cpp b/lib_calculator_app/expression.cpp index e69de29b..32b76397 100644 --- a/lib_calculator_app/expression.cpp +++ b/lib_calculator_app/expression.cpp @@ -0,0 +1,27 @@ +#include "expression.h" +Expression::Expression(size_t id, std::string expression) : + _expression_id(id), _expression(expression) { + try { + _lexems = Parser::parse(_expression); + build_polish_notation(); + extract_variables(); + } + catch (const std::exception& ex) { + throw std::logic_error( + "Expression ID: " + std::to_string(id) + ": " + ex.what() + ); + } +} +Expression::Expression(size_t id, const List& list) : + _expression_id(id), _lexems(list), + _expression("Constructed by lexems") { + try { + build_polish_notation(); + extract_variables(); + } + catch (const std::exception& ex) { + throw std::logic_error( + "Expression ID: " + std::to_string(id) + ": " + ex.what() + ); + } +} From c84bde4a539945dde17a6c940413afe9e4b84ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 12 Dec 2025 18:05:53 +0300 Subject: [PATCH 213/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=B2=D0=BE=D0=B9=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B2?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/lexem.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h index 1e572a43..0dd9b69c 100644 --- a/lib_calculator_app/lexem.h +++ b/lib_calculator_app/lexem.h @@ -1,5 +1,8 @@ +#ifndef LEXEM_H +#define LEXEM_H #include "../lib_list/List.h" #include +#include enum TypeLexem { Constant, OpenBracket, @@ -45,3 +48,4 @@ struct Lexem { return function; } }; +#endif \ No newline at end of file From e67f2a86da6e388e1294f16146c294d608dd6125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 12 Dec 2025 18:16:26 +0300 Subject: [PATCH 214/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=BF=D0=BE=20=D1=83=D0=BC=D0=BE=D0=BB?= =?UTF-8?q?=D1=87=D0=B0=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/lexem.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h index 0dd9b69c..9d6fe751 100644 --- a/lib_calculator_app/lexem.h +++ b/lib_calculator_app/lexem.h @@ -19,7 +19,8 @@ struct Lexem { double value; int priority; double (*function)(double); - + Lexem() : name(""), type(Constant), value(0.0), + priority(-1), function(nullptr) {} Lexem(std::string _name, TypeLexem _type, double _value = DBL_MAX, int _priority = -1, double(*_function)(double) = nullptr): name(_name), type(_type), value(_value), From 7dc56f297465981aab81da719535be237c519eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 12 Dec 2025 18:20:01 +0300 Subject: [PATCH 215/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D1=81=D1=82=D1=80=D0=BE=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=9E=D0=9F=D0=97=20=D0=B8=20=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=B2=D1=81=D0=BF?= =?UTF-8?q?=D0=BE=D0=BC=D0=BE=D0=B3=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.cpp | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib_calculator_app/expression.cpp b/lib_calculator_app/expression.cpp index 32b76397..7e379ac9 100644 --- a/lib_calculator_app/expression.cpp +++ b/lib_calculator_app/expression.cpp @@ -25,3 +25,68 @@ Expression::Expression(size_t id, const List& list) : ); } } +Expression::Expression(const Expression& other) + : _expression(other._expression), + _expression_id(other._expression_id), + _variables_map(other._variables_map), + _lexems(other._lexems), + _polish_record(other._polish_record) { +} +bool Expression::isOperatorOrFunction(const Lexem& lexem) { + return lexem.getType() == Operator || lexem.getType() == Function; +} +bool Expression::shouldPopFromStack(const Lexem& stackTop, const Lexem& current) { + return stackTop.getType() != OpenBracket && + isOperatorOrFunction(stackTop) && + stackTop.getPriority() >= current.getPriority(); +} +void Expression::build_polish_notation() { + List output; + Stack stack(_lexems.get_size()); + for (auto it = _lexems.begin(); it != _lexems.end(); ++it) { + Lexem current = *it; + switch (current.getType()) { + case Constant: + case Variable: + output.push_back(current); + break; + case Function: + case OpenBracket: + stack.push(current); + break; + case Operator: { + while (!stack.is_empty() && shouldPopFromStack(stack.top(), current)) { + output.push_back(stack.top()); + stack.pop(); + } + stack.push(current); + break; + } + case CloseBracket: { + while (!stack.is_empty() && stack.top().getType() != OpenBracket) { + output.push_back(stack.top()); + stack.pop(); + } + if (stack.is_empty()) { + throw std::logic_error("Unmatched ')'"); + } + stack.pop(); + if (!stack.is_empty() && stack.top().getType() == Function) { + output.push_back(stack.top()); + stack.pop(); + } + break; + } + default: + throw std::logic_error("Unknown token type"); + } + } + while (!stack.is_empty()) { + if (stack.top().getType() == OpenBracket) { + throw std::logic_error("Unmatched '('"); + } + output.push_back(stack.top()); + stack.pop(); + } + _polish_record = output; +} From 1fc3f1a9bf5cdf3f467847594c67d088a0471ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 12 Dec 2025 18:20:57 +0300 Subject: [PATCH 216/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.h | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index d7da8e1f..e086ff74 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -1,25 +1,28 @@ #pragma once +#include "lexem.h" #include "../lib_list/List.h" -#include "lexem.h" -#include "parser.h" +#include "../lib_stack/stack.h" #include -#include #include #include +#include "parser.h" +#include class Expression { private: std::string _expression; size_t _expression_id; - std::map _variables_map; + std::map _variables_map; List _lexems; List _polish_record; void build_polish_notation(); + bool isOperatorOrFunction(const Lexem& lexem); + bool shouldPopFromStack(const Lexem& stackTop, const Lexem& current); public: Expression(size_t id, std::string expression); - Expression(size_t id, const List& list); - - const std::string& get_expression() const { - return _expression; + Expression(size_t id, const List& list); + Expression(const Expression& other); + const std::string& get_expression() const { + return _expression; } size_t get_id() const { return _expression_id; @@ -27,23 +30,21 @@ class Expression { const std::map& get_variables() const { return _variables_map; } - const List& get_lexems() const { - return _lexems; + const List& get_lexems() const { + return _lexems; } - const List& get_polish_record() const { - return _polish_record; + const List& get_polish_record() const { + return _polish_record; } void set_variable(const std::string& name, double value); double get_variable(const std::string& name) const; bool has_variable(const std::string& name) const; - void clear_variables() { - _variables_map.clear(); + void clear_variables() { + _variables_map.clear(); } - double calculate(); - void set_variables(); void validate_variables() const; void print_variables() const; void extract_variables(); -}; +}; \ No newline at end of file From ad0200386e5a0ab4edd6af6b301e2a90e5506deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 12 Dec 2025 18:21:37 +0300 Subject: [PATCH 217/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.h | 1 - 1 file changed, 1 deletion(-) diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index b219329a..7164df44 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -3,7 +3,6 @@ #include "lexem.h" #include #include -#include "parser.h" #include #include #include From c5ebe47ad24a053b1e039282df1bdf665a043687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 13 Dec 2025 19:58:26 +0300 Subject: [PATCH 218/275] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Expressio?= =?UTF-8?q?n,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B,=20=D1=81=D0=B2=D1=8F?= =?UTF-8?q?=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.cpp | 83 +++++++++++++++++++++++++++---- lib_calculator_app/expression.h | 26 +++------- 2 files changed, 81 insertions(+), 28 deletions(-) diff --git a/lib_calculator_app/expression.cpp b/lib_calculator_app/expression.cpp index 7e379ac9..99d92c3a 100644 --- a/lib_calculator_app/expression.cpp +++ b/lib_calculator_app/expression.cpp @@ -4,7 +4,6 @@ Expression::Expression(size_t id, std::string expression) : try { _lexems = Parser::parse(_expression); build_polish_notation(); - extract_variables(); } catch (const std::exception& ex) { throw std::logic_error( @@ -17,7 +16,6 @@ Expression::Expression(size_t id, const List& list) : _expression("Constructed by lexems") { try { build_polish_notation(); - extract_variables(); } catch (const std::exception& ex) { throw std::logic_error( @@ -25,13 +23,6 @@ Expression::Expression(size_t id, const List& list) : ); } } -Expression::Expression(const Expression& other) - : _expression(other._expression), - _expression_id(other._expression_id), - _variables_map(other._variables_map), - _lexems(other._lexems), - _polish_record(other._polish_record) { -} bool Expression::isOperatorOrFunction(const Lexem& lexem) { return lexem.getType() == Operator || lexem.getType() == Function; } @@ -90,3 +81,77 @@ void Expression::build_polish_notation() { } _polish_record = output; } +double Expression::calculate() { + + + // Алгоритм: + // 1. Проверить, что все переменные заданы + // 2. Пройти по _polish_record + // 3. Использовать стек для вычислений + // 4. Вернуть результат + + + + +} +void Expression::print_variables() const { + std::vector all_vars; + for (const auto& lexem : _lexems) { + if (lexem.type == TypeLexem::Variable) { + if (std::find(all_vars.begin(), all_vars.end(), lexem.name) == all_vars.end()) { + all_vars.push_back(lexem.name); + } + } + } + if (all_vars.empty()) { + std::cout << "Нет переменных\n"; + return; + } + for (size_t i = 0; i < all_vars.size(); ++i) { + if (i > 0) { + std::cout << ", "; + } + if (has_variable(all_vars[i])) { + std::cout << all_vars[i] << " = " << _variables_map.at(all_vars[i]); + } else { + std::cout << all_vars[i] << " = ?"; + } + } +} +bool Expression::has_variable(const std::string& name) const { + return _variables_map.find(name) != _variables_map.end(); +} +void Expression::set_variable(const std::string& name, double value) { + _variables_map[name] = value; +} +void Expression::set_variables() { + std::string var_name; + double value; + // Показываем, какие переменные есть + std::cout << "Доступные переменные: "; + print_variables(); + std::cout << std::endl; + std::cout << "Введите имя переменной (или 'stop' для выхода): "; + std::cin >> var_name; + while (var_name != "stop") { + // Проверяем, есть ли такая переменная + bool found = false; + for (const auto& lexem : _lexems) { + if (lexem.getType() == Variable && lexem.getName() == var_name) { + found = true; + break; + } + } + if (!found) { + std::cout << "Ошибка: переменная '" << var_name + << "' не найдена в выражении\n"; + } + else { + std::cout << "Введите значение для " << var_name << ": "; + std::cin >> value; + set_variable(var_name, value); + } + std::cout << "Введите имя переменной (или 'stop' для выхода): "; + std::cin >> var_name; + } +} diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index e086ff74..17760aa9 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -14,37 +14,25 @@ class Expression { std::map _variables_map; List _lexems; List _polish_record; - void build_polish_notation(); - bool isOperatorOrFunction(const Lexem& lexem); bool shouldPopFromStack(const Lexem& stackTop, const Lexem& current); + bool isOperatorOrFunction(const Lexem& lexem); + void build_polish_notation(); + + bool has_variable(const std::string& name) const; public: Expression(size_t id, std::string expression); Expression(size_t id, const List& list); - Expression(const Expression& other); const std::string& get_expression() const { return _expression; } size_t get_id() const { return _expression_id; } - const std::map& get_variables() const { - return _variables_map; - } - const List& get_lexems() const { - return _lexems; - } - const List& get_polish_record() const { - return _polish_record; - } - void set_variable(const std::string& name, double value); - double get_variable(const std::string& name) const; - bool has_variable(const std::string& name) const; void clear_variables() { _variables_map.clear(); } - double calculate(); + void set_variable(const std::string& name, double value); void set_variables(); - void validate_variables() const; + double calculate(); void print_variables() const; - void extract_variables(); -}; \ No newline at end of file +}; From 822e3ca264ac88c1301cbad8aabf6cf55f64f5e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 13 Dec 2025 20:01:10 +0300 Subject: [PATCH 219/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20=D0=B7=D0=B0?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index c9d45616..3107b63b 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -58,8 +58,8 @@ List Parser::parse(std::string expression) { lexems.push_back(lexem); lastWasOperatorOrBracketOrFunction = true; } - i++; // функции, переменные Рё модуль - } else if (isLetter(c) || c == '_') { //это или функция или переменная + i++; + } else if (isLetter(c) || c == '_') { //function/variable std::string word = ""; word += c; i++; @@ -88,19 +88,15 @@ List Parser::parse(std::string expression) { Lexem lexem(word, TypeLexem::Variable); lexems.push_back(lexem); lastWasOperatorOrBracketOrFunction = false; - } else { size_t errorPos = i - word.length(); std::string errorMsg = formatError(expression, errorPos, "Invalid variable/function format: '" + word + "'"); throw std::logic_error(errorMsg); } - } else if (c == '|') { //модуль - Lexem lexem(std::string(1,c), TypeLexem::OpenAbs); - - lexems.push_back(lexem); - lastWasOperatorOrBracketOrFunction = false; - i++; + } + else if (c == '|') { + throw std::logic_error("Module || not yet implemented");// TODO: implement module support } else { size_t errorPos = i; @@ -108,10 +104,7 @@ List Parser::parse(std::string expression) { "Unknown character: '" + std::string(1, c) + "'"); throw std::logic_error(errorMsg); } - - - } // цикл while i < expr. lenght - + } // cycle while i < expr. lenght if (lastWasOperatorOrBracketOrFunction && !(lexems.get_size() == 0) ) { size_t errorPos = expression.length() - 1; std::string errorMsg = formatError(expression, errorPos, @@ -229,5 +222,8 @@ double (*Parser::getFunctionByName(const std::string& name))(double) { if (name == "tg") { return Parser::getTg; } + if (name == "abs") { + return Parser::getAbs; + } return nullptr; } From 760e29c42ea0da931f5b012d14b1ad694fc6cdaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 13:27:18 +0300 Subject: [PATCH 220/275] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_stack/stack.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib_stack/stack.h b/lib_stack/stack.h index b70bd318..1a50b9d5 100644 --- a/lib_stack/stack.h +++ b/lib_stack/stack.h @@ -7,6 +7,7 @@ class Stack { public: Stack(size_t size); Stack(const Stack& other); + size_t size(); const Tvector& getData() const; Stack& operator=(const Stack& other); bool operator ==(const Stack& other) const; @@ -19,6 +20,10 @@ class Stack { void clear() noexcept; }; template +size_t Stack::size() { + return _data.get_size(); +} +template Stack::Stack(size_t size): _data(size) { } template From ac6ef2869821fe06f8793c5fe513bebcb1b7a311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 13:36:12 +0300 Subject: [PATCH 221/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B2=D1=81=D0=BF=D0=BE=D0=BC=D0=BE=D0=B3=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20getOp()=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=8B?= =?UTF-8?q?=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=D1=8B?= =?UTF-8?q?=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/lexem.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h index 9d6fe751..473c7ce9 100644 --- a/lib_calculator_app/lexem.h +++ b/lib_calculator_app/lexem.h @@ -48,5 +48,14 @@ struct Lexem { double (*getFunction() const)(double) { return function; } + char getOp() const { + if (type != Operator && type != UnOperator) { + throw std::logic_error("Лексема не является оператором"); + } + if (name.empty()) { + return '\0'; + } + return name[0]; + } }; -#endif \ No newline at end of file +#endif From e320bdb02694ecf7e8f4e98ef1fc5901e5cb0b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:04:51 +0300 Subject: [PATCH 222/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Expressio?= =?UTF-8?q?n,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B2=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B8?= =?UTF-8?q?=D0=B8=20=D1=82.=D0=B4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.cpp | 128 ++++++++++++++++++++++++------ lib_calculator_app/expression.h | 1 + 2 files changed, 106 insertions(+), 23 deletions(-) diff --git a/lib_calculator_app/expression.cpp b/lib_calculator_app/expression.cpp index 99d92c3a..1020906e 100644 --- a/lib_calculator_app/expression.cpp +++ b/lib_calculator_app/expression.cpp @@ -59,7 +59,7 @@ void Expression::build_polish_notation() { stack.pop(); } if (stack.is_empty()) { - throw std::logic_error("Unmatched ')'"); + throw std::logic_error("Непарная ')'"); } stack.pop(); if (!stack.is_empty() && stack.top().getType() == Function) { @@ -69,34 +69,119 @@ void Expression::build_polish_notation() { break; } default: - throw std::logic_error("Unknown token type"); + throw std::logic_error("Неизвестный тип лексемы"); } } while (!stack.is_empty()) { if (stack.top().getType() == OpenBracket) { - throw std::logic_error("Unmatched '('"); + throw std::logic_error("Непарная '('"); } output.push_back(stack.top()); stack.pop(); } _polish_record = output; } +double Expression::applyOperation(double a, double b, char op) const { + switch (op) { + case '+': + return a + b; + case '-': + return a - b; + case '*': + return a * b; + case '/': + if (fabs(b) < 1e-12) { //1e-12 ~ 0.000000000001 + throw std::logic_error("Деление на ноль!"); + } + return a / b; + case '^': + return pow(a, b); + default: + throw std::logic_error(std::string("Неизвестный оператор: ") + op); + } +} double Expression::calculate() { + for (Lexem lexem : _lexems) { + if (lexem.getType() == Variable && !has_variable(lexem.getName())) { + throw std::logic_error( + "Невозможно вычислить: переменная '" + lexem.getName() + "' не задана" + ); + } + } + Stack stack(_polish_record.get_size()); + for (Lexem lexem : _polish_record) { + switch (lexem.getType()) { + case Constant: + stack.push(lexem.getValue()); + break; + case Variable: + stack.push(_variables_map.at(lexem.getName())); + break; + case Operator: { + if (stack.size() < 2) { + throw std::logic_error("Недостаточно операндов для оператора '" + + std::string(1, lexem.getOp()) + "'"); + } + double b = stack.top(); + stack.pop(); + double a = stack.top(); + stack.pop(); + double result; + try { + result = applyOperation(a, b, lexem.getOp()); + } + catch (const std::exception& ex) { + throw std::logic_error( + "Ошибка при выполнении операции: " + std::string(ex.what()) + ); + } + stack.push(result); + break; + } + case Function: { + if (stack.size() < 1) { + throw std::logic_error("Недостаточно аргументов для функции '" + + lexem.getName() + "'"); + } + double arg = stack.top(); + stack.pop(); + double result; + std::string funcName = lexem.getName(); + if (funcName == "sin") { + result = sin(arg); + } + else if (funcName == "cos") { + result = cos(arg); + } + else if (funcName == "tg") { + result = tan(arg); + if (std::isinf(result)) { + throw std::logic_error("Тангенс не определен для данного угла"); + } + } + else if (funcName == "abs") { + result = fabs(arg); + } + else { + throw std::logic_error("Неизвестная функция: " + funcName); + } + stack.push(result); + break; + } - // Алгоритм: - // 1. Проверить, что все переменные заданы - // 2. Пройти по _polish_record - // 3. Использовать стек для вычислений - // 4. Вернуть результат - - - - + default: + throw std::logic_error("Неподдерживаемый тип лексемы при вычислении"); + } + } + if (stack.size() != 1) { + throw std::logic_error("Ошибка вычисления: некорректное выражение"); + } + return stack.top(); } void Expression::print_variables() const { std::vector all_vars; - for (const auto& lexem : _lexems) { + for (Lexem lexem : _lexems) { if (lexem.type == TypeLexem::Variable) { if (std::find(all_vars.begin(), all_vars.end(), lexem.name) == all_vars.end()) { all_vars.push_back(lexem.name); @@ -127,31 +212,28 @@ void Expression::set_variable(const std::string& name, double value) { void Expression::set_variables() { std::string var_name; double value; - // Показываем, какие переменные есть std::cout << "Доступные переменные: "; print_variables(); - std::cout << std::endl; - std::cout << "Введите имя переменной (или 'stop' для выхода): "; + std::cout << "\n"; + std::cout << "Введите имя переменной (или stop для выхода): "; std::cin >> var_name; while (var_name != "stop") { - // Проверяем, есть ли такая переменная bool found = false; - for (const auto& lexem : _lexems) { + for (Lexem lexem : _lexems) { if (lexem.getType() == Variable && lexem.getName() == var_name) { found = true; break; } - } - if (!found) { + } + if (found == false) { std::cout << "Ошибка: переменная '" << var_name << "' не найдена в выражении\n"; - } - else { + } else { std::cout << "Введите значение для " << var_name << ": "; std::cin >> value; set_variable(var_name, value); } - std::cout << "Введите имя переменной (или 'stop' для выхода): "; + std::cout << "Введите имя переменной (или stop для выхода): "; std::cin >> var_name; } } diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index 17760aa9..84847bcf 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -34,5 +34,6 @@ class Expression { void set_variable(const std::string& name, double value); void set_variables(); double calculate(); + double applyOperation(double a, double b, char op) const; void print_variables() const; }; From e9837605d4bf4a5e54906198092080c2c8ebeaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:05:42 +0300 Subject: [PATCH 223/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/lexem.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib_calculator_app/lexem.h b/lib_calculator_app/lexem.h index 473c7ce9..22c93617 100644 --- a/lib_calculator_app/lexem.h +++ b/lib_calculator_app/lexem.h @@ -10,7 +10,6 @@ enum TypeLexem { Variable, Function, Operator, - UnOperator, OpenAbs, CloseAbs }; struct Lexem { @@ -49,7 +48,7 @@ struct Lexem { return function; } char getOp() const { - if (type != Operator && type != UnOperator) { + if (type != Operator) { throw std::logic_error("Лексема не является оператором"); } if (name.empty()) { From f025a702baedc774307c299be5def12633087bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:19:03 +0300 Subject: [PATCH 224/275] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF?= =?UTF-8?q?=D0=B0=D1=80=D1=81=D0=B8=D0=BD=D0=B3=D0=B0=20=D0=B2=D1=8B=D1=80?= =?UTF-8?q?=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 26 ++++++++++++++++++++++++-- lib_calculator_app/parser.h | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index 3107b63b..6d94e81f 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -1,10 +1,31 @@ п»ї#include "parser.h" +void Parser::handleAbsBracket(char c, List& lexems, + bool& lastWasOperatorOrBracketOrFunction, + bool& absOpened) { + if (c != '|') return; + + if (!absOpened) { + // Открывающая | - преобразуем РІ abs( + lexems.push_back(Lexem("abs", TypeLexem::Function, DBL_MAX, 4, + getFunctionByName("abs"))); + lexems.push_back(Lexem("(", TypeLexem::OpenBracket)); + absOpened = true; + lastWasOperatorOrBracketOrFunction = true; + } + else { + // Закрывающая | - преобразуем РІ ) + lexems.push_back(Lexem(")", TypeLexem::CloseBracket)); + absOpened = false; + lastWasOperatorOrBracketOrFunction = false; + } +} List Parser::parse(std::string expression) { if (expression.empty()) { throw std::logic_error("Expression is empty"); } List lexems; bool lastWasOperatorOrBracketOrFunction = true; + bool absOpened = false; size_t i = 0; while (i < expression.length()) { char c = expression[i]; @@ -96,7 +117,8 @@ List Parser::parse(std::string expression) { } } else if (c == '|') { - throw std::logic_error("Module || not yet implemented");// TODO: implement module support + handleAbsBracket(c, lexems, lastWasOperatorOrBracketOrFunction, absOpened); + i++; } else { size_t errorPos = i; @@ -161,7 +183,7 @@ bool Parser::isValidVariableName(const std::string& name) { } bool Parser::isFunction(const std::string& name) { const std::string functions[] = { - "sin", "cos", "tg" + "sin", "cos", "tg", "abs" }; for (const auto& function : functions) { if (name == function) { diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index 7164df44..9eb3cdac 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -25,4 +25,7 @@ namespace Parser { int getOperatorPriority(char c); std::string formatError(const std::string& expression, size_t position, const std::string& message); double (*getFunctionByName(const std::string& name))(double); + void Parser::handleAbsBracket(char c, List& lexems, + bool& lastWasOperatorOrBracketOrFunction, + bool& absOpened); } From c785daabe55381d0741df9eb7278d15febd8e65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:20:49 +0300 Subject: [PATCH 225/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index 6d94e81f..19c126d1 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -5,7 +5,7 @@ void Parser::handleAbsBracket(char c, List& lexems, if (c != '|') return; if (!absOpened) { - // Открывающая | - преобразуем РІ abs( + // Opened - abs( lexems.push_back(Lexem("abs", TypeLexem::Function, DBL_MAX, 4, getFunctionByName("abs"))); lexems.push_back(Lexem("(", TypeLexem::OpenBracket)); @@ -13,7 +13,7 @@ void Parser::handleAbsBracket(char c, List& lexems, lastWasOperatorOrBracketOrFunction = true; } else { - // Закрывающая | - преобразуем РІ ) + // closed - ) lexems.push_back(Lexem(")", TypeLexem::CloseBracket)); absOpened = false; lastWasOperatorOrBracketOrFunction = false; From 2870f196e673286e8a62f2674e06e335b9a40156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:27:48 +0300 Subject: [PATCH 226/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index 19c126d1..ac25c17c 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -2,9 +2,7 @@ void Parser::handleAbsBracket(char c, List& lexems, bool& lastWasOperatorOrBracketOrFunction, bool& absOpened) { - if (c != '|') return; - - if (!absOpened) { + if (!absOpened) { // Opened - abs( lexems.push_back(Lexem("abs", TypeLexem::Function, DBL_MAX, 4, getFunctionByName("abs"))); From cd2ebf7eb7e70a4cd900bcef8ce0d0a086b65ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:35:54 +0300 Subject: [PATCH 227/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.h | 3 +++ lib_calculator_app/parser.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index 84847bcf..9aa34f68 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -31,6 +31,9 @@ class Expression { void clear_variables() { _variables_map.clear(); } + std::map variables_map() { + return _variables_map; + } void set_variable(const std::string& name, double value); void set_variables(); double calculate(); diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index 9eb3cdac..dad37827 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -25,7 +25,7 @@ namespace Parser { int getOperatorPriority(char c); std::string formatError(const std::string& expression, size_t position, const std::string& message); double (*getFunctionByName(const std::string& name))(double); - void Parser::handleAbsBracket(char c, List& lexems, + void handleAbsBracket(char c, List& lexems, bool& lastWasOperatorOrBracketOrFunction, bool& absOpened); } From aec3350816af1e5e4367b61ad65723f663c082e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 14:49:31 +0300 Subject: [PATCH 228/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=BF=D0=BE=20=D1=83=D0=BC=D0=BE=D0=BB?= =?UTF-8?q?=D1=87=D0=B0=D0=BD=D0=B8=D1=8E=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE=D0=B9=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=B8=D0=BC=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B8=20=D1=81=20Tvector=20=D0=BF=D1=80=D0=B8=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=B2=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B0=20=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index 9aa34f68..40d85b91 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -20,6 +20,7 @@ class Expression { bool has_variable(const std::string& name) const; public: + Expression() : _expression_id(0), _expression("") {} Expression(size_t id, std::string expression); Expression(size_t id, const List& list); const std::string& get_expression() const { From c8d892e8e5955fbef3e7356876463c2bb0f69910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 20:51:42 +0300 Subject: [PATCH 229/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/expression.cpp | 16 ++-------------- lib_calculator_app/expression.h | 3 +-- lib_calculator_app/parser.cpp | 28 ++++++++++++++++++---------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/lib_calculator_app/expression.cpp b/lib_calculator_app/expression.cpp index 1020906e..24577776 100644 --- a/lib_calculator_app/expression.cpp +++ b/lib_calculator_app/expression.cpp @@ -11,18 +11,6 @@ Expression::Expression(size_t id, std::string expression) : ); } } -Expression::Expression(size_t id, const List& list) : - _expression_id(id), _lexems(list), - _expression("Constructed by lexems") { - try { - build_polish_notation(); - } - catch (const std::exception& ex) { - throw std::logic_error( - "Expression ID: " + std::to_string(id) + ": " + ex.what() - ); - } -} bool Expression::isOperatorOrFunction(const Lexem& lexem) { return lexem.getType() == Operator || lexem.getType() == Function; } @@ -33,7 +21,7 @@ bool Expression::shouldPopFromStack(const Lexem& stackTop, const Lexem& current) } void Expression::build_polish_notation() { List output; - Stack stack(_lexems.get_size()); + Stack stack(_polish_record.get_size()); for (auto it = _lexems.begin(); it != _lexems.end(); ++it) { Lexem current = *it; switch (current.getType()) { @@ -108,7 +96,7 @@ double Expression::calculate() { ); } } - Stack stack(_polish_record.get_size()); + Stack stack(0); for (Lexem lexem : _polish_record) { switch (lexem.getType()) { case Constant: diff --git a/lib_calculator_app/expression.h b/lib_calculator_app/expression.h index 40d85b91..3c775414 100644 --- a/lib_calculator_app/expression.h +++ b/lib_calculator_app/expression.h @@ -22,7 +22,6 @@ class Expression { public: Expression() : _expression_id(0), _expression("") {} Expression(size_t id, std::string expression); - Expression(size_t id, const List& list); const std::string& get_expression() const { return _expression; } @@ -32,7 +31,7 @@ class Expression { void clear_variables() { _variables_map.clear(); } - std::map variables_map() { + const std::map& variables_map() const { return _variables_map; } void set_variable(const std::string& name, double value); diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index ac25c17c..2f697810 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -22,7 +22,7 @@ List Parser::parse(std::string expression) { throw std::logic_error("Expression is empty"); } List lexems; - bool lastWasOperatorOrBracketOrFunction = true; + bool lastWasOperatorOrBracketOrFunction = false; bool absOpened = false; size_t i = 0; while (i < expression.length()) { @@ -63,22 +63,30 @@ List Parser::parse(std::string expression) { lexems.push_back(lexem); lastWasOperatorOrBracketOrFunction = false; i++; - } else if (isBinaryOperator(c)) { + } + else if (isBinaryOperator(c)) { bool isUnary = isUnaryOperator(c, lastWasOperatorOrBracketOrFunction); - if (isUnary && c != '+') { - Lexem lexem1(0.0); - Lexem lexem2("-", TypeLexem::Operator, DBL_MAX, 1); - lexems.push_back(lexem1); - lexems.push_back(lexem2); - lastWasOperatorOrBracketOrFunction = true; - } else { + if (isUnary) { + if (c == '-') { + Lexem lexem1(0.0); + Lexem lexem2("-", TypeLexem::Operator, DBL_MAX, 1); + lexems.push_back(lexem1); + lexems.push_back(lexem2); + lastWasOperatorOrBracketOrFunction = true; + } + else if (c == '+') { + lastWasOperatorOrBracketOrFunction = true; + } + } + else { int priority = getOperatorPriority(c); Lexem lexem(std::string(1, c), TypeLexem::Operator, DBL_MAX, priority); lexems.push_back(lexem); lastWasOperatorOrBracketOrFunction = true; } i++; - } else if (isLetter(c) || c == '_') { //function/variable + } + else if (isLetter(c) || c == '_') { //function/variable std::string word = ""; word += c; i++; From 47e28ddbaf41952c6509410c90a6b1469b47f96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 21:50:53 +0300 Subject: [PATCH 230/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB=D1=8F?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D0=B2=D1=8B=D1=80?= =?UTF-8?q?=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 255 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 233 insertions(+), 22 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 3e651e30..d0905094 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,38 +1,40 @@ -// Copyright 2024 Julia Zabytia +// Copyright 2025 Julia Zabytia //#define EASY_EXAMPLE //#define CIRCLES_AND_SPHERES -#define MATRIX +#define CALCULATOR #ifdef EASY_EXAMPLE #include #include #include "../lib_easy_example/easy_example.h" int main() { - int a, b; - float result; + int a, b; + float result; - a = 1; b = 4; + a = 1; b = 4; - try { - result = division(a, b); - std::cout << a << " / " << b << " = " - << std::setprecision(2) << result << std::endl; - } catch (std::exception err) { - std::cerr << err.what() << std::endl; - } + try { + result = division(a, b); + std::cout << a << " / " << b << " = " + << std::setprecision(2) << result << std::endl; + } + catch (std::exception err) { + std::cerr << err.what() << std::endl; + } - a = 1; b = 0; + a = 1; b = 0; - try { - result = division(a, b); - std::cout << a << " / " << b << " = " - << std::setprecision(2) << result << std::endl; - } catch (std::exception err) { - std::cerr << err.what() << std::endl; - } + try { + result = division(a, b); + std::cout << a << " / " << b << " = " + << std::setprecision(2) << result << std::endl; + } + catch (std::exception err) { + std::cerr << err.what() << std::endl; + } - return 0; + return 0; } #endif @@ -401,7 +403,7 @@ int main() { default: { std::cout << "Invalid input! Please try again\n"; break; - } + } } } std::cout << "\nPress Enter to continue..."; @@ -413,3 +415,212 @@ int main() { return 0; } #endif +#ifdef CALCULATOR +#include "../lib_calculator_app/expression.h" +#include "../lib_vector/vector.h" +#include +#include +#include +#include +void print_table_header() { + std::cout << "+" << std::string(80, '-') << "+" << std::endl; + std::cout << "| " << std::left << std::setw(4) << "ID" + << "| " << std::setw(40) << "ВЫРАЖЕНИЯ" + << "| " << std::setw(30) << "ЗНАЧЕНИЯ ПЕРЕМЕННЫХ" + << " |" << std::endl; + std::cout << "+" << std::string(80, '-') << "+" << std::endl; +} +void show_all_expressions(Tvector& expressions) { + if (expressions.get_size() == 0) { + std::cout << "| " << std::setw(78) << "Нет выражений" << " |" << std::endl; + } + else { + for (size_t i = 0; i < expressions.get_size(); ++i) { + std::cout << "| " << std::setw(4) << expressions[i].get_id() + << "| " << std::setw(40) << expressions[i].get_expression() + << "| "; + std::vector vars; + for (auto& pair : expressions[i].variables_map()) { + std::stringstream ss; + ss << std::fixed << std::setprecision(3) << pair.second; + vars.push_back(pair.first + "=" + ss.str()); + } + if (vars.empty()) { + std::cout << std::setw(30) << "Нет переменных"; + } + else { + std::string vars_str; + for (size_t j = 0; j < vars.size(); ++j) { + if (j > 0) vars_str += ", "; + vars_str += vars[j]; + } + std::cout << std::setw(30) << vars_str.substr(0, 30); + } + std::cout << " |" << std::endl; + } + } + std::cout << "+" << std::string(80, '-') << "+" << std::endl; +} +void create_new_expression(Tvector& expressions, int& next_id) { + system("cls"); + std::cout << "Введите новое выражение: "; + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::string str; + std::getline(std::cin, str); + if (str.empty()) { + std::cout << "Ошибка: выражение не может быть пустым!\n"; + system("pause"); + return; + } + try { + Expression new_expression(next_id, str); + expressions.push_back(new_expression); + next_id++; + + std::cout << "Выражение успешно создано!\n"; + system("pause"); + } + catch (const std::exception& ex) { + std::cout << "Ошибка создания выражения: " << ex.what() << "\n"; + system("pause"); + } +} +void delete_expression(Tvector& expressions) { + system("cls"); + if (expressions.get_size() == 0) { + std::cout << "Нет выражений для удаления.\n"; + system("pause"); + return; + } + std::cout << "Список выражений:\n"; + for (size_t i = 0; i < expressions.get_size(); ++i) { + std::cout << i + 1 << ". ID: " << expressions[i].get_id() + << " - " << expressions[i].get_expression() << "\n"; + } + std::cout << "\nВведите номер выражения для удаления\n"; + size_t choice; + std::cin >> choice; + if (choice < 1 || choice > expressions.get_size()) { + std::cout << "Неверный номер\n"; + system("pause"); + return; + } + try { + expressions.erase(choice-1); + std::cout << "Выражение успешно удалено\n"; + system("pause"); + } + catch (const std::exception& ex) { + std::cout << "Ошибка: " << ex.what() << "\n"; + system("pause"); + } +} +void set_variables_for_expression(Tvector& expressions) { + system("cls"); + if (expressions.get_size() == 0) { + std::cout << "Нет выражений для задания переменных.\n"; + system("pause"); + return; + } + std::cout << "Список выражений:\n"; + print_table_header(); + show_all_expressions(expressions); + std::cout << "\nВведите ID выражения для задания переменных: "; + int id; + std::cin >> id; + if (id < 1 || id > static_cast(expressions.get_size())) { + std::cout << "Неверный ID\n"; + system("pause"); + return; + } + try { + expressions[id - 1].set_variables(); + std::cout << "Переменные успешно заданы\n"; + system("pause"); + } + catch (const std::exception& ex) { + std::cout << "Ошибка: " << ex.what() << "\n"; + system("pause"); + } +} +void calculate_expression(Tvector& expressions) { + system("cls"); + if (expressions.get_size() == 0) { + std::cout << "Нет выражений для вычисления\n"; + system("pause"); + return; + } + std::cout << "Список выражений:\n"; + print_table_header(); + show_all_expressions(expressions); + std::cout << "\nВведите ID выражения для вычисления: "; + int id; + std::cin >> id; + if (id < 1 || id > static_cast(expressions.get_size())) { + std::cout << "Неверный ID\n"; + system("pause"); + return; + } + try { + double result = expressions[id - 1].calculate(); + std::cout << "\nРезультат вычисления: " << result << "\n"; + system("pause"); + } + catch (const std::exception& ex) { + std::cout << "Ошибка при вычислении: " << ex.what() << "\n"; + system("pause"); + } +} +int main() { + setlocale(LC_ALL, "Russian"); + Tvector expressions; + int next_id = 1; + while (true) { + system("cls"); + std::cout << "\n" << std::string(50, '=') << std::endl; + std::cout << "КАЛЬКУЛЯТОР АРИФМЕТИЧЕСКИХ ВЫРАЖЕНИЙ" << std::endl; + std::cout << std::string(50, '=') << std::endl; + print_table_header(); + show_all_expressions(expressions); + std::cout << "\nМЕНЮ:" << std::endl; + std::cout << "1. Создать новое выражение" << std::endl; + std::cout << "2. Удалить выражение" << std::endl; + std::cout << "3. Задать переменные" << std::endl; + std::cout << "4. Вычислить значение выражения" << std::endl; + std::cout << "5. Выход" << std::endl; + std::cout << std::string(50, '-') << std::endl; + std::cout << "Ваш выбор: "; + int choice; + bool want_exit = false; + std::cin >> choice; + switch (choice) { + case 1: + create_new_expression(expressions, next_id); + break; + case 2: + delete_expression(expressions); + break; + case 3: + set_variables_for_expression(expressions); + break; + case 4: + calculate_expression(expressions); + break; + case 5: + want_exit = true; + break; + default: + std::cout << "\nНеверный выбор. Попробуйте снова." << std::endl; + std::cout << "\nНажмите Enter для продолжения..."; + std::cin.ignore(); + std::cin.get(); + break; + } + + if (want_exit) { + break; + } + } + return 0; +} +#endif \ No newline at end of file From ee5053174c9a601ece96fefb4918389be22f34f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 21:51:47 +0300 Subject: [PATCH 231/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE=D1=80=D1=80?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=BD=D0=BE=D0=B9=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D1=8B=20=D1=81=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=D0=BC=20=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=B8=D0=B7=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_vector/vector.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib_vector/vector.h b/lib_vector/vector.h index e0592a31..421992ad 100644 --- a/lib_vector/vector.h +++ b/lib_vector/vector.h @@ -641,11 +641,27 @@ if (position >= _size) { throw std::out_of_range("Invalid position"); } - size_t real_pos = get_real_position(position); - _states[real_pos] = State::deleted; + _states[position] = State::deleted; _deleted++; if (_deleted * 100 > _size * MAX_PERCENT_DELETED) { - compact_storage(); + T* new_data = new T[_size - _deleted + RESERVE_MEMORY]; + State* new_states = new State[_size - _deleted + RESERVE_MEMORY]; + size_t new_index = 0; + for (size_t i = 0; i < _size; i++) { + if (_states[i] == State::busy) { + new_data[new_index] = std::move(_data[i]); + new_states[new_index] = State::busy; + new_index++; + } + } + delete[] _data; + delete[] _states; + + _data = new_data; + _states = new_states; + _size = new_index; + _capacity = _size + RESERVE_MEMORY; + _deleted = 0; } } template From 2094bc905d16459c8657fb2ce3aaaeab56ae625e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 22:12:40 +0300 Subject: [PATCH 232/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 21 +++++++++++++++++++++ lib_calculator_app/parser.h | 1 + 2 files changed, 22 insertions(+) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index 2f697810..f4a3afa4 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -17,6 +17,13 @@ void Parser::handleAbsBracket(char c, List& lexems, lastWasOperatorOrBracketOrFunction = false; } } +bool Parser::isValidNumberFormat(const std::string& number) { + int dotCount = 0; + for (char c : number) { + if (c == '.') dotCount++; + } + return dotCount <= 1; +} List Parser::parse(std::string expression) { if (expression.empty()) { throw std::logic_error("Expression is empty"); @@ -40,6 +47,20 @@ List Parser::parse(std::string expression) { number += expression[i]; i++; } + if (!isValidNumberFormat(number)) { + size_t errorPos = i - number.length(); + std::string errorMsg = formatError(expression, errorPos, + "Invalid number format: '" + number + "'"); + throw std::logic_error(errorMsg); + } + if (i < expression.length() && + (isLetter(expression[i]) || expression[i] == '_') && + expression[i] != 'e' && expression[i] != 'E') { // исключаем научную запись + size_t errorPos = i - number.length(); + std::string errorMsg = formatError(expression, errorPos, + "Invalid variable/function format: '" + number + expression[i] + "'"); + throw std::logic_error(errorMsg); + } try { double constant = std::stod(number); Lexem lexem(constant); diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index dad37827..a5c43727 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -22,6 +22,7 @@ namespace Parser { bool isFunction(const std::string& name); bool isBinaryOperator(char c); bool isUnaryOperator(char c, bool lastWasOperatorOrBracket); + bool isValidNumberFormat(const std::string& number); int getOperatorPriority(char c); std::string formatError(const std::string& expression, size_t position, const std::string& message); double (*getFunctionByName(const std::string& name))(double); From 080f40f1d2a90e84013d3d9eb371c8b7890f1db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 22:32:45 +0300 Subject: [PATCH 233/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 27 +++++++++++++++++++++++++++ lib_calculator_app/parser.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index f4a3afa4..056c3450 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -1,4 +1,27 @@ п»ї#include "parser.h" +bool Parser::check_brackets(std::string str) { + Stack stack(str.length()); + stack.clear(); + for (char c : str) { + if (c == '(' || c == '[' || c == '{') { + stack.push(c); + } + else if (c == ')' || c == ']' || c == '}') { + if (stack.is_empty()) { + return false; + } + char top = stack.top(); + stack.pop(); + + if ((c == ')' && top != '(') || + (c == ']' && top != '[') || + (c == '}' && top != '{')) { + return false; + } + } + } + return stack.is_empty(); +} void Parser::handleAbsBracket(char c, List& lexems, bool& lastWasOperatorOrBracketOrFunction, bool& absOpened) { @@ -28,10 +51,14 @@ List Parser::parse(std::string expression) { if (expression.empty()) { throw std::logic_error("Expression is empty"); } + if (check_brackets(expression) == false) { + throw std::logic_error("Mismatched brackets"); + } List lexems; bool lastWasOperatorOrBracketOrFunction = false; bool absOpened = false; size_t i = 0; + while (i < expression.length()) { char c = expression[i]; if (c == ' ') { diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index a5c43727..11a55fca 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -1,5 +1,6 @@ #pragma once #include "../lib_list/List.h" +#include "../lib_stack/stack.h" #include "lexem.h" #include #include @@ -9,6 +10,7 @@ #include namespace Parser { List parse(std::string expression); + bool check_brackets(std::string str); double getSin(double value); double getCos(double value); double getTg(double value); From d232b7afceea276d0fd7a5653f1afa1bf198f1e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 22:33:37 +0300 Subject: [PATCH 234/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_calculator_app.cpp | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/test_calculator_app.cpp diff --git a/tests/test_calculator_app.cpp b/tests/test_calculator_app.cpp new file mode 100644 index 00000000..258a6091 --- /dev/null +++ b/tests/test_calculator_app.cpp @@ -0,0 +1,73 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_calculator_app/expression.h" +TEST(TestCalculatorAppLib, parse_expression_empty) { + // Arrange & Act + std::string expression = ""; + // Assert + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_expression_invalid_number_format) { + // Arrange & Act + std::string expression = "a+b-2...33"; + // Assert + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_expression_invalid_variable_format) { + // Arrange & Act + std::string expression = "(a+b)*9d-10"; + // Assert + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_expression_invalid_funcion_format) { + // Arrange & Act + std::string expression = "a+b-cos("; + // Assert + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_expression_end_with_operator) { + // Arrange & Act + std::string expression = "c/d-a*(s-h)+"; + // Assert + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_expression_with_unkown_symbol) { + // Arrange & Act + std::string expression = "c/d-a*$"; + // Assert + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_expression_mismatched_brackets) { + std::string expression = "(a+b"; + ASSERT_THROW(Parser::parse(expression), std::logic_error); +} +TEST(TestCalculatorAppLib, parse_correct_expression1) { + std::string expression = "a+b*2"; + ASSERT_NO_THROW(Parser::parse(expression)); +} +TEST(TestCalculatorAppLib, parse_correct_expression2) { + std::string expression = "-a+b*2-(c-d)"; + ASSERT_NO_THROW(Parser::parse(expression)); +} + + + + + + + + + + + +//TEST(TestCalculatorAppLib, build_polish_notation) { +// // Arrange & Act +// +// // Assert +//} +//TEST(TestCalculatorAppLib, calculate_expression) { +// // Arrange & Act +// +// // Assert +//} From 6e165988bcf0ed2c323272b01e17b40f91c1553f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 22:51:46 +0300 Subject: [PATCH 235/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_calculator_app.cpp | 163 +++++++++++++++++++++++++++++++--- 1 file changed, 153 insertions(+), 10 deletions(-) diff --git a/tests/test_calculator_app.cpp b/tests/test_calculator_app.cpp index 258a6091..5442481c 100644 --- a/tests/test_calculator_app.cpp +++ b/tests/test_calculator_app.cpp @@ -50,24 +50,167 @@ TEST(TestCalculatorAppLib, parse_correct_expression2) { std::string expression = "-a+b*2-(c-d)"; ASSERT_NO_THROW(Parser::parse(expression)); } +TEST(TestCalculatorAppLib, parse_correct_expression3_with_functions) { + std::string expression = "sin(x)+cos(y)"; + ASSERT_NO_THROW(Parser::parse(expression)); +} +TEST(TestCalculatorAppLib, parse_correct_expression4_with_abs) { + std::string expression = "|a-b|+c"; + ASSERT_NO_THROW(Parser::parse(expression)); +} +TEST(TestCalculatorAppLib, parse_correct_expression5_complex) { + std::string expression = "a+b*(c-d)/e^2"; + ASSERT_NO_THROW(Parser::parse(expression)); +} +TEST(TestCalculatorAppLib, calculate_simple_expression) { + // Arrange + std::string expr_str = "2+3"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 5.0); +} +TEST(TestCalculatorAppLib, calculate_with_variables) { + // Arrange + std::string expr_str = "a+b"; + Expression expr(1, expr_str); + expr.set_variable("a", 2.5); + expr.set_variable("b", 3.5); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 6.0); +} +TEST(TestCalculatorAppLib, calculate_with_operations) { + // Arrange + std::string expr_str = "2+3*4"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 14.0); +} +TEST(TestCalculatorAppLib, calculate_with_brackets) { + // Arrange + std::string expr_str = "(2+3)*4"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 20.0); +} +TEST(TestCalculatorAppLib, calculate_with_division) { + // Arrange + std::string expr_str = "10/2"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 5.0); +} +TEST(TestCalculatorAppLib, calculate_with_power) { + // Arrange + std::string expr_str = "2^3"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 8.0); +} +TEST(TestCalculatorAppLib, calculate_with_function) { + // Arrange + std::string expr_str = "sin(0)"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 0.0); +} +TEST(TestCalculatorAppLib, calculate_with_abs) { + // Arrange + std::string expr_str = "|-5|"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 5.0); +} +TEST(TestCalculatorAppLib, calculate_complex_expression) { + // Arrange + std::string expr_str = "2+3*sin(0)-|-5|/5"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + // 2 + 3*0 - 5/5 = 2 - 1 = 1 + ASSERT_DOUBLE_EQ(expr.calculate(), 1.0); +} +TEST(TestCalculatorAppLib, calculate_division_by_zero) { + // Arrange + std::string expr_str = "1/0"; + Expression expr(1, expr_str); + + // Act & Assert + ASSERT_THROW(expr.calculate(), std::logic_error); +} +TEST(TestCalculatorAppLib, calculate_missing_variable) { + // Arrange + std::string expr_str = "a+5"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_THROW(expr.calculate(), std::logic_error); +} +TEST(TestCalculatorAppLib, calculate_with_unary_minus1) { + // Arrange + std::string expr_str = "3+(-5)"; + Expression expr(1, expr_str); -//TEST(TestCalculatorAppLib, build_polish_notation) { -// // Arrange & Act -// -// // Assert -//} -//TEST(TestCalculatorAppLib, calculate_expression) { -// // Arrange & Act -// -// // Assert -//} + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), -2.0); +} +TEST(TestCalculatorAppLib, calculate_with_unary_minus2) { + // Arrange + std::string expr_str = "-5+3"; + Expression expr(1, expr_str); + + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), -2.0); +} +TEST(TestCalculatorAppLib, calculate_with_nested_functions) { + // Arrange + std::string expr_str = "sin(cos(0))"; + Expression expr(1, expr_str); + + // Act & Assert + ASSERT_NO_THROW(expr.calculate()); + ASSERT_NEAR(expr.calculate(), 0.8414709848, 1e-9); +} +TEST(TestCalculatorAppLib, expression_getters) { + // Arrange + std::string expr_str = "a+b"; + Expression expr(42, expr_str); + expr.set_variable("a", 2.0); + expr.set_variable("b", 3.0); + + // Act & Assert + ASSERT_EQ(expr.get_id(), 42); + ASSERT_EQ(expr.get_expression(), "a+b"); + ASSERT_DOUBLE_EQ(expr.calculate(), 5.0); +} +TEST(TestCalculatorAppLib, expression_variables_map) { + // Arrange + std::string expr_str = "x+y"; + Expression expr(1, expr_str); + expr.set_variable("x", 10.0); + expr.set_variable("y", 20.0); + // Act + auto vars = expr.variables_map(); + // Assert + ASSERT_EQ(vars.size(), 2); + ASSERT_DOUBLE_EQ(vars["x"], 10.0); + ASSERT_DOUBLE_EQ(vars["y"], 20.0); +} From e17963416aa8276652e64ca80fbde537b3458a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 22:53:21 +0300 Subject: [PATCH 236/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D0=B0=20=D1=83=D0=BD=D0=B0=D1=80=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=B8=D0=BD=D1=83=D1=81=D0=B0:=20=D1=83=D1=87?= =?UTF-8?q?=D0=B8=D1=82=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=20=D0=BC=D0=B8=D0=BD?= =?UTF-8?q?=D1=83=D1=81=20=D0=B2=20=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=B5=20?= =?UTF-8?q?=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_calculator_app/parser.cpp | 6 +++--- lib_calculator_app/parser.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib_calculator_app/parser.cpp b/lib_calculator_app/parser.cpp index 056c3450..0bd3df58 100644 --- a/lib_calculator_app/parser.cpp +++ b/lib_calculator_app/parser.cpp @@ -113,7 +113,7 @@ List Parser::parse(std::string expression) { i++; } else if (isBinaryOperator(c)) { - bool isUnary = isUnaryOperator(c, lastWasOperatorOrBracketOrFunction); + bool isUnary = isUnaryOperator(c, lastWasOperatorOrBracketOrFunction, i); if (isUnary) { if (c == '-') { Lexem lexem1(0.0); @@ -249,11 +249,11 @@ bool Parser::isFunction(const std::string& name) { bool Parser::isBinaryOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'; } -bool Parser::isUnaryOperator(char c, bool lastWasOperatorOrBracket) { +bool Parser::isUnaryOperator(char c, bool lastWasOperatorOrBracket, size_t position) { if (c != '-') { return false; } - return lastWasOperatorOrBracket; + return position == 0 || lastWasOperatorOrBracket; } int Parser::getOperatorPriority(char c) { switch (c) { diff --git a/lib_calculator_app/parser.h b/lib_calculator_app/parser.h index 11a55fca..4316b483 100644 --- a/lib_calculator_app/parser.h +++ b/lib_calculator_app/parser.h @@ -23,7 +23,7 @@ namespace Parser { bool isValidVariableName(const std::string& name); bool isFunction(const std::string& name); bool isBinaryOperator(char c); - bool isUnaryOperator(char c, bool lastWasOperatorOrBracket); + bool isUnaryOperator(char c, bool lastWasOperatorOrBracket, size_t position); bool isValidNumberFormat(const std::string& number); int getOperatorPriority(char c); std::string formatError(const std::string& expression, size_t position, const std::string& message); From 6f41f069485487fc5ffe2686eb03f6214073fb14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 23:07:56 +0300 Subject: [PATCH 237/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=BE=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=9E=D0=9F=D0=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_calculator_app.cpp | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/test_calculator_app.cpp b/tests/test_calculator_app.cpp index 5442481c..ad80333d 100644 --- a/tests/test_calculator_app.cpp +++ b/tests/test_calculator_app.cpp @@ -2,6 +2,7 @@ #include #include #include "../lib_calculator_app/expression.h" + TEST(TestCalculatorAppLib, parse_expression_empty) { // Arrange & Act std::string expression = ""; @@ -214,3 +215,82 @@ TEST(TestCalculatorAppLib, expression_variables_map) { ASSERT_DOUBLE_EQ(vars["x"], 10.0); ASSERT_DOUBLE_EQ(vars["y"], 20.0); } +TEST(TestCalculatorAppLib, expression_build_polish_notation_simple_addition) { + // Arrange + std::string expr_str = "a+b"; + Expression expr(1, expr_str); + // Act & Assert + ASSERT_NO_THROW(Expression expr(1, expr_str)); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_operator_priority) { + // Arrange & Act + Expression expr(1, "a+b*c"); + // Assert + expr.set_variable("a", 1.0); + expr.set_variable("b", 2.0); + expr.set_variable("c", 3.0); + ASSERT_NO_THROW(expr.calculate()); + ASSERT_DOUBLE_EQ(expr.calculate(), 7.0); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_with_parentheses) { + // Arrange & Act + Expression expr(1, "(a+b)*c"); + // Assert + expr.set_variable("a", 1.0); + expr.set_variable("b", 2.0); + expr.set_variable("c", 3.0); + ASSERT_DOUBLE_EQ(expr.calculate(), 9.0); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_complex) { + // Arrange & Act + Expression expr(1, "a+b*(c-d)/e"); + // Assert + expr.set_variable("a", 1.0); + expr.set_variable("b", 2.0); + expr.set_variable("c", 5.0); + expr.set_variable("d", 3.0); + expr.set_variable("e", 4.0); + ASSERT_DOUBLE_EQ(expr.calculate(), 2.0); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_power_operator) { + // Arrange & Act + Expression expr(1, "a^b*c"); + // Assert + expr.set_variable("a", 2.0); + expr.set_variable("b", 3.0); + expr.set_variable("c", 4.0); + ASSERT_DOUBLE_EQ(expr.calculate(), 32.0); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_with_functions) { + // Arrange & Act + Expression expr(1, "sin(a)+cos(b)"); + // Assert + expr.set_variable("a", 0.0); + expr.set_variable("b", 0.0); + ASSERT_DOUBLE_EQ(expr.calculate(), 1.0); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_nested_functions) { + // Arrange & Act + Expression expr(1, "sin(cos(a))"); + // Assert + expr.set_variable("a", 0.0); + ASSERT_NEAR(expr.calculate(), 0.8414709848, 1e-9); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_with_abs_bars) { + // Arrange & Act + Expression expr(1, "|a-b|+c"); + // Assert + expr.set_variable("a", 5.0); + expr.set_variable("b", 8.0); + expr.set_variable("c", 2.0); + // |5-8| + 2 = 3 + 2 = 5 + ASSERT_DOUBLE_EQ(expr.calculate(), 5.0); +} +TEST(TestCalculatorAppLib, expression_build_polish_notation_unary_minus) { + // Arrange & Act + Expression expr(1, "-a+b"); + // Assert + expr.set_variable("a", 5.0); + expr.set_variable("b", 3.0); + ASSERT_DOUBLE_EQ(expr.calculate(), -2.0); +} From 4dd14bf3c93994a2851e03bd3630e4f701ac5ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 23:29:48 +0300 Subject: [PATCH 238/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_doubly_linked_list.cpp | 294 ++++++++++++++++++++++++++++++ tests/test_list.cpp | 190 +++++++++++++++++++ 2 files changed, 484 insertions(+) diff --git a/tests/test_doubly_linked_list.cpp b/tests/test_doubly_linked_list.cpp index b6d3111f..fcb98e41 100644 --- a/tests/test_doubly_linked_list.cpp +++ b/tests/test_doubly_linked_list.cpp @@ -491,3 +491,297 @@ TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_empty_list) { it2 += 5; EXPECT_EQ(it2, list.end()); } +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_prefix_increment) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act + auto it = list.begin(); + ++it; + + // Assert + EXPECT_EQ(20, *it); + ++it; + EXPECT_EQ(30, *it); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_postfix_increment) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act + auto it = list.begin(); + auto it_copy = it++; + + // Assert + EXPECT_EQ(10, *it_copy); + EXPECT_EQ(20, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_prefix_decrement) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + // Act + auto it = list.end(); + + it = list.begin(); + ++it; + ++it; + + --it; + + // Assert + EXPECT_EQ(20, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_postfix_decrement) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act + auto it = list.begin(); + ++it; + ++it; + + auto it_copy = it--; + + // Assert + EXPECT_EQ(30, *it_copy); + EXPECT_EQ(20, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_compound_addition) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + + // Act + auto it = list.begin(); + it += 3; + + // Assert + EXPECT_EQ(40, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_compound_subtraction) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + // Act + auto it = list.end(); + it = list.begin(); + it += 4; + + it -= 2; + + // Assert + EXPECT_EQ(30, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_equality_operators) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act & Assert + auto it1 = list.begin(); + auto it2 = list.begin(); + EXPECT_TRUE(it1 == it2); + + ++it1; + EXPECT_FALSE(it1 == it2); + EXPECT_TRUE(it1 != it2); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_dereference_const) { + // Arrange + DoublyLinkedList list; + list.push_back(100); + list.push_back(200); + + // Act + const auto& const_list = list; + auto it = const_list.begin(); + + // Assert + EXPECT_EQ(100, *it); + ++it; + EXPECT_EQ(200, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_range_based_for_loop) { + // Arrange + DoublyLinkedList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + list.push_back(4); + list.push_back(5); + + // Act + std::vector values; + for (const auto& value : list) { + values.push_back(value); + } + + // Assert + ASSERT_EQ(5, values.size()); + EXPECT_EQ(1, values[0]); + EXPECT_EQ(2, values[1]); + EXPECT_EQ(3, values[2]); + EXPECT_EQ(4, values[3]); + EXPECT_EQ(5, values[4]); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_range_based_for_loop_modify) { + // Arrange + DoublyLinkedList list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + for (auto& value : list) { + value += 10; + } + // Assert + auto it = list.begin(); + EXPECT_EQ(11, *it); + ++it; + EXPECT_EQ(12, *it); + ++it; + EXPECT_EQ(13, *it); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_manual_for_loop) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + + // Act + std::vector values; + for (auto it = list.begin(); it != list.end(); ++it) { + values.push_back(*it); + } + + // Assert + ASSERT_EQ(4, values.size()); + EXPECT_EQ(10, values[0]); + EXPECT_EQ(20, values[1]); + EXPECT_EQ(30, values[2]); + EXPECT_EQ(40, values[3]); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_manual_reverse_traversal) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + // Act + std::vector values; + + auto it = list.begin(); + ++it; + ++it; + values.push_back(*it); + --it; + values.push_back(*it); + --it; + values.push_back(*it); + + // Assert + ASSERT_EQ(3, values.size()); + EXPECT_EQ(30, values[0]); + EXPECT_EQ(20, values[1]); + EXPECT_EQ(10, values[2]); +} +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_copy_constructor) { + // Arrange + DoublyLinkedList list; + list.push_back(100); + list.push_back(200); + + // Act + auto it1 = list.begin(); + auto it2 = it1; + + // Assert + EXPECT_TRUE(it1 == it2); + EXPECT_EQ(100, *it1); + EXPECT_EQ(100, *it2); + + ++it1; + EXPECT_FALSE(it1 == it2); + EXPECT_EQ(200, *it1); + EXPECT_EQ(100, *it2); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_assignment_operator) { + // Arrange + DoublyLinkedList list; + list.push_back(100); + list.push_back(200); + list.push_back(300); + + // Act + auto it1 = list.begin(); + auto it2 = list.begin(); + ++it2; + ++it2; + + it1 = it2; + + // Assert + EXPECT_TRUE(it1 == it2); + EXPECT_EQ(300, *it1); + EXPECT_EQ(300, *it2); +} + +TEST(TestDoublyLinkedListLib, doubly_linked_list_iterator_bidirectional_traversal) { + // Arrange + DoublyLinkedList list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + + // Act + auto it = list.begin(); + ++it; + ++it; + --it; + + // Assert + EXPECT_EQ(20, *it); + + ++it; + ++it; + --it; + --it; + EXPECT_EQ(20, *it); +} diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 4ea32003..8d76e0f2 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -472,3 +472,193 @@ TEST(TestListLib, list_iterator_empty_list) { it2 += 5; EXPECT_EQ(it2, list.end()); } +TEST(TestListLib, list_iterator_prefix_increment) { + // Arrange + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act + auto it = list.begin(); + ++it; + + // Assert + EXPECT_EQ(20, *it); + ++it; + EXPECT_EQ(30, *it); +} + +TEST(TestListLib, list_iterator_postfix_increment) { + // Arrange + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act + auto it = list.begin(); + auto it_copy = it++; + + // Assert + EXPECT_EQ(10, *it_copy); + EXPECT_EQ(20, *it); +} + +TEST(TestListLib, list_iterator_compound_addition) { + // Arrange + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + list.push_back(50); + + // Act + auto it = list.begin(); + it += 2; + + // Assert + EXPECT_EQ(30, *it); + + it += 2; + EXPECT_EQ(50, *it); +} + +TEST(TestListLib, list_iterator_equality_operators) { + // Arrange + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + + // Act & Assert + auto it1 = list.begin(); + auto it2 = list.begin(); + EXPECT_TRUE(it1 == it2); + + ++it1; + EXPECT_FALSE(it1 == it2); + EXPECT_TRUE(it1 != it2); +} + +TEST(TestListLib, list_iterator_dereference_const) { + // Arrange + List list; + list.push_back(100); + list.push_back(200); + + // Act + const auto& const_list = list; + auto it = const_list.begin(); + + // Assert + EXPECT_EQ(100, *it); + ++it; + EXPECT_EQ(200, *it); +} +TEST(TestListLib, list_iterator_range_based_for_loop) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + list.push_back(4); + list.push_back(5); + // Act + std::vector values; + for (const auto& value : list) { + values.push_back(value); + } + // Assert + ASSERT_EQ(5, values.size()); + EXPECT_EQ(1, values[0]); + EXPECT_EQ(2, values[1]); + EXPECT_EQ(3, values[2]); + EXPECT_EQ(4, values[3]); + EXPECT_EQ(5, values[4]); +} +TEST(TestListLib, list_iterator_range_based_for_loop_modify) { + // Arrange + List list; + list.push_back(1); + list.push_back(2); + list.push_back(3); + + // Act + for (auto& value : list) { + value *= 2; + } + // Assert + auto it = list.begin(); + EXPECT_EQ(2, *it); + ++it; + EXPECT_EQ(4, *it); + ++it; + EXPECT_EQ(6, *it); +} +TEST(TestListLib, list_iterator_manual_for_loop) { + // Arrange + List list; + list.push_back(10); + list.push_back(20); + list.push_back(30); + list.push_back(40); + + // Act + std::vector values; + for (auto it = list.begin(); it != list.end(); ++it) { + values.push_back(*it); + } + // Assert + ASSERT_EQ(4, values.size()); + EXPECT_EQ(10, values[0]); + EXPECT_EQ(20, values[1]); + EXPECT_EQ(30, values[2]); + EXPECT_EQ(40, values[3]); +} +TEST(TestListLib, list_iterator_dereference_end_exception) { + // Arrange + List list; + list.push_back(10); + // Act & Assert + auto it = list.end(); + EXPECT_THROW(*it, std::runtime_error); + auto begin_it = list.begin(); + ++begin_it; + EXPECT_THROW(*begin_it, std::runtime_error); +} +TEST(TestListLib, list_iterator_copy_constructor) { + // Arrange + List list; + list.push_back(100); + list.push_back(200); + // Act + auto it1 = list.begin(); + auto it2 = it1; + // Assert + EXPECT_TRUE(it1 == it2); + EXPECT_EQ(100, *it1); + EXPECT_EQ(100, *it2); + ++it1; + EXPECT_FALSE(it1 == it2); + EXPECT_EQ(200, *it1); + EXPECT_EQ(100, *it2); +} +TEST(TestListLib, list_iterator_assignment_operator) { + // Arrange + List list; + list.push_back(100); + list.push_back(200); + list.push_back(300); + // Act + auto it1 = list.begin(); + auto it2 = list.begin(); + ++it2; + ++it2; + it1 = it2; + // Assert + EXPECT_TRUE(it1 == it2); + EXPECT_EQ(300, *it1); + EXPECT_EQ(300, *it2); +} From 6e418bb9bc7e7f7ca01042970db8b8b78ccb1acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 15 Dec 2025 23:36:59 +0300 Subject: [PATCH 239/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20?= =?UTF-8?q?=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_vector.cpp | 364 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 348 insertions(+), 16 deletions(-) diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index b374b308..c27ad9fb 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -389,22 +389,6 @@ TEST(TestVectorLib, erase1) { EXPECT_EQ(expected_result, actual_result); } TEST(TestVectorLib, erase2) { - // Arrange - int data[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - size_t size = 15; - Tvector vector(data, size); - vector.erase(1); - vector.erase(1); - vector.erase(1); - // Act - bool actual_result = vector.get_deleted() == 0 - && vector.get_size() == 12 - && vector.get_capacity() == 27; - // Assert - bool expected_result = true; - EXPECT_EQ(expected_result, actual_result); -} -TEST(TestVectorLib, erase3) { // Arrange bool actual_result = true; int data[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; @@ -543,3 +527,351 @@ TEST(TestVectorLib, vector_iterator_empty_list) { it2 += 7; EXPECT_EQ(it2, vector.end()); } +TEST(TestVectorLib, vector_iterator_prefix_increment) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + + auto it = vector.begin(); + ++it; + EXPECT_EQ(20, *it); + ++it; + EXPECT_EQ(30, *it); +} + +TEST(TestVectorLib, vector_iterator_postfix_increment) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + + auto it = vector.begin(); + auto it_copy = it++; + + EXPECT_EQ(10, *it_copy); + EXPECT_EQ(20, *it); +} + +TEST(TestVectorLib, vector_iterator_prefix_decrement) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + + auto it = vector.begin(); + ++it; + ++it; + --it; + + EXPECT_EQ(20, *it); +} + +TEST(TestVectorLib, vector_iterator_postfix_decrement) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + + auto it = vector.begin(); + ++it; + ++it; + auto it_copy = it--; + + EXPECT_EQ(30, *it_copy); + EXPECT_EQ(20, *it); +} + +TEST(TestVectorLib, vector_iterator_compound_addition) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + vector.push_back(40); + vector.push_back(50); + + auto it = vector.begin(); + it += 3; + EXPECT_EQ(40, *it); + + it += 1; + EXPECT_EQ(50, *it); +} + +TEST(TestVectorLib, vector_iterator_compound_subtraction) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + vector.push_back(40); + vector.push_back(50); + + auto it = vector.begin(); + it += 4; + it -= 2; + + EXPECT_EQ(30, *it); + + it -= 2; + EXPECT_EQ(10, *it); +} + +TEST(TestVectorLib, vector_iterator_equality_operators) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + + auto it1 = vector.begin(); + auto it2 = vector.begin(); + EXPECT_TRUE(it1 == it2); + + ++it1; + EXPECT_FALSE(it1 == it2); + EXPECT_TRUE(it1 != it2); +} + +TEST(TestVectorLib, vector_iterator_dereference) { + Tvector vector; + vector.push_back(100); + vector.push_back(200); + + auto it = vector.begin(); + EXPECT_EQ(100, *it); + + ++it; + EXPECT_EQ(200, *it); +} + +TEST(TestVectorLib, vector_iterator_dereference_const) { + Tvector vector; + vector.push_back(100); + vector.push_back(200); + + const Tvector& const_vector = vector; + auto it = const_vector.begin(); + + EXPECT_EQ(100, *it); + ++it; + EXPECT_EQ(200, *it); +} + +TEST(TestVectorLib, vector_iterator_dereference_modify) { + Tvector vector; + vector.push_back(100); + vector.push_back(200); + + auto it = vector.begin(); + *it = 500; + + EXPECT_EQ(500, vector[0]); +} + +TEST(TestVectorLib, vector_iterator_copy_constructor) { + Tvector vector; + vector.push_back(100); + vector.push_back(200); + + auto it1 = vector.begin(); + auto it2 = it1; + + EXPECT_TRUE(it1 == it2); + EXPECT_EQ(100, *it1); + EXPECT_EQ(100, *it2); + + ++it1; + EXPECT_FALSE(it1 == it2); + EXPECT_EQ(200, *it1); + EXPECT_EQ(100, *it2); +} + +TEST(TestVectorLib, vector_iterator_assignment_operator) { + Tvector vector; + vector.push_back(100); + vector.push_back(200); + vector.push_back(300); + + auto it1 = vector.begin(); + auto it2 = vector.begin(); + ++it2; + ++it2; + + it1 = it2; + + EXPECT_TRUE(it1 == it2); + EXPECT_EQ(300, *it1); + EXPECT_EQ(300, *it2); +} + +TEST(TestVectorLib, vector_iterator_default_constructor) { + Tvector::Iterator it; + + Tvector vector; + vector.push_back(10); + + auto it2 = vector.begin(); + it = it2; + + EXPECT_EQ(10, *it); +} +TEST(TestVectorLib, vector_iterator_dereference_nullptr_exception) { + Tvector::Iterator it; + EXPECT_THROW(*it, std::runtime_error); +} + +TEST(TestVectorLib, vector_iterator_range_based_for_loop) { + Tvector vector; + vector.push_back(1); + vector.push_back(2); + vector.push_back(3); + vector.push_back(4); + vector.push_back(5); + + int sum = 0; + for (const auto& value : vector) { + sum += value; + } + + EXPECT_EQ(15, sum); +} + +TEST(TestVectorLib, vector_iterator_range_based_for_loop_modify) { + Tvector vector; + vector.push_back(1); + vector.push_back(2); + vector.push_back(3); + + for (auto& value : vector) { + value *= 2; + } + + auto it = vector.begin(); + EXPECT_EQ(2, *it); + ++it; + EXPECT_EQ(4, *it); + ++it; + EXPECT_EQ(6, *it); +} + +TEST(TestVectorLib, vector_iterator_manual_for_loop) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + vector.push_back(30); + vector.push_back(40); + + std::vector values; + for (auto it = vector.begin(); it != vector.end(); ++it) { + values.push_back(*it); + } + + ASSERT_EQ(4, values.size()); + EXPECT_EQ(10, values[0]); + EXPECT_EQ(20, values[1]); + EXPECT_EQ(30, values[2]); + EXPECT_EQ(40, values[3]); +} + +TEST(TestVectorLib, vector_iterator_begin_end_empty) { + Tvector vector; + + EXPECT_EQ(vector.begin(), vector.end()); + + auto it = vector.begin(); + ++it; + EXPECT_EQ(it, vector.end()); +} + +TEST(TestVectorLib, vector_iterator_begin_end_non_empty) { + Tvector vector; + vector.push_back(10); + vector.push_back(20); + + auto begin = vector.begin(); + auto end = vector.end(); + + EXPECT_NE(begin, end); + + auto it = begin; + ++it; + ++it; + EXPECT_EQ(it, end); +} + +TEST(TestVectorLib, vector_iterator_reverse_iteration) { + Tvector vector; + vector.push_back(1); + vector.push_back(2); + vector.push_back(3); + vector.push_back(4); + vector.push_back(5); + + std::vector reversed; + + auto it = vector.end(); + --it; + + while (true) { + reversed.push_back(*it); + if (it == vector.begin()) { + break; + } + --it; + } + + ASSERT_EQ(5, reversed.size()); + EXPECT_EQ(5, reversed[0]); + EXPECT_EQ(4, reversed[1]); + EXPECT_EQ(3, reversed[2]); + EXPECT_EQ(2, reversed[3]); + EXPECT_EQ(1, reversed[4]); +} + +TEST(TestVectorLib, vector_iterator_arithmetic_mixed) { + Tvector vector; + for (int i = 0; i < 10; ++i) { + vector.push_back(i * 10); + } + + auto it = vector.begin(); + it += 5; + EXPECT_EQ(50, *it); + + --it; + EXPECT_EQ(40, *it); + + it -= 2; + EXPECT_EQ(20, *it); + + it++; + EXPECT_EQ(30, *it); + + ++it; + EXPECT_EQ(40, *it); +} + +TEST(TestVectorLib, vector_iterator_self_assignment) { + Tvector vector; + vector.push_back(100); + vector.push_back(200); + + auto it = vector.begin(); + it = it; + + EXPECT_EQ(100, *it); +} + +TEST(TestVectorLib, vector_iterator_comparison_with_different_vectors) { + Tvector vector1; + vector1.push_back(10); + + Tvector vector2; + vector2.push_back(10); + + auto it1 = vector1.begin(); + auto it2 = vector2.begin(); + + + EXPECT_FALSE(it1 == it2); + EXPECT_TRUE(it1 != it2); +} From 677441dc27aa140325fcdb8086f77c2f2d6c9dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 9 Feb 2026 10:36:40 +0300 Subject: [PATCH 240/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B8=D1=80=D0=B8=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=B8=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89?= =?UTF-8?q?=D0=B8=20DSU=20=D0=B8=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=D0=B8=D1=80=D0=B8=D0=BD=D1=82=D0=B0=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=8D=D0=BA=D1=80=D0=B0=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 234 ++++++++++++++++++++++++++-------------- 1 file changed, 154 insertions(+), 80 deletions(-) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index bc6c3cf7..821805a7 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -1,5 +1,5 @@ #pragma once -#include "../lib_matrix/matrix.h" +//#include "../lib_matrix/matrix.h" #include "../lib_stack/stack.h" #include "../lib_list/List.h" #include "../lib_dsu/dsu.h" @@ -9,57 +9,57 @@ #include #include #include -template -std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { - T min_val = matrix[x][y]; - size_t min_x = x; - size_t min_y = y; - if (x > 0 && matrix[x - 1][y] < min_val) { - min_val = matrix[x - 1][y]; - min_x = x - 1; - min_y = y; - } - if (x < matrix.getM() - 1 && matrix[x + 1][y] < min_val) { - min_val = matrix[x + 1][y]; - min_x = x + 1; - min_y = y; - } - if (y > 0 && matrix[x][y - 1] < min_val) { - min_val = matrix[x][y - 1]; - min_x = x; - min_y = y - 1; - } - if (y < matrix.getN() - 1 && matrix[x][y + 1] < min_val) { - min_val = matrix[x][y + 1]; - min_x = x; - min_y = y + 1; - } - return { min_x, min_y }; -} +//template +//std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { +// T min_val = matrix[x][y]; +// size_t min_x = x; +// size_t min_y = y; +// if (x > 0 && matrix[x - 1][y] < min_val) { +// min_val = matrix[x - 1][y]; +// min_x = x - 1; +// min_y = y; +// } +// if (x < matrix.getM() - 1 && matrix[x + 1][y] < min_val) { +// min_val = matrix[x + 1][y]; +// min_x = x + 1; +// min_y = y; +// } +// if (y > 0 && matrix[x][y - 1] < min_val) { +// min_val = matrix[x][y - 1]; +// min_x = x; +// min_y = y - 1; +// } +// if (y < matrix.getN() - 1 && matrix[x][y + 1] < min_val) { +// min_val = matrix[x][y + 1]; +// min_x = x; +// min_y = y + 1; +// } +// return { min_x, min_y }; +//} size_t getRandomIndex(size_t max) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution dist(0, max - 1); return dist(gen); } -template -T find_local_minimum(const Matrix& matrix) { - size_t x = getRandomIndex(matrix.getM()); - size_t y = getRandomIndex(matrix.getN()); - - while (true) { - std::pair min_coords = find_min_neighbor_coords(matrix, x, y); - size_t new_x = min_coords.first; - size_t new_y = min_coords.second; - - if (new_x == x && new_y == y) { - return matrix[x][y]; - } - - x = new_x; - y = new_y; - } -} +//template +//T find_local_minimum(const Matrix& matrix) { +// size_t x = getRandomIndex(matrix.getM()); +// size_t y = getRandomIndex(matrix.getN()); +// +// while (true) { +// std::pair min_coords = find_min_neighbor_coords(matrix, x, y); +// size_t new_x = min_coords.first; +// size_t new_y = min_coords.second; +// +// if (new_x == x && new_y == y) { +// return matrix[x][y]; +// } +// +// x = new_x; +// y = new_y; +// } +//} bool check_brackets(std::string str) { Stack stack(str.length()); stack.clear(); @@ -227,46 +227,120 @@ Node* find_loop_start(List& list) { } return it2_slow; } -size_t get_count_of_islands(Matrix& matrix) { - if (matrix.is_empty()) { - return 0; - } - size_t N = matrix.getN(); - size_t M = matrix.getM(); - DSU islands(M * N); - - // Все 8 направлений - int directions[8][2] = { - {-1, -1}, {-1, 0}, {-1, 1}, // верхние - {0, -1}, {0, 1}, // левый, правый - {1, -1}, {1, 0}, {1, 1} // нижние - }; +//size_t get_count_of_islands(Matrix& matrix) { +// if (matrix.is_empty()) { +// return 0; +// } +// size_t N = matrix.getN(); +// size_t M = matrix.getM(); +// DSU islands(M * N); +// +// // Все 8 направлений +// int directions[8][2] = { +// {-1, -1}, {-1, 0}, {-1, 1}, // верхние +// {0, -1}, {0, 1}, // левый, правый +// {1, -1}, {1, 0}, {1, 1} // нижние +// }; +// +// for (size_t i = 0; i < M; i++) { +// for (size_t j = 0; j < N; j++) { +// if (matrix[i][j] == 1) { +// // Проверяем всех 8 соседей +// for (auto& dir : directions) { +// int ni = i + dir[0]; +// int nj = j + dir[1]; +// +// if (ni >= 0 && ni < M && nj >= 0 && nj < N) { +// if (matrix[ni][nj] == 1) { +// islands.dsu_union(i * N + j, ni * N + nj); +// } +// } +// } +// } +// } +// } +// +// std::unordered_set unique_roots; +// for (size_t i = 0; i < M; i++) { +// for (size_t j = 0; j < N; j++) { +// if (matrix[i][j] == 1) { +// unique_roots.insert(islands.dsu_find_recursive(i * N + j)); +// } +// } +// } +// return unique_roots.size(); +//} +DSU generate(int x, int y, int n, int m) { + DSU dsu(n * m); + dsu.dsu_union(x, y); + // соединяем соседние с вероятностью 70 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + int current = i * m + j; - for (size_t i = 0; i < M; i++) { - for (size_t j = 0; j < N; j++) { - if (matrix[i][j] == 1) { - // Проверяем всех 8 соседей - for (auto& dir : directions) { - int ni = i + dir[0]; - int nj = j + dir[1]; - - if (ni >= 0 && ni < M && nj >= 0 && nj < N) { - if (matrix[ni][nj] == 1) { - islands.dsu_union(i * N + j, ni * N + nj); - } - } - } + if (j < m - 1 && getRandomIndex(9) >= 3) { + int right = i * m + (j + 1); + dsu.dsu_union(current, right); + } + if (i < n - 1 && getRandomIndex(9) >= 3) { + int bottom = (i + 1) * m + j; + dsu.dsu_union(current, bottom); } } } + return dsu; +} +void print_labirint(DSU labirint, int n, int m) { + int entry = 0; + int exit = n * m - 1; + std::cout << "+"; + for (int j = 0; j < m; j++) { + std::cout << "---+"; + } + std::cout << std::endl; + for (int i = 0; i < n; i++) { + std::cout << "|"; + for (int j = 0; j < m; j++) { + int cell = i * m + j; + if (cell < 10) { + std::cout << " " << cell << " "; + } + else { + std::cout << cell << " "; + } + if (j < m - 1) { + int right_cell = i * m + (j + 1); + if (labirint.dsu_find_recursive(cell) == labirint.dsu_find_recursive(right_cell)) { + std::cout << " "; + } + else { + std::cout << "|"; + } + } + else { + std::cout << "|"; + } + } + std::cout << std::endl; + if (i < n - 1) { + std::cout << "+"; + for (int j = 0; j < m; j++) { + int cell = i * m + j; + int bottom_cell = (i + 1) * m + j; - std::unordered_set unique_roots; - for (size_t i = 0; i < M; i++) { - for (size_t j = 0; j < N; j++) { - if (matrix[i][j] == 1) { - unique_roots.insert(islands.dsu_find_recursive(i * N + j)); + if (labirint.dsu_find_recursive(cell) == labirint.dsu_find_recursive(bottom_cell)) { + std::cout << " +"; + } + else { + std::cout << "---+"; + } } + std::cout << std::endl; } } - return unique_roots.size(); + std::cout << "+"; + for (int j = 0; j < m; j++) { + std::cout << "---+"; + } + std::cout << std::endl; } From d10e923283b552be2987e58d5b9a6ce61f0fe400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 9 Feb 2026 10:38:52 +0300 Subject: [PATCH 241/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B4=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9,=20=D1=81?= =?UTF-8?q?=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D1=81=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=BB=D0=B0=D0=B1=D0=B8=D1=80=D0=B8=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20=D1=81=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D1=8C=D1=8E=20DSU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main/main.cpp b/main/main.cpp index d0905094..50e3f9ce 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2,7 +2,9 @@ //#define EASY_EXAMPLE //#define CIRCLES_AND_SPHERES -#define CALCULATOR +//#define CALCULATOR +#define DSU_LABIRINT + #ifdef EASY_EXAMPLE #include #include @@ -623,4 +625,19 @@ int main() { } return 0; } +#endif +#ifdef DSU_LABIRINT +#include "../lib_dsu/dsu.h" +#include "../lib_algoritm/algoritm.h" +int main() { + int n = 5, m = 10; + int x = 1; + int y = n * m - 1; + + DSU dsu = generate(x, y, n, m); + print_labirint(dsu, n, m); + + return 0; + +} #endif \ No newline at end of file From f3c88deefd16544fb627bbf8526879f9c338c4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 9 Feb 2026 16:31:39 +0300 Subject: [PATCH 242/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=20=D0=BD=D0=B5=D0=B9=20=D0=B2=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B2=D0=B5=D1=82=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_matrix/CMakeLists.txt | 1 + lib_matrix/matrix.cpp | 1 + lib_matrix/matrix.h | 189 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 lib_matrix/CMakeLists.txt create mode 100644 lib_matrix/matrix.cpp create mode 100644 lib_matrix/matrix.h diff --git a/lib_matrix/CMakeLists.txt b/lib_matrix/CMakeLists.txt new file mode 100644 index 00000000..91c1bacc --- /dev/null +++ b/lib_matrix/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Matrix) \ No newline at end of file diff --git a/lib_matrix/matrix.cpp b/lib_matrix/matrix.cpp new file mode 100644 index 00000000..0d415b5a --- /dev/null +++ b/lib_matrix/matrix.cpp @@ -0,0 +1 @@ +#include "matrix.h" \ No newline at end of file diff --git a/lib_matrix/matrix.h b/lib_matrix/matrix.h new file mode 100644 index 00000000..10acab73 --- /dev/null +++ b/lib_matrix/matrix.h @@ -0,0 +1,189 @@ +#pragma once +#include +#include "../lib_math_vector/math_vector.h" +template class TriangleMatrix; + +template +class Matrix : public MathVector> { +protected: + size_t _M; + size_t _N; + Matrix transpose() const { + Matrix result(_N, _M); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < _N; ++j) { + result[j][i] = (*this)[i][j]; + } + } + return result; + } +public: + Matrix(); + Matrix(size_t M, size_t N); + Matrix(T* data, size_t M, size_t N); + Matrix(const Matrix& other); + virtual ~Matrix(); + size_t getM() const noexcept; + size_t getN() const noexcept; + + Matrix operator * (T value) const; + Matrix operator / (T value) const; + + Matrix& operator *= (T value); + Matrix& operator /= (T value); + + Matrix operator + (const Matrix& other_matrix) const; + Matrix operator - (const Matrix& other_matrix) const; + Matrix operator * (const Matrix& other_matrix) const; + + MathVector operator * (const MathVector& vector) const; + + Matrix& operator += (const Matrix& other_matrix); + Matrix& operator -= (const Matrix& other_matrix); + template + friend std::ostream& operator<<(std::ostream&, const Matrix& matrix); + + Matrix& operator=(const Matrix& other); +}; +template +size_t Matrix::getM() const noexcept { + return _M; +} +template +size_t Matrix::getN() const noexcept { + return _N; +} +template +Matrix::Matrix() : MathVector>(), _M(0), _N(0) {} +template +Matrix::Matrix(size_t M, size_t N) : MathVector>(M) { + _M = M; + _N = N; + for (size_t i = 0; i < _M; ++i) { + (*this)[i] = MathVector(_N); + } +} +template +Matrix::Matrix(T* data, size_t M, size_t N) : MathVector>(M) { + _M = M; + _N = N; + for (size_t i = 0; i < _M; ++i) { + (*this)[i] = MathVector(data + i * _N, _N); + } +} +template +Matrix::Matrix(const Matrix& other) : + MathVector>(other), _M(other._M), _N(other._N) {} +template +Matrix::~Matrix() = default; +template +Matrix Matrix::operator * (T value) const { + return Matrix(*this) *= value; +} +template +Matrix Matrix::operator / (T value) const { + if (value == 0) { + throw std::logic_error("Division by zero!"); + } + return Matrix(*this) /= value; +} +template +Matrix& Matrix::operator *= (T value) { + template + Matrix& Matrix::operator *= (T value) { + for (size_t i = 0; i < _M; ++i) { + (*this)[i] *= value; + } + return *this; + } +} +template +Matrix& Matrix::operator /= (T value) { + if (value == 0) { + throw std::logic_error("Division by zero!"); + } + for (size_t i = 0; i < _M; ++i) { + (*this)[i] /= value; + } + return *this; +} +template +Matrix Matrix::operator + (const Matrix& other_matrix) const { + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + return Matrix(*this) += other_matrix; +} +template +Matrix Matrix::operator - (const Matrix& other_matrix) const { + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + return Matrix(*this) -= other_matrix; +} +template +Matrix Matrix::operator * (const Matrix& other_matrix) const { + if (_N != other_matrix.getM()) { + throw std::logic_error + ("The sizes of the matrices are not compatible for this operation!"); + } + Matrix result(_M, other_matrix.getN()); + Matrix matrix_t = other_matrix.transpose(); + for (size_t i = 0; i < _M; ++i) { + for (size_t j = 0; j < matrix_t.getM(); ++j) { + result[i][j] = (*this)[i] * matrix_t[j]; + } + } + return result; +} +template +MathVector Matrix::operator * (const MathVector& vector) const { + if (_N != vector.get_size()) { + throw std::logic_error + ("Size of matrix aren't compatible with vector's size for this operation!"); + } + MathVector result(_M); + for (size_t i = 0; i < _M; ++i) { + result[i] = (*this)[i] * vector; + } + return result; +} +template +Matrix& Matrix::operator += (const Matrix& other_matrix) { + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + for (size_t i = 0; i < _M; ++i) { + (*this)[i] += other_matrix[i]; + } + return *this; +} +template +Matrix& Matrix::operator -= (const Matrix& other_matrix) { + if (_M != other_matrix.getM() || _N != other_matrix.getN()) { + throw std::logic_error("The matrices have different sizes!"); + } + for (size_t i = 0; i < _M; ++i) { + (*this)[i] -= other_matrix[i]; + } + return *this; +} +template +Matrix& Matrix::operator=(const Matrix& other) { + if (this != &other) { + MathVector>::operator=(other); + _M = other._M; + _N = other._N; + } + return *this; +} +template +std::ostream& operator<<(std::ostream& out, const Matrix& matrix) { + for (size_t i = 0; i < matrix.getM(); ++i) { + for (size_t j = 0; j < matrix.getN(); ++j) { + out << matrix[i][j] << "\t"; + } + out << std::endl; + } + return out; +} From 49de3345bd77e95948f710b3d1f478b5291517e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 9 Feb 2026 20:32:55 +0300 Subject: [PATCH 243/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B4=D0=B2=D0=B5=20=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=81=D0=B8=D0=B8=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9?= =?UTF-8?q?=20=D0=BF=D0=BE=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8E=20=D0=BB=D0=B0=D0=B1=D0=B8=D1=80=D0=B8=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D0=B8=20?= =?UTF-8?q?DSU=20-=20=D1=81=20=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D0=B5?= =?UTF-8?q?=D0=B9=20=D1=84=D0=BB=D0=B0=D0=B3=D0=BE=D0=B2=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=B5=D0=B3=D1=80=D0=B0=D0=B4=20(1=20-=20?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=8C=20=D1=81=D1=82=D0=B5=D0=BD=D0=B0,=200?= =?UTF-8?q?=20-=20=D0=BD=D0=B5=D1=82)=20=D0=B8=20=D0=B1=D0=B5=D0=B7=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D1=80=D0=B8=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_algoritm/algoritm.h | 273 ++++++++++++++++++++++++---------------- 1 file changed, 167 insertions(+), 106 deletions(-) diff --git a/lib_algoritm/algoritm.h b/lib_algoritm/algoritm.h index 821805a7..9b198a40 100644 --- a/lib_algoritm/algoritm.h +++ b/lib_algoritm/algoritm.h @@ -1,5 +1,5 @@ #pragma once -//#include "../lib_matrix/matrix.h" +#include "../lib_matrix/matrix.h" #include "../lib_stack/stack.h" #include "../lib_list/List.h" #include "../lib_dsu/dsu.h" @@ -9,57 +9,57 @@ #include #include #include -//template -//std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { -// T min_val = matrix[x][y]; -// size_t min_x = x; -// size_t min_y = y; -// if (x > 0 && matrix[x - 1][y] < min_val) { -// min_val = matrix[x - 1][y]; -// min_x = x - 1; -// min_y = y; -// } -// if (x < matrix.getM() - 1 && matrix[x + 1][y] < min_val) { -// min_val = matrix[x + 1][y]; -// min_x = x + 1; -// min_y = y; -// } -// if (y > 0 && matrix[x][y - 1] < min_val) { -// min_val = matrix[x][y - 1]; -// min_x = x; -// min_y = y - 1; -// } -// if (y < matrix.getN() - 1 && matrix[x][y + 1] < min_val) { -// min_val = matrix[x][y + 1]; -// min_x = x; -// min_y = y + 1; -// } -// return { min_x, min_y }; -//} +template +std::pair find_min_neighbor_coords(const Matrix& matrix, size_t x, size_t y) { + T min_val = matrix[x][y]; + size_t min_x = x; + size_t min_y = y; + if (x > 0 && matrix[x - 1][y] < min_val) { + min_val = matrix[x - 1][y]; + min_x = x - 1; + min_y = y; + } + if (x < matrix.getM() - 1 && matrix[x + 1][y] < min_val) { + min_val = matrix[x + 1][y]; + min_x = x + 1; + min_y = y; + } + if (y > 0 && matrix[x][y - 1] < min_val) { + min_val = matrix[x][y - 1]; + min_x = x; + min_y = y - 1; + } + if (y < matrix.getN() - 1 && matrix[x][y + 1] < min_val) { + min_val = matrix[x][y + 1]; + min_x = x; + min_y = y + 1; + } + return { min_x, min_y }; +} size_t getRandomIndex(size_t max) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution dist(0, max - 1); return dist(gen); } -//template -//T find_local_minimum(const Matrix& matrix) { -// size_t x = getRandomIndex(matrix.getM()); -// size_t y = getRandomIndex(matrix.getN()); -// -// while (true) { -// std::pair min_coords = find_min_neighbor_coords(matrix, x, y); -// size_t new_x = min_coords.first; -// size_t new_y = min_coords.second; -// -// if (new_x == x && new_y == y) { -// return matrix[x][y]; -// } -// -// x = new_x; -// y = new_y; -// } -//} +template +T find_local_minimum(const Matrix& matrix) { + size_t x = getRandomIndex(matrix.getM()); + size_t y = getRandomIndex(matrix.getN()); + + while (true) { + std::pair min_coords = find_min_neighbor_coords(matrix, x, y); + size_t new_x = min_coords.first; + size_t new_y = min_coords.second; + + if (new_x == x && new_y == y) { + return matrix[x][y]; + } + + x = new_x; + y = new_y; + } +} bool check_brackets(std::string str) { Stack stack(str.length()); stack.clear(); @@ -227,62 +227,126 @@ Node* find_loop_start(List& list) { } return it2_slow; } -//size_t get_count_of_islands(Matrix& matrix) { -// if (matrix.is_empty()) { -// return 0; -// } -// size_t N = matrix.getN(); -// size_t M = matrix.getM(); -// DSU islands(M * N); -// -// // Все 8 направлений -// int directions[8][2] = { -// {-1, -1}, {-1, 0}, {-1, 1}, // верхние -// {0, -1}, {0, 1}, // левый, правый -// {1, -1}, {1, 0}, {1, 1} // нижние -// }; -// -// for (size_t i = 0; i < M; i++) { -// for (size_t j = 0; j < N; j++) { -// if (matrix[i][j] == 1) { -// // Проверяем всех 8 соседей -// for (auto& dir : directions) { -// int ni = i + dir[0]; -// int nj = j + dir[1]; -// -// if (ni >= 0 && ni < M && nj >= 0 && nj < N) { -// if (matrix[ni][nj] == 1) { -// islands.dsu_union(i * N + j, ni * N + nj); -// } -// } -// } -// } -// } -// } -// -// std::unordered_set unique_roots; -// for (size_t i = 0; i < M; i++) { -// for (size_t j = 0; j < N; j++) { -// if (matrix[i][j] == 1) { -// unique_roots.insert(islands.dsu_find_recursive(i * N + j)); -// } -// } -// } -// return unique_roots.size(); -//} -DSU generate(int x, int y, int n, int m) { +size_t get_count_of_islands(Matrix& matrix) { + if (matrix.is_empty()) { + return 0; + } + size_t N = matrix.getN(); + size_t M = matrix.getM(); + DSU islands(M * N); + + // Все 8 направлений + int directions[8][2] = { + {-1, -1}, {-1, 0}, {-1, 1}, // верхние + {0, -1}, {0, 1}, // левый, правый + {1, -1}, {1, 0}, {1, 1} // нижние + }; + + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + if (matrix[i][j] == 1) { + // Проверяем всех 8 соседей + for (auto& dir : directions) { + int ni = i + dir[0]; + int nj = j + dir[1]; + + if (ni >= 0 && ni < M && nj >= 0 && nj < N) { + if (matrix[ni][nj] == 1) { + islands.dsu_union(i * N + j, ni * N + nj); + } + } + } + } + } + } + + std::unordered_set unique_roots; + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + if (matrix[i][j] == 1) { + unique_roots.insert(islands.dsu_find_recursive(i * N + j)); + } + } + } + return unique_roots.size(); +} + +Matrix generate1(int x, int y, size_t n, size_t m) { + Matrix matrix(n + 1, m + 1); + for (size_t i = 0; i < n + 1; i++) { + for (size_t j = 0; j < m + 1; j++) { + matrix[i][j] = true; + } + } + DSU dsu(n * m); + dsu.dsu_union(x, y); + for (size_t i = 0; i < n; i++) { + for (size_t j = 0; j < m; j++) { + size_t current = i * m + j; + if (j < m - 1 && getRandomIndex(9) >= 3) { + size_t right = i * m + (j + 1); + dsu.dsu_union(current, right); + + matrix[i + 1][j + 1] = false; + if (j + 2 <= m) matrix[i + 1][j + 2] = false; + } + if (i < n - 1 && getRandomIndex(9) >= 3) { + int bottom = (i + 1) * m + j; + dsu.dsu_union(current, bottom); + + matrix[i + 1][j + 1] = false; + if (i + 2 <= n) matrix[i + 2][j + 1] = false; + } + } + } + return matrix; +} +void print_labirint1(Matrix matrix, size_t n, size_t m) { + for (size_t j = 0; j < m; j++) { + std::cout << "+---"; + } + std::cout << "+" << std::endl; + + for (size_t i = 0; i < n; i++) { + std::cout << "|"; + for (size_t j = 0; j < m; j++) { + int cell_num = i * m + j; + if (cell_num < 10) std::cout << " " << cell_num << " "; + else std::cout << cell_num << " "; + + bool has_right_wall = matrix[i][j + 1] && matrix[i + 1][j + 1]; + std::cout << (has_right_wall ? "|" : " "); + } + std::cout << std::endl; + + if (i < n - 1) { + for (size_t j = 0; j < m; j++) { + std::cout << "+"; + bool has_bottom_wall = matrix[i + 1][j] && matrix[i + 1][j + 1]; + std::cout << (has_bottom_wall ? "---" : " "); + } + std::cout << "+" << std::endl; + } + } + + for (size_t j = 0; j < m; j++) { + std::cout << "+---"; + } + std::cout << "+" << std::endl; +} + +DSU generate2(int x, int y, int n, int m) { DSU dsu(n * m); dsu.dsu_union(x, y); - // соединяем соседние с вероятностью 70 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int current = i * m + j; - if (j < m - 1 && getRandomIndex(9) >= 3) { + if (j < m - 1 && getRandomIndex(9) >= 3) { int right = i * m + (j + 1); dsu.dsu_union(current, right); } - if (i < n - 1 && getRandomIndex(9) >= 3) { + if (i < n - 1 && getRandomIndex(9) >= 3) { int bottom = (i + 1) * m + j; dsu.dsu_union(current, bottom); } @@ -290,31 +354,27 @@ DSU generate(int x, int y, int n, int m) { } return dsu; } -void print_labirint(DSU labirint, int n, int m) { - int entry = 0; - int exit = n * m - 1; +void print_labirint2(DSU labirint, int n, int m) { std::cout << "+"; for (int j = 0; j < m; j++) { std::cout << "---+"; } std::cout << std::endl; + for (int i = 0; i < n; i++) { std::cout << "|"; + for (int j = 0; j < m; j++) { int cell = i * m + j; - if (cell < 10) { - std::cout << " " << cell << " "; - } - else { - std::cout << cell << " "; - } + if (cell < 10) std::cout << " " << cell << " "; + else std::cout << cell << " "; if (j < m - 1) { int right_cell = i * m + (j + 1); if (labirint.dsu_find_recursive(cell) == labirint.dsu_find_recursive(right_cell)) { std::cout << " "; } else { - std::cout << "|"; + std::cout << "|"; } } else { @@ -327,20 +387,21 @@ void print_labirint(DSU labirint, int n, int m) { for (int j = 0; j < m; j++) { int cell = i * m + j; int bottom_cell = (i + 1) * m + j; - if (labirint.dsu_find_recursive(cell) == labirint.dsu_find_recursive(bottom_cell)) { std::cout << " +"; } else { - std::cout << "---+"; + std::cout << "---+"; } } std::cout << std::endl; } } + + // Нижняя граница - всегда стена std::cout << "+"; for (int j = 0; j < m; j++) { std::cout << "---+"; } std::cout << std::endl; -} +} \ No newline at end of file From 8ba0b5b1211bf4cd76877ed0beeec6e6be7e2d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 10 Feb 2026 16:12:51 +0300 Subject: [PATCH 244/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=9C=D0=BE=D0=BD=D0=BE=D0=BC=20=D0=B8=20=D0=B2=D1=81=D0=B5=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D1=8B?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/CMakeLists.txt | 1 + lib_monom/monom.cpp | 1 + lib_monom/monom.h | 65 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 lib_monom/CMakeLists.txt create mode 100644 lib_monom/monom.cpp create mode 100644 lib_monom/monom.h diff --git a/lib_monom/CMakeLists.txt b/lib_monom/CMakeLists.txt new file mode 100644 index 00000000..c81f4342 --- /dev/null +++ b/lib_monom/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Monom) \ No newline at end of file diff --git a/lib_monom/monom.cpp b/lib_monom/monom.cpp new file mode 100644 index 00000000..48f83514 --- /dev/null +++ b/lib_monom/monom.cpp @@ -0,0 +1 @@ +#include "monom.h" diff --git a/lib_monom/monom.h b/lib_monom/monom.h new file mode 100644 index 00000000..ca24f622 --- /dev/null +++ b/lib_monom/monom.h @@ -0,0 +1,65 @@ +#include +#include +#include +class Monom { +private: + static constexpr size_t VAR_COUNT = 3; //Для всех объектов класса Monom существует неизменная константа (вычисляется во время компиляции) + double _coefficient; + int _powers[VAR_COUNT]; +public: + double get_coefficient() const noexcept { + return _coefficient; + } + int powerX() const noexcept { + return _powers[0]; + } + int powerY() const noexcept { + return _powers[1]; + } + int powerZ() const noexcept { + return _powers[2]; + } + void set_coefficient(double value) { + _coefficient = value; + } + void set_powers(int value1, int value2, int value3) { + _powers[0] = value1; + _powers[1] = value2; + _powers[2] = value3; + } + Monom(double coefficient = 0.0): _coefficient(coefficient) { + for (size_t i = 0; i < VAR_COUNT; i++) { + _powers[i] = 0; + } + } + Monom(double coefficient, int x_power, int y_power, int z_power) + : _coefficient(coefficient) + , _powers{ x_power, y_power, z_power } { + } + Monom(const Monom& other) = default; + bool is_similar(const Monom& other) const noexcept; + bool operator ==(const Monom& other_monom) const; //проверку подобия двух мономов(мономы подобны, если у них одинаковые степени); + bool operator !=(const Monom& other_monom) const; + bool operator >(const Monom& other_monom) const; + bool operator <(const Monom& other_monom) const; + + Monom& operator=(const Monom& other) = default; + + Monom operator +(const Monom& other_monom) const; + Monom operator -(const Monom& other_monom) const; + Monom operator *(const Monom& other_monom) const; + Monom operator /(const Monom& other_monom) const; + Monom operator /(double value) const; + + Monom& operator +=(const Monom& other_monom); + Monom& operator -=(const Monom& other_monom); + Monom& operator *=(const Monom& other_monom); + Monom& operator /=(const Monom& other_monom); + + Monom operator -() const; //должен возвращать новый моном с противоположным коэффициентом + + double calculate(double x, double y, double z); + + friend std::ostream& operator<<(std::ostream& out, const Monom& monom); + friend std::istream& operator>>(std::istream& in, const Monom& monom); +}; From 6e5731d0d9f7f68dc2c72cfaf1b5c96031152b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 10 Feb 2026 16:44:17 +0300 Subject: [PATCH 245/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=83=D0=BD=D0=B0=D1=80=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=B8=D0=BD=D1=83=D1=81=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BC=D0=BE=D0=BD=D0=BE=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib_monom/monom.h b/lib_monom/monom.h index ca24f622..07ba829c 100644 --- a/lib_monom/monom.h +++ b/lib_monom/monom.h @@ -37,7 +37,6 @@ class Monom { , _powers{ x_power, y_power, z_power } { } Monom(const Monom& other) = default; - bool is_similar(const Monom& other) const noexcept; bool operator ==(const Monom& other_monom) const; //проверку подобия двух мономов(мономы подобны, если у них одинаковые степени); bool operator !=(const Monom& other_monom) const; bool operator >(const Monom& other_monom) const; @@ -56,8 +55,9 @@ class Monom { Monom& operator *=(const Monom& other_monom); Monom& operator /=(const Monom& other_monom); - Monom operator -() const; //должен возвращать новый моном с противоположным коэффициентом - + Monom operator -() const { + return Monom(_coefficient * (-1), _powers[0], _powers[1], _powers[2]); + } double calculate(double x, double y, double z); friend std::ostream& operator<<(std::ostream& out, const Monom& monom); From 2510d00b689f0bc1b20cde276bed37a608deb969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 10 Feb 2026 17:59:42 +0300 Subject: [PATCH 246/275] =?UTF-8?q?=D0=92=20.h=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20con?= =?UTF-8?q?st=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0?= =?UTF-8?q?=20calculate(),=20=D0=B2=20.cpp=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=20=3D=3D,=20!=3D,=20+,=20-,=20*,=20/=20=D0=B8=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=B4=20calculate()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++ lib_monom/monom.h | 4 +-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib_monom/monom.cpp b/lib_monom/monom.cpp index 48f83514..39ba9bfd 100644 --- a/lib_monom/monom.cpp +++ b/lib_monom/monom.cpp @@ -1 +1,73 @@ #include "monom.h" +const double EPSILON = 1e-10; +bool Monom::operator ==(const Monom& other_monom) const { + return this->powerX() == other_monom.powerX() && + this->powerY() == other_monom.powerY() && + this->powerZ() == other_monom.powerZ(); +} +bool Monom::operator !=(const Monom& other_monom) const { + return !(*this == other_monom); +} +//bool Monom::operator >(const Monom& other_monom) const {//? +// if (this->powerX() <= other_monom.powerX()) { +// return false; +// } +// return true; +//} +//bool Monom::operator <(const Monom& other_monom) const {//? +// if (this->powerX() >= other_monom.powerX()) { +// return false; +// } +// return true; +//} +Monom Monom::operator +(const Monom& other_monom) const { + if (*this != other_monom) { + throw std::logic_error("Monoms must be similar"); + } + double new_coefficient = this->get_coefficient() + other_monom.get_coefficient(); + return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); +} +Monom Monom::operator -(const Monom& other_monom) const { + if (*this != other_monom) { + throw std::logic_error("Monoms must be similar"); + } + double new_coefficient = this->get_coefficient() - other_monom.get_coefficient(); + return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); +} +Monom Monom::operator *(const Monom& other_monom) const { + double new_coefficient = this->get_coefficient() * other_monom.get_coefficient(); + int new_x = this->powerX() + other_monom.powerX(); + int new_y = this->powerY() + other_monom.powerY(); + int new_z = this->powerZ() + other_monom.powerZ(); + return Monom(new_coefficient, new_x, new_y, new_z); +} +Monom Monom::operator /(const Monom& other_monom) const { + if (std::abs(other_monom.get_coefficient()) < EPSILON) { + throw std::logic_error("Division by zero"); + } + + if (other_monom.powerX() > this->powerX() || + other_monom.powerY() > this->powerY() || + other_monom.powerZ() > this->powerZ()) { + throw std::logic_error("Negative power in division"); + } + + double new_coefficient = this->get_coefficient() / other_monom.get_coefficient(); + int new_x = this->powerX() - other_monom.powerX(); + int new_y = this->powerY() - other_monom.powerY(); + int new_z = this->powerZ() - other_monom.powerZ(); + return Monom(new_coefficient, new_x, new_y, new_z); +} +Monom Monom::operator /(double value) const { + if (std::abs(value) < EPSILON) { + throw std::logic_error("Division by zero"); + } + double new_coefficient = this->get_coefficient() / value; + return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); +} +double Monom::calculate(double x, double y, double z) const { + return _coefficient * + std::pow(x, _powers[0]) * + std::pow(y, _powers[1]) * + std::pow(z, _powers[2]); +} diff --git a/lib_monom/monom.h b/lib_monom/monom.h index 07ba829c..44c078d1 100644 --- a/lib_monom/monom.h +++ b/lib_monom/monom.h @@ -37,7 +37,7 @@ class Monom { , _powers{ x_power, y_power, z_power } { } Monom(const Monom& other) = default; - bool operator ==(const Monom& other_monom) const; //проверку подобия двух мономов(мономы подобны, если у них одинаковые степени); + bool operator ==(const Monom& other_monom) const; bool operator !=(const Monom& other_monom) const; bool operator >(const Monom& other_monom) const; bool operator <(const Monom& other_monom) const; @@ -58,7 +58,7 @@ class Monom { Monom operator -() const { return Monom(_coefficient * (-1), _powers[0], _powers[1], _powers[2]); } - double calculate(double x, double y, double z); + double calculate(double x, double y, double z) const; friend std::ostream& operator<<(std::ostream& out, const Monom& monom); friend std::istream& operator>>(std::istream& in, const Monom& monom); From b4022ecc2e3f748e643f2e292f3442d67d809ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 10 Feb 2026 18:10:15 +0300 Subject: [PATCH 247/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D1=88=D0=B8?= =?UTF-8?q?=D1=85=D1=81=D1=8F=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D0=BE=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/lib_monom/monom.cpp b/lib_monom/monom.cpp index 39ba9bfd..0bfa981a 100644 --- a/lib_monom/monom.cpp +++ b/lib_monom/monom.cpp @@ -45,13 +45,11 @@ Monom Monom::operator /(const Monom& other_monom) const { if (std::abs(other_monom.get_coefficient()) < EPSILON) { throw std::logic_error("Division by zero"); } - if (other_monom.powerX() > this->powerX() || other_monom.powerY() > this->powerY() || other_monom.powerZ() > this->powerZ()) { throw std::logic_error("Negative power in division"); } - double new_coefficient = this->get_coefficient() / other_monom.get_coefficient(); int new_x = this->powerX() - other_monom.powerX(); int new_y = this->powerY() - other_monom.powerY(); @@ -65,6 +63,48 @@ Monom Monom::operator /(double value) const { double new_coefficient = this->get_coefficient() / value; return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); } +Monom& Monom::operator +=(const Monom& other_monom) { + if (*this != other_monom) { + throw std::logic_error("Monoms must be similar"); + } + double new_coefficient = this->get_coefficient() + other_monom.get_coefficient(); + this->set_coefficient(new_coefficient); + return *this; +} +Monom& Monom::operator -=(const Monom& other_monom) { + if (*this != other_monom) { + throw std::logic_error("Monoms must be similar"); + } + double new_coefficient = this->get_coefficient() - other_monom.get_coefficient(); + this->set_coefficient(new_coefficient); + return *this; +} +Monom& Monom::operator *=(const Monom& other_monom) { + double new_coefficient = this->get_coefficient() * other_monom.get_coefficient(); + int new_x = this->powerX() + other_monom.powerX(); + int new_y = this->powerY() + other_monom.powerY(); + int new_z = this->powerZ() + other_monom.powerZ(); + this->set_coefficient(new_coefficient); + this->set_powers(new_x, new_y, new_z); + return *this; +} +Monom& Monom::operator /=(const Monom& other_monom) { + if (std::abs(other_monom.get_coefficient()) < EPSILON) { + throw std::logic_error("Division by zero"); + } + if (other_monom.powerX() > this->powerX() || + other_monom.powerY() > this->powerY() || + other_monom.powerZ() > this->powerZ()) { + throw std::logic_error("Negative power in division"); + } + double new_coefficient = this->get_coefficient() / other_monom.get_coefficient(); + int new_x = this->powerX() - other_monom.powerX(); + int new_y = this->powerY() - other_monom.powerY(); + int new_z = this->powerZ() - other_monom.powerZ(); + this->set_coefficient(new_coefficient); + this->set_powers(new_x, new_y, new_z); + return *this; +} double Monom::calculate(double x, double y, double z) const { return _coefficient * std::pow(x, _powers[0]) * From 3fab68290a5a6e8b2ae860a933ca009366efe071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Tue, 10 Feb 2026 19:05:08 +0300 Subject: [PATCH 248/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib_monom/monom.h b/lib_monom/monom.h index 44c078d1..ce87503c 100644 --- a/lib_monom/monom.h +++ b/lib_monom/monom.h @@ -34,7 +34,10 @@ class Monom { } Monom(double coefficient, int x_power, int y_power, int z_power) : _coefficient(coefficient) - , _powers{ x_power, y_power, z_power } { + , _powers{ x_power, y_power, z_power } { + if (x_power < 0 || y_power < 0 || z_power < 0) { + throw std::invalid_argument("Powers cannot be negative"); + } } Monom(const Monom& other) = default; bool operator ==(const Monom& other_monom) const; @@ -61,5 +64,5 @@ class Monom { double calculate(double x, double y, double z) const; friend std::ostream& operator<<(std::ostream& out, const Monom& monom); - friend std::istream& operator>>(std::istream& in, const Monom& monom); + friend std::istream& operator>>(std::istream& input, Monom& monom); }; From e6fdc6868d104efdebfc35cded3058f11cfaf9d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 09:25:19 +0300 Subject: [PATCH 249/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=8E=D1=89=D0=B8=D1=85=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D1=83=D0=BC=D0=BD?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib_monom/monom.h b/lib_monom/monom.h index ce87503c..55312403 100644 --- a/lib_monom/monom.h +++ b/lib_monom/monom.h @@ -50,19 +50,21 @@ class Monom { Monom operator +(const Monom& other_monom) const; Monom operator -(const Monom& other_monom) const; Monom operator *(const Monom& other_monom) const; + Monom operator *(double value) const; Monom operator /(const Monom& other_monom) const; Monom operator /(double value) const; Monom& operator +=(const Monom& other_monom); Monom& operator -=(const Monom& other_monom); Monom& operator *=(const Monom& other_monom); + Monom& operator *=(double value); Monom& operator /=(const Monom& other_monom); + Monom& operator /=(double value); Monom operator -() const { return Monom(_coefficient * (-1), _powers[0], _powers[1], _powers[2]); } double calculate(double x, double y, double z) const; - friend std::ostream& operator<<(std::ostream& out, const Monom& monom); friend std::istream& operator>>(std::istream& input, Monom& monom); }; From 1547f74135b04f882bb0f08b8541fb88fb57782e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 09:49:45 +0300 Subject: [PATCH 250/275] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20>,(const Monom& other_monom) const {//? -// if (this->powerX() <= other_monom.powerX()) { -// return false; -// } -// return true; -//} -//bool Monom::operator <(const Monom& other_monom) const {//? -// if (this->powerX() >= other_monom.powerX()) { -// return false; -// } -// return true; -//} +bool Monom::operator >(const Monom& other_monom) const { + int total_degree_this = this->powerX() + this->powerY() + this->powerZ(); + int total_degree_other = other_monom.powerX() + other_monom.powerY() + other_monom.powerZ(); + + if (total_degree_this == total_degree_other) { + + int pow_x_this = this->powerX(); + int pow_y_this = this->powerY(); + int pow_z_this = this->powerZ(); + + int pow_x_other = other_monom.powerX(); + int pow_y_other = other_monom.powerY(); + int pow_z_other = other_monom.powerZ(); + + if (pow_x_this != pow_x_other) { + return (pow_x_this > pow_x_other); + } + if (pow_y_this != pow_y_other) { + return (pow_y_this > pow_y_other); + } + if (pow_z_this != pow_z_other) { + return (pow_z_this > pow_z_other); + } + return false; + } + return total_degree_this > total_degree_other; +} +bool Monom::operator<(const Monom& other_monom) const { + return other_monom > *this; +} Monom Monom::operator +(const Monom& other_monom) const { if (*this != other_monom) { throw std::logic_error("Monoms must be similar"); @@ -111,3 +129,20 @@ double Monom::calculate(double x, double y, double z) const { std::pow(y, _powers[1]) * std::pow(z, _powers[2]); } +//std::ostream& operator<<(std::ostream& out, const Monom& monom) { +// double coefficient = monom.get_coefficient(); +// int power_x = monom.powerX(); +// int power_y = monom.powerY(); +// int power_z = monom.powerZ(); +// if (std::abs(coefficient) < EPSILON) { +// out << "0"; +// return out; +// } +// return out; +//} +//std::istream& operator>>(std::istream& input, Monom& monom) { +// double coefficient; +// int power_x, power_y, power_z; +// input >> coefficient >> power_x >> power_y >> power_z; +// return input; +//} From e210827f2fb51bd1904d375a05fbc35d9eae5fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 09:57:10 +0300 Subject: [PATCH 251/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=8E=D1=89=D0=B8=D1=85=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib_monom/monom.cpp b/lib_monom/monom.cpp index 8c1bd542..e87ee849 100644 --- a/lib_monom/monom.cpp +++ b/lib_monom/monom.cpp @@ -59,6 +59,10 @@ Monom Monom::operator *(const Monom& other_monom) const { int new_z = this->powerZ() + other_monom.powerZ(); return Monom(new_coefficient, new_x, new_y, new_z); } +Monom Monom::operator *(double value) const { + double new_coefficient = value * this->get_coefficient(); + return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); +} Monom Monom::operator /(const Monom& other_monom) const { if (std::abs(other_monom.get_coefficient()) < EPSILON) { throw std::logic_error("Division by zero"); @@ -106,6 +110,10 @@ Monom& Monom::operator *=(const Monom& other_monom) { this->set_powers(new_x, new_y, new_z); return *this; } +Monom& Monom::operator *=(double value) { + this->set_coefficient(this->get_coefficient() * value); + return *this; +} Monom& Monom::operator /=(const Monom& other_monom) { if (std::abs(other_monom.get_coefficient()) < EPSILON) { throw std::logic_error("Division by zero"); @@ -123,6 +131,13 @@ Monom& Monom::operator /=(const Monom& other_monom) { this->set_powers(new_x, new_y, new_z); return *this; } +Monom& Monom::operator /=(double value) { + if (std::abs(value < EPSILON)) { + throw std::invalid_argument("Division by zero"); + } + this->set_coefficient(this->get_coefficient() / value); + return *this; +} double Monom::calculate(double x, double y, double z) const { return _coefficient * std::pow(x, _powers[0]) * From 8526f773d52f2afc526930f8575153f57d9be568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 10:38:45 +0300 Subject: [PATCH 252/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20=D0=9C=D0=BE=D0=BD=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_monom.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/test_monom.cpp diff --git a/tests/test_monom.cpp b/tests/test_monom.cpp new file mode 100644 index 00000000..cfabbd64 --- /dev/null +++ b/tests/test_monom.cpp @@ -0,0 +1,39 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_monom/monom.h" +const double TEST_EPSILON = 1e-10; +TEST(TestMonomLib, monom_default_constructor) { + // Arrange & Act + Monom monom; + // Assert + EXPECT_TRUE(std::abs(monom.get_coefficient()) < TEST_EPSILON); + EXPECT_EQ(monom.powerX(), 0); + EXPECT_EQ(monom.powerY(), 0); + EXPECT_EQ(monom.powerZ(), 0); +} +TEST(TestMonomLib, monom_constructor_with_arguments) { + // Arrange & Act + double coefficient = 1.5; + int power_x = 2; + int power_y = 3; + int power_z = 5; + Monom monom(coefficient, power_x, power_y, power_z); + // Assert + EXPECT_EQ(monom.get_coefficient(), coefficient); + EXPECT_EQ(monom.powerX(), power_x); + EXPECT_EQ(monom.powerY(), power_y); + EXPECT_EQ(monom.powerZ(), power_z); +} +TEST(TestMonomLib, constructor_with_arguments_throws_on_negative_power_x) { + ASSERT_THROW(Monom(1.0, -1, 0, 0), std::invalid_argument); +} +TEST(TestMonomLib, constructor_with_arguments_throws_on_negative_power_y) { + ASSERT_THROW(Monom(1.0, 0, -1, 0), std::invalid_argument); +} +TEST(TestMonomLib, constructor_with_arguments_throws_on_negative_power_z) { + ASSERT_THROW(Monom(1.0, 0, 0, -1), std::invalid_argument); +} +TEST(TestMonomLib, constructor_with_arguments_throws_on_all_negative_powers) { + ASSERT_THROW(Monom(1.0, -1, -2, -3), std::invalid_argument); +} From b15773a63c4c34754b552f3acfde75ca9d279ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 11:27:44 +0300 Subject: [PATCH 253/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=BE=D0=B1=D0=B8=D1=8F=20=D0=BC=D0=BE=D0=BD?= =?UTF-8?q?=D0=BE=D0=BC=D0=BE=D0=B2=20=D0=B8=20>, monom2); +} +TEST(TestMonomLib, monom1_less_monom2_true) { + // Arrange & Act + Monom monom1(10, 1, 1, 2); + Monom monom2(34, 2, 3, 4); + // Assert + EXPECT_TRUE(monom1 < monom2); +} +TEST(TestMonomLib, monom1_greater_monom2_false) { + // Arrange & Act + Monom monom1(130, 10, 3, 4); + Monom monom2(35, 2, 3, 389); + // Assert + EXPECT_FALSE(monom1 > monom2); +} +TEST(TestMonomLib, monom1_less_monom2_false) { + // Arrange & Act + Monom monom1(10, 1, 39, 2); + Monom monom2(34, 2, 3, 4); + // Assert + EXPECT_FALSE(monom1 < monom2); } From ca4bf2d6ed15d63411092b0f49bf0b8fae2ca49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 11:54:03 +0300 Subject: [PATCH 254/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D1=84=D0=BC=D0=B5=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8?= =?UTF-8?q?=D1=85=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=20(=D0=B1=D0=B5=D0=B7=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2?= =?UTF-8?q?=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F),=20=D0=B0=20=D1=82?= =?UTF-8?q?=D0=B0=D0=BA=D0=B6=D0=B5=20=D0=BD=D0=B0=20=D1=81=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B0=D0=B8=20=D1=81=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=D0=BC=D0=B8=20(=D0=BE=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5=20=D1=81=D1=82=D0=B5=D0=BF=D0=B5=D0=BD=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=200.0?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_monom.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/tests/test_monom.cpp b/tests/test_monom.cpp index 11b9e66d..a37ab913 100644 --- a/tests/test_monom.cpp +++ b/tests/test_monom.cpp @@ -88,8 +88,90 @@ TEST(TestMonomLib, monom1_greater_monom2_false) { } TEST(TestMonomLib, monom1_less_monom2_false) { // Arrange & Act - Monom monom1(10, 1, 39, 2); + Monom monom1(10.1, 1, 39, 2); Monom monom2(34, 2, 3, 4); // Assert EXPECT_FALSE(monom1 < monom2); } +TEST(TestMonomLib, add_monoms) { + // Arrange & Act + Monom monom1(23, 1, 5, 2); + Monom monom2(44, 1, 5, 2); + Monom result = monom1 + monom2; + // Assert + EXPECT_EQ(result.get_coefficient(), monom1.get_coefficient() + monom2.get_coefficient()); +} +TEST(TestMonomLib, add_monoms_with_exception) { + // Arrange & Act + Monom monom1(23, 1, 5, 2); + Monom monom2(44, 1, 55, 2); + // Assert + ASSERT_THROW(monom1 + monom2, std::invalid_argument); +} +TEST(TestMonomLib, sub_monoms) { + // Arrange & Act + Monom monom1(33.9, 1, 5, 2); + Monom monom2(44, 1, 5, 2); + Monom result = monom1 - monom2; + // Assert + EXPECT_EQ(result.get_coefficient(), monom1.get_coefficient() - monom2.get_coefficient()); +} +TEST(TestMonomLib, sub_monoms_with_exception) { + // Arrange & Act + Monom monom1(23, 1, 5, 2); + Monom monom2(4.4, 1, 5, 772); + // Assert + ASSERT_THROW(monom1 - monom2, std::invalid_argument); +} +TEST(TestMonomLib,mult_monoms) { + // Arrange & Act + Monom monom1(23.8, 33, 5, 2); + Monom monom2(44, 2, 4555, 7); + Monom result = monom1 * monom2; + // Assert + EXPECT_EQ(result.get_coefficient(), monom1.get_coefficient() * monom2.get_coefficient()); + EXPECT_EQ(result.powerX(), monom1.powerX() + monom2.powerX()); + EXPECT_EQ(result.powerY(), monom1.powerY() + monom2.powerY()); + EXPECT_EQ(result.powerZ(), monom1.powerZ() + monom2.powerZ()); +} +TEST(TestMonomLib, div_monoms) { + // Arrange & Act + Monom monom1(23.9, 33, 4555, 22); + Monom monom2(33.3, 2, 5, 7); + Monom result = monom1 / monom2; + // Assert + EXPECT_EQ(result.get_coefficient(), monom1.get_coefficient() / monom2.get_coefficient()); + EXPECT_EQ(result.powerX(), monom1.powerX() - monom2.powerX()); + EXPECT_EQ(result.powerY(), monom1.powerY() - monom2.powerY()); + EXPECT_EQ(result.powerZ(), monom1.powerZ() - monom2.powerZ()); +} +TEST(TestMonomLib, div_monoms_with_exception) { + // Arrange & Act + Monom monom1(25.9, 2, 3, 1); + Monom monom2(3.3, 3, 2, 1); + // Assert + ASSERT_THROW(monom1 / monom2, std::invalid_argument); +} +TEST(TestMonomLib, monom_mult_value) { + // Arrange & Act + Monom monom(223.39, 33, 4555, 22); + double value = 22.5; + Monom result = monom * value; + // Assert + EXPECT_EQ(result.get_coefficient(), monom.get_coefficient() * value); +} +TEST(TestMonomLib, monom_div_value) { + // Arrange & Act + Monom monom(223.39, 33, 4555, 22); + double value = 223.5; + Monom result = monom / value; + // Assert + EXPECT_EQ(result.get_coefficient(), monom.get_coefficient() / value); +} +TEST(TestMonomLib, monom_div_value_with_exception) { + // Arrange & Act + Monom monom(223.39, 333, 4, 2); + double value = 0.0; + // Assert + ASSERT_THROW(monom / value, std::invalid_argument); +} From c3219823ca6893b605c69932d7e79a1ee5afc25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 11:59:40 +0300 Subject: [PATCH 255/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5=D1=82=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B=20=D1=81=20=D0=BF=D1=80=D0=B8=D1=81?= =?UTF-8?q?=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_monom.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/tests/test_monom.cpp b/tests/test_monom.cpp index a37ab913..d7b553e8 100644 --- a/tests/test_monom.cpp +++ b/tests/test_monom.cpp @@ -175,3 +175,134 @@ TEST(TestMonomLib, monom_div_value_with_exception) { // Assert ASSERT_THROW(monom / value, std::invalid_argument); } +TEST(TestMonomLib, plus_equals_operator) { + // Arrange & Act + Monom monom1(23, 1, 5, 2); + Monom monom2(44, 1, 5, 2); + monom1 += monom2; + // Assert + EXPECT_EQ(monom1.get_coefficient(), 67); + EXPECT_EQ(monom1.powerX(), 1); + EXPECT_EQ(monom1.powerY(), 5); + EXPECT_EQ(monom1.powerZ(), 2); +} +TEST(TestMonomLib, plus_equals_operator_with_exception) { + // Arrange & Act + Monom monom1(23, 1, 5, 2); + Monom monom2(44, 1, 55, 2); + // Assert + ASSERT_THROW(monom1 += monom2, std::invalid_argument); +} +TEST(TestMonomLib, minus_equals_operator) { + // Arrange & Act + Monom monom1(50, 3, 2, 1); + Monom monom2(30, 3, 2, 1); + monom1 -= monom2; + // Assert + EXPECT_EQ(monom1.get_coefficient(), 20); + EXPECT_EQ(monom1.powerX(), 3); + EXPECT_EQ(monom1.powerY(), 2); + EXPECT_EQ(monom1.powerZ(), 1); +} +TEST(TestMonomLib, minus_equals_operator_with_exception) { + // Arrange & Act + Monom monom1(50, 3, 2, 1); + Monom monom2(30, 3, 2, 11); + // Assert + ASSERT_THROW(monom1 -= monom2, std::invalid_argument); +} +TEST(TestMonomLib, multiply_equals_monom_operator) { + // Arrange & Act + Monom monom1(2, 3, 2, 1); + Monom monom2(3, 1, 2, 3); + monom1 *= monom2; + // Assert + EXPECT_EQ(monom1.get_coefficient(), 6); + EXPECT_EQ(monom1.powerX(), 4); + EXPECT_EQ(monom1.powerY(), 4); + EXPECT_EQ(monom1.powerZ(), 4); +} +TEST(TestMonomLib, multiply_equals_scalar_operator) { + // Arrange & Act + Monom monom(5, 2, 3, 1); + double value = 3.5; + monom *= value; + // Assert + EXPECT_EQ(monom.get_coefficient(), 17.5); + EXPECT_EQ(monom.powerX(), 2); + EXPECT_EQ(monom.powerY(), 3); + EXPECT_EQ(monom.powerZ(), 1); +} +TEST(TestMonomLib, divide_equals_monom_operator) { + // Arrange & Act + Monom monom1(12, 4, 3, 2); + Monom monom2(3, 1, 1, 1); + monom1 /= monom2; + // Assert + EXPECT_EQ(monom1.get_coefficient(), 4); + EXPECT_EQ(monom1.powerX(), 3); + EXPECT_EQ(monom1.powerY(), 2); + EXPECT_EQ(monom1.powerZ(), 1); +} +TEST(TestMonomLib, divide_equals_monom_operator_with_exception_division_by_zero) { + // Arrange & Act + Monom monom1(12, 4, 3, 2); + Monom monom2(0, 1, 1, 1); + // Assert + ASSERT_THROW(monom1 /= monom2, std::invalid_argument); +} +TEST(TestMonomLib, divide_equals_monom_operator_with_exception_negative_power) { + // Arrange & Act + Monom monom1(12, 1, 2, 3); + Monom monom2(3, 2, 1, 1); + // Assert + ASSERT_THROW(monom1 /= monom2, std::invalid_argument); +} +TEST(TestMonomLib, divide_equals_scalar_operator) { + // Arrange & Act + Monom monom(15, 2, 3, 1); + double value = 3.0; + monom /= value; + // Assert + EXPECT_EQ(monom.get_coefficient(), 5); + EXPECT_EQ(monom.powerX(), 2); + EXPECT_EQ(monom.powerY(), 3); + EXPECT_EQ(monom.powerZ(), 1); +} +TEST(TestMonomLib, divide_equals_scalar_operator_with_exception) { + // Arrange & Act + Monom monom(15, 2, 3, 1); + double value = 0.0; + // Assert + ASSERT_THROW(monom /= value, std::invalid_argument); +} +TEST(TestMonomLib, calculate_method) { + // Arrange & Act + Monom monom(2, 2, 1, 3); + double x = 3.0; + double y = 2.0; + double z = 1.0; + double result = monom.calculate(x, y, z); + // Assert + EXPECT_EQ(result, 36); +} +TEST(TestMonomLib, calculate_method_with_zero_powers) { + // Arrange & Act + Monom monom(5, 0, 0, 0); + double x = 10.0; + double y = 20.0; + double z = 30.0; + double result = monom.calculate(x, y, z); + // Assert + EXPECT_EQ(result, 5); +} +TEST(TestMonomLib, calculate_method_with_negative_coefficient) { + // Arrange & Act + Monom monom(-3, 1, 2, 0); + double x = 2.0; + double y = 3.0; + double z = 1.0; + double result = monom.calculate(x, y, z); + // Assert + EXPECT_EQ(result, -54); +} From 02a1bfd1fb5e3ffd7ccb8214514892212be48419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Wed, 11 Feb 2026 12:11:50 +0300 Subject: [PATCH 256/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=20=D0=B2=D0=B2=D0=BE=D0=B4=D0=B0-=D0=B2?= =?UTF-8?q?=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20=D0=B8=20=D0=BD=D0=B5=D0=B1?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_monom/monom.cpp | 90 +++++++++++++++++++++++++++++++-------------- lib_monom/monom.h | 3 ++ 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/lib_monom/monom.cpp b/lib_monom/monom.cpp index e87ee849..5621484c 100644 --- a/lib_monom/monom.cpp +++ b/lib_monom/monom.cpp @@ -40,14 +40,14 @@ bool Monom::operator<(const Monom& other_monom) const { } Monom Monom::operator +(const Monom& other_monom) const { if (*this != other_monom) { - throw std::logic_error("Monoms must be similar"); + throw std::invalid_argument("Monoms must be similar"); } double new_coefficient = this->get_coefficient() + other_monom.get_coefficient(); return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); } Monom Monom::operator -(const Monom& other_monom) const { if (*this != other_monom) { - throw std::logic_error("Monoms must be similar"); + throw std::invalid_argument("Monoms must be similar"); } double new_coefficient = this->get_coefficient() - other_monom.get_coefficient(); return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); @@ -65,12 +65,12 @@ Monom Monom::operator *(double value) const { } Monom Monom::operator /(const Monom& other_monom) const { if (std::abs(other_monom.get_coefficient()) < EPSILON) { - throw std::logic_error("Division by zero"); + throw std::invalid_argument("Division by zero"); } if (other_monom.powerX() > this->powerX() || other_monom.powerY() > this->powerY() || other_monom.powerZ() > this->powerZ()) { - throw std::logic_error("Negative power in division"); + throw std::invalid_argument("Negative power in division"); } double new_coefficient = this->get_coefficient() / other_monom.get_coefficient(); int new_x = this->powerX() - other_monom.powerX(); @@ -80,14 +80,14 @@ Monom Monom::operator /(const Monom& other_monom) const { } Monom Monom::operator /(double value) const { if (std::abs(value) < EPSILON) { - throw std::logic_error("Division by zero"); + throw std::invalid_argument("Division by zero"); } double new_coefficient = this->get_coefficient() / value; return Monom(new_coefficient, this->powerX(), this->powerY(), this->powerZ()); } Monom& Monom::operator +=(const Monom& other_monom) { if (*this != other_monom) { - throw std::logic_error("Monoms must be similar"); + throw std::invalid_argument("Monoms must be similar"); } double new_coefficient = this->get_coefficient() + other_monom.get_coefficient(); this->set_coefficient(new_coefficient); @@ -95,7 +95,7 @@ Monom& Monom::operator +=(const Monom& other_monom) { } Monom& Monom::operator -=(const Monom& other_monom) { if (*this != other_monom) { - throw std::logic_error("Monoms must be similar"); + throw std::invalid_argument("Monoms must be similar"); } double new_coefficient = this->get_coefficient() - other_monom.get_coefficient(); this->set_coefficient(new_coefficient); @@ -116,12 +116,12 @@ Monom& Monom::operator *=(double value) { } Monom& Monom::operator /=(const Monom& other_monom) { if (std::abs(other_monom.get_coefficient()) < EPSILON) { - throw std::logic_error("Division by zero"); + throw std::invalid_argument("Division by zero"); } if (other_monom.powerX() > this->powerX() || other_monom.powerY() > this->powerY() || other_monom.powerZ() > this->powerZ()) { - throw std::logic_error("Negative power in division"); + throw std::invalid_argument("Negative power in division"); } double new_coefficient = this->get_coefficient() / other_monom.get_coefficient(); int new_x = this->powerX() - other_monom.powerX(); @@ -132,7 +132,7 @@ Monom& Monom::operator /=(const Monom& other_monom) { return *this; } Monom& Monom::operator /=(double value) { - if (std::abs(value < EPSILON)) { + if (std::abs(value) < EPSILON) { throw std::invalid_argument("Division by zero"); } this->set_coefficient(this->get_coefficient() / value); @@ -144,20 +144,56 @@ double Monom::calculate(double x, double y, double z) const { std::pow(y, _powers[1]) * std::pow(z, _powers[2]); } -//std::ostream& operator<<(std::ostream& out, const Monom& monom) { -// double coefficient = monom.get_coefficient(); -// int power_x = monom.powerX(); -// int power_y = monom.powerY(); -// int power_z = monom.powerZ(); -// if (std::abs(coefficient) < EPSILON) { -// out << "0"; -// return out; -// } -// return out; -//} -//std::istream& operator>>(std::istream& input, Monom& monom) { -// double coefficient; -// int power_x, power_y, power_z; -// input >> coefficient >> power_x >> power_y >> power_z; -// return input; -//} +std::ostream& operator<<(std::ostream& out, const Monom& monom) { + double coefficient = monom.get_coefficient(); + int power_x = monom.powerX(); + int power_y = monom.powerY(); + int power_z = monom.powerZ(); + + if (std::abs(coefficient) < EPSILON) { + out << "0"; + return out; + } + if (std::abs(coefficient - 1.0) > EPSILON || + (power_x == 0 && power_y == 0 && power_z == 0)) { + out << coefficient; + } + if (power_x > 0) { + out << "x"; + if (power_x > 1) { + out << "^" << power_x; + } + } + if (power_y > 0) { + out << "y"; + if (power_y > 1) { + out << "^" << power_y; + } + } + if (power_z > 0) { + out << "z"; + if (power_z > 1) { + out << "^" << power_z; + } + } + if (std::abs(coefficient - 1.0) < EPSILON && + power_x == 0 && power_y == 0 && power_z == 0) { + out << "1"; + } + return out; +} +std::istream& operator>>(std::istream& input, Monom& monom) { + double coefficient; + int power_x, power_y, power_z; + input >> coefficient >> power_x >> power_y >> power_z; + if (input) { + if (power_x >= 0 && power_y >= 0 && power_z >= 0) { + monom.set_coefficient(coefficient); + monom.set_powers(power_x, power_y, power_z); + } + else { + input.setstate(std::ios::failbit); + } + } + return input; +} diff --git a/lib_monom/monom.h b/lib_monom/monom.h index 55312403..412d4085 100644 --- a/lib_monom/monom.h +++ b/lib_monom/monom.h @@ -23,6 +23,9 @@ class Monom { _coefficient = value; } void set_powers(int value1, int value2, int value3) { + if (value1 < 0 || value2 < 0 || value3 < 0) { + throw std::invalid_argument("Powers cannot be negative"); + } _powers[0] = value1; _powers[1] = value2; _powers[2] = value3; From 6e32682a138b2ea65a08282fc88f0bd308ffca21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 12 Feb 2026 16:42:19 +0300 Subject: [PATCH 257/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=9F?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE=D0=BC=20(=D0=B2=20=D1=82=D0=BE?= =?UTF-8?q?=D0=BC=20=D1=87=D0=B8=D1=81=D0=BB=D0=B5=20=D0=BE=D0=B1=D1=8A?= =?UTF-8?q?=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20-=20=D0=BF=D0=BE=D0=BB=D0=BD=D0=BE=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/CMakeLists.txt | 1 + lib_polynom/polynom.cpp | 2 ++ lib_polynom/polynom.h | 57 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 lib_polynom/CMakeLists.txt create mode 100644 lib_polynom/polynom.cpp create mode 100644 lib_polynom/polynom.h diff --git a/lib_polynom/CMakeLists.txt b/lib_polynom/CMakeLists.txt new file mode 100644 index 00000000..e0f32730 --- /dev/null +++ b/lib_polynom/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(Polynom) \ No newline at end of file diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp new file mode 100644 index 00000000..c307ab71 --- /dev/null +++ b/lib_polynom/polynom.cpp @@ -0,0 +1,2 @@ +#include "polynom.h" +const double EPSILON = 1e-10; diff --git a/lib_polynom/polynom.h b/lib_polynom/polynom.h new file mode 100644 index 00000000..68a92bde --- /dev/null +++ b/lib_polynom/polynom.h @@ -0,0 +1,57 @@ +#include "../lib_monom/monom.h" +#include "../lib_list/List.h" +#include +#include +#include + +class Polynom { +private: + List _polynom; + + void simplify(); // удалить нулевые, объединить подобные + void sort(); // упорядочить +public: + Polynom() : _polynom() {} + Polynom(const Monom& monom) { + _polynom.push_back(monom); + } + Polynom(const std::string& string); + Polynom(const char* str); + + Polynom& operator=(const Polynom& other); + + Polynom operator +(const Polynom& other_polynom) const; + Polynom operator -(const Polynom& other_polynom) const; + Polynom operator *(const Polynom& other_polynom) const; + /*Polynom operator /(const Polynom& other_polynom) const;*/ + + Polynom& operator +=(const Polynom& other_polynom); + Polynom& operator -=(const Polynom& other_polynom); + Polynom& operator *=(const Polynom& other_polynom); + /* Polynom& operator /=(const Polynom& other_polynom);*/ + + Polynom operator +(const Monom& other_monom) const; + Polynom operator -(const Monom& other_monom) const; + Polynom operator *(const Monom& other_monom) const; + Polynom operator /(const Monom& other_monom) const; + + Polynom& operator +=(const Monom& other_monom); + Polynom& operator -=(const Monom& other_monom); + Polynom& operator *=(const Monom& other_monom); + Polynom& operator /=(const Monom& other_monom); + // Унарные операторы + Polynom operator-() const; + + // Вычисление значения + double calculate(double x, double y, double z) const; + + // Методы для работы с полиномом + bool is_zero() const noexcept { + return _polynom.is_empty(); + } + size_t size() const noexcept { + return _polynom.get_size(); + } + friend std::ostream& operator<<(std::ostream& out, const Polynom& polynom); + friend std::istream& operator>>(std::istream& input, Polynom& polynom); +}; From 5c6c0a622758bdf3232b74ad23e558104d5fa946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 12 Feb 2026 17:05:52 +0300 Subject: [PATCH 258/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=20+,=20-,=20*=20=D0=B4=D0=BB=D1=8F=20=D0=B4?= =?UTF-8?q?=D0=B2=D1=83=D1=85=20=D0=BF=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE=D0=BC?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp index c307ab71..bcfb8044 100644 --- a/lib_polynom/polynom.cpp +++ b/lib_polynom/polynom.cpp @@ -1,2 +1,26 @@ #include "polynom.h" const double EPSILON = 1e-10; + +Polynom Polynom::operator +(const Polynom& other_polynom) const { + Polynom result = *this; + for (auto it = other_polynom._polynom.begin(); it != other_polynom._polynom.end(); it++) { + result += *it; + } + return result; +} +Polynom Polynom::operator -(const Polynom& other_polynom) const { + Polynom result = *this; + for (auto it = other_polynom._polynom.begin(); it != other_polynom._polynom.end(); it++) { + result -= *it; + } + return result; +} +Polynom Polynom::operator *(const Polynom& other_polynom) const { // x^2, y // y^2, z + Polynom result; + for (auto it1 = _polynom.begin(); it1 != _polynom.end(); it1++) { + for (auto it2 = other_polynom._polynom.begin(); it2 != other_polynom._polynom.end(); it2++) { + result += (*it1) * (*it2); + } + } + return result; +} From 38d71d361c119147a0676daf5506be8ed818adb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 14 Feb 2026 15:48:14 +0300 Subject: [PATCH 259/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=83=D0=BD=D0=B0=D1=80=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=B8=D0=BD=D1=83=D1=81=D0=B0=20=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D1=85=20=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D1=84=D0=BC=D0=B5=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=D1=85=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.cpp | 54 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp index bcfb8044..333ac50f 100644 --- a/lib_polynom/polynom.cpp +++ b/lib_polynom/polynom.cpp @@ -15,7 +15,7 @@ Polynom Polynom::operator -(const Polynom& other_polynom) const { } return result; } -Polynom Polynom::operator *(const Polynom& other_polynom) const { // x^2, y // y^2, z +Polynom Polynom::operator *(const Polynom& other_polynom) const { Polynom result; for (auto it1 = _polynom.begin(); it1 != _polynom.end(); it1++) { for (auto it2 = other_polynom._polynom.begin(); it2 != other_polynom._polynom.end(); it2++) { @@ -24,3 +24,55 @@ Polynom Polynom::operator *(const Polynom& other_polynom) const { // x^2, y // } return result; } +Polynom& Polynom::operator +=(const Polynom& other_polynom) { + *this = *this + other_polynom; + return *this; +} +Polynom& Polynom::operator -=(const Polynom& other_polynom) { + *this = *this - other_polynom; + return *this; +} +Polynom& Polynom::operator *=(const Polynom& other_polynom) { + *this = *this * other_polynom; + return *this; +} +Polynom Polynom::operator +(const Monom& other_monom) const { + Polynom result = *this; + result += other_monom; + result.simplify(); + result.sort(); + return result; +} +Polynom Polynom::operator -(const Monom& other_monom) const { + Polynom result = *this; + result -= other_monom; + result.simplify(); + result.sort(); + return result; +} + +Polynom& Polynom::operator +=(const Monom& other_monom) { + *this = *this + other_monom; + return *this; +} +Polynom& Polynom::operator -=(const Monom& other_monom) { + *this = *this - other_monom; + return *this; +} +Polynom& Polynom::operator=(const Polynom& other) { + if (this != &other) { + _polynom = other._polynom; + } + return *this; +} +Polynom Polynom::operator-() const { + Polynom result; + for (auto it = _polynom.begin(); it != _polynom.end(); it++) { + result += -(*it); + } + return result; +} +double Polynom::calculate(double x, double y, double z) const { + double result; + +} From c2b721a56e5a97802212658f9e8385657719a951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 14 Feb 2026 16:18:56 +0300 Subject: [PATCH 260/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp index 333ac50f..f8d0708c 100644 --- a/lib_polynom/polynom.cpp +++ b/lib_polynom/polynom.cpp @@ -73,6 +73,28 @@ Polynom Polynom::operator-() const { return result; } double Polynom::calculate(double x, double y, double z) const { - double result; + double result = 0.0; + for (auto it = _polynom.begin(); it != _polynom.end(); it++) { + result += (*it).calculate(x, y, z); + } + return result; +} +std::ostream& operator<<(std::ostream& out, const Polynom& polynom) { + if (polynom.is_zero()) { + out << "0"; + return out; + } + auto it = polynom._polynom.begin(); + out << *it; + for (++it; it != polynom._polynom.end(); it++) { + double coef = (*it).get_coefficient(); + if (coef > 0) { + out << " + " << *it; + } + else { + out << " - " << -(*it); + } + } + return out; } From 9065be73c4905cbb8a962285eaae28138e73eeaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 14 Feb 2026 16:20:12 +0300 Subject: [PATCH 261/275] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=9F?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.h | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib_polynom/polynom.h b/lib_polynom/polynom.h index 68a92bde..801ebf77 100644 --- a/lib_polynom/polynom.h +++ b/lib_polynom/polynom.h @@ -3,7 +3,6 @@ #include #include #include - class Polynom { private: List _polynom; @@ -17,35 +16,32 @@ class Polynom { } Polynom(const std::string& string); Polynom(const char* str); - + Polynom::Polynom(const Polynom& other) { + _polynom = other._polynom; + } Polynom& operator=(const Polynom& other); Polynom operator +(const Polynom& other_polynom) const; Polynom operator -(const Polynom& other_polynom) const; Polynom operator *(const Polynom& other_polynom) const; - /*Polynom operator /(const Polynom& other_polynom) const;*/ + //Polynom operator /(const Polynom& other_polynom) const; Polynom& operator +=(const Polynom& other_polynom); Polynom& operator -=(const Polynom& other_polynom); Polynom& operator *=(const Polynom& other_polynom); - /* Polynom& operator /=(const Polynom& other_polynom);*/ + //Polynom& operator /=(const Polynom& other_polynom); Polynom operator +(const Monom& other_monom) const; Polynom operator -(const Monom& other_monom) const; - Polynom operator *(const Monom& other_monom) const; - Polynom operator /(const Monom& other_monom) const; + //Polynom operator /(const Monom& other_monom) const; Polynom& operator +=(const Monom& other_monom); Polynom& operator -=(const Monom& other_monom); - Polynom& operator *=(const Monom& other_monom); - Polynom& operator /=(const Monom& other_monom); - // Унарные операторы + //Polynom& operator /=(const Monom& other_monom); + Polynom operator-() const; - // Вычисление значения double calculate(double x, double y, double z) const; - - // Методы для работы с полиномом bool is_zero() const noexcept { return _polynom.is_empty(); } From 6078b2ab45a2c875022507cd0abd961a5fa412a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 15 Feb 2026 15:50:49 +0300 Subject: [PATCH 262/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib_polynom/polynom.h b/lib_polynom/polynom.h index 801ebf77..e18bc7ed 100644 --- a/lib_polynom/polynom.h +++ b/lib_polynom/polynom.h @@ -2,13 +2,14 @@ #include "../lib_list/List.h" #include #include +#include #include class Polynom { private: List _polynom; - void simplify(); // удалить нулевые, объединить подобные - void sort(); // упорядочить + void simplify(); + void sort(); public: Polynom() : _polynom() {} Polynom(const Monom& monom) { @@ -24,20 +25,18 @@ class Polynom { Polynom operator +(const Polynom& other_polynom) const; Polynom operator -(const Polynom& other_polynom) const; Polynom operator *(const Polynom& other_polynom) const; - //Polynom operator /(const Polynom& other_polynom) const; Polynom& operator +=(const Polynom& other_polynom); Polynom& operator -=(const Polynom& other_polynom); Polynom& operator *=(const Polynom& other_polynom); - //Polynom& operator /=(const Polynom& other_polynom); Polynom operator +(const Monom& other_monom) const; Polynom operator -(const Monom& other_monom) const; - //Polynom operator /(const Monom& other_monom) const; + Polynom operator /(const Monom& other_monom) const; Polynom& operator +=(const Monom& other_monom); Polynom& operator -=(const Monom& other_monom); - //Polynom& operator /=(const Monom& other_monom); + Polynom& operator /=(const Monom& other_monom); Polynom operator-() const; From c908f73eac54a104a129dca648a198d9d03eb312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sun, 15 Feb 2026 15:53:41 +0300 Subject: [PATCH 263/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D0=B2=D0=B2=D0=BE=D0=B4=D0=B0,=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20sort=20=D0=B8=20simplify=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.cpp | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp index f8d0708c..21b69fc8 100644 --- a/lib_polynom/polynom.cpp +++ b/lib_polynom/polynom.cpp @@ -98,3 +98,98 @@ std::ostream& operator<<(std::ostream& out, const Polynom& polynom) { } return out; } +std::istream& operator>>(std::istream& input, Polynom& polynom) { + polynom = Polynom(); + char c; + bool positive = true; + std::string monom_str; + while (input.get(c)) { + if (c == '+' || c == '-') { + if (!monom_str.empty()) { + try { + Monom m; + std::istringstream iss(monom_str); + iss >> m; + if (!positive) { + m = -m; + } + polynom._polynom.push_back(m); + } + catch (std::exception ex) { + input.setstate(std::ios::failbit); + return input; + } + monom_str.clear(); + } + positive = (c == '+'); + } + else if (!isspace(c)) { + monom_str += c; + } + } + if (!monom_str.empty()) { + try { + Monom m; + std::istringstream iss(monom_str); + iss >> m; + if (!positive) { + m = -m; + } + polynom._polynom.push_back(m); + } + catch (std::exception ex) { + input.setstate(std::ios::failbit); + return input; + } + } + polynom.sort(); + polynom.simplify(); + + return input; +} +void Polynom::sort() { + size_t n = _polynom.get_size(); + for (size_t i = 0; i < n; ++i) { + for (size_t j = i + 1; j < n; ++j) { + auto it_i = _polynom.begin(); + for (size_t k = 0; k < i; ++k) ++it_i; + + auto it_j = _polynom.begin(); + for (size_t k = 0; k < j; ++k) ++it_j; + if (!(*it_i > *it_j)) { + Monom temp = *it_i; + *it_i = *it_j; + *it_j = temp; + } + } + } +} +void Polynom::simplify() { + sort(); + size_t i = 0; + while (i + 1 < _polynom.get_size()) { + auto it1 = _polynom.begin(); + for (size_t k = 0; k < i; ++k) ++it1; + auto it2 = it1; + ++it2; + if ((*it1) == (*it2)) { + double new_coef = (*it1).get_coefficient() + (*it2).get_coefficient(); + (*it1).set_coefficient(new_coef); + _polynom.erase(i + 1); + } + else { + ++i; + } + } + i = 0; + while (i < _polynom.get_size()) { + auto it = _polynom.begin(); + for (size_t k = 0; k < i; ++k) ++it; + if (std::abs((*it).get_coefficient()) < EPSILON) { + _polynom.erase(i); + } + else { + ++i; + } + } +} From 771f15cda908c8b5512b08f5f6f7ea190ad31e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 16 Feb 2026 09:24:03 +0300 Subject: [PATCH 264/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=A1?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D1=81=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=BF=D1=83=D1=81=D0=BA=D0=B0=D0=BC=D0=B8,=20=D0=B2=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BC=20=D1=87=D0=B8=D1=81=D0=BB=D0=B5=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=20=D1=81=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_skip_list/CMakeLists.txt | 1 + lib_skip_list/skip_list.cpp | 1 + lib_skip_list/skip_list.h | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 lib_skip_list/CMakeLists.txt create mode 100644 lib_skip_list/skip_list.cpp create mode 100644 lib_skip_list/skip_list.h diff --git a/lib_skip_list/CMakeLists.txt b/lib_skip_list/CMakeLists.txt new file mode 100644 index 00000000..63e5c186 --- /dev/null +++ b/lib_skip_list/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(SkipList) \ No newline at end of file diff --git a/lib_skip_list/skip_list.cpp b/lib_skip_list/skip_list.cpp new file mode 100644 index 00000000..5fc215d4 --- /dev/null +++ b/lib_skip_list/skip_list.cpp @@ -0,0 +1 @@ +#include "skip_list.h" diff --git a/lib_skip_list/skip_list.h b/lib_skip_list/skip_list.h new file mode 100644 index 00000000..71b15150 --- /dev/null +++ b/lib_skip_list/skip_list.h @@ -0,0 +1,22 @@ +#pragma once +#include "../lib_list/List.h" +template +struct Node { //Мн-во указателей на первые узлы на каждом уровне списка + std::pair data_; + Node** next_ +public: + Node(); + ~Node(); +}; +template +class SkipList { + size_t _max_count_levels; + size_t _current_count_levels; + List _heads; +public: + void insert(const Tkey& key, const Tvalue& value); + void print() const noexcept; +protected: + size_t flip_coint() const noexcept; + Node* find_nearest(const Tkey& key) const noexcept; +}; From a4ad88aefcbf987c29520fa2abda383fca0d0d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 16 Feb 2026 09:44:49 +0300 Subject: [PATCH 265/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=BE=D0=B1=D1=8A=D1=8F?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D1=81=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BF=D1=83=D1=81=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_skip_list/skip_list.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib_skip_list/skip_list.h b/lib_skip_list/skip_list.h index 71b15150..2ac2c3b3 100644 --- a/lib_skip_list/skip_list.h +++ b/lib_skip_list/skip_list.h @@ -1,22 +1,25 @@ #pragma once #include "../lib_list/List.h" -template +template struct Node { //Мн-во указателей на первые узлы на каждом уровне списка std::pair data_; - Node** next_ + Node** next_; + size_t height_; public: Node(); ~Node(); }; -template +template class SkipList { size_t _max_count_levels; size_t _current_count_levels; List _heads; public: + SkipList(); + ~SkipList(); void insert(const Tkey& key, const Tvalue& value); void print() const noexcept; protected: - size_t flip_coint() const noexcept; + size_t flip_coin() const noexcept; Node* find_nearest(const Tkey& key) const noexcept; }; From 7bef94aecfdad15505538fbe5452cb9aa0cc5096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 16 Feb 2026 09:47:05 +0300 Subject: [PATCH 266/275] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B5=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib_polynom/polynom.h b/lib_polynom/polynom.h index e18bc7ed..bb4b662e 100644 --- a/lib_polynom/polynom.h +++ b/lib_polynom/polynom.h @@ -32,11 +32,9 @@ class Polynom { Polynom operator +(const Monom& other_monom) const; Polynom operator -(const Monom& other_monom) const; - Polynom operator /(const Monom& other_monom) const; Polynom& operator +=(const Monom& other_monom); Polynom& operator -=(const Monom& other_monom); - Polynom& operator /=(const Monom& other_monom); Polynom operator-() const; From 6430d3b48e46d6c8756a4e8a4dcabaa5497a2972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 16 Feb 2026 10:11:47 +0300 Subject: [PATCH 267/275] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_skip_list/skip_list.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib_skip_list/skip_list.h b/lib_skip_list/skip_list.h index 2ac2c3b3..20f5e8a4 100644 --- a/lib_skip_list/skip_list.h +++ b/lib_skip_list/skip_list.h @@ -1,25 +1,25 @@ #pragma once #include "../lib_list/List.h" template -struct Node { //Мн-во указателей на первые узлы на каждом уровне списка +struct SkipListNode { //Мн-во указателей на первые узлы на каждом уровне списка std::pair data_; - Node** next_; + SkipListNode** next_; size_t height_; public: - Node(); - ~Node(); + SkipListNode(); + ~SkipListNode(); }; template class SkipList { size_t _max_count_levels; size_t _current_count_levels; - List _heads; + List*> _heads; public: SkipList(); ~SkipList(); void insert(const Tkey& key, const Tvalue& value); void print() const noexcept; protected: - size_t flip_coin() const noexcept; - Node* find_nearest(const Tkey& key) const noexcept; + size_t flip_coin() const noexcept; //возвращает рандомный уровень для insert нового элемента + SkipListNode* find_nearest(const Tkey& key) const noexcept; }; From 927b0d8180e7dd60df8065f3e1a2d1f6eb797fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 16 Feb 2026 10:17:16 +0300 Subject: [PATCH 268/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D0=BA=D1=83=D1=82?= =?UTF-8?q?=D0=BE=D1=80=20=D0=BF=D0=BE=20=D1=83=D0=BC=D0=BE=D0=BB=D1=87?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8E=20=D0=B8=20=D0=BD=D0=B0=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_polynom.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/test_polynom.cpp diff --git a/tests/test_polynom.cpp b/tests/test_polynom.cpp new file mode 100644 index 00000000..03ab1b81 --- /dev/null +++ b/tests/test_polynom.cpp @@ -0,0 +1,59 @@ +// Copyright 2025 Julia Zabytina +#include +#include +#include "../lib_polynom/polynom.h" +const double TEST_EPSILON = 1e-10; +TEST(TestPolynomLib, polynom_default_constructor) { + // Arrange & Act + Polynom polynom; + // Assert + EXPECT_EQ(polynom.size(), 0); + EXPECT_TRUE(polynom.is_zero()); +} +TEST(TestPolynomLib, polynom_constructor_from_monom) { + // Arrange + Monom monom(22.3); + + // Act + Polynom polynom(monom); + + // Assert + EXPECT_EQ(polynom.size(), 1); + std::ostringstream oss; + oss << polynom; + EXPECT_EQ(oss.str(), "22.3"); +} + +TEST(TestPolynomLib, polynom_constructor_from_monom_with_powers) { + // Arrange + Monom monom(2.5, 2, 1, 0); + + // Act + Polynom polynom(monom); + + // Assert + EXPECT_EQ(polynom.size(), 1); + std::ostringstream oss; + oss << polynom; + EXPECT_EQ(oss.str(), "2.5x^2y"); +} +TEST(TestPolynomLib, polynom_copy_constructor) { + // Arrange + Monom monom(3.14, 1, 1, 0); + Polynom original(monom); + + // Act + Polynom copy(original); + + // Assert + EXPECT_EQ(original.size(), copy.size()); + std::ostringstream oss_original, oss_copy; + oss_original << original; + oss_copy << copy; + EXPECT_EQ(oss_original.str(), oss_copy.str()); +} + + + + + From edc7931aa2624145f3723d90db9eb5238e60d2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Mon, 16 Feb 2026 18:42:04 +0300 Subject: [PATCH 269/275] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20CMakeLists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 61be80d0..aa494f79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,8 @@ add_subdirectory(lib_queue) add_subdirectory(lib_queue_on_list) add_subdirectory(lib_stack_on_list) +add_subdirectory(lib_monom) +add_subdirectory(lib_polynom) add_subdirectory(lib_dsu) add_subdirectory(lib_doubly_linked_list) From ed2f54f2da183f405b0f2b4b6f1422c78453104d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 20 Feb 2026 19:20:38 +0300 Subject: [PATCH 270/275] =?UTF-8?q?=D0=A4=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.cpp | 103 +++++++++++++++++++++++++++++----------- lib_polynom/polynom.h | 23 ++++++--- 2 files changed, 92 insertions(+), 34 deletions(-) diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp index 21b69fc8..542151c1 100644 --- a/lib_polynom/polynom.cpp +++ b/lib_polynom/polynom.cpp @@ -1,6 +1,26 @@ #include "polynom.h" -const double EPSILON = 1e-10; +Polynom::Polynom(const std::string& str) { + if (str.empty()) { + return; + } + std::stringstream ss(str); + std::string token; + char sign = '+'; + while (ss >> token) { + if (token == "+" || token == "-") { + sign = token[0]; + continue; + } + Monom monom = parseMonom(token); + if (sign == '-') { + monom.set_coefficient(-monom.get_coefficient()); + } + *this += monom; + sign = '+'; + } + simplify(); +} Polynom Polynom::operator +(const Polynom& other_polynom) const { Polynom result = *this; for (auto it = other_polynom._polynom.begin(); it != other_polynom._polynom.end(); it++) { @@ -36,27 +56,25 @@ Polynom& Polynom::operator *=(const Polynom& other_polynom) { *this = *this * other_polynom; return *this; } -Polynom Polynom::operator +(const Monom& other_monom) const { + +Polynom Polynom::operator+(const Monom& other_monom) const { Polynom result = *this; result += other_monom; - result.simplify(); - result.sort(); return result; } Polynom Polynom::operator -(const Monom& other_monom) const { Polynom result = *this; result -= other_monom; - result.simplify(); - result.sort(); return result; } - -Polynom& Polynom::operator +=(const Monom& other_monom) { - *this = *this + other_monom; +Polynom& Polynom::operator+=(const Monom& other_monom) { + _polynom.push_back(other_monom); + simplify(); return *this; } -Polynom& Polynom::operator -=(const Monom& other_monom) { - *this = *this - other_monom; +Polynom& Polynom::operator-=(const Monom& other_monom) { + _polynom.push_back(-other_monom); + simplify(); return *this; } Polynom& Polynom::operator=(const Polynom& other) { @@ -115,7 +133,7 @@ std::istream& operator>>(std::istream& input, Polynom& polynom) { } polynom._polynom.push_back(m); } - catch (std::exception ex) { + catch (std::exception& ex) { input.setstate(std::ios::failbit); return input; } @@ -137,7 +155,7 @@ std::istream& operator>>(std::istream& input, Polynom& polynom) { } polynom._polynom.push_back(m); } - catch (std::exception ex) { + catch (std::exception& ex) { input.setstate(std::ios::failbit); return input; } @@ -148,20 +166,18 @@ std::istream& operator>>(std::istream& input, Polynom& polynom) { return input; } void Polynom::sort() { - size_t n = _polynom.get_size(); - for (size_t i = 0; i < n; ++i) { - for (size_t j = i + 1; j < n; ++j) { - auto it_i = _polynom.begin(); - for (size_t k = 0; k < i; ++k) ++it_i; - - auto it_j = _polynom.begin(); - for (size_t k = 0; k < j; ++k) ++it_j; - if (!(*it_i > *it_j)) { - Monom temp = *it_i; - *it_i = *it_j; - *it_j = temp; - } - } + if (_polynom.get_size() <= 1) return; + std::vector temp; + for (auto it = _polynom.begin(); it != _polynom.end(); ++it) { + temp.push_back(*it); + } + std::sort(temp.begin(), temp.end(), + [](const Monom& a, const Monom& b) { return a > b; }); + while (!_polynom.is_empty()) { + _polynom.pop_back(); + } + for (const auto& m : temp) { + _polynom.push_back(m); } } void Polynom::simplify() { @@ -193,3 +209,36 @@ void Polynom::simplify() { } } } +Monom Polynom::parseMonom(const std::string& token) const { + if (token.empty()) { + throw std::invalid_argument("Empty monom token"); + } + double coefficient = 1.0; + int powers[3] = { 0, 0, 0 }; // [x, y, z] + size_t pos = 0; + if (isdigit(token[pos]) || token[pos] == '.') { + size_t end; + coefficient = std::stod(token.substr(pos), &end); + pos = end; + } + while (pos < token.length()) { + char var = token[pos]; + pos++; + + int var_index = -1; + if (var == 'x') var_index = 0; + else if (var == 'y') var_index = 1; + else if (var == 'z') var_index = 2; + else throw std::invalid_argument("Invalid variable: " + std::string(1, var)); + + int power = 1; + if (pos < token.length() && token[pos] == '^') { + pos++; + size_t end; + power = std::stoi(token.substr(pos), &end); + pos += end; + } + powers[var_index] = power; + } + return Monom(coefficient, powers[0], powers[1], powers[2]); +} diff --git a/lib_polynom/polynom.h b/lib_polynom/polynom.h index bb4b662e..32815ab0 100644 --- a/lib_polynom/polynom.h +++ b/lib_polynom/polynom.h @@ -2,25 +2,34 @@ #include "../lib_list/List.h" #include #include +#include +#include #include #include class Polynom { private: List _polynom; - + static constexpr double EPSILON = 1e-10; void simplify(); void sort(); + Monom parseMonom(const std::string& token) const; public: - Polynom() : _polynom() {} + Polynom() : _polynom() {} // Polynom(const Monom& monom) { _polynom.push_back(monom); - } + } // Polynom(const std::string& string); - Polynom(const char* str); - Polynom::Polynom(const Polynom& other) { - _polynom = other._polynom; + Polynom(const char* str) { + if (!str) { + throw std::invalid_argument("Null string pointer"); + } + // Преобразуем C-строку в std::string и вызываем другой конструктор + *this = Polynom(std::string(str)); } - Polynom& operator=(const Polynom& other); + Polynom(const Polynom& other) { + _polynom = other._polynom; + } // + Polynom& operator=(const Polynom& other); // Polynom operator +(const Polynom& other_polynom) const; Polynom operator -(const Polynom& other_polynom) const; From 28a41ca4f34ded61244af4ccc99e6c8fc266968a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Fri, 20 Feb 2026 19:21:36 +0300 Subject: [PATCH 271/275] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_polynom.cpp | 54 ++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/tests/test_polynom.cpp b/tests/test_polynom.cpp index 03ab1b81..86cc6d79 100644 --- a/tests/test_polynom.cpp +++ b/tests/test_polynom.cpp @@ -13,24 +13,19 @@ TEST(TestPolynomLib, polynom_default_constructor) { TEST(TestPolynomLib, polynom_constructor_from_monom) { // Arrange Monom monom(22.3); - // Act Polynom polynom(monom); - // Assert EXPECT_EQ(polynom.size(), 1); std::ostringstream oss; oss << polynom; EXPECT_EQ(oss.str(), "22.3"); } - TEST(TestPolynomLib, polynom_constructor_from_monom_with_powers) { // Arrange Monom monom(2.5, 2, 1, 0); - // Act Polynom polynom(monom); - // Assert EXPECT_EQ(polynom.size(), 1); std::ostringstream oss; @@ -41,10 +36,8 @@ TEST(TestPolynomLib, polynom_copy_constructor) { // Arrange Monom monom(3.14, 1, 1, 0); Polynom original(monom); - // Act Polynom copy(original); - // Assert EXPECT_EQ(original.size(), copy.size()); std::ostringstream oss_original, oss_copy; @@ -52,8 +45,45 @@ TEST(TestPolynomLib, polynom_copy_constructor) { oss_copy << copy; EXPECT_EQ(oss_original.str(), oss_copy.str()); } - - - - - +//TEST(TestPolynomLib, polynom_assignment_operator) { +// // Arrange +// Monom monom1(10.2, 1, 3, 8); +// Monom monom2(5.5, 2, 1, 0); +// Polynom original(monom1); +// Polynom other(monom2); +// // Act +// other = original; // Присваивание существующему объекту +// // Assert +// EXPECT_EQ(original.size(), other.size()); +// std::ostringstream oss_original, oss_other; +// oss_original << original; +// oss_other << other; +// EXPECT_EQ(oss_original.str(), oss_other.str()); +//} +//TEST(TestPolynomLib, polynom_assignment_to_self) { +// // Arrange +// Monom monom(10.2, 1, 3, 8); +// Polynom polynom(monom); +// std::ostringstream before; +// before << polynom; +// // Act +// polynom = polynom; // Присваивание самому себе +// // Assert +// std::ostringstream after; +// after << polynom; +// EXPECT_EQ(before.str(), after.str()); // Должен остаться неизменным +//} +//TEST(TestPolynomLib, polynom_assignment_deep_copy) { +// // Arrange +// Polynom p1("2*x*y + 3*z"); +// Polynom p2("5*x"); +// // Act +// p2 = p1; +// // Изменяем оригинал +// p1 = Polynom("100*x"); // Создаем новый полином +// // Assert +// std::ostringstream oss_p2; +// oss_p2 << p2; +// // p2 должен остаться старым значением ("2*x*y + 3*z") +// EXPECT_EQ(oss_p2.str(), "2*x*y + 3*z"); +//} From 83b6243e5cc782499361617ebe4c44e6e346ff12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 21 Feb 2026 16:06:45 +0300 Subject: [PATCH 272/275] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=B0=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_list/List.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib_list/List.h b/lib_list/List.h index eed0d2cf..5226cd53 100644 --- a/lib_list/List.h +++ b/lib_list/List.h @@ -227,6 +227,8 @@ List& List::operator=(const List& other) { _head = _head->next; delete temporary; } + _head = nullptr; + _tail = nullptr; _count_elements = 0; for (auto it = other.begin(); it != other.end(); ++it) { this->push_back(*it); From ffe5d75a7989be051d934003de2181262a10e6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 21 Feb 2026 16:45:05 +0300 Subject: [PATCH 273/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B0=D1=80=D0=B8=D1=84=D0=BC=D0=B5=D1=82=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=BE=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=B2=D1=88=D0=B5=D1=81=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_polynom.cpp | 250 ++++++++++++++++++++++++++++++++++------- 1 file changed, 208 insertions(+), 42 deletions(-) diff --git a/tests/test_polynom.cpp b/tests/test_polynom.cpp index 86cc6d79..8ca7f46e 100644 --- a/tests/test_polynom.cpp +++ b/tests/test_polynom.cpp @@ -45,45 +45,211 @@ TEST(TestPolynomLib, polynom_copy_constructor) { oss_copy << copy; EXPECT_EQ(oss_original.str(), oss_copy.str()); } -//TEST(TestPolynomLib, polynom_assignment_operator) { -// // Arrange -// Monom monom1(10.2, 1, 3, 8); -// Monom monom2(5.5, 2, 1, 0); -// Polynom original(monom1); -// Polynom other(monom2); -// // Act -// other = original; // Присваивание существующему объекту -// // Assert -// EXPECT_EQ(original.size(), other.size()); -// std::ostringstream oss_original, oss_other; -// oss_original << original; -// oss_other << other; -// EXPECT_EQ(oss_original.str(), oss_other.str()); -//} -//TEST(TestPolynomLib, polynom_assignment_to_self) { -// // Arrange -// Monom monom(10.2, 1, 3, 8); -// Polynom polynom(monom); -// std::ostringstream before; -// before << polynom; -// // Act -// polynom = polynom; // Присваивание самому себе -// // Assert -// std::ostringstream after; -// after << polynom; -// EXPECT_EQ(before.str(), after.str()); // Должен остаться неизменным -//} -//TEST(TestPolynomLib, polynom_assignment_deep_copy) { -// // Arrange -// Polynom p1("2*x*y + 3*z"); -// Polynom p2("5*x"); -// // Act -// p2 = p1; -// // Изменяем оригинал -// p1 = Polynom("100*x"); // Создаем новый полином -// // Assert -// std::ostringstream oss_p2; -// oss_p2 << p2; -// // p2 должен остаться старым значением ("2*x*y + 3*z") -// EXPECT_EQ(oss_p2.str(), "2*x*y + 3*z"); -//} +TEST(TestPolynomLib, polynom_assignment_operator) { + // Arrange + Monom monom1(10.2, 1, 3, 8); + Monom monom2(5.5, 2, 1, 0); + Polynom original(monom1); + Polynom other(monom2); + // Act + other = original; + // Assert + EXPECT_EQ(original.size(), other.size()); + std::ostringstream oss_original, oss_other; + oss_original << original; + oss_other << other; + EXPECT_EQ(oss_original.str(), oss_other.str()); +} +TEST(TestPolynomLib, polynom_self_assignment) { + // Arrange + Monom monom(10.2, 1, 3, 8); + Polynom polynom(monom); + std::ostringstream before; + before << polynom; + // Act + polynom = polynom; + // Assert + std::ostringstream after; + after << polynom; + EXPECT_EQ(before.str(), after.str()); +} +TEST(TestPolynomLib, polynom_add_polynom) { + // Arrange + Monom monom1(1.2, 1, 3, 8); + Polynom polynom1(monom1); + Monom monom2(102.12, 1, 4, 2); + Polynom polynom2(monom2); + // Act + Polynom result; + result = polynom1 + polynom2; + // Assert + EXPECT_EQ(result.size(), 2); + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "1.2xy^3z^8 + 102.12xy^4z^2"); +} +TEST(TestPolynomLib, polynom_sub_polynom) { + // Arrange + Monom monom1(211.2, 1, 3, 8); + Polynom polynom1(monom1); + Monom monom2(33.33, 1, 4, 2); + Polynom polynom2(monom2); + // Act + Polynom result; + result = polynom1 - polynom2; + // Assert + EXPECT_EQ(result.size(), 2); + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "211.2xy^3z^8 - 33.33xy^4z^2"); +} +TEST(TestPolynomLib, polynom_mult_polynom) { + // Arrange + Monom monom1(77.2, 22, 3, 8); + Polynom polynom1(monom1); + Monom monom2(10, 1, 49, 2); + Polynom polynom2(monom2); + // Act + Polynom result; + result = polynom1 * polynom2; + // Assert + EXPECT_EQ(result.size(), 1); + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "772x^23y^52z^10"); +} +TEST(TestPolynomLib, polynom_add_assign_polynom) { + // Arrange + Monom monom1(1.2, 1, 3, 8); + Polynom polynom1(monom1); + Monom monom2(102.12, 1, 4, 2); + Polynom polynom2(monom2); + // Act + polynom1 += polynom2; + // Assert + EXPECT_EQ(polynom1.size(), 2); + std::ostringstream oss; + oss << polynom1; + EXPECT_EQ(oss.str(), "1.2xy^3z^8 + 102.12xy^4z^2"); +} +TEST(TestPolynomLib, polynom_sub_assign_polynom) { + // Arrange + Monom monom1(211.2, 1, 3, 8); + Polynom polynom1(monom1); + Monom monom2(33.33, 1, 4, 2); + Polynom polynom2(monom2); + // Act + polynom1 -= polynom2; + // Assert + EXPECT_EQ(polynom1.size(), 2); + std::ostringstream oss; + oss << polynom1; + EXPECT_EQ(oss.str(), "211.2xy^3z^8 - 33.33xy^4z^2"); +} +TEST(TestPolynomLib, polynom_mult_assign_polynom) { + // Arrange + Monom monom1(77.2, 22, 3, 8); + Polynom polynom1(monom1); + Monom monom2(10, 1, 49, 2); + Polynom polynom2(monom2); + // Act + polynom1 *= polynom2; + // Assert + EXPECT_EQ(polynom1.size(), 1); + std::ostringstream oss; + oss << polynom1; + EXPECT_EQ(oss.str(), "772x^23y^52z^10"); +} +TEST(TestPolynomLib, polynom_add_monom) { + // Arrange + Monom monom1(1.2, 4, 4, 8); + Polynom polynom(monom1); + Monom monom2(66.8, 1, 4, 4); + // Act + Polynom result; + result = polynom + monom2; + // Assert + EXPECT_EQ(result.size(), 2); + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "1.2x^4y^4z^8 + 66.8xy^4z^4"); +} +TEST(TestPolynomLib, polynom_sub_monom) { + // Arrange + Monom monom1(2233.4, 1, 2, 8); + Polynom polynom(monom1); + Monom monom2(99.9, 2, 2, 2); + // Act + Polynom result; + result = polynom - monom2; + // Assert + EXPECT_EQ(result.size(), 2); + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "2233.4xy^2z^8 - 99.9x^2y^2z^2"); +} +TEST(TestPolynomLib, polynom_add_assign_monom) { + // Arrange + Monom monom1(1.2, 4, 4, 8); + Polynom polynom(monom1); + Monom monom2(66.8, 1, 4, 4); + // Act + polynom += monom2; + // Assert + EXPECT_EQ(polynom.size(), 2); + std::ostringstream oss; + oss << polynom; + EXPECT_EQ(oss.str(), "1.2x^4y^4z^8 + 66.8xy^4z^4"); +} +TEST(TestPolynomLib, polynom_sub_assign_monom) { + // Arrange + Monom monom1(2233.4, 1, 2, 8); + Polynom polynom(monom1); + Monom monom2(99.9, 2, 2, 2); + // Act + polynom -= monom2; + // Assert + EXPECT_EQ(polynom.size(), 2); + std::ostringstream oss; + oss << polynom; + EXPECT_EQ(oss.str(), "2233.4xy^2z^8 - 99.9x^2y^2z^2"); +} +TEST(TestPolynomLib, polynom_unary_minus) { + // Arrange + Polynom polynom("22.4xy^2z^8 - 99.9x^2y^2z^2 + 33.7y^3z"); + // Act + polynom = -polynom; + // Assert + EXPECT_EQ(polynom.size(), 3); + std::ostringstream oss; + oss << polynom; + EXPECT_EQ(oss.str(), "-22.4xy^2z^8 + 99.9x^2y^2z^2 - 33.7y^3z"); +} +TEST(TestPolynomLib, polynom_calculate) { + // Arrange + Polynom polynom("2x + 3y - 4z"); + // Act + double result = polynom.calculate(5, 7, 2); + // Assert + EXPECT_EQ(result, 23); +} +TEST(TestPolynomLib, polynom_calculate_very_large) { + // Arrange + Polynom polynom("10x^5y^3 - 7x^2y^4z + 3yz^5"); + // Act + double result = polynom.calculate(5, 4, 3); + // Assert + EXPECT_EQ(result, 1868516); +} +TEST(TestPolynomLib, polynom_is_zero_expect_true) { + // Arrange + Polynom polynom; + // Act & Assert + EXPECT_TRUE(polynom.is_zero()); +} +TEST(TestPolynomLib, polynom_is_zero_expect_false) { + // Arrange + Polynom polynom("2x + 32.3y - 4z^5"); + // Act & Assert + EXPECT_FALSE(polynom.is_zero()); +} From a6cf8fb738b8127518d9fb1f47729eb3c8b67fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 21 Feb 2026 16:58:04 +0300 Subject: [PATCH 274/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=20*?= =?UTF-8?q?=20(=D0=BF=D0=BE=D0=BB=D0=B8=D0=BD=D0=BE=D0=BC=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=BD=D0=BE=D0=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_polynom.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/test_polynom.cpp b/tests/test_polynom.cpp index 8ca7f46e..56c3a57b 100644 --- a/tests/test_polynom.cpp +++ b/tests/test_polynom.cpp @@ -188,6 +188,42 @@ TEST(TestPolynomLib, polynom_sub_monom) { oss << result; EXPECT_EQ(oss.str(), "2233.4xy^2z^8 - 99.9x^2y^2z^2"); } +TEST(TestPolynomLib, polynom_mult_monom) { + // Arrange + Polynom polynom("2x^2 + 3y"); + Monom monom(4, 1, 0, 0); + // Act + Polynom result = polynom * monom; + // Assert + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "8x^3 + 12xy"); + EXPECT_EQ(result.size(), 2); +} +TEST(TestPolynomLib, polynom_mult_monom_with_zero_power) { + // Arrange + Polynom polynom("5x^2 + 2y^3 + 7"); + Monom monom(3, 0, 0, 0); + // Act + Polynom result = polynom * monom; + // Assert + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "15x^2 + 6y^3 + 21"); + EXPECT_EQ(result.size(), 3); +} +TEST(TestPolynomLib, polynom_mult_monom_negative_coefficient) { + // Arrange + Polynom polynom("4x^2 - 2y"); + Monom monom(-3, 1, 0, 0); + // Act + Polynom result = polynom * monom; + // Assert + std::ostringstream oss; + oss << result; + EXPECT_EQ(oss.str(), "-12x^3 + 6xy"); + EXPECT_EQ(result.size(), 2); +} TEST(TestPolynomLib, polynom_add_assign_monom) { // Arrange Monom monom1(1.2, 4, 4, 8); @@ -214,6 +250,17 @@ TEST(TestPolynomLib, polynom_sub_assign_monom) { oss << polynom; EXPECT_EQ(oss.str(), "2233.4xy^2z^8 - 99.9x^2y^2z^2"); } +TEST(TestPolynomLib, polynom_mult_assign_monom) { + // Arrange + Polynom polynom("2x^2 + 3y"); + Monom monom(4, 1, 0, 0); + // Act + polynom *= monom; + // Assert + std::ostringstream oss; + oss << polynom; + EXPECT_EQ(oss.str(), "8x^3 + 12xy"); +} TEST(TestPolynomLib, polynom_unary_minus) { // Arrange Polynom polynom("22.4xy^2z^8 - 99.9x^2y^2z^2 + 33.7y^3z"); From 109591894c97661be11fff57cfda26947da87808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D0=BB=D1=8F=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Sat, 21 Feb 2026 16:59:34 +0300 Subject: [PATCH 275/275] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20*=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D0=B8=D0=BD?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=20=D0=B8=20=D0=BC=D0=BE=D0=BD=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_polynom/polynom.cpp | 13 ++++++++++++- lib_polynom/polynom.h | 12 +++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib_polynom/polynom.cpp b/lib_polynom/polynom.cpp index 542151c1..a8da31ae 100644 --- a/lib_polynom/polynom.cpp +++ b/lib_polynom/polynom.cpp @@ -56,7 +56,6 @@ Polynom& Polynom::operator *=(const Polynom& other_polynom) { *this = *this * other_polynom; return *this; } - Polynom Polynom::operator+(const Monom& other_monom) const { Polynom result = *this; result += other_monom; @@ -67,6 +66,12 @@ Polynom Polynom::operator -(const Monom& other_monom) const { result -= other_monom; return result; } +Polynom Polynom::operator *(const Monom& other_monom) const { + Polynom result = *this; + result *= other_monom; + return result; +} + Polynom& Polynom::operator+=(const Monom& other_monom) { _polynom.push_back(other_monom); simplify(); @@ -77,6 +82,12 @@ Polynom& Polynom::operator-=(const Monom& other_monom) { simplify(); return *this; } +Polynom& Polynom::operator *=(const Monom& other_monom) { + for (auto it = _polynom.begin(); it != _polynom.end(); it++) { + *it = *it * other_monom; + } + return *this; +} Polynom& Polynom::operator=(const Polynom& other) { if (this != &other) { _polynom = other._polynom; diff --git a/lib_polynom/polynom.h b/lib_polynom/polynom.h index 32815ab0..42fa776a 100644 --- a/lib_polynom/polynom.h +++ b/lib_polynom/polynom.h @@ -14,10 +14,10 @@ class Polynom { void sort(); Monom parseMonom(const std::string& token) const; public: - Polynom() : _polynom() {} // + Polynom() : _polynom() {} Polynom(const Monom& monom) { _polynom.push_back(monom); - } // + } Polynom(const std::string& string); Polynom(const char* str) { if (!str) { @@ -28,11 +28,11 @@ class Polynom { } Polynom(const Polynom& other) { _polynom = other._polynom; - } // - Polynom& operator=(const Polynom& other); // + } + Polynom& operator=(const Polynom& other); Polynom operator +(const Polynom& other_polynom) const; - Polynom operator -(const Polynom& other_polynom) const; + Polynom operator -(const Polynom& other_polynom) const; Polynom operator *(const Polynom& other_polynom) const; Polynom& operator +=(const Polynom& other_polynom); @@ -41,7 +41,9 @@ class Polynom { Polynom operator +(const Monom& other_monom) const; Polynom operator -(const Monom& other_monom) const; + Polynom operator *(const Monom& other_monom) const; + Polynom& operator *=(const Monom& other_monom); Polynom& operator +=(const Monom& other_monom); Polynom& operator -=(const Monom& other_monom);