1
0

」 feat: finished ex01

This commit is contained in:
2025-04-16 10:37:35 +02:00
parent ec713f8528
commit 7847fdc9e3
5 changed files with 136 additions and 9 deletions

3
.gitignore vendored
View File

@ -4,3 +4,6 @@ obj/
compile_commands.json compile_commands.json
.cache .cache
*.d *.d
easyfind
Span

View File

@ -6,13 +6,13 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 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 SHELL = bash
NAME = easyfind NAME = Span
CC = c++ CC = c++
@ -22,7 +22,7 @@ SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) 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 RED = \033[0;31m
GREEN = \033[0;32m GREEN = \033[0;32m

View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 09:20:40 by adjoly #+# #+# */
/* Updated: 2025/04/16 10:36:54 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
#include <algorithm>
#include <iterator>
#include <list>
#include <stdexcept>
#include <string>
#include <iostream>
#include <vector>
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<int> sorted = _vec;
uint32_t shortedSpan = UINT32_MAX;
std::sort(sorted.begin(), sorted.end());
for (std::vector<int>::iterator it = sorted.begin(), next = sorted.begin() + 1;
next != sorted.end(); next++, it++) {
if (static_cast<uint32_t>(*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<int>::iterator min = std::min_element(_vec.begin(), _vec.end());
std::vector<int>::iterator max = std::max_element(_vec.begin(), _vec.end());
return (*max - *min);
}

View File

@ -6,20 +6,29 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 19:54:33 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 #pragma once
#include <vector>
#include <stdint.h>
class Span { class Span {
public: public:
Span(unsigned int); Span(unsigned int);
~Span(void); ~Span(void);
void addNumber(int); void addNumber(int);
int shortestSpan(void);
int longestSpan(void);
int shortestSpan(void);
int longestSpan(void);
private: private:
Span(void);
std::vector<int> _vec;
uint32_t _size;
uint32_t _i;
}; };

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/16 10:13:48 by adjoly #+# #+# */
/* Updated: 2025/04/16 10:17:16 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
#include <exception>
#include <iostream>
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;
}