d07 ex01 ok

This commit is contained in:
hugogogo
2022-03-10 19:12:00 +01:00
parent 3433fafd87
commit a38d7184e3
7 changed files with 198 additions and 0 deletions

79
d07/ex01/Makefile Normal file
View File

@@ -0,0 +1,79 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value \ . += append to a variable #
# VARIABLES . value . != set result of command #
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = iter
#TYPE = c
TYPE = cpp
ifeq "$(TYPE)" "c"
CC = c
EXT = c
else ifeq "$(TYPE)" "cpp"
CC = c++
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 \
Iter.hpp \
Print.hpp \
ToUpper.hpp \
Decrement.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

View File

@@ -0,0 +1,13 @@
#ifndef DECREMENT_HPP
# define DECREMENT_HPP
# include <iostream>
# include <string>
template< typename T >
void Decrement(T & e) {
--e;;
}
#endif

11
d07/ex01/headers/Iter.hpp Normal file
View File

@@ -0,0 +1,11 @@
#ifndef ITER_HPP
# define ITER_HPP
template< typename T, typename F >
void Iter(T *arr, size_t len, F & f)
{
for (size_t i = 0; i < len; i++)
f(arr[i]);
}
#endif

View File

@@ -0,0 +1,13 @@
#ifndef PRINT_HPP
# define PRINT_HPP
# include <iostream>
# include <string>
template< typename T >
void Print(T const & e) {
std::cout << "[" << e << "]\n";
}
#endif

View File

@@ -0,0 +1,14 @@
#ifndef TOUPPER_HPP
# define TOUPPER_HPP
# include <iostream>
# include <string>
# include <cctype>
template< typename T >
void ToUpper(T & e) {
e = toupper(e);
}
#endif

25
d07/ex01/headers/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

43
d07/ex01/main.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <iostream>
#include <string>
#include "colors.h"
#include "Iter.hpp"
#include "Print.hpp"
#include "ToUpper.hpp"
#include "Decrement.hpp"
#define N_TEST "2"
int main() {
int i = 0;
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests char :" RESET "\n";
{
char arr[] = "abracadabra";
size_t len = sizeof(arr) / sizeof(arr[0]);
::Iter(arr, len, Print<char>);
std::cout << B_BLUE "ToUpper :" RESET "\n";
::Iter(arr, len, ToUpper<char>);
::Iter(arr, len, Print<char>);
std::cout << B_BLUE "Decrement :" RESET "\n";
::Iter(arr, len, Decrement<char>);
::Iter(arr, len, Print<char>);
}
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests int :" RESET "\n";
{
int arr[] = {1256, 23, 4352, -3287, 0, 24, 4376382};
size_t len = sizeof(arr) / sizeof(arr[0]);
::Iter(arr, len, Print<int>);
std::cout << B_BLUE "Decrement :" RESET "\n";
::Iter(arr, len, Decrement<int>);
::Iter(arr, len, Print<int>);
}
return 0;
}