feat(08): finishing the ex02

This commit is contained in:
Raphael 2025-04-04 14:29:04 +02:00
parent 81317b6661
commit 45100a4ed7
4 changed files with 288 additions and 0 deletions

126
cpp08/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/04/04 13:30:55 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = mutated
# 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}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MutantStack.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/04 13:32:29 by rparodi #+# #+# */
/* Updated: 2025/04/04 14:08:09 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#include <stack>
#include <iterator>
#include <list>
template <typename T>
class MutantStack : public std::stack<T> {
public:
MutantStack();
MutantStack(const MutantStack &copy);
MutantStack<T>& operator=(const MutantStack<T> &assign);
~MutantStack();
typedef typename std::stack<T>::container_type container_type;
typedef typename container_type::iterator iterator;
typedef typename container_type::const_iterator const_iterator;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
private:
};
#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"
#include "MutantStack.tpp"

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MutantStack.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/04 13:39:16 by rparodi #+# #+# */
/* Updated: 2025/04/04 14:15:31 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
template <typename T>
MutantStack<T>::MutantStack() : std::stack<T>() {
std::cout << CLR_CYAN << "[MutantStack] Default constructor called" << CLR_RESET << std::endl;
}
template <typename T>
MutantStack<T>::MutantStack(const MutantStack<T> &copy) : std::stack<T>(copy) {
std::cout << CLR_CYAN << "[MutantStack] Copy constructor called" << CLR_RESET << std::endl;
}
template <typename T>
MutantStack<T>& MutantStack<T>::operator=(const MutantStack<T> &assign) {
std::cout << CLR_CYAN << "[MutantStack] Assignment operator called" << CLR_RESET << std::endl;
if (this != &assign) {
std::stack<T>::operator=(assign);
}
return *this;
}
template <typename T>
MutantStack<T>::~MutantStack() {
std::cout << CLR_MAGENTA << "[MutantStack] Destructor called" << CLR_RESET << std::endl;
}
template <typename T>
typename MutantStack<T>::iterator MutantStack<T>::begin() {
return this->c.begin();
}
template <typename T>
typename MutantStack<T>::iterator MutantStack<T>::end() {
return this->c.end();
}

65
cpp08/ex02/main.cpp Normal file
View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/04 13:33:02 by rparodi #+# #+# */
/* Updated: 2025/04/04 14:27:54 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "MutantStack.hpp"
int main()
{
std::list<int> lst;
MutantStack<int> mstack;
lst.push_back(5);
lst.push_back(17);
mstack.push(5);
mstack.push(17);
std::cout << std::endl << CLR_YELLOW << "\t[ Testing top() method ]" << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "List:\t\t" << CLR_GOLD << lst.back() << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "MutantStack:\t" << CLR_GOLD << mstack.top() << CLR_RESET << std::endl;
mstack.pop();
lst.pop_back();
std::cout << std::endl << CLR_YELLOW << "\t[ Testing size() method ]" << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "List:\t\t" << CLR_GOLD << lst.size() << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "MutantStack:\t" << CLR_GOLD << mstack.size() << CLR_RESET << std::endl;
mstack.push(3);
lst.push_back(3);
mstack.push(5);
lst.push_back(5);
mstack.push(737);
lst.push_back(737);
mstack.push(0);
lst.push_back(0);
std::list<int>::iterator lst_it = lst.begin();
std::list<int>::iterator lst_ite = lst.end();
MutantStack<int>::iterator mstack_it = mstack.begin();
MutantStack<int>::iterator mstack_ite = mstack.end();
lst_it++;
lst_it--;
++mstack_it;
--mstack_it;
std::cout << std::endl << CLR_YELLOW << "\t[ Testing iterator ]" << CLR_RESET << std::endl;
std::cout << CLR_BLUE << "List:" << CLR_RESET << "\t\t";
while (lst_it != lst_ite)
{
std::cout << CLR_GOLD << *lst_it << CLR_RESET << " ";
++lst_it;
}
std::cout << std::endl << CLR_BLUE << "MutantStack:" << CLR_RESET << "\t";
while (mstack_it != mstack_ite)
{
std::cout << CLR_GOLD << *mstack_it << CLR_RESET << " ";
++mstack_it;
}
std::cout << std::endl;
std::cout << std::endl << CLR_YELLOW << "\t[ Testing stack conversion ]" << CLR_RESET << std::endl;
std::stack<int> s(mstack);
std::cout << CLR_BLUE << "Stack size:\t" << CLR_GOLD << s.size() << CLR_RESET << std::endl << std::endl;
return 0;
}