From 021636df08f697e2406f4fbabf1e92314b703d13 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 26 Aug 2025 18:58:54 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20added=20e?= =?UTF-8?q?x01,=20might=20need=20adjustments=20:c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex01/Makefile | 58 ++++++++++++++++++++++++++++++++++++++++ ex01/Span.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ ex01/Span.hpp | 20 ++++++++++++++ ex01/main.cpp | 15 +++++++++++ 4 files changed, 167 insertions(+) create mode 100644 ex01/Makefile create mode 100644 ex01/Span.cpp create mode 100644 ex01/Span.hpp create mode 100644 ex01/main.cpp diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..ffb0111 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou +#include +#include + +#include + +#include "Span.hpp" + +Span::Span(void) +: _size(0), _max_size(0) +{ +} + +Span::Span(unsigned int size) +: _size(0), _max_size(size) +{ +} + +Span::Span(Span &other) +: _size(other._size), _max_size(other._max_size) +{ + this->_data = other._data; +} + +Span &Span::operator=(Span &other) +{ + this->_max_size = other._max_size; + this->_size = other._size; + this->_data = other._data; + + return *this; +} + +Span::~Span(void) +{ +} + +void Span::addNumber(int value) +{ + if (_size == _max_size) + throw std::runtime_error("max span size reached"); + _size++; + _data.push_back(value); + +} + +int Span::shortestSpan(void) +{ + if (_size < 2) + throw std::runtime_error("not enough values in the span"); + + std::vector sorted = _data; + size_t shortest = std::numeric_limits::max(); + + std::sort(sorted.begin(), sorted.end()); + + for (unsigned int i = 0; i < _size - 1; i++) + { + if ((unsigned int)(sorted[i + 1] - sorted[i]) < shortest) + shortest = sorted[i + 1] - sorted[i]; + } + return shortest; +} + +int Span::longestSpan(void) +{ + if (_size < 2) + throw std::runtime_error("not enough values in the span"); + + std::vector::iterator min = std::min_element(_data.begin(), _data.end()); + std::vector::iterator max = std::max_element(_data.begin(), _data.end()); + + return (*max - *min); +} diff --git a/ex01/Span.hpp b/ex01/Span.hpp new file mode 100644 index 0000000..73eaf25 --- /dev/null +++ b/ex01/Span.hpp @@ -0,0 +1,20 @@ +#include + +class Span { +public: + Span(void); + Span(unsigned int); + Span(Span &); + Span &operator=(Span &); + ~Span(void); + + void addNumber(int); + + int shortestSpan(void); + int longestSpan(void); + +private: + std::vector _data; + std::size_t _size; + std::size_t _max_size; +}; diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..e48f492 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,15 @@ +#include "Span.hpp" +#include + +int main() +{ + Span sp(5); + sp.addNumber(6); + 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; +}