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/ex01 | |
| 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/ex01')
| -rw-r--r-- | cpp08/ex01/main.cpp | 30 | ||||
| -rw-r--r-- | cpp08/ex01/span.cpp | 67 | ||||
| -rw-r--r-- | cpp08/ex01/span.hpp | 37 |
3 files changed, 134 insertions, 0 deletions
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 |
