diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ac92a30 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/CPP/cpp2/cpp05/ex02", + "program": "/home/fgras-ca/Bureau/CPP/cpp2/cpp05/ex02/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 32d6a19..6499df3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,53 +1,112 @@ { - "files.associations": { - "iostream": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "typeinfo": "cpp", - "cstring": "cpp" - } + "files.associations": { + "iostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "cstring": "cpp", + "*.tpp": "c", + "list": "cpp" +}, + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false } \ No newline at end of file diff --git a/cpp08/ex00/Makefile b/cpp08/ex00/Makefile new file mode 100644 index 0000000..e92c2d6 --- /dev/null +++ b/cpp08/ex00/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 \ No newline at end of file diff --git a/cpp08/ex00/easyfind.hpp b/cpp08/ex00/easyfind.hpp new file mode 100644 index 0000000..d3452af --- /dev/null +++ b/cpp08/ex00/easyfind.hpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* easyfind.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 //Pour std::find +#include //Pour std::distance +#include //Pour std::exception +#include +#include +#include +#include + +#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 +void easyfind(T& container, int toFind); + +#include "easyfind.tpp" + +#endif \ No newline at end of file diff --git a/cpp08/ex00/easyfind.tpp b/cpp08/ex00/easyfind.tpp new file mode 100644 index 0000000..96dcf22 --- /dev/null +++ b/cpp08/ex00/easyfind.tpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* easyfind.tpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +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 \ No newline at end of file diff --git a/cpp08/ex00/main.cpp b/cpp08/ex00/main.cpp new file mode 100644 index 0000000..c59f9cc --- /dev/null +++ b/cpp08/ex00/main.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + std::vector 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: (1, 2, 3, 4, 5) find 3 and 6" << RESET << std::endl; + easyfind(vecInt, 3); + easyfind(vecInt, 6); + + // Exemple avec std::list + std::list 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: (10, 20, 30, 40, 50) find 30 and 60" << RESET << std::endl; + easyfind(listInt, 30); + easyfind(listInt, 60); + + // Exemple avec std::vector + std::vector 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: (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 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); +} diff --git a/cpp08/ex01/Makefile b/cpp08/ex01/Makefile new file mode 100644 index 0000000..14e71ba --- /dev/null +++ b/cpp08/ex01/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/03 15:48:48 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 = span + +SRC = main.cpp \ + Span.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 "./span" 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 \ No newline at end of file diff --git a/cpp08/ex01/Span.cpp b/cpp08/ex01/Span.cpp new file mode 100644 index 0000000..c608147 --- /dev/null +++ b/cpp08/ex01/Span.cpp @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/03 16:20:25 by fgras-ca #+# #+# */ +/* Updated: 2024/03/03 18:28:26 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" + +Span::Span(unsigned int N): capacity(N) +{ + std::cout << GREEN << "Span constructor called!" << RESET << std::endl; +} + +Span::Span(const Span& other): numbers(other.numbers), capacity(other.capacity) +{ + std::cout << CYAN << "Copy constructor called for Span." << RESET << std::endl; +} + +Span& Span::operator=(const Span& other) +{ + std::cout << YELLOW << "Copy assignment Span operator called" << RESET << std::endl; + if (this != &other) + { + numbers = other.numbers; + capacity = other.capacity; + } + return (*this); +} + +Span::~Span() +{ + std::cout << RED << "Span is destroyed!" << RESET << std::endl; +} + +void Span::addNumber(int number) +{ + if (numbers.size() >= capacity) + throw std::length_error("Span is full."); + numbers.push_back(number); +} + +int Span::shortestSpan() +{ + if (numbers.size() < 2) + throw std::logic_error("Not enough elements for span."); + std::sort(numbers.begin(), numbers.end()); + int minSpan = std::numeric_limits::max(); + for (size_t i = 1; i < numbers.size(); ++i) + { + int span = numbers[i] - numbers[i - 1]; + if (span < minSpan) + minSpan = span; + } + return (minSpan); +} + +int Span::longestSpan() +{ + if (numbers.size() < 2) + throw std::logic_error("Not enough elements for span."); + + // Utilisation de std::min_element et std::max_element séparément + std::vector::iterator minIt = std::min_element(numbers.begin(), numbers.end()); + std::vector::iterator maxIt = std::max_element(numbers.begin(), numbers.end()); + + // Calcul de la span la plus longue + return (*maxIt - *minIt); +} + +template +void Span::addRange(Iterator begin, Iterator end) +{ + while (begin != end) + { + addNumber(*begin); + ++begin; + } +} \ No newline at end of file diff --git a/cpp08/ex01/Span.hpp b/cpp08/ex01/Span.hpp new file mode 100644 index 0000000..7925e5d --- /dev/null +++ b/cpp08/ex01/Span.hpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/03 15:53:40 by fgras-ca #+# #+# */ +/* Updated: 2024/03/03 16:49:04 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SPAN_HPP +#define SPAN_HPP + +#include +#include +#include +#include +#include + +#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" + +class Span +{ +public: + Span(unsigned int N); //constructor + Span(const Span& other); //copy constructor + Span& operator=(const Span& other); //operateur d'assignation + ~Span(); //destructor + + void addNumber(int number); + int shortestSpan(); + int longestSpan(); + template + void addRange(Iterator begin, Iterator end); + +private: + std::vector numbers; + unsigned int capacity; +}; + +#endif \ No newline at end of file diff --git a/cpp08/ex01/main.cpp b/cpp08/ex01/main.cpp new file mode 100644 index 0000000..8bfe8ef --- /dev/null +++ b/cpp08/ex01/main.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/03 17:00:49 by fgras-ca #+# #+# */ +/* Updated: 2024/03/03 17:59:58 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" +#include // Pour std::rand et std::srand +#include // Pour std::time +#include + +int main() +{ + // Test initial + Span sp = Span(5); + sp.addNumber(6); + sp.addNumber(3); + sp.addNumber(17); + sp.addNumber(9); + sp.addNumber(11); + std::cout << sp.shortestSpan() << std::endl; + std::cout << sp.longestSpan() << std::endl; + + // Ajout de tests pour les exceptions + try + { + Span emptySpan(10); + emptySpan.shortestSpan(); + } + catch (std::exception& e) + { + std::cout << RED << "\nException caught for shortestSpan with less than 2 elements: " << RESET << e.what() << std::endl; + } + + try + { + Span singleElementSpan(10); + singleElementSpan.addNumber(1); + singleElementSpan.longestSpan(); + } + catch (std::exception& e) + { + std::cout << RED << "Exception caught for longestSpan with less than 2 elements: " << RESET << e.what() << std::endl; + } + + try + { + Span fullSpan(1); + fullSpan.addNumber(1); + fullSpan.addNumber(2); // Devrait lancer une exception + } + catch (std::exception& e) + { + std::cout << RED << "Exception caught for adding to a full Span: " << RESET << e.what() << std::endl; + } + + // Interaction en ligne de commande + unsigned int N; + std::cout << YELLOW << "\nEnter the number of elements to generate: " << RESET; + std::cin >> N; + + Span userSpan(N); + std::set uniqueNumbers; + while(uniqueNumbers.size() < N) + { + int number = std::rand() % 100000; // Générer un nombre aléatoire entre 0 et 999 + if(uniqueNumbers.insert(number).second) + { // Vérifie si l'insertion est réussie (élément n'était pas déjà présent) + userSpan.addNumber(number); + std::cout << BLUE << "Generated number: " << RESET << number << std::endl; + } + } + + if (N > 1) + { + std::cout << CYAN << "Shortest span (userSpan): " << RESET << userSpan.shortestSpan() << std::endl; + std::cout << CYAN << "Longest span (userSpan): " << RESET << userSpan.longestSpan() << std::endl; + } + else + { + std::cout << RED << "Not enough elements to calculate spans." << RESET << std::endl; + } + + return (0); +} diff --git a/cpp08/ex02/Makefile b/cpp08/ex02/Makefile new file mode 100644 index 0000000..20068c9 --- /dev/null +++ b/cpp08/ex02/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/03 18:13:54 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 = mutant + +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 "./mutant" 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 \ No newline at end of file diff --git a/cpp08/ex02/MutantStack.hpp b/cpp08/ex02/MutantStack.hpp new file mode 100644 index 0000000..b1d63a2 --- /dev/null +++ b/cpp08/ex02/MutantStack.hpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MutantStack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/03 18:22:10 by fgras-ca #+# #+# */ +/* Updated: 2024/03/03 18:35:45 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MUTANTSTACK_HPP +#define MUTANTSTACK_HPP + +#include +#include +#include + +template > +class MutantStack : public std::stack +{ +public: + MutantStack(); // Constructeur par défaut + MutantStack(const MutantStack& other); // Constructeur de copie + ~MutantStack(); // Destructeur + MutantStack& operator=(const MutantStack& other); // Opérateur d'assignation + + typedef typename Container::iterator iterator; + typedef typename Container::const_iterator const_iterator; + + iterator begin(); + iterator end(); + const_iterator begin() const; + const_iterator end() const; + +private: + using std::stack::c; // Utilisez 'c' pour accéder au conteneur sous-jacent +}; + +#include "MutantStack.tpp" +#endif diff --git a/cpp08/ex02/MutantStack.tpp b/cpp08/ex02/MutantStack.tpp new file mode 100644 index 0000000..6a66714 --- /dev/null +++ b/cpp08/ex02/MutantStack.tpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MutantStack.tpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/03 18:23:49 by fgras-ca #+# #+# */ +/* Updated: 2024/03/03 18:37:30 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// Constructeur par défaut +template +MutantStack::MutantStack() : std::stack() +{ + std::cout << "MutantStack constructor called!" << std::endl; +} +// Constructeur de copie +template +MutantStack::MutantStack(const MutantStack& other) : std::stack(other) +{ + std::cout << "Copy constructor called for MutantStack." << std::endl; +} +// Destructeur +template +MutantStack::~MutantStack() +{ + std::cout << "MutantStack is destroyed!" << std::endl; +} +// Opérateur d'assignation +template +MutantStack& MutantStack::operator=(const MutantStack& other) +{ + std::cout << "Copy assignment MutantStack operator called" << std::endl; + if (this != &other) + { + std::stack::operator=(other); + } + return (*this); +} +// Début de l'itérateur +template +typename MutantStack::iterator MutantStack::begin() +{ + return (this->c.begin()); +} +// Fin de l'itérateur +template +typename MutantStack::iterator MutantStack::end() +{ + return (this->c.end()); +} +// Début de l'itérateur constant +template +typename MutantStack::const_iterator MutantStack::begin() const +{ + return (this->c.begin()); +} +// Fin de l'itérateur constant +template +typename MutantStack::const_iterator MutantStack::end() const +{ + return (this->c.end()); +} diff --git a/cpp08/ex02/main.cpp b/cpp08/ex02/main.cpp new file mode 100644 index 0000000..82fbde0 --- /dev/null +++ b/cpp08/ex02/main.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/03 18:29:10 by fgras-ca #+# #+# */ +/* Updated: 2024/03/03 18:30:06 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "MutantStack.hpp" + +int main() +{ + MutantStack mstack; + mstack.push(5); + mstack.push(17); + std::cout << mstack.top() << std::endl; + mstack.pop(); + std::cout << mstack.size() << std::endl; + mstack.push(3); + mstack.push(5); + mstack.push(737); + //[...] + mstack.push(0); + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + ++it; + --it; + while (it != ite) + { + std::cout << *it << std::endl; + ++it; + } + std::stack s(mstack); + return (0); +} \ No newline at end of file