diff --git a/.vscode/settings.json b/.vscode/settings.json index cb996a9..32d6a19 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,53 @@ { "files.associations": { - "iostream": "cpp" + "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" } } \ No newline at end of file diff --git a/cpp05/ex02/AForm.cpp b/cpp05/ex02/AForm.cpp index 0b9a6b3..8575f4b 100644 --- a/cpp05/ex02/AForm.cpp +++ b/cpp05/ex02/AForm.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ -/* Updated: 2024/02/22 14:36:36 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/02 20:36:08 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp05/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp index 794108d..522d103 100644 --- a/cpp05/ex02/Bureaucrat.hpp +++ b/cpp05/ex02/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ -/* Updated: 2024/02/22 15:29:48 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/02 19:04:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,15 @@ #include #include "AForm.hpp" -#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" +#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 AForm; diff --git a/cpp06/ex01/convert b/cpp06/ex01/convert deleted file mode 100755 index 9d03221..0000000 Binary files a/cpp06/ex01/convert and /dev/null differ diff --git a/cpp07/ex00/Makefile b/cpp07/ex00/Makefile new file mode 100644 index 0000000..208c3bc --- /dev/null +++ b/cpp07/ex00/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/02 19:20:09 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 = whatever + +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 "./whatever" 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/cpp07/ex00/main.cpp b/cpp07/ex00/main.cpp new file mode 100644 index 0000000..5d3821d --- /dev/null +++ b/cpp07/ex00/main.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 19:06:16 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 19:13:43 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "whatever.hpp" + +int main() +{ + int a = 2; + int b = 3; + ::swap(a, b); + std::cout << RED << "a = " << RESET << a << RED << ", b = " << RESET << b << std::endl; + std::cout << GREEN << "min( a, b ) = " << RESET << ::min(a, b) << std::endl; + std::cout << MAGENTA << "max( a, b ) = " << RESET << ::max(a, b) << std::endl; + + std::string c = "chaine1"; + std::string d = "chaine2"; + ::swap(c, d); + std::cout << RED << "c = " << RESET << c << RED << ", d = " << RESET << d << std::endl; + std::cout << GREEN << "min( c, d ) = " << RESET << ::min(c, d) << std::endl; + std::cout << MAGENTA << "max( c, d ) = " << RESET << ::max(c, d) << std::endl; + + return (0); +} \ No newline at end of file diff --git a/cpp07/ex00/whatever.hpp b/cpp07/ex00/whatever.hpp new file mode 100644 index 0000000..c9a346a --- /dev/null +++ b/cpp07/ex00/whatever.hpp @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* whatever.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 18:56:24 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 19:05:10 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef WHATEVER_HPP +#define WHATEVER_HPP + +#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 swap(T& a, T& b) +{ + T temp = a; + a = b; + b = temp; +} + +template +T const &min(T const &a, T const &b) +{ + return (a < b) ? a : b; +} + +template +T const &max(T const &a, T const &b) +{ + return (a > b) ? a : b; +} + +#endif \ No newline at end of file diff --git a/cpp07/ex01/Makefile b/cpp07/ex01/Makefile new file mode 100644 index 0000000..8c5a96d --- /dev/null +++ b/cpp07/ex01/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 \ No newline at end of file diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp new file mode 100644 index 0000000..c4edbac --- /dev/null +++ b/cpp07/ex01/iter.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +#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 iter(T* array, size_t length, void (*func)(T& element)) +{ + for (size_t i = 0; i < length; ++i) + { + func(array[i]); + } +} + +#endif \ No newline at end of file diff --git a/cpp07/ex01/main.cpp b/cpp07/ex01/main.cpp new file mode 100644 index 0000000..bcbc73b --- /dev/null +++ b/cpp07/ex01/main.cpp @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include // Pour std::toupper +#include // 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); +} diff --git a/cpp07/ex02/Array.hpp b/cpp07/ex02/Array.hpp new file mode 100644 index 0000000..58bfba8 --- /dev/null +++ b/cpp07/ex02/Array.hpp @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Array.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 20:05:14 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 20:31:33 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARRAY_HPP +#define ARRAY_HPP + +#include // Pour size_t +#include // Pour std::out_of_range +#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 +class Array +{ +private: + T* elements; + size_t n; + +public: + // Constructeur par défaut + Array() : elements(new T[0]), n(0) + { + std::cout << GREEN << "Array default constructor called!" << RESET << std::endl; + } + + // Constructeur avec taille + Array(unsigned int n) : elements(new T[n]), n(n) + { + std::cout << GREEN << "Array with size constructor called!" << RESET << std::endl; + } + + // Constructeur de copie + Array(const Array& other) : elements(new T[other.n]), n(other.n) + { + for (size_t i = 0; i < n; ++i) + { + elements[i] = other.elements[i]; + } + } + // Destructeur + ~Array() + { + delete[] elements; + std::cout << RED << "Array " << static_cast(this->elements) << " is destroyed!" << RESET << std::endl; + } + // Opérateur d'assignation + Array& operator=(const Array& other) + { + if (this != &other) + { + delete[] elements; + n = other.n; + elements = new T[n]; + for (size_t i = 0; i < n; ++i) + { + elements[i] = other.elements[i]; + } + } + return (*this); + } + // Accès aux éléments + T& operator[](size_t index) + { + if (index >= n) throw std::out_of_range("Index out of bounds"); + return (elements[index]); + } + // Taille du tableau + size_t size() const + { + return (n); + } +}; + +#endif diff --git a/cpp07/ex02/Makefile b/cpp07/ex02/Makefile new file mode 100644 index 0000000..1236580 --- /dev/null +++ b/cpp07/ex02/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/02 20:05:50 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 = array + +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 "./array" 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/cpp07/ex02/main.cpp b/cpp07/ex02/main.cpp new file mode 100644 index 0000000..bfe49a2 --- /dev/null +++ b/cpp07/ex02/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/02 20:15:43 by fgras-ca #+# #+# */ +/* Updated: 2024/03/02 20:34:26 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Array.hpp" + +int main() +{ + Array intArray(5); + for (size_t i = 0; i < intArray.size(); ++i) + { + intArray[i] = static_cast(i * 2); + std::cout << BLUE << "intArray[" << i << "] = " << RESET << intArray[i] << std::endl; + } + + // Test de copie + Array copiedArray = intArray; + std::cout << GREEN << "copiedArray size: " << RESET << copiedArray.size() << std::endl; + + // Modifier l'original ne devrait pas affecter la copie + intArray[0] = 100; + std::cout << YELLOW << "\nAfter modification: " << RESET << std::endl; + std::cout << "intArray[0] = " << intArray[0] << ", copiedArray[0] = " << copiedArray[0] << std::endl; + + // Test d'accès hors limites + try + { + intArray[intArray.size()] = 10; // Devrait lancer une exception + } + catch (const std::out_of_range& e) + { + std::cout << RED << "\nException caught: " << RESET << e.what() << std::endl; + } + return (0); +}