diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-15 09:28:09 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-15 09:29:39 +0200 |
| commit | 52a866baba3cbb05ecf0be88f1b50991b4a26b52 (patch) | |
| tree | 70d32f262c8faddce90ce71cce046019da8b10dd /cpp08 | |
| parent | 7ac812044bfe771178752a52d70b18bb297c1891 (diff) | |
| download | piscine_cpp-52a866baba3cbb05ecf0be88f1b50991b4a26b52.tar.gz piscine_cpp-52a866baba3cbb05ecf0be88f1b50991b4a26b52.tar.bz2 piscine_cpp-52a866baba3cbb05ecf0be88f1b50991b4a26b52.zip | |
cpp08 probably done, ex02 has a useless file in this implementation?
Diffstat (limited to 'cpp08')
| -rw-r--r-- | cpp08/.keep | 0 | ||||
| -rw-r--r-- | cpp08/ex00/easyfind.hpp | 24 | ||||
| -rw-r--r-- | cpp08/ex00/main.cpp | 46 | ||||
| -rw-r--r-- | cpp08/ex01/main.cpp | 30 | ||||
| -rw-r--r-- | cpp08/ex01/span.cpp | 67 | ||||
| -rw-r--r-- | cpp08/ex01/span.hpp | 37 | ||||
| -rw-r--r-- | cpp08/ex02/main.cpp | 45 | ||||
| -rw-r--r-- | cpp08/ex02/mutantstack.cpp | 15 | ||||
| -rw-r--r-- | cpp08/ex02/mutantstack.hpp | 130 |
9 files changed, 394 insertions, 0 deletions
diff --git a/cpp08/.keep b/cpp08/.keep deleted file mode 100644 index e69de29..0000000 --- a/cpp08/.keep +++ /dev/null diff --git a/cpp08/ex00/easyfind.hpp b/cpp08/ex00/easyfind.hpp new file mode 100644 index 0000000..68e176c --- /dev/null +++ b/cpp08/ex00/easyfind.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* easyfind.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#ifndef EASYFIND_HPP +# define EASYFIND_HPP + +# include <algorithm> + +template<typename T> +typename T::iterator easyfind(T& container, int x) +{ + return std::find(container.begin(), container.end(), x); +} + +#endif diff --git a/cpp08/ex00/main.cpp b/cpp08/ex00/main.cpp new file mode 100644 index 0000000..8824f99 --- /dev/null +++ b/cpp08/ex00/main.cpp @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#include <iostream> +#include <vector> +#include <list> +#include <deque> +#include "easyfind.hpp" + +int main() +{ + int a[] = {10, 20, 30, 40, 50, 60}; + std::vector<int> b(a, a + 6); + 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, 70) != b.end()) << std::endl; + std::cout << (easyfind(c, 80) != c.end()) << std::endl; + std::cout << (easyfind(d, 90) != d.end()) << std::endl; + + return 0; +} diff --git a/cpp08/ex01/main.cpp b/cpp08/ex01/main.cpp new file mode 100644 index 0000000..c239eaf --- /dev/null +++ b/cpp08/ex01/main.cpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#include <iostream> +#include "span.hpp" + +int main() +{ + 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; + + return 0; +} diff --git a/cpp08/ex01/span.cpp b/cpp08/ex01/span.cpp new file mode 100644 index 0000000..a9bcaf6 --- /dev/null +++ b/cpp08/ex01/span.cpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* span.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#include "span.hpp" + +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]), + m_size(other.m_size), + m_fillIndex(other.m_fillIndex) +{ + for (unsigned int i = 0; i < m_fillIndex; i++) + m_under[i] = other.m_under[i]; +} + +void Span::operator=(Span const& other) +{ + delete [] m_under; + 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(unsigned int n) : m_under(new int[n]), m_size(n), m_fillIndex(0) +{} + +void Span::addNumber(int x) +{ + if (m_fillIndex == m_size) + throw std::exception(); + m_under[m_fillIndex] = x; + m_fillIndex++; +} + +int Span::shortestSpan() const +{ + if (m_size <= 1) + throw std::exception(); + std::sort(m_under, m_under + m_fillIndex); + return m_under[1] - m_under[0]; +} + +int Span::longestSpan() const +{ + if (m_size <= 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 new file mode 100644 index 0000000..0af8453 --- /dev/null +++ b/cpp08/ex01/span.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* span.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#ifndef SPAN_HPP +# define SPAN_HPP + +# include <algorithm> + +class Span +{ +public: + Span(); + Span(Span const& other); + void operator=(Span const& other); + ~Span(); + + Span(unsigned int n); + void addNumber(int x); + int shortestSpan() const; + int longestSpan() const; + +private: + int* m_under; + unsigned int m_size; + unsigned int m_fillIndex; +}; + +#endif diff --git a/cpp08/ex02/main.cpp b/cpp08/ex02/main.cpp new file mode 100644 index 0000000..f3b1ec5 --- /dev/null +++ b/cpp08/ex02/main.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#include <iostream> +#include "mutantstack.hpp" + +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; + ++it; + } + std::stack<int> s(mstack); + return 0; +} diff --git a/cpp08/ex02/mutantstack.cpp b/cpp08/ex02/mutantstack.cpp new file mode 100644 index 0000000..47cc71b --- /dev/null +++ b/cpp08/ex02/mutantstack.cpp @@ -0,0 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mutantstack.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 07:45:13 by charles #+# #+# */ +/* Updated: 2020/04/15 09:24:23 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "mutantstack.hpp" + +// I'm missing something? diff --git a/cpp08/ex02/mutantstack.hpp b/cpp08/ex02/mutantstack.hpp new file mode 100644 index 0000000..2cbeb9e --- /dev/null +++ b/cpp08/ex02/mutantstack.hpp @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mutantstack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#ifndef MUTANTSTACK_HPP +# define MUTANTSTACK_HPP + +# include <stack> + +template<typename T> +class MutantStack : public std::stack<T> +{ +public: + MutantStack() : std::stack<T>() + {} + + MutantStack(MutantStack const& other) : std::stack<T>(other) + {} + + void operator=(MutantStack const& other) + { + std::stack<T>::operator=(other); + } + + ~MutantStack() + {} + + class iterator + { + public: + iterator(iterator const& other) + : 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; + } + + ~iterator() + {} + + iterator(MutantStack<T>& parentStack, unsigned int n) + : m_parentStack(parentStack), m_pos(n) + {} + + T& operator*() + { + std::stack<T> tmp; + + for (unsigned int i = m_pos; i != 0; i--) + { + tmp.push(m_parentStack.top()); + m_parentStack.pop(); + } + T& res = m_parentStack.top(); + while (tmp.size() != 0) + { + m_parentStack.push(tmp.top()); + tmp.pop(); + } + 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; + } + + bool operator==(iterator const& right) + { + return m_pos == right.m_pos; + } + + bool operator!=(iterator const& right) + { + return !(*this == right); + } + + private: + iterator() : m_pos(0) + {} + + MutantStack<T>& m_parentStack; + int m_pos; + }; + + iterator begin() + { + return iterator(*this, 0); + } + + iterator end() + { + return iterator(*this, this->size()); + } +}; + +#endif |
