diff --git a/d08/ex02/Makefile b/d08/ex02/Makefile new file mode 100644 index 0000000..1715a65 --- /dev/null +++ b/d08/ex02/Makefile @@ -0,0 +1,80 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = mutantstack + +#TYPE = c +TYPE = cpp + +ifeq "$(TYPE)" "c" + CC = c + EXT = c +else ifeq "$(TYPE)" "cpp" + CC = clang++ + EXT = cpp +endif + +CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +ifeq "$(TYPE)" "cpp" + CFLAGS += -std=c++98 +endif + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = . +SRCS = main.cpp + +D_HEADERS = . +HEADERS = +#HEADERS = colors.h \ +# MutantStack.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) + +ifeq "$(D_OBJS)" "." + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) + $(CC) $(CFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) + $(CC) $(OBJS) -o $@ $(LIBS) + +leaks: $(NAME) + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) + +clean: + $(RM_OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d08/ex02/MutantStack.hpp b/d08/ex02/MutantStack.hpp new file mode 100644 index 0000000..e0c2eaf --- /dev/null +++ b/d08/ex02/MutantStack.hpp @@ -0,0 +1,29 @@ +#ifndef MUTANTSTACK_HPP +# define MUTANTSTACK_HPP + +#include + +template +class MutantStack : public std::stack { + +public: + typedef T * iterator; + typedef T const * const_iterator; + + iterator begin() {return (&this->top() - (this->size() - 1));} + iterator end() {return (&this->top()+ 1);} + const_iterator begin() const {return (&this->top() - (this->size() - 1));} + const_iterator end() const {return (&this->top()+ 1);} + +// typedef typename std::stack::container_type::iterator iterator; +// typedef typename std::stack::container_type::const_iterator const_iterator; + +// iterator begin() {return this->c.begin();} +// iterator end() {return this->c.end();} +// const_iterator begin() const {return this->c.begin();} +// const_iterator end() const {return this->c.end();} + +}; + +#endif + diff --git a/d08/ex02/colors.h b/d08/ex02/colors.h new file mode 100644 index 0000000..0374e42 --- /dev/null +++ b/d08/ex02/colors.h @@ -0,0 +1,25 @@ +#ifndef COLORS_H +# define COLORS_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/d08/ex02/main.cpp b/d08/ex02/main.cpp new file mode 100644 index 0000000..8caf72f --- /dev/null +++ b/d08/ex02/main.cpp @@ -0,0 +1,62 @@ +#include +#include "colors.h" +#include "MutantStack.hpp" + +#define N_TEST "1" + +int main() { + int i = 0; + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "test simple iterator :" RESET "\n"; + { + MutantStack mstack; + + mstack.push(7); + mstack.push(987); + mstack.push(9); + mstack.push(8); + mstack.push(34); + mstack.push(1); + + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + + for (; it != ite; it++) + std::cout << *it << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests subject :" RESET "\n"; + { + MutantStack 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; +} + diff --git a/d08/ex02/mutantstack b/d08/ex02/mutantstack new file mode 100755 index 0000000..80bd292 Binary files /dev/null and b/d08/ex02/mutantstack differ