This commit is contained in:
2024-03-02 20:37:01 +01:00
parent c3a7af50b0
commit d36af81adb
13 changed files with 591 additions and 12 deletions

60
cpp07/ex01/Makefile Normal file
View File

@@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/03/02 20:03:32 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
DEF_COLOR = \033[0m
GRAY = \033[0;90m
RED = \033[0;91m
GREEN = \033[0;92m
YELLOW = \033[0;93m
BLUE = \033[0;94m
MAGENTA = \033[0;95m
CYAN = \033[0;96m
WHITE = \033[0;97m
ORANGE = \033[38;5;214m
NAME = iter
SRC = main.cpp \
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
OBJS = ${SRC:.cpp=.o}
all : $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./iter" to execute the program!!$(DEF_COLOR)"
$(LOGO)
clean :
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
$(RM) $(OBJS)
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
$(LOGO)
fclean : clean
@echo "$(RED)Delete program name... $(DEF_COLOR)"
$(RM) $(NAME)
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
$(LOGO)
re : fclean all
.PHONY : all clean fclean re

37
cpp07/ex01/iter.hpp Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iter.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/02 19:21:08 by fgras-ca #+# #+# */
/* Updated: 2024/03/02 19:56:14 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ITER_HPP
#define ITER_HPP
#include <iostream>
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
template<typename T>
void iter(T* array, size_t length, void (*func)(T& element))
{
for (size_t i = 0; i < length; ++i)
{
func(array[i]);
}
}
#endif

100
cpp07/ex01/main.cpp Normal file
View File

@@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/02 19:26:15 by fgras-ca #+# #+# */
/* Updated: 2024/03/02 20:03:13 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "iter.hpp"
#include <iostream>
#include <cctype> // Pour std::toupper
#include <cstring> // Pour la fonction std::strlen et std::strcpy
// Fonction pour doubler les valeurs
void doubleValue(int& value)
{
value *= 2;
}
// Fonction pour convertir une chaîne en majuscules
void toUpperCase(std::string& str)
{
for (size_t i = 0; i < str.size(); ++i)
{
str[i] = std::toupper(str[i]);
}
}
// Un objet personnalisé pour démontrer l'utilisation avec iter
class Person
{
public:
std::string name;
int age;
// Constructeur par défaut
Person() : name(""), age(0) {}
// Constructeur paramétrique
Person(std::string n, int a) : name(n), age(a) {}
// Destructeur
~Person() {}
// Constructeur de copie
Person(const Person& other) : name(other.name), age(other.age) {}
// Opérateur d'affectation
Person& operator=(const Person& other)
{
if (this != &other) // Protection contre l'auto-affectation
{
name = other.name;
age = other.age;
}
return (*this);
}
};
// Fonction pour afficher les attributs d'un objet Person
void displayPerson(Person& person)
{
std::cout << person.name << ", " << person.age << " years old" << std::endl;
}
int main()
{
int nums[] = {1, 2, 3, 4, 5};
std::string strings[] = {"hello", "world", "iter", "function", "test"};
Person people[3] =
{
Person("Alice", 30),
Person("Bob", 25),
Person("Charlie", 35)
};
std::cout << YELLOW << "Original nums: " << RESET;
for (int i = 0; i < 5; ++i) std::cout << nums[i] << " ";
std::cout << std::endl;
iter(nums, 5, doubleValue);
std::cout << YELLOW << "\nDoubled nums: " << RESET;
for (int i = 0; i < 5; ++i) std::cout << nums[i] << " ";
std::cout << std::endl;
iter(strings, 5, toUpperCase);
std::cout << MAGENTA << "\nStrings to upper case:" << RESET << std::endl;
for (int i = 0; i < 5; ++i) std::cout << strings[i] << std::endl;
std::cout << RED << "\nDisplay people:" << RESET << std::endl;
iter(people, 3, displayPerson);
return (0);
}