feat: finishing the ex02 of module 07

This commit is contained in:
Raphael 2025-03-28 23:11:29 +01:00
parent 02ff08f913
commit e6c05b47c1
4 changed files with 325 additions and 0 deletions

67
cpp07/ex02/Array.hpp Normal file
View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 20:40:58 by rparodi #+# #+# */
/* Updated: 2025/03/28 22:28:19 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#include <string>
template <typename T>
class Array {
public:
Array();
Array(unsigned int n);
Array(Array const &copy);
Array &operator=(Array const &assign);
~Array();
unsigned int size() const;
T & operator[](unsigned int i) const;
private:
T *_array;
unsigned int _size;
};
template <typename T>
std::ostream& operator<<(std::ostream& output, Array<T> const &array);
#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"
#define CLR_LIGHT_BLACK "\033[0;90m"
#define CLR_LIGHT_RED "\033[0;91m"
#define CLR_LIGHT_GREEN "\033[0;92m"
#define CLR_LIGHT_YELLOW "\033[0;93m"
#define CLR_LIGHT_BLUE "\033[0;94m"
#define CLR_LIGHT_MAGENTA "\033[0;95m"
#define CLR_LIGHT_CYAN "\033[0;96m"
#define CLR_LIGHT_WHITE "\033[0;97m"
#define CLR_BOLD_BLACK "\033[1;30m"
#define CLR_BOLD_RED "\033[1;31m"
#define CLR_BOLD_GREEN "\033[1;32m"
#define CLR_BOLD_YELLOW "\033[1;33m"
#define CLR_BOLD_BLUE "\033[1;34m"
#define CLR_BOLD_MAGENTA "\033[1;35m"
#define CLR_BOLD_CYAN "\033[1;36m"
#define CLR_BOLD_WHITE "\033[1;37m"
#include "Array.tpp"

73
cpp07/ex02/Array.tpp Normal file
View file

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Array.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 20:47:57 by rparodi #+# #+# */
/* Updated: 2025/03/28 22:31:24 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Array.hpp"
template <typename T>
Array<T>::Array() : _array(NULL), _size(0) {
std::cout << "[Array] Default constructor called" << std::endl;
}
template <typename T>
Array<T>::Array(unsigned int n) : _array(new T[n]), _size(n) {
std::cout << "[Array] Constructor with size called" << std::endl;
}
template <typename T>
Array<T>::Array(Array const &copy) : _array(new T[copy._size]), _size(copy._size) {
for (unsigned int i = 0; i < copy._size; i++) {
this->_array[i] = copy._array[i];
}
std::cout << "[Array] Copy constructor called" << std::endl;
}
template <typename T>
Array<T> & Array<T>::operator=(Array const &assign) {
if (this != &assign) {
delete[] this->_array;
this->_array = new T[assign._size];
this->_size = assign._size;
for (unsigned int i = 0; i < assign._size; i++) {
this->_array[i] = assign._array[i];
}
}
std::cout << "[Array] Assignation operator called" << std::endl;
return *this;
}
template <typename T>
Array<T>::~Array() {
delete[] this->_array;
std::cout << "[Array] Destructor called" << std::endl;
}
template <typename T>
T & Array<T>::operator[](unsigned int i) const {
if (i >= this->_size) {
throw std::out_of_range("Index out of range");
}
return this->_array[i];
}
template <typename T>
unsigned int Array<T>::size() const {
return this->_size;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, Array<T> const &Array) {
output << CLR_BLUE << "Size:\t" << CLR_GOLD << Array.size() << CLR_RESET << std::endl << std::endl;
for (unsigned int i = 0; i < Array.size(); i++) {
output << CLR_BLUE << "tab[" << CLR_GOLD << i << CLR_BLUE << "]\t" << CLR_GOLD << Array[i] << CLR_RESET << std::endl;
}
return output;
}

126
cpp07/ex02/Makefile Normal file
View file

@ -0,0 +1,126 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2025/03/28 23:09:21 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = array
# 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 = 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}

59
cpp07/ex02/main.cpp Normal file
View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 22:01:37 by rparodi #+# #+# */
/* Updated: 2025/03/28 23:08:46 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Array.hpp"
#include <cstdlib>
#include <ctime>
int main()
{
std::cout << CLR_YELLOW << "\t[ Testing with an empty tab ]" << CLR_RESET << std::endl;
Array<int> empty;
std::cout << empty << std::endl;
std::cout << CLR_YELLOW << "\t[ Testing with a tab with strings ]" << CLR_RESET << std::endl;
Array<std::string> strings(3);
strings[0] = "Hello";
strings[1] = "World";
strings[2] = "!";
std::cout << strings << std::endl << std::endl;
std::cout << CLR_YELLOW << "\t[ Testing deep copy Array<float> ]" << CLR_RESET << std::endl;
Array<float> original_floats(4);
for (unsigned int i = 0; i < original_floats.size(); i++)
original_floats[i] = i + (i * 0.5f);
Array<float> copied_floats(original_floats);
copied_floats[1] = 42.42f;
std::cout << std::endl << CLR_GREEN << "[ Original ]" << CLR_RESET << std::endl << std::endl << original_floats;
std::cout << std::endl << CLR_GREEN << "[ Copied (modified) ]" << CLR_RESET << std::endl << std::endl << copied_floats << std::endl;
std::cout << CLR_YELLOW << "\t[ Testing with a tab with integer ]" << CLR_RESET << std::endl;
Array<int> digits(42);
std::srand(time(NULL));
for (unsigned int i = 0; i < digits.size(); i++) {
try {
digits[i] = std::rand();
} catch (std::exception &e) {
std::cerr << CLR_RED << e.what() << CLR_RESET << std::endl;
}
}
std::cout << std::endl << digits << CLR_RESET << std::endl;
std::cout << CLR_YELLOW << "\t[ Testing out of bands (too far above)]" << CLR_RESET << std::endl;
try {
digits[99];
} catch (std::exception &e) {
std::cerr << CLR_RED << e.what() << CLR_RESET << std::endl;
}
std::cout << CLR_YELLOW << "\t[ Testing out of bands (too far below)]" << CLR_RESET << std::endl;
try {
digits[-42];
} catch (std::exception &e) {
std::cerr << CLR_RED << e.what() << CLR_RESET << std::endl;
}
}