From ca8a9c7010fdd814577fd8054b2342dcf7f8ec50 Mon Sep 17 00:00:00 2001 From: aortigos Date: Tue, 21 Apr 2026 12:00:29 +0200 Subject: [PATCH] ex02 --- ex02/Makefile | 43 ++++++++++++++++++++++ ex02/MutantStack/MutantStack.hpp | 43 ++++++++++++++++++++++ ex02/MutantStack/MutantStack.tpp | 63 ++++++++++++++++++++++++++++++++ ex02/main.cpp | 40 ++++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 ex02/Makefile create mode 100644 ex02/MutantStack/MutantStack.hpp create mode 100644 ex02/MutantStack/MutantStack.tpp create mode 100644 ex02/main.cpp diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..7d648d7 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,43 @@ +NAME = mutanstack + +SRC = main.cpp + + +OBJ = $(SRC:.cpp=.o) + +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 + +GREEN = \033[0;32m +RED = \033[0;31m +RESET = \033[0m + +TOTAL := $(words $(SRC)) +COUNT = 0 + +all: $(NAME) + +$(NAME): $(OBJ) + @rm -f .build_start + @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) + @printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)" + +%.o: %.cpp + @if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi + @$(eval COUNT = $(shell echo $$(($(COUNT)+1)))) + @PERCENT=$$(($(COUNT)*100/$(TOTAL))); \ + BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \ + SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \ + printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL) + @$(CC) $(CFLAGS) -c $< -o $@ + +clean: + @printf "$(RED)Eliminando...\n$(RESET)" + @rm -f $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex02/MutantStack/MutantStack.hpp b/ex02/MutantStack/MutantStack.hpp new file mode 100644 index 0000000..abfc4df --- /dev/null +++ b/ex02/MutantStack/MutantStack.hpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MutantStack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/04/21 11:32:08 by aortigos #+# #+# */ +/* Updated: 2026/04/21 11:32:08 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MUTANTSTACK_HPP +# define MUTANTSTACK_HPP + +# include +# include + +template +class MutantStack : public std::stack +{ + private: + + public: + MutantStack(); + MutantStack(const MutantStack &other); + MutantStack& operator=(const MutantStack &other); + ~MutantStack(); + + typedef typename std::stack::container_type::iterator iterator; + typedef typename std::stack::container_type::const_iterator const_iterator; + + iterator begin(); + iterator end(); + + const_iterator begin() const; + const_iterator end() const; + +}; + +#include "MutantStack.tpp" + +#endif diff --git a/ex02/MutantStack/MutantStack.tpp b/ex02/MutantStack/MutantStack.tpp new file mode 100644 index 0000000..db9f8c9 --- /dev/null +++ b/ex02/MutantStack/MutantStack.tpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MutantStack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/04/21 11:32:08 by aortigos #+# #+# */ +/* Updated: 2026/04/21 11:32:08 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +template +MutantStack::MutantStack() : std::stack() +{ + // std::cout << "Span default constructor called" << std::endl; +} + +template +MutantStack::MutantStack(const MutantStack &other) +{ + *this = other; +} + +template +MutantStack& MutantStack::operator=(const MutantStack &other) +{ + if (this != &other) + { + std::stack::operator=(other); + } + return (*this); +} + +template +MutantStack::~MutantStack() +{ + // std::cout << "MutantStack destructor called" << std::endl; +} + +template +typename MutantStack::iterator MutantStack::begin() +{ + return (this->c.begin()); +} + +template +typename MutantStack::iterator MutantStack::end() +{ + return (this->c.end()); +} + +template +typename MutantStack::const_iterator MutantStack::begin() const +{ + return (this->c.begin()); +} + +template +typename MutantStack::const_iterator MutantStack::end() const +{ + return (this->c.end()); +} \ No newline at end of file diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..a4dfd3b --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + ++it; + --it; + while (it != ite) + { + std::cout << *it << std::endl; + ++it; + } + std::stack s(mstack); + + return (0); +} \ No newline at end of file