From 7847fdc9e365fdf35bead90a7832a6b2b394965c Mon Sep 17 00:00:00 2001 From: adjoly Date: Wed, 16 Apr 2025 10:37:35 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20finished?= =?UTF-8?q?=20ex01?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ ex01/Makefile | 6 ++-- ex01/Span.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ ex01/Span.hpp | 21 +++++++++---- ex01/main.cpp | 34 +++++++++++++++++++++ 5 files changed, 136 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 96e6889..8cc1fd1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ obj/ compile_commands.json .cache *.d + +easyfind +Span diff --git a/ex01/Makefile b/ex01/Makefile index 7225175..c165077 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -6,13 +6,13 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/04/14 19:06:23 by adjoly ### ########.fr # +# Updated: 2025/04/16 10:35:18 by adjoly ### ########.fr # # # # **************************************************************************** # SHELL = bash -NAME = easyfind +NAME = Span CC = c++ @@ -22,7 +22,7 @@ SRCS = $(shell find . -name '*.cpp') OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) -FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP +FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP -g RED = \033[0;31m GREEN = \033[0;32m diff --git a/ex01/Span.cpp b/ex01/Span.cpp index e69de29..c55bb5d 100644 --- a/ex01/Span.cpp +++ b/ex01/Span.cpp @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/15 09:20:40 by adjoly #+# #+# */ +/* Updated: 2025/04/16 10:36:54 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" +#include +#include +#include +#include +#include +#include +#include + +static void _log(std::string emoji, std::string what, std::string who, + std::string str) { +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」" << what << ": " << str << std::endl; + else + std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + +Span::Span(unsigned int n) : _size(n), _i(0) { + _log("➕", "Span", "", "n constructor called"); +} + +Span::Span(void) : _size(1), _i(0) { + _log("➕", "Span", "", "default constructor called"); +} + +Span::~Span(void) { _log("➖", "Span", "", "destructor called"); } + +void Span::addNumber(int nb) { + _log("➕", "Span", "", "addNumber function called"); + if (_i >= _size) { + throw std::out_of_range("Span is full"); + } + _vec.push_back(nb); + _i++; +} + +int Span::shortestSpan(void) { + if (_i < 2) { + throw std::runtime_error("not enough item in Span"); + } + + std::vector sorted = _vec; + uint32_t shortedSpan = UINT32_MAX; + + std::sort(sorted.begin(), sorted.end()); + + for (std::vector::iterator it = sorted.begin(), next = sorted.begin() + 1; + next != sorted.end(); next++, it++) { + if (static_cast(*next - *it) < shortedSpan) { + shortedSpan = *next - *it; + } + } + return shortedSpan; +} + +int Span::longestSpan(void) { + if (_i < 2) { + throw std::runtime_error("not enough item in Span"); + } + + std::vector::iterator min = std::min_element(_vec.begin(), _vec.end()); + std::vector::iterator max = std::max_element(_vec.begin(), _vec.end()); + return (*max - *min); +} diff --git a/ex01/Span.hpp b/ex01/Span.hpp index 3938ca1..e108498 100644 --- a/ex01/Span.hpp +++ b/ex01/Span.hpp @@ -6,20 +6,29 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 19:54:33 by adjoly #+# #+# */ -/* Updated: 2025/04/14 20:09:51 by adjoly ### ########.fr */ +/* Updated: 2025/04/16 10:12:43 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once +#include +#include + class Span { public: - Span(unsigned int); - ~Span(void); + Span(unsigned int); + ~Span(void); - void addNumber(int); + void addNumber(int); + + int shortestSpan(void); + int longestSpan(void); - int shortestSpan(void); - int longestSpan(void); private: + Span(void); + + std::vector _vec; + uint32_t _size; + uint32_t _i; }; diff --git a/ex01/main.cpp b/ex01/main.cpp index e69de29..b120451 100644 --- a/ex01/main.cpp +++ b/ex01/main.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/16 10:13:48 by adjoly #+# #+# */ +/* Updated: 2025/04/16 10:17:16 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" +#include +#include + +int main(void) { + Span span(5); + + try { + std::cout << "Filling span" << std::endl; + span.addNumber(1); + span.addNumber(4); + span.addNumber(10); + span.addNumber(7); + span.addNumber(8); + span.addNumber(3); + } catch (std::exception &e) { + std::cerr << e.what() << std::endl; + } + + std::cout << span.shortestSpan() << std::endl; + std::cout << span.longestSpan() << std::endl; +}