feat: finishing the ex01

This commit is contained in:
Raphael 2025-04-03 16:26:42 +02:00
parent 2644e424a8
commit 81317b6661
5 changed files with 296 additions and 0 deletions

1
.gitignore vendored
View file

@ -30,3 +30,4 @@ function
iter iter
array array
easyfind easyfind
span

127
cpp08/ex01/Makefile Normal file
View file

@ -0,0 +1,127 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2025/04/03 15:36:14 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = span
# Commands
CXX = c++
RM = rm -rf
# Flags
# Mandatory flags for 42
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
# Flags to have the dependences (can be removed for correction)
CXXFLAGS += -MMD
# Flags to have the debug (can be removed for correction)
DEBUG = -g3
# DEBUG += -fsanitize=address
CXXFLAGS += $(DEBUG)
# Sources
SRC = Span.cpp \
main.cpp
# Objects
OBJDIRNAME = ./build
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
# Colors
GREEN = \033[32m
GREY = \033[0;90m
RED = \033[0;31m
GOLD = \033[38;5;220m
END = \033[0m
# Rules
# All (make all)
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) footer
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Clean (make clean)
clean:
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
@$(RM) $(OBJDIRNAME)
# Clean (make fclean)
fclean: clean
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
@$(RM) $(NAME)
@echo ""
# Restart (make re)
re: header fclean all
# Dependences for all
$(NAME): $(OBJ)
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Creating the objects
$(OBJDIRNAME)/%.o: %.cpp
@mkdir -p $(dir $@)
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
@$(CXX) $(CXXFLAGS) -o $@ -c $<
# Header
header:
@clear
@printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n'
@printf '$(GOLD) ******* * ******* $(END)\n'
@printf '$(GOLD) ****** ******* $(END)\n'
@printf '$(GOLD) ******* ******* $(END)\n'
@printf '$(GOLD) ******************* ******* * $(END)\n'
@printf '$(GOLD) ******************* ******* *** $(END)\n'
@printf '$(GOLD) ****** ******* ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GREY) Made by rparodi$(END)\n\n'
# Footer
footer:
@printf "\n"
@printf "$(GOLD) ,_ _,$(END)\n"
@printf "$(GOLD) | \\___//|$(END)\n"
@printf "$(GOLD) |=6 6=|$(END)\n"
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
@printf "$(GOLD) ) \` ( ,$(END)\n"
@printf "$(GOLD) / \\ (('$(END)\n"
@printf "$(GOLD) | | ))$(END)\n"
@printf "$(GOLD) /| | | |\\_//$(END)\n"
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
@printf "$(GOLD) '\"' '\"'$(END)\n"
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
clangd:
@echo -e \
"CompileFlags:\n" \
" Add:\n" \
" - \"-std=c++98 -Wall -Wextra -Werror\"\n" \
" - \"-I"$(shell pwd)"/includes\"\n" \
" - \"-xc++\"\n" \
> .clangd
# Phony
.PHONY: all bonus clean fclean re clangd
-include ${OBJ:.o=.d}

88
cpp08/ex01/Span.cpp Normal file
View file

@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 13:54:01 by rparodi #+# #+# */
/* Updated: 2025/04/03 16:23:31 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
#include <algorithm>
Span::Span() : _size(0), _vec() {
std::cout << CLR_CYAN << "[Span] Default constructor called" << CLR_RESET << std::endl;
}
Span::Span(unsigned int size) : _size(size), _vec() {
std::cout << CLR_CYAN << "[Span] Constructor with size called" << CLR_RESET << std::endl;
}
Span::Span(Span const &copy) : _size(copy._size), _vec(_size) {
std::cout << CLR_CYAN << "[Span] Copy constructor called" << CLR_RESET << std::endl;
}
Span& Span::operator=(Span const &assign) {
if (this != &assign) {
this->_size = assign._size;
this->_vec = assign._vec;
}
std::cout << CLR_CYAN << "[Span] Assign operator called" << CLR_RESET << std::endl;
return *this;
}
Span::~Span() {
std::cout << CLR_MAGENTA << "[Span] Destructor called" << CLR_RESET << std::endl;
}
void Span::addNumber(int number) {
++_size;
if (_vec.size() > this->_size) {
--_size;
throw std::out_of_range("Span is full");
} else {
_vec.push_back(number);
}
}
/**
* @brief Find the shortest span between two numbers in the vector
*
* @return difference between the two closest numbers
*/
unsigned int Span::shortestSpan() {
if (_vec.size() < 2) {
throw std::out_of_range("Not enough numbers in Span");
}
unsigned int diff = std::numeric_limits<unsigned int>::max();
for (size_t i = 0; i < _vec.size(); i++) {
for (size_t j = i + 1; j < _vec.size(); j++) {
if (_vec.at(i) > _vec.at(j) && static_cast<unsigned int>(std::abs(_vec.at(i) - _vec.at(j))) < diff) {
diff = std::abs(_vec.at(i) - _vec.at(j));
}
if (_vec.at(j) > _vec.at(i) && static_cast<unsigned int>(std::abs(_vec.at(j) - _vec.at(i))) < diff) {
diff = std::abs(_vec[j] - _vec[i]);
}
}
}
return diff;
}
/**
* @brief Find the longest span between two numbers in the vector
*
* @return difference between the two farthest numbers
*/
unsigned int Span::longestSpan() {
if (_vec.size() < 2) {
throw std::logic_error("Not enough elements to find a span");
}
int maxVal = *std::max_element(_vec.begin(), _vec.end());
int minVal = *std::min_element(_vec.begin(), _vec.end());
return static_cast<unsigned int>(maxVal - minVal);
}

49
cpp08/ex01/Span.hpp Normal file
View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 13:54:05 by rparodi #+# #+# */
/* Updated: 2025/04/03 15:37:55 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <limits>
#define CLR_RESET "\033[0m"
#define CLR_BLACK "\033[0;30m"
#define CLR_RED "\033[0;31m"
#define CLR_GREEN "\033[0;32m"
#define CLR_YELLOW "\033[0;33m"
#define CLR_BLUE "\033[0;34m"
#define CLR_MAGENTA "\033[0;35m"
#define CLR_CYAN "\033[0;36m"
#define CLR_WHITE "\033[0;37m"
#define CLR_GOLD "\033[38;5;220m"
#define CLR_GREY "\033[38;5;240m"
class Span {
public:
Span();
Span(unsigned int size);
Span(Span const &copy);
Span& operator=(const Span &assign);
~Span();
void addNumber(int number);
unsigned int shortestSpan();
unsigned int longestSpan();
private:
unsigned int _size;
std::vector<int> _vec;
};

31
cpp08/ex01/main.cpp Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 14:09:32 by rparodi #+# #+# */
/* Updated: 2025/04/03 15:21:18 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
int main() {
try {
Span sp = Span(5);
sp.addNumber(5);
sp.addNumber(3);
sp.addNumber(17);
sp.addNumber(9);
sp.addNumber(11);
std::cout << std::endl<< CLR_YELLOW << "[ Test request by subject (2 / 14)]" << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "Shortest span: " << CLR_GOLD << sp.shortestSpan() << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "Longest span: " << CLR_GOLD << sp.longestSpan() << CLR_RESET << std::endl;
} catch (std::exception &e) {
std::cerr << CLR_RED << "Exception: " << e.what() << CLR_RESET << std::endl;
}
return 0;
}