Skip to content

Commit b7ababa

Browse files
committed
cpp08 ex02 done
1 parent 95c35f8 commit b7ababa

4 files changed

Lines changed: 165 additions & 0 deletions

File tree

cpp08/ex02/Makefile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
NAME = ex02
2+
WFLAGS = -Wall -Werror -Wextra
3+
CPPFLAGS = -std=c++98 -O0 -I ./include/
4+
DEBUG_FLAGS =
5+
6+
SOURCES = $(wildcard ./src/*.cpp)
7+
SOURCES := $(patsubst ./src/%,%, $(SOURCES))
8+
OBJECTS = $(addprefix ./.objs/,$(SOURCES:.cpp=.o))
9+
10+
HEADERS = $(wildcard ./include/*.hpp)
11+
12+
ifdef REMOVE_W_FLAGS
13+
WFLAGS =
14+
endif
15+
16+
ifdef DEBUG
17+
DEBUG_FLAGS := -g3
18+
endif
19+
20+
ifdef REMOVE_STD
21+
CPPFLAGS := $(subst -std=c++98,,$(CPPFLAGS))
22+
endif
23+
24+
CYAN = \033[36m
25+
RESET = \033[0m
26+
27+
all: ./.objs/ $(NAME)
28+
29+
./.objs/:
30+
@mkdir ./.objs/
31+
32+
$(NAME): $(OBJECTS)
33+
@printf "$(CYAN)Compiling...$(RESET)\n"
34+
@c++ $(CPPFLAGS) $(DEBUG_FLAGS) $(WFLAGS) $(OBJECTS) -o $(NAME)
35+
@printf "$(CYAN)Target ./$(NAME) done$(RESET)\n"
36+
37+
./.objs/%.o: ./src/%.cpp $(HEADERS)
38+
@c++ $(CPPFLAGS) $(DEBUG_FLAGS) $(WFLAGS) -c $< -o $@
39+
40+
clean:
41+
@rm -rf ./.objs/
42+
43+
fclean: clean
44+
@rm -rf $(NAME)
45+
46+
re: fclean all

cpp08/ex02/include/Defines.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
5+
#define print(s) std::cout << s << std::endl
6+
7+
#define YELLOW_BOLD "\001\033[1;33m\002"
8+
#define GREEN_BOLD "\001\033[1;32m\002"
9+
#define RED_BOLD "\001\033[1;31m\002"
10+
#define BLUE_BOLD "\001\033[1;94m\002"
11+
#define PURPLE_BOLD "\001\033[1;35m\002"
12+
#define CYAN_BOLD "\001\033[1;36m\002"
13+
#define WHITE_BOLD "\001\033[1;37m\002"
14+
15+
#define YELLOW "\001\033[33m\002"
16+
#define GREEN "\001\033[32m\002"
17+
#define RED "\001\033[31m\002"
18+
#define BLUE "\001\033[94m\002"
19+
#define PURPLE "\001\033[35m\002"
20+
#define CYAN "\001\033[36m\002"
21+
#define WHITE "\001\033[37m\002"
22+
23+
#define RESET "\001\033[0m\002"
24+
#define TITTLE "\n\t\t"
25+
#define SUB_TITTLE "\n\t"

cpp08/ex02/include/MutantStack.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <deque>
2+
#include <stack>
3+
4+
template <typename T, typename C = std::deque<T> >
5+
class MutantStack : public std::stack<T, C>
6+
{
7+
public:
8+
typedef typename std::stack<T, C>::container_type::iterator iterator;
9+
10+
MutantStack<T, C> () : std::stack<T, C> (){};
11+
MutantStack<T, C> (const MutantStack<T, C> &rhs) : std::stack<T, C> (rhs){};
12+
MutantStack<T, C> &
13+
operator= (const MutantStack<T, C> &rhs)
14+
{
15+
if (this != &rhs)
16+
this->c.operator= (rhs);
17+
return (*this);
18+
};
19+
~MutantStack<T, C> (){};
20+
21+
iterator
22+
begin ()
23+
{
24+
return (this->c.begin ());
25+
};
26+
27+
iterator
28+
end ()
29+
{
30+
return (this->c.end ());
31+
};
32+
};

cpp08/ex02/src/main.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "Defines.hpp"
2+
#include "MutantStack.hpp"
3+
4+
#include <list>
5+
#include <vector>
6+
7+
int
8+
main (void)
9+
{
10+
print (TITTLE YELLOW_BOLD "CPP Module 08 - ex02" RESET);
11+
print (SUB_TITTLE YELLOW
12+
"Int stack with default underlying deque container" RESET);
13+
14+
MutantStack<int> mstack;
15+
16+
mstack.push (5);
17+
mstack.push (17);
18+
mstack.push (3);
19+
mstack.push (5);
20+
mstack.push (737);
21+
mstack.push (0);
22+
23+
MutantStack<int>::iterator it, ite;
24+
it = mstack.begin ();
25+
ite = mstack.end ();
26+
for (; it != ite; ++it)
27+
print (*it);
28+
29+
print (SUB_TITTLE YELLOW
30+
"String stack with verctor as underlying container" RESET);
31+
MutantStack<std::string, std::vector<std::string> > mstack2;
32+
33+
mstack2.push ("Hello");
34+
mstack2.push ("World");
35+
mstack2.push ("!");
36+
mstack2.push ("I'm");
37+
mstack2.push ("a");
38+
mstack2.push ("MutantStack");
39+
40+
MutantStack<std::string, std::vector<std::string> >::iterator it2, ite2;
41+
it2 = mstack2.begin ();
42+
ite2 = mstack2.end ();
43+
for (; it2 != ite2; ++it2)
44+
print (*it2);
45+
46+
MutantStack<float, std::list<float> > mstack3;
47+
48+
mstack3.push (5.5);
49+
mstack3.push (17.17);
50+
mstack3.push (3.3);
51+
mstack3.push (5.5);
52+
mstack3.push (737.737);
53+
mstack3.push (0.0);
54+
55+
print (SUB_TITTLE YELLOW
56+
"Float stack with list as underlying container" RESET);
57+
MutantStack<float, std::list<float> >::iterator it3, ite3;
58+
it3 = mstack3.begin ();
59+
ite3 = mstack3.end ();
60+
for (; it3 != ite3; ++it3)
61+
print (*it3);
62+
}

0 commit comments

Comments
 (0)