From: Lukas Jiriste Date: Fri, 25 Oct 2024 08:33:21 +0000 (+0200) Subject: Add method for brain to acquire and express ideas X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=6699469eea1f778769bb07808b7b2a06df87781e;p=42%2FCPP_Module_04 Add method for brain to acquire and express ideas --- diff --git a/ex01/Brain.cpp b/ex01/Brain.cpp index ec96903..b0bce81 100644 --- a/ex01/Brain.cpp +++ b/ex01/Brain.cpp @@ -3,6 +3,8 @@ #include Brain::Brain() + : m_is_full(0) + , m_index(0) { std::cout << "Brain default cstr\n"; } @@ -22,7 +24,32 @@ Brain &Brain::operator=(const Brain &other) { if (this == &other) return (*this); - for (size_t i(0); i < CAPACITY; ++i) - ideas[i] = other.ideas[i]; + m_is_full = other.m_is_full; + for (m_index = 0; + (m_is_full && m_index < CAPACITY) || (m_index < other.m_index); + ++m_index) + m_ideas[m_index] = other.m_ideas[m_index]; + m_index = other.m_index; return (*this); } + +void Brain::addIdea(std::string idea) +{ + ++m_index; + if (m_index == CAPACITY) + m_is_full = 1; + m_index %= CAPACITY; + m_ideas[m_index] = idea; +} + +void Brain::listIdeas() const +{ + size_t i; + + if (m_is_full) + i = 0; + else + i = m_index + 1; + for (; i != m_index; i = (i + 1) % CAPACITY) + std::cout << m_ideas[i] << '\n'; +} diff --git a/ex01/Brain.h b/ex01/Brain.h index 94a09c3..713fe0c 100644 --- a/ex01/Brain.h +++ b/ex01/Brain.h @@ -8,7 +8,9 @@ class Brain private: static const size_t CAPACITY = 100; - std::string ideas[CAPACITY]; + bool m_is_full; + size_t m_index; + std::string m_ideas[CAPACITY]; public: Brain(); @@ -16,6 +18,9 @@ class Brain ~Brain(); Brain &operator=(const Brain &other); + + void addIdea(std::string idea); + void listIdeas() const; }; #endif // BRAIN_H