Add method for brain to acquire and express ideas
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 08:33:21 +0000 (10:33 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 08:33:21 +0000 (10:33 +0200)
ex01/Brain.cpp
ex01/Brain.h

index ec969035dd72fbca6663241a672e8d8db466321c..b0bce8116ef9c3e22887734b67ffc8b00deb272f 100644 (file)
@@ -3,6 +3,8 @@
 #include <iostream>
 
 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';
+}
index 94a09c394636d1f2bfa04bf3ddb8711fa5f55724..713fe0cb67bef6d50d19c90308d08dd260bd9fbd 100644 (file)
@@ -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