diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-12-15 14:25:57 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-12-15 14:25:57 +0100 |
| commit | 4115f22c50ee0571e88a8ec44c0241693af7ed38 (patch) | |
| tree | 9e702e34ba360aa5f10f8f84b37b97b6ca8dd731 /cpp08/ex01/span.cpp | |
| parent | 78efa932fb42289904fe542cfc152978397ae37c (diff) | |
| download | piscine_cpp-4115f22c50ee0571e88a8ec44c0241693af7ed38.tar.gz piscine_cpp-4115f22c50ee0571e88a8ec44c0241693af7ed38.tar.bz2 piscine_cpp-4115f22c50ee0571e88a8ec44c0241693af7ed38.zip | |
Fixing cpp08/{00,01}
Diffstat (limited to 'cpp08/ex01/span.cpp')
| -rw-r--r-- | cpp08/ex01/span.cpp | 34 |
1 files changed, 17 insertions, 17 deletions
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]; } |
