d08 ex02 fini, piscine finie :p

This commit is contained in:
hugogogo
2022-03-14 12:53:25 +01:00
parent 2f90a1ee13
commit 2e68c5daef
5 changed files with 196 additions and 0 deletions

80
d08/ex02/Makefile Normal file
View File

@@ -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

29
d08/ex02/MutantStack.hpp Normal file
View File

@@ -0,0 +1,29 @@
#ifndef MUTANTSTACK_HPP
# define MUTANTSTACK_HPP
#include <stack>
template <typename T>
class MutantStack : public std::stack<T> {
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<T>::container_type::iterator iterator;
// typedef typename std::stack<T>::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

25
d08/ex02/colors.h Normal file
View File

@@ -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

62
d08/ex02/main.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include <iostream>
#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<int> mstack;
mstack.push(7);
mstack.push(987);
mstack.push(9);
mstack.push(8);
mstack.push(34);
mstack.push(1);
MutantStack<int>::iterator it = mstack.begin();
MutantStack<int>::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<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;
}

BIN
d08/ex02/mutantstack Executable file

Binary file not shown.