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

60
cpp08/ex02/Makefile Normal file
View File

@@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/03/03 18:13:54 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 = mutant
SRC = main.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 "./mutant" 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

View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MutantStack.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 18:22:10 by fgras-ca #+# #+# */
/* Updated: 2024/03/03 18:35:45 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MUTANTSTACK_HPP
#define MUTANTSTACK_HPP
#include <stack>
#include <iostream>
#include <deque>
template<typename T, typename Container = std::deque<T> >
class MutantStack : public std::stack<T, Container>
{
public:
MutantStack(); // Constructeur par défaut
MutantStack(const MutantStack& other); // Constructeur de copie
~MutantStack(); // Destructeur
MutantStack& operator=(const MutantStack& other); // Opérateur d'assignation
typedef typename Container::iterator iterator;
typedef typename Container::const_iterator const_iterator;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
private:
using std::stack<T, Container>::c; // Utilisez 'c' pour accéder au conteneur sous-jacent
};
#include "MutantStack.tpp"
#endif

View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MutantStack.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 18:23:49 by fgras-ca #+# #+# */
/* Updated: 2024/03/03 18:37:30 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
// Constructeur par défaut
template<typename T, typename Container>
MutantStack<T, Container>::MutantStack() : std::stack<T, Container>()
{
std::cout << "MutantStack constructor called!" << std::endl;
}
// Constructeur de copie
template<typename T, typename Container>
MutantStack<T, Container>::MutantStack(const MutantStack& other) : std::stack<T, Container>(other)
{
std::cout << "Copy constructor called for MutantStack." << std::endl;
}
// Destructeur
template<typename T, typename Container>
MutantStack<T, Container>::~MutantStack()
{
std::cout << "MutantStack is destroyed!" << std::endl;
}
// Opérateur d'assignation
template<typename T, typename Container>
MutantStack<T, Container>& MutantStack<T, Container>::operator=(const MutantStack& other)
{
std::cout << "Copy assignment MutantStack operator called" << std::endl;
if (this != &other)
{
std::stack<T, Container>::operator=(other);
}
return (*this);
}
// Début de l'itérateur
template<typename T, typename Container>
typename MutantStack<T, Container>::iterator MutantStack<T, Container>::begin()
{
return (this->c.begin());
}
// Fin de l'itérateur
template<typename T, typename Container>
typename MutantStack<T, Container>::iterator MutantStack<T, Container>::end()
{
return (this->c.end());
}
// Début de l'itérateur constant
template<typename T, typename Container>
typename MutantStack<T, Container>::const_iterator MutantStack<T, Container>::begin() const
{
return (this->c.begin());
}
// Fin de l'itérateur constant
template<typename T, typename Container>
typename MutantStack<T, Container>::const_iterator MutantStack<T, Container>::end() const
{
return (this->c.end());
}

39
cpp08/ex02/main.cpp Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 18:29:10 by fgras-ca #+# #+# */
/* Updated: 2024/03/03 18:30:06 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "MutantStack.hpp"
int main()
{
MutantStack<int> mstack;
mstack.push(5);
mstack.push(17);
std::cout << mstack.top() << std::endl;
mstack.pop();
std::cout << mstack.size() << std::endl;
mstack.push(3);
mstack.push(5);
mstack.push(737);
//[...]
mstack.push(0);
MutantStack<int>::iterator it = mstack.begin();
MutantStack<int>::iterator ite = mstack.end();
++it;
--it;
while (it != ite)
{
std::cout << *it << std::endl;
++it;
}
std::stack<int> s(mstack);
return (0);
}