mirror of
https://github.com/Ladebeze66/cpp-partie-2.git
synced 2026-11-09 13:47:14 +01:00
ex08
This commit is contained in:
60
cpp08/ex00/Makefile
Normal file
60
cpp08/ex00/Makefile
Normal 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/03 14:27:18 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 = find
|
||||
|
||||
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 "./find" 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
|
||||
39
cpp08/ex00/easyfind.hpp
Normal file
39
cpp08/ex00/easyfind.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* easyfind.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 14:10:45 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 14:58:04 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EASYFIND_HPP
|
||||
#define EASYFIND_HPP
|
||||
|
||||
#include <algorithm> //Pour std::find
|
||||
#include <iterator> //Pour std::distance
|
||||
#include <exception> //Pour std::exception
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#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 easyfind(T& container, int toFind);
|
||||
|
||||
#include "easyfind.tpp"
|
||||
|
||||
#endif
|
||||
30
cpp08/ex00/easyfind.tpp
Normal file
30
cpp08/ex00/easyfind.tpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* easyfind.tpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 14:14:40 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 15:10:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EASYFIND_TPP
|
||||
#define EASYFIND_TPP
|
||||
|
||||
template<typename T>
|
||||
void easyfind(T& container, int toFind)
|
||||
{
|
||||
typename T::iterator it = std::find(container.begin(), container.end(), toFind);
|
||||
if (it == container.end())
|
||||
{
|
||||
std::cout << RED << "Not found." << RESET << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << GREEN << "Found at position: " << RESET << std::distance(container.begin(), it) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
68
cpp08/ex00/main.cpp
Normal file
68
cpp08/ex00/main.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 14:26:05 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 15:08:46 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "easyfind.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
// Exemple avec std::vector<int>
|
||||
std::vector<int> vecInt;
|
||||
vecInt.push_back(1);
|
||||
vecInt.push_back(2);
|
||||
vecInt.push_back(3);
|
||||
vecInt.push_back(4);
|
||||
vecInt.push_back(5);
|
||||
std::cout << MAGENTA << "Testing with std::vector<int>: (1, 2, 3, 4, 5) find 3 and 6" << RESET << std::endl;
|
||||
easyfind(vecInt, 3);
|
||||
easyfind(vecInt, 6);
|
||||
|
||||
// Exemple avec std::list<int>
|
||||
std::list<int> listInt;
|
||||
listInt.push_back(10);
|
||||
listInt.push_back(20);
|
||||
listInt.push_back(30);
|
||||
listInt.push_back(40);
|
||||
listInt.push_back(50);
|
||||
std::cout << MAGENTA << "\nTesting with std::list<int>: (10, 20, 30, 40, 50) find 30 and 60" << RESET << std::endl;
|
||||
easyfind(listInt, 30);
|
||||
easyfind(listInt, 60);
|
||||
|
||||
// Exemple avec std::vector<char>
|
||||
std::vector<char> vecChar;
|
||||
vecChar.push_back('a');
|
||||
vecChar.push_back('b');
|
||||
vecChar.push_back('c');
|
||||
vecChar.push_back('d');
|
||||
vecChar.push_back('e');
|
||||
std::cout << MAGENTA << "\nTesting with std::vector<char>: (a, b, c, d, e) find c and f" << RESET << std::endl;
|
||||
easyfind(vecChar, 'c'); // Note: 'c' est promu en int pour la comparaison
|
||||
easyfind(vecChar, 'f'); // Note: 'f' est promu en int pour la comparaison
|
||||
|
||||
// Partie dynamique avec entrée utilisateur
|
||||
std::vector<int> vecUserInput;
|
||||
std::string line;
|
||||
|
||||
std::cout << MAGENTA << "\nDynamic testing - Enter numbers (type 'end' to finish):" << RESET << std::endl;
|
||||
while (std::getline(std::cin, line) && line != "end")
|
||||
{
|
||||
vecUserInput.push_back(std::atoi(line.c_str()));
|
||||
}
|
||||
|
||||
std::cout << CYAN << "Enter value to find:" << RESET << std::endl;
|
||||
int toFind;
|
||||
std::cin >> toFind;
|
||||
|
||||
// Utilisation de easyfind avec le vecteur rempli dynamiquement
|
||||
easyfind(vecUserInput, toFind);
|
||||
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user