diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ed05fc1..66f63345 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,9 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example +add_subdirectory(stack_lib) +add_subdirectory(checkbreckets) + add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main option(BTEST "build test?" ON) # указываем подключаем ли google-тесты (ON или YES) или нет (OFF или NO) diff --git a/checkbreckets/CMakeList.txt b/checkbreckets/CMakeList.txt new file mode 100644 index 00000000..19c357b0 --- /dev/null +++ b/checkbreckets/CMakeList.txt @@ -0,0 +1,2 @@ +create_project_lib(checkbreckets) +add_depend(checkbreckets stack_lib) \ No newline at end of file diff --git a/checkbreckets/CMakeLists.txt b/checkbreckets/CMakeLists.txt new file mode 100644 index 00000000..17e83972 --- /dev/null +++ b/checkbreckets/CMakeLists.txt @@ -0,0 +1,2 @@ +create_project_lib(Algorithms) +add_depend(Algorithms stack_lib ..\\stack_lib) diff --git a/checkbreckets/checkbreckets.cpp b/checkbreckets/checkbreckets.cpp new file mode 100644 index 00000000..731b7f6d --- /dev/null +++ b/checkbreckets/checkbreckets.cpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include "checkbreckets.h" + + +bool check_brackets(const std::string& val) { + int open_count = 0; + for (char c : val) { + if (c == '(' || c == '[' || c == '{') { + open_count++; + } + } + Stack stack(open_count + 1); + for (char c : val) { + if (c == '(' || c == '[' || c == '{') { + stack.push(c); + } + else if (c == ')' || c == ']' || c == '}') { + if (stack.is_empty()) { + return false; + } + char top = stack.top(); + if ((c == ')' && top == '(') || (c == ']' && top == '[') || (c == '}' && top == '{')) { + stack.pop(); + } + else { + return false; + } + } + } + + return stack.is_empty(); +} \ No newline at end of file diff --git a/checkbreckets/checkbreckets.h b/checkbreckets/checkbreckets.h new file mode 100644 index 00000000..aa85daa3 --- /dev/null +++ b/checkbreckets/checkbreckets.h @@ -0,0 +1,96 @@ +#pragma once +#include +#include + +template +class Stack { + T* _data; + int _size, _top; +public: + Stack(int size = 100); + Stack(const Stack& other); + ~Stack(); + Stack& operator=(const Stack& other); + void push(const T& val); + void pop(); + inline T top() const; + inline bool is_empty() const noexcept; + inline bool is_full() const noexcept; + void clear() noexcept; + int capacity() const noexcept { return _size; } + int count() const noexcept { return _top + 1; } +}; + +template +Stack::Stack(int size) : _size(size > 0 ? size : 100), _top(-1) { + _data = new T[_size]; +} + +template +Stack::Stack(const Stack& other) : _size(other._size), _top(other._top) { + _data = new T[_size]; + for (int i = 0; i <= _top; ++i) { + _data[i] = other._data[i]; + } +} + + +template +Stack::~Stack() { + delete[] _data; +} + + +template +Stack& Stack::operator=(const Stack& other) { + if (this != &other) { + delete[] _data; + _size = other._size; + _top = other._top; + _data = new T[_size]; + for (int i = 0; i <= _top; ++i) { + _data[i] = other._data[i]; + } + } + return *this; +} + + +template +void Stack::push(const T& val) { + if (is_full()) { + throw std::logic_error("stack is full"); + } + _data[++_top] = val; +} + +template +void Stack::pop() { + if (is_empty()) { + throw std::logic_error("stack is empty"); + } + --_top; +} + +template +inline T Stack::top() const { + if (is_empty()) { + throw std::logic_error("stack is empty"); + } + return _data[_top]; +} + +template +inline bool Stack::is_empty() const noexcept { + return _top == -1; +} + +template +inline bool Stack::is_full() const noexcept { + return _top == _size - 1; +} + +template +void Stack::clear() noexcept { + _top = -1; +} diff --git a/checkbreckets/checkbreckets.vcxproj b/checkbreckets/checkbreckets.vcxproj new file mode 100644 index 00000000..f4c3e651 --- /dev/null +++ b/checkbreckets/checkbreckets.vcxproj @@ -0,0 +1,146 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {e8f000ba-1e3c-48a7-88e2-f2640edfc98d} + checkbreckets + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + {9fa79273-5cab-343e-a74d-9bde3288771f} + + + + + + \ No newline at end of file diff --git a/checkbreckets/checkbreckets.vcxproj.filters b/checkbreckets/checkbreckets.vcxproj.filters new file mode 100644 index 00000000..6790918b --- /dev/null +++ b/checkbreckets/checkbreckets.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + + + Исходные файлы + + + + + + \ No newline at end of file diff --git a/stack_lib/CMakeLists.txt b/stack_lib/CMakeLists.txt new file mode 100644 index 00000000..9b7ffcca --- /dev/null +++ b/stack_lib/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(stack_lib) \ No newline at end of file diff --git a/stack_lib/stack_lib.cpp b/stack_lib/stack_lib.cpp new file mode 100644 index 00000000..e69de29b diff --git a/stack_lib/stack_lib.h b/stack_lib/stack_lib.h new file mode 100644 index 00000000..831a17ac --- /dev/null +++ b/stack_lib/stack_lib.h @@ -0,0 +1,96 @@ +#pragma once +#include +#include + +template +class Stack { + T* _data; + int _size, _top; +public: + Stack(int size = 100); + Stack(const Stack& other); + ~Stack(); + Stack& operator=(const Stack& other); + void push(const T& val); + void pop(); + inline T top() const; + inline bool is_empty() const noexcept; + inline bool is_full() const noexcept; + void clear() noexcept; + int capacity() const noexcept { return _size; } + int count() const noexcept { return _top + 1; } +}; + +template +Stack::Stack(int size) : _size(size > 0 ? size : 100), _top(-1) { + _data = new T[_size]; +} + +template +Stack::Stack(const Stack& other) : _size(other._size), _top(other._top) { + _data = new T[_size]; + for (int i = 0; i <= _top; ++i) { + _data[i] = other._data[i]; + } +} + + +template +Stack::~Stack() { + delete[] _data; +} + + +template +Stack& Stack::operator=(const Stack& other) { + if (this != &other) { + delete[] _data; + _size = other._size; + _top = other._top; + _data = new T[_size]; + for (int i = 0; i <= _top; ++i) { + _data[i] = other._data[i]; + } + } + return *this; +} + + +template +void Stack::push(const T& val) { + if (is_full()) { + throw std::logic_error("stack is full"); + } + _data[++_top] = val; +} + +template +void Stack::pop() { + if (is_empty()) { + throw std::logic_error("stack is empty"); + } + --_top; +} + +template +inline T Stack::top() const { + if (is_empty()) { + throw std::logic_error("stack is empty"); + } + return _data[_top]; +} + +template +inline bool Stack::is_empty() const noexcept { + return _top == -1; +} + +template +inline bool Stack::is_full() const noexcept { + return _top == _size - 1; +} + +template +void Stack::clear() noexcept { + _top = -1; +} \ No newline at end of file diff --git a/tests/test_stack_lib.cpp b/tests/test_stack_lib.cpp new file mode 100644 index 00000000..e69de29b