aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cpp08/ex00/easyfind.hpp9
-rw-r--r--cpp08/ex00/main.cpp29
-rw-r--r--cpp08/ex01/main.cpp108
-rw-r--r--cpp08/ex01/span.cpp34
-rw-r--r--cpp08/ex01/span.hpp17
-rw-r--r--cpp08/ex02/main.cpp45
-rw-r--r--cpp08/ex02/mutantstack.hpp84
7 files changed, 190 insertions, 136 deletions
diff --git a/cpp08/ex00/easyfind.hpp b/cpp08/ex00/easyfind.hpp
index 68e176c..7523f53 100644
--- a/cpp08/ex00/easyfind.hpp
+++ b/cpp08/ex00/easyfind.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 05:53:04 by charles #+# #+# */
-/* Updated: 2020/04/15 06:47:27 by charles ### ########.fr */
+/* Updated: 2020/12/15 11:58:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,9 +16,12 @@
# include <algorithm>
template<typename T>
-typename T::iterator easyfind(T& container, int x)
+int& easyfind(T& container, int x)
{
- return std::find(container.begin(), container.end(), x);
+ typename T::iterator found = std::find(container.begin(), container.end(), x);
+ if (found == container.end())
+ throw std::exception();
+ return *found;
}
#endif
diff --git a/cpp08/ex00/main.cpp b/cpp08/ex00/main.cpp
index 8824f99..af8f81c 100644
--- a/cpp08/ex00/main.cpp
+++ b/cpp08/ex00/main.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 05:57:10 by charles #+# #+# */
-/* Updated: 2020/04/15 06:47:52 by charles ### ########.fr */
+/* Updated: 2020/12/15 11:59:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,24 +23,17 @@ int main()
std::list<int> c(a, a + 6);
std::deque<int> d(a, a + 6);
- std::cout << (easyfind(b, 10) != b.end()) << std::endl;
- std::cout << (easyfind(c, 20) != c.end()) << std::endl;
- std::cout << (easyfind(d, 30) != d.end()) << std::endl;
- std::cout << (easyfind(b, 40) != b.end()) << std::endl;
- std::cout << (easyfind(c, 50) != c.end()) << std::endl;
- std::cout << (easyfind(d, 60) != d.end()) << std::endl;
+ std::cout << easyfind(b, 10) << std::endl;
+ std::cout << easyfind(c, 20) << std::endl;
+ std::cout << easyfind(d, 30) << std::endl;
+ std::cout << easyfind(b, 40) << std::endl;
+ std::cout << easyfind(c, 50) << std::endl;
+ std::cout << easyfind(d, 60) << std::endl;
- std::cout << *easyfind(b, 10) << std::endl;
- std::cout << *easyfind(c, 20) << std::endl;
- std::cout << *easyfind(d, 30) << std::endl;
- std::cout << *easyfind(b, 40) << std::endl;
- std::cout << *easyfind(c, 50) << std::endl;
- std::cout << *easyfind(d, 60) << std::endl;
-
-
- std::cout << (easyfind(b, 70) != b.end()) << std::endl;
- std::cout << (easyfind(c, 80) != c.end()) << std::endl;
- std::cout << (easyfind(d, 90) != d.end()) << std::endl;
+ try { std::cout << easyfind(b, 70) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { std::cout << easyfind(c, 80) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { std::cout << easyfind(d, 90) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { std::cout << easyfind(d, -1) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; }
return 0;
}
diff --git a/cpp08/ex01/main.cpp b/cpp08/ex01/main.cpp
index c239eaf..c702748 100644
--- a/cpp08/ex01/main.cpp
+++ b/cpp08/ex01/main.cpp
@@ -6,25 +6,115 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 06:50:51 by charles #+# #+# */
-/* Updated: 2020/04/15 07:19:40 by charles ### ########.fr */
+/* Updated: 2020/12/15 13:52:20 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
+#include <fstream>
+#include <cstdlib>
#include "span.hpp"
int main()
{
- Span sp = Span(5);
+ {
+ std::cout << "======================== SUBJECT MAIN =======================" << std::endl;
+ Span sp = Span(5);
+ sp.addNumber(5);
+ sp.addNumber(3);
+ sp.addNumber(17);
+ sp.addNumber(9);
+ sp.addNumber(11);
+ std::cout << sp.shortestSpan() << std::endl;
+ std::cout << sp.longestSpan() << std::endl;
+ }
- sp.addNumber(5);
- sp.addNumber(3);
- sp.addNumber(17);
- sp.addNumber(9);
- sp.addNumber(11);
+ std::cout << std::endl;
- std::cout << sp.shortestSpan() << std::endl;
- std::cout << sp.longestSpan() << std::endl;
+ {
+ std::cout << "======================== DEFAULT CONSTRUCTOR =======================" << std::endl;
+ Span p;
+ try { p.addNumber(0); } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { p.shortestSpan(); } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { p.longestSpan(); } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "======================== SINGLE ELEMENT =======================" << std::endl;
+ Span p(1);
+ try { p.addNumber(1); } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { p.addNumber(2); } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { p.shortestSpan(); } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ try { p.longestSpan(); } catch (std::exception &e) { std::cout << e.what() << std::endl; }
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "======================== TWO ELEMENT =======================" << std::endl;
+ Span p(2);
+ try { p.addNumber(1); } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { p.addNumber(4); } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { std::cout << p.shortestSpan() << std::endl; } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { std::cout << p.longestSpan() << std::endl; } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ }
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "======================== DUP ELEMENT =======================" << std::endl;
+ Span p(3);
+ try { p.addNumber(4); } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { p.addNumber(4); } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { p.addNumber(6); } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { std::cout << p.shortestSpan() << std::endl; } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ try { std::cout << p.longestSpan() << std::endl; } catch (std::exception &e) { std::cout << "SHOULD NOT PRINT" << std::endl; }
+ }
+
+
+ std::cout << std::endl;
+
+ {
+ std::cout << "======================== RANGE ELEMENT =======================" << std::endl;
+ Span sp(5);
+ int ns[] = {5, 3, 17, 9, 11};
+ sp.addNumber(ns, ns + (sizeof(ns) / sizeof(int)));
+ std::cout << sp.shortestSpan() << std::endl;
+ std::cout << sp.longestSpan() << std::endl;
+ }
+
+ int seed;
+ std::ifstream devRandom("/dev/random");
+ if (devRandom.is_open())
+ {
+ devRandom.read((char*)&seed, sizeof(int));
+ devRandom.close();
+ }
+ else
+ {
+ seed = time(NULL);
+ }
+ srand(seed);
+
+ {
+ std::cout << "======================== 10000 ELEMENT =======================" << std::endl;
+ Span sp(10000);
+ for (size_t i = 0; i < 10000; i++)
+ sp.addNumber(rand());
+ std::cout << sp.shortestSpan() << std::endl;
+ std::cout << sp.longestSpan() << std::endl;
+ }
+
+ {
+ std::cout << "======================== 100000 ELEMENT =======================" << std::endl;
+ Span sp(100000);
+ for (size_t i = 0; i < 100000; i++)
+ sp.addNumber(rand());
+ std::cout << sp.shortestSpan() << std::endl;
+ std::cout << sp.longestSpan() << std::endl;
+ }
return 0;
}
diff --git a/cpp08/ex01/span.cpp b/cpp08/ex01/span.cpp
index a9bcaf6..ab17049 100644
--- a/cpp08/ex01/span.cpp
+++ b/cpp08/ex01/span.cpp
@@ -6,14 +6,14 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 06:57:44 by charles #+# #+# */
-/* Updated: 2020/04/15 07:22:42 by charles ### ########.fr */
+/* Updated: 2020/12/15 12:11:38 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "span.hpp"
-Span::Span() : m_under(new int[0]), m_size(0), m_fillIndex(0)
-{}
+Span::Span()
+ : m_under(new int[0]), m_size(0), m_fillIndex(0) {}
Span::Span(Span const& other)
: m_under(new int[other.m_size]),
@@ -27,24 +27,21 @@ Span::Span(Span const& other)
void Span::operator=(Span const& other)
{
delete [] m_under;
- m_size = other.m_size;
- m_under = new int[m_size];
+ m_size = other.m_size;
+ m_under = new int[m_size];
m_fillIndex = other.m_fillIndex;
for (unsigned int i = 0; i < m_fillIndex; i++)
m_under[i] = other.m_under[i];
}
-Span::~Span()
-{
- delete [] m_under;
-}
+Span::~Span() { delete [] m_under; }
-Span::Span(unsigned int n) : m_under(new int[n]), m_size(n), m_fillIndex(0)
-{}
+Span::Span(unsigned int n)
+ : m_under(new int[n]), m_size(n), m_fillIndex(0) {}
void Span::addNumber(int x)
{
- if (m_fillIndex == m_size)
+ if (m_fillIndex >= m_size)
throw std::exception();
m_under[m_fillIndex] = x;
m_fillIndex++;
@@ -52,16 +49,19 @@ void Span::addNumber(int x)
int Span::shortestSpan() const
{
- if (m_size <= 1)
- throw std::exception();
- std::sort(m_under, m_under + m_fillIndex);
+ setupSpan();
return m_under[1] - m_under[0];
}
int Span::longestSpan() const
{
- if (m_size <= 1)
+ setupSpan();
+ return m_under[m_fillIndex - 1] - m_under[0];
+}
+
+void Span::setupSpan() const
+{
+ if (m_fillIndex <= 1)
throw std::exception();
std::sort(m_under, m_under + m_fillIndex);
- return m_under[m_fillIndex - 1] - m_under[0];
}
diff --git a/cpp08/ex01/span.hpp b/cpp08/ex01/span.hpp
index 0af8453..6b72fb5 100644
--- a/cpp08/ex01/span.hpp
+++ b/cpp08/ex01/span.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 06:55:07 by charles #+# #+# */
-/* Updated: 2020/04/15 07:19:46 by charles ### ########.fr */
+/* Updated: 2020/12/15 12:22:31 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,14 +24,25 @@ public:
~Span();
Span(unsigned int n);
+
void addNumber(int x);
+
+ template <typename InputIterator>
+ void addNumber(InputIterator begin, InputIterator end)
+ {
+ for (; begin != end; ++begin)
+ addNumber(*begin);
+ }
+
int shortestSpan() const;
- int longestSpan() const;
+ int longestSpan() const;
private:
- int* m_under;
+ int* m_under;
unsigned int m_size;
unsigned int m_fillIndex;
+
+ void setupSpan() const;
};
#endif
diff --git a/cpp08/ex02/main.cpp b/cpp08/ex02/main.cpp
index f3b1ec5..cb138b3 100644
--- a/cpp08/ex02/main.cpp
+++ b/cpp08/ex02/main.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 07:30:43 by charles #+# #+# */
-/* Updated: 2020/04/15 09:21:12 by charles ### ########.fr */
+/* Updated: 2020/12/15 14:24:54 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,31 +15,28 @@
int main()
{
- MutantStack<int> mstack;
-
- mstack.push(5);
- mstack.push(17);
-
- std::cout << mstack.top() << std::endl;
-
- mstack.pop();
-
- std::cout << mstack.size() << std::endl;
-
- mstack.push(3);
- mstack.push(5);
- mstack.push(737);
- mstack.push(0);
- MutantStack<int>::iterator it = mstack.begin();
- MutantStack<int>::iterator ite = mstack.end();
-
- ++it;
- --it;
- while (it != ite)
{
- std::cout << *it << std::endl;
+ std::cout << "=================== SUBJECT MAIN ======================" << std::endl;
+ MutantStack<int> mstack;
+ mstack.push(5);
+ mstack.push(17);
+ std::cout << mstack.top() << std::endl;
+ mstack.pop();
+ std::cout << mstack.size() << std::endl;
+ mstack.push(3);
+ mstack.push(5);
+ mstack.push(737);
+ mstack.push(0);
+ MutantStack<int>::iterator it = mstack.begin();
+ MutantStack<int>::iterator ite = mstack.end();
++it;
+ --it;
+ while (it != ite)
+ {
+ std::cout << *it << std::endl;
+ ++it;
+ }
+ std::stack<int> s(mstack);
}
- std::stack<int> s(mstack);
return 0;
}
diff --git a/cpp08/ex02/mutantstack.hpp b/cpp08/ex02/mutantstack.hpp
index 2cbeb9e..56a5bbf 100644
--- a/cpp08/ex02/mutantstack.hpp
+++ b/cpp08/ex02/mutantstack.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/15 07:29:42 by charles #+# #+# */
-/* Updated: 2020/04/15 09:25:55 by charles ### ########.fr */
+/* Updated: 2020/12/15 14:21:26 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,43 +15,38 @@
# include <stack>
-template<typename T>
+template <typename T>
class MutantStack : public std::stack<T>
{
public:
- MutantStack() : std::stack<T>()
- {}
+ MutantStack() : std::stack<T>() {}
- MutantStack(MutantStack const& other) : std::stack<T>(other)
- {}
+ MutantStack(MutantStack const& other) : std::stack<T>(other) {}
- void operator=(MutantStack const& other)
+ MutantStack& operator=(MutantStack const& other)
{
std::stack<T>::operator=(other);
+ return *this;
}
- ~MutantStack()
- {}
+ ~MutantStack() {}
class iterator
{
public:
iterator(iterator const& other)
- : m_parentStack(other.m_parentStack), m_pos(other.m_pos)
- {}
+ : m_parentStack(other.m_parentStack), m_pos(other.m_pos) {}
void operator=(iterator const& other)
{
m_parentStack = other.m_parentStack;
- m_pos = other.m_pos;
+ m_pos = other.m_pos;
}
- ~iterator()
- {}
+ ~iterator() {}
iterator(MutantStack<T>& parentStack, unsigned int n)
- : m_parentStack(parentStack), m_pos(n)
- {}
+ : m_parentStack(parentStack), m_pos(n) {}
T& operator*()
{
@@ -70,61 +65,26 @@ public:
}
return res;
}
- // T& operator->();
- // pre increment;
- iterator& operator++()
- {
- m_pos++;
- return *this;
- }
-
- // post increment;
- iterator& operator++(int)
- {
- m_pos++;
- return *this;
- }
-
- iterator& operator--()
- {
- m_pos--;
- return *this;
- }
-
- iterator& operator--(int)
- {
- m_pos--;
- return *this;
- }
+ T* operator->() { return &(*(*this)); }
- bool operator==(iterator const& right)
- {
- return m_pos == right.m_pos;
- }
+ iterator& operator++() { m_pos++; return *this; }
+ iterator& operator++(int) { m_pos++; return *this; }
+ iterator& operator--() { m_pos--; return *this; }
+ iterator& operator--(int) { m_pos--; return *this; }
- bool operator!=(iterator const& right)
- {
- return !(*this == right);
- }
+ bool operator==(iterator const& right) { return m_pos == right.m_pos; }
+ bool operator!=(iterator const& right) { return !(*this == right); }
private:
- iterator() : m_pos(0)
- {}
+ iterator() : m_pos(0) {}
MutantStack<T>& m_parentStack;
- int m_pos;
+ int m_pos;
};
- iterator begin()
- {
- return iterator(*this, 0);
- }
-
- iterator end()
- {
- return iterator(*this, this->size());
- }
+ iterator begin() { return iterator(*this, 0); }
+ iterator end() { return iterator(*this, this->size()); }
};
#endif