d08 ex00 ok

This commit is contained in:
hugogogo
2022-03-13 18:33:48 +01:00
parent 96e78e34a1
commit a3be2c8f16
4 changed files with 183 additions and 0 deletions

76
d08/ex00/Makefile Normal file
View File

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

25
d08/ex00/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

24
d08/ex00/easyfind.hpp Normal file
View File

@@ -0,0 +1,24 @@
#ifndef EASYFIND_HPP
# define EASYFIND_HPP
# include "colors.h"
# include <algorithm>
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

58
d08/ex00/main.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include <iostream>
#include <string>
#include "colors.h"
#include "easyfind.hpp"
#include <list>
#include <vector>
#include <deque>
#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<int> container;
standardTest(container);
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests vector :" RESET "\n";
{
std::vector<int> container;
standardTest(container);
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests deque :" RESET "\n";
{
std::deque<int> container;
standardTest(container);
}
return 0;
}