This commit is contained in:
2024-03-03 18:40:03 +01:00
parent d36af81adb
commit 8be6db72f0
15 changed files with 842 additions and 51 deletions

61
cpp08/ex01/Makefile Normal file
View File

@@ -0,0 +1,61 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/03/03 15:48:48 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
DEF_COLOR = \033[0m
GRAY = \033[0;90m
RED = \033[0;91m
GREEN = \033[0;92m
YELLOW = \033[0;93m
BLUE = \033[0;94m
MAGENTA = \033[0;95m
CYAN = \033[0;96m
WHITE = \033[0;97m
ORANGE = \033[38;5;214m
NAME = span
SRC = main.cpp \
Span.cpp \
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
OBJS = ${SRC:.cpp=.o}
all : $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./span" to execute the program!!$(DEF_COLOR)"
$(LOGO)
clean :
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
$(RM) $(OBJS)
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
$(LOGO)
fclean : clean
@echo "$(RED)Delete program name... $(DEF_COLOR)"
$(RM) $(NAME)
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
$(LOGO)
re : fclean all
.PHONY : all clean fclean re

84
cpp08/ex01/Span.cpp Normal file
View File

@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 16:20:25 by fgras-ca #+# #+# */
/* Updated: 2024/03/03 18:28:26 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
Span::Span(unsigned int N): capacity(N)
{
std::cout << GREEN << "Span constructor called!" << RESET << std::endl;
}
Span::Span(const Span& other): numbers(other.numbers), capacity(other.capacity)
{
std::cout << CYAN << "Copy constructor called for Span." << RESET << std::endl;
}
Span& Span::operator=(const Span& other)
{
std::cout << YELLOW << "Copy assignment Span operator called" << RESET << std::endl;
if (this != &other)
{
numbers = other.numbers;
capacity = other.capacity;
}
return (*this);
}
Span::~Span()
{
std::cout << RED << "Span is destroyed!" << RESET << std::endl;
}
void Span::addNumber(int number)
{
if (numbers.size() >= capacity)
throw std::length_error("Span is full.");
numbers.push_back(number);
}
int Span::shortestSpan()
{
if (numbers.size() < 2)
throw std::logic_error("Not enough elements for span.");
std::sort(numbers.begin(), numbers.end());
int minSpan = std::numeric_limits<int>::max();
for (size_t i = 1; i < numbers.size(); ++i)
{
int span = numbers[i] - numbers[i - 1];
if (span < minSpan)
minSpan = span;
}
return (minSpan);
}
int Span::longestSpan()
{
if (numbers.size() < 2)
throw std::logic_error("Not enough elements for span.");
// Utilisation de std::min_element et std::max_element séparément
std::vector<int>::iterator minIt = std::min_element(numbers.begin(), numbers.end());
std::vector<int>::iterator maxIt = std::max_element(numbers.begin(), numbers.end());
// Calcul de la span la plus longue
return (*maxIt - *minIt);
}
template<typename Iterator>
void Span::addRange(Iterator begin, Iterator end)
{
while (begin != end)
{
addNumber(*begin);
++begin;
}
}

51
cpp08/ex01/Span.hpp Normal file
View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 15:53:40 by fgras-ca #+# #+# */
/* Updated: 2024/03/03 16:49:04 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SPAN_HPP
#define SPAN_HPP
#include <vector>
#include <exception>
#include <algorithm>
#include <limits>
#include <iostream>
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
class Span
{
public:
Span(unsigned int N); //constructor
Span(const Span& other); //copy constructor
Span& operator=(const Span& other); //operateur d'assignation
~Span(); //destructor
void addNumber(int number);
int shortestSpan();
int longestSpan();
template<typename Iterator>
void addRange(Iterator begin, Iterator end);
private:
std::vector<int> numbers;
unsigned int capacity;
};
#endif

91
cpp08/ex01/main.cpp Normal file
View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 17:00:49 by fgras-ca #+# #+# */
/* Updated: 2024/03/03 17:59:58 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Span.hpp"
#include <cstdlib> // Pour std::rand et std::srand
#include <ctime> // Pour std::time
#include <set>
int main()
{
// Test initial
Span sp = Span(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;
// Ajout de tests pour les exceptions
try
{
Span emptySpan(10);
emptySpan.shortestSpan();
}
catch (std::exception& e)
{
std::cout << RED << "\nException caught for shortestSpan with less than 2 elements: " << RESET << e.what() << std::endl;
}
try
{
Span singleElementSpan(10);
singleElementSpan.addNumber(1);
singleElementSpan.longestSpan();
}
catch (std::exception& e)
{
std::cout << RED << "Exception caught for longestSpan with less than 2 elements: " << RESET << e.what() << std::endl;
}
try
{
Span fullSpan(1);
fullSpan.addNumber(1);
fullSpan.addNumber(2); // Devrait lancer une exception
}
catch (std::exception& e)
{
std::cout << RED << "Exception caught for adding to a full Span: " << RESET << e.what() << std::endl;
}
// Interaction en ligne de commande
unsigned int N;
std::cout << YELLOW << "\nEnter the number of elements to generate: " << RESET;
std::cin >> N;
Span userSpan(N);
std::set<int> uniqueNumbers;
while(uniqueNumbers.size() < N)
{
int number = std::rand() % 100000; // Générer un nombre aléatoire entre 0 et 999
if(uniqueNumbers.insert(number).second)
{ // Vérifie si l'insertion est réussie (élément n'était pas déjà présent)
userSpan.addNumber(number);
std::cout << BLUE << "Generated number: " << RESET << number << std::endl;
}
}
if (N > 1)
{
std::cout << CYAN << "Shortest span (userSpan): " << RESET << userSpan.shortestSpan() << std::endl;
std::cout << CYAN << "Longest span (userSpan): " << RESET << userSpan.longestSpan() << std::endl;
}
else
{
std::cout << RED << "Not enough elements to calculate spans." << RESET << std::endl;
}
return (0);
}