mirror of
https://github.com/Ladebeze66/cpp-partie-2.git
synced 2025-12-16 05:57:57 +01:00
ex07
This commit is contained in:
parent
c3a7af50b0
commit
d36af81adb
50
.vscode/settings.json
vendored
50
.vscode/settings.json
vendored
@ -1,5 +1,53 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/11 14:39:51 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/11 12:57:28 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 <iostream>
|
#include <iostream>
|
||||||
#include "AForm.hpp"
|
#include "AForm.hpp"
|
||||||
|
|
||||||
#define RESET "\033[0m"
|
#define RESET "\033[0m"
|
||||||
#define BLACK "\033[30m"
|
#define BLACK "\033[30m"
|
||||||
#define RED "\033[31m"
|
#define RED "\033[31m"
|
||||||
#define GREEN "\033[32m"
|
#define GREEN "\033[32m"
|
||||||
#define YELLOW "\033[33m"
|
#define YELLOW "\033[33m"
|
||||||
#define BLUE "\033[34m"
|
#define BLUE "\033[34m"
|
||||||
#define MAGENTA "\033[35m"
|
#define MAGENTA "\033[35m"
|
||||||
#define CYAN "\033[36m"
|
#define CYAN "\033[36m"
|
||||||
#define WHITE "\033[37m"
|
#define WHITE "\033[37m"
|
||||||
|
|
||||||
class AForm;
|
class AForm;
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
60
cpp07/ex00/Makefile
Normal file
60
cpp07/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/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
|
||||||
33
cpp07/ex00/main.cpp
Normal file
33
cpp07/ex00/main.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/02 19:06:16 by fgras-ca #+# #+# */
|
||||||
|
/* Updated: 2024/03/02 19:13:43 by fgras-ca ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
46
cpp07/ex00/whatever.hpp
Normal file
46
cpp07/ex00/whatever.hpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* whatever.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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<typename T>
|
||||||
|
void swap(T& a, T& b)
|
||||||
|
{
|
||||||
|
T temp = a;
|
||||||
|
a = b;
|
||||||
|
b = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T const &min(T const &a, T const &b)
|
||||||
|
{
|
||||||
|
return (a < b) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T const &max(T const &a, T const &b)
|
||||||
|
{
|
||||||
|
return (a > b) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
60
cpp07/ex01/Makefile
Normal file
60
cpp07/ex01/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/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
37
cpp07/ex01/iter.hpp
Normal 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
100
cpp07/ex01/main.cpp
Normal 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);
|
||||||
|
}
|
||||||
92
cpp07/ex02/Array.hpp
Normal file
92
cpp07/ex02/Array.hpp
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Array.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <cstddef> // Pour size_t
|
||||||
|
#include <stdexcept> // Pour std::out_of_range
|
||||||
|
#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>
|
||||||
|
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<void*>(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
|
||||||
60
cpp07/ex02/Makefile
Normal file
60
cpp07/ex02/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/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
|
||||||
43
cpp07/ex02/main.cpp
Normal file
43
cpp07/ex02/main.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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<int> intArray(5);
|
||||||
|
for (size_t i = 0; i < intArray.size(); ++i)
|
||||||
|
{
|
||||||
|
intArray[i] = static_cast<int>(i * 2);
|
||||||
|
std::cout << BLUE << "intArray[" << i << "] = " << RESET << intArray[i] << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test de copie
|
||||||
|
Array<int> 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);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user