Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов
set(EXAMPLES_DIR examples) # Определим переменную с именем директории
set(TASKS_DIR tasks)

add_subdirectory(tasks)
#add_subdirectory(tasks)

# Создать исполняемый файл для каждого примера
if (BUILD_EXAMPLES_01_WEEK)
Expand All @@ -32,4 +32,4 @@ if (BUILD_EXAMPLES_01_WEEK)
add_example(functions_recursive ${EXAMPLES_DIR}/functions_recursive.cpp)
add_example(io_streams ${EXAMPLES_DIR}/io_streams.cpp)
add_example(main_with_args ${EXAMPLES_DIR}/main_with_args.cpp)
endif()
endif()
4 changes: 2 additions & 2 deletions 02_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов
set(EXAMPLES_DIR examples) # Определим переменную с именем директории
set(TASKS_DIR tasks)

add_subdirectory(tasks)
#add_subdirectory(tasks)

# Создать исполняемый файл для каждого примера
if (BUILD_EXAMPLES_02_WEEK)
Expand All @@ -17,4 +17,4 @@ if (BUILD_EXAMPLES_02_WEEK)
add_example(ptr_access ${EXAMPLES_DIR}/ptr_access.cpp)
add_example(ptr_arithmetic ${EXAMPLES_DIR}/ptr_arithmetic.cpp)
add_example(reinterpret ${EXAMPLES_DIR}/reinterpret.cpp)
endif()
endif()
4 changes: 2 additions & 2 deletions 03_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов
set(EXAMPLES_DIR examples) # Определим переменную с именем директории
set(TASKS_DIR tasks)

add_subdirectory(tasks)
#add_subdirectory(tasks)

# Создать исполняемый файл для каждого примера
if (BUILD_EXAMPLES_03_WEEK)
add_example(struct_examples ${EXAMPLES_DIR}/struct_examples.cpp)
add_example(union_examples ${EXAMPLES_DIR}/union_examples.cpp)
add_example(vector_examples ${EXAMPLES_DIR}/vector_examples.cpp)
endif()
endif()
2 changes: 1 addition & 1 deletion 04_week/tasks/phasor/phasor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ struct AlgTag {};


class Phasor {

};
188 changes: 188 additions & 0 deletions 04_week/tasks/ring_buffer/ring_buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,194 @@
#include <vector>



class RingBuffer {
public:

// ---- Конструкторы -----
explicit RingBuffer(size_t capacity); // Конструктор от емкости
RingBuffer(size_t capacity, int initialValue); // Конструткор от емкасти и начального значения
Copy link
Contributor Author

Choose a reason for hiding this comment

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

емкОсти, вместимости

RingBuffer(std::initializer_list<int> init); // конструктор по размеру контейнера

// ---- Методы -----
void Push(int value);
bool TryPush(int value);

bool Pop();
bool TryPop(int& value);

int& Front();
const int& Front() const;
int& Back();
const int& Back() const;

bool Empty() const {return countElem == 0;}
bool Full() const {return countElem == buf.capacity();}
size_t Size() const{return countElem;}
size_t Capacity() const{return buf.capacity();}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

проблемы с пробелами, и лучше сделать единообразно и вынести все методы вне класса


void Clear() {prevElem = nextElem = countElem = 0;}
void Resize(size_t newCapacity);

std::vector<int> Vector() const;

bool operator==(const RingBuffer& other) const;
bool operator!=(const RingBuffer& other) const;
int& operator[](size_t index);
const int& operator[](size_t index) const;

private:
std::vector<int> buf;
size_t prevElem = 0;
size_t nextElem = 0;
size_t countElem = 0; // счетчик текущего элемента


size_t NextIndex(size_t i) const {
if((i + 1) >= buf.capacity()) return 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

лишние пробелы перед return не пробела после if

return i+1;
}


};

RingBuffer::RingBuffer(size_t capacity) {
if(capacity == 0) capacity = 1; // Емкость 0 не должна быть
buf.reserve(capacity);
buf.resize(capacity);
prevElem = 0;
nextElem = 0;
countElem = 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

лучше сделать в списке инициализации. для буфера предварительный reserve излишний в данном случае

}

RingBuffer::RingBuffer(size_t capacity, int initialValue) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

лишняя строка

if(capacity == 0) capacity = 1; // Емкость 0 не должна быть
buf.reserve(capacity);
buf.resize(capacity);
prevElem = 0;
nextElem = 0;
countElem = 0;
for (size_t i = 0; i < capacity; ++i) Push(initialValue);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

правильнее все действия выполнить в списке инициализации, для buf воспользоваться конструктором


}

RingBuffer::RingBuffer(std::initializer_list<int> init) {

size_t capacity = init.size();
if(capacity == 0) capacity = 1; // Емкость 0 не должна быть

buf.reserve(capacity);
buf.resize(capacity);
prevElem = 0;
nextElem = 0;
countElem = 0;

for (int value : init) Push(value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

аналогично, все можно сделать в списке инициализации

}

void RingBuffer::Push(int value) {
if (Full()) {
buf[nextElem] = value;
prevElem = NextIndex(prevElem);
nextElem = NextIndex(nextElem);
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

лучше убрать else сделать одну короткую ветвь с return

buf[nextElem] = value;
nextElem = NextIndex(nextElem);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

общие действия которые выполняются в любом случае, лучше вынести, чтобы не дублировать

++countElem;
}
}

bool RingBuffer::TryPush(int value) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

код Push и TryPush достаточно похож, так что можно было переиспользовать один в другом

if (Full()) return false;
buf[nextElem] = value;
nextElem = NextIndex(nextElem);
++countElem;
return true;
}

bool RingBuffer::Pop() {
if (Empty()) return false;
prevElem = NextIndex(prevElem);
--countElem;
return true;
}

bool RingBuffer::TryPop(int& value) {
if (Empty()) return false;
value = buf[prevElem];
Pop();
return true;
}

int& RingBuffer::operator[](size_t i) {
if(i >= countElem) return buf[countElem];
return buf[((i + prevElem)%buf.capacity())];

}

const int& RingBuffer::operator[](size_t i) const {
if(i >= countElem) return buf[countElem];
return buf[((i + prevElem)%buf.capacity())];
}

int& RingBuffer::Front() {
if(nextElem == 0) return buf[buf.capacity() - 1];
return buf[nextElem - 1];
}

const int& RingBuffer::Front() const {
if(nextElem == 0) return buf[buf.capacity() - 1];
return buf[nextElem - 1];
}

int& RingBuffer::Back() {
return buf[prevElem];
}

const int& RingBuffer::Back() const {
return buf[prevElem];
}






Copy link
Contributor Author

Choose a reason for hiding this comment

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

лишние пустые строки

void RingBuffer::Resize(size_t newCapacity) {
newCapacity = newCapacity ? newCapacity : 1;
if (newCapacity == buf.capacity()) return;

size_t newCount = countElem < newCapacity ? countElem : newCapacity;
std::vector<int> newBuffer = {};
newBuffer.reserve(newCapacity);
newBuffer.resize(newCapacity);

for (size_t i = 0; i < newCount; ++i) {
newBuffer[i] = (*this)[countElem - newCount + i];
}

buf = std::move(newBuffer);
prevElem = 0;
nextElem = newCount % newCapacity;
countElem = newCount;
}

std::vector<int> RingBuffer::Vector() const {
std::vector<int> result;
result.reserve(countElem);
for (size_t i = 0; i < countElem; ++i) result.push_back((*this)[i]);
return result;
}

bool RingBuffer::operator==(const RingBuffer& other) const {
if (countElem != other.countElem || buf.capacity() != other.buf.capacity())
return false;
for (size_t i = 0; i < countElem; ++i)
if ((*this)[i] != other[i]) return false;
return true;
}

bool RingBuffer::operator!=(const RingBuffer& other) const {
return !(*this == other);
}
66 changes: 66 additions & 0 deletions 04_week/tasks/stack/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,71 @@


class Stack {
public:
// методы:

void Push(int newData); // Добавление нэлемента в стек

bool Pop(void); // удаление элемента с верхушки стека

int& Top(); // Доступ к элементу на вершине стека

const int& Top() const; // Доступ к элементу на вершине стека

bool Empty() const; // Проверка отсутсвия элементов на вершине

size_t Size() const; // Размер стека

void Clear(); // Очистка стека

void Swap(Stack& other); // меняем местами элементы

// определение для ==
Copy link
Contributor Author

Choose a reason for hiding this comment

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

лишний комментарий

bool operator==(const Stack& x) const {return data == x.data;}

// определение для !=
bool operator!=(const Stack& x) const {return !(data == x.data);}

Copy link
Contributor Author

@18thday 18thday Jan 21, 2026

Choose a reason for hiding this comment

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

обычно методы группирует без лишних пустых строк, например операторы сравнения можно объявить без пустой строки, также для методов доступа, например, методов изменения

private:
std::vector<int> data; // данные

};



// в Методах для Stack используем методы для vector

void Stack::Push(int newData){
data.push_back(newData);
}

bool Stack::Pop(void){
if(data.empty()) return false;
data.pop_back();
return true;
}

int& Stack::Top(){
return data.back();
}

const int& Stack::Top() const{
return data.back();
}

bool Stack::Empty() const{
return data.empty();
}

size_t Stack::Size() const{
return data.size();
}

void Stack::Clear() {
data.clear();
}

void Stack::Swap(Stack& other){
data.swap(other.data);
}