」 feat: added ex01, might need adjustments :c

This commit is contained in:
y-syo
2025-08-26 18:58:54 +02:00
parent fd0bfc5420
commit 021636df08
4 changed files with 167 additions and 0 deletions

58
ex01/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/26 17:21:38 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CC = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = ex01
SRCS = main.cpp Span.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

74
ex01/Span.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <stdexcept>
#include <algorithm>
#include <limits>
#include <iostream>
#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<int> sorted = _data;
size_t shortest = std::numeric_limits<size_t>::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<int>::iterator min = std::min_element(_data.begin(), _data.end());
std::vector<int>::iterator max = std::max_element(_data.begin(), _data.end());
return (*max - *min);
}

20
ex01/Span.hpp Normal file
View File

@@ -0,0 +1,20 @@
#include <vector>
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<int> _data;
std::size_t _size;
std::size_t _max_size;
};

15
ex01/main.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "Span.hpp"
#include <iostream>
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;
}