diff --git a/d08/ex00/Makefile b/d08/ex00/Makefile new file mode 100644 index 0000000..09b79b3 --- /dev/null +++ b/d08/ex00/Makefile @@ -0,0 +1,76 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = easyfind + +#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 = colors.h \ + easyfind.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) + +clean: + $(RM_OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d08/ex00/colors.h b/d08/ex00/colors.h new file mode 100644 index 0000000..0374e42 --- /dev/null +++ b/d08/ex00/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/ex00/easyfind.hpp b/d08/ex00/easyfind.hpp new file mode 100644 index 0000000..9496691 --- /dev/null +++ b/d08/ex00/easyfind.hpp @@ -0,0 +1,24 @@ +#ifndef EASYFIND_HPP +# define EASYFIND_HPP + +# include "colors.h" +# include + +class easyfindException : public std::exception { + virtual char const *what(void) const throw() { + return "not found"; + } +}; + +template < typename T > +typename T::const_iterator easyfind(T const & container, int nb) { + typename T::const_iterator it; + + it = std::find(container.begin(), container.end(), nb); + if (it == container.end()) + throw easyfindException(); + return it; +} + +#endif + diff --git a/d08/ex00/main.cpp b/d08/ex00/main.cpp new file mode 100644 index 0000000..1b0a966 --- /dev/null +++ b/d08/ex00/main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include "colors.h" + +#include "easyfind.hpp" +#include +#include +#include + +#define N_TEST "3" + +template < typename T > +void standardTest(T container) { + typename T::const_iterator it; + int test; + + for (int i = -9 ; i < 10 ; i++) + container.push_back(i); + for (it = container.begin(); it != container.end(); it++) + std::cout << *it << ": " << &*it << "\n"; + std::cout << "\n"; + test = -3; + try { + it = easyfind(container, test); + std::cout << *it << ": " << &*it << "\n"; + } + catch (std::exception const & e) { + std::cout << test << ": " << e.what() << "\n"; + } +} + +int main() { + int i = 0; + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests list :" RESET "\n"; + { + std::list container; + standardTest(container); + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests vector :" RESET "\n"; + { + std::vector container; + standardTest(container); + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests deque :" RESET "\n"; + { + std::deque container; + standardTest(container); + } + + return 0; +} +