This commit is contained in:
Ladebeze66 2024-03-02 17:39:42 +01:00
parent 12ec1fdecc
commit c3a7af50b0
18 changed files with 575 additions and 39 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"iostream": "cpp"
}
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 12:48:27 by fgras-ca ### ########.fr */
/* Updated: 2024/02/26 15:57:56 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,23 +19,25 @@
class Bureaucrat;
class Form
class AForm
{
private:
const std::string name;
const
bool isSigned;
const int gradeRequiredToSign;
const int gradeRequiredToExecute;
public:
//constructueur
Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
AForm(const::std::string& name);
AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie
Form(const Form& other);
AForm(const AForm& other);
// Opérateur d'affectation
Form& operator=(const Form& other);
AForm& operator=(const AForm& other);
// Destructeur
~Form();
~AForm();
std::string getName() const;
@ -48,17 +50,23 @@ public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const throw();
const char* what() const noexcept override
{
return ("Grade too high");
}
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw();
const char* what() const noexcept override
{
return ("Grade too low");
}
};
};
// Surcharge de l'opérateur d'insertion
std::ostream& operator<<(std::ostream& os, const Form& form);
std::ostream& operator<<(std::ostream& os, const AForm& form);
#endif

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/22 12:41:21 by fgras-ca ### ########.fr #
# Updated: 2024/02/26 14:35:12 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #

View File

@ -6,21 +6,17 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/25 19:33:13 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 20:06:27 by fgras-ca ### ########.fr */
/* Updated: 2024/02/26 16:03:04 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ScalarConverter.hpp"
ScalarConverter::ScalarConverter()
{
std::cout << GREEN << "ScalarConverter default constructor called!" << RESET << std::endl;
}
{}
ScalarConverter::ScalarConverter(const ScalarConverter&)
{
std::cout << CYAN << "Copy constructor called for ScalarConverter." << RESET << std::endl;
}
{}
ScalarConverter& ScalarConverter::operator=(const ScalarConverter&)
{
@ -28,9 +24,7 @@ ScalarConverter& ScalarConverter::operator=(const ScalarConverter&)
}
ScalarConverter::~ScalarConverter()
{
std::cout << RED << "ScalarConverter is destroyed!" << RESET << std::endl;
}
{}
char ScalarConverter::convertToChar(double num)
{
@ -60,6 +54,28 @@ double ScalarConverter::convertToDouble(const std::string& literal)
void ScalarConverter::convert(const std::string& literal)
{
// Traitement spécial si la chaîne représente un caractère unique
if (literal.length() == 1 && !isdigit(literal[0]))
{
char c = literal[0];
printAsChar(c);
// Convertir directement le caractère en numérique pour les autres types
double num = static_cast<double>(c);
try
{
printAsInt(static_cast<int>(c));
}
catch (const std::exception& e)
{
std::cout << "int: " << e.what() << std::endl;
}
printAsFloat(static_cast<float>(num));
printAsDouble(num);
}
else
{
// L'approche originale pour les entrées considérées comme numériques
double num = convertToDouble(literal);
try
{
@ -83,9 +99,9 @@ void ScalarConverter::convert(const std::string& literal)
float f = convertToFloat(num);
printAsFloat(f);
printAsDouble(num);
}
}
void ScalarConverter::printAsChar(char c)
{

24
cpp06/ex01/Data.hpp Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Data.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:59:05 by fgras-ca #+# #+# */
/* Updated: 2024/02/26 16:11:24 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DATA_HPP
# define DATA_HPP
# include <string>
typedef struct s_data
{
std::string dummy_str;
} Data;
#endif

61
cpp06/ex01/Makefile Normal file
View File

@ -0,0 +1,61 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/26 16:28:12 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 = serializer
SRC = Serializer.cpp \
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 "./serializer" 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

42
cpp06/ex01/Serializer.cpp Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Serializer.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:00:38 by fgras-ca #+# #+# */
/* Updated: 2024/02/26 16:04:34 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Serializer.hpp"
// Constructeur privé
Serializer::Serializer()
{}
// Constructeur de copie privé
Serializer::Serializer(const Serializer&)
{
// Généralement vide car la classe ne doit pas être copiée.
}
// Opérateur d'affectation privé
Serializer& Serializer::operator=(const Serializer&)
{ // Généralement vide car l'affectation ne doit pas être effectuée.
return (*this);
}
// Destructeur privé
Serializer::~Serializer()
{
// Généralement vide car la classe ne doit pas être instanciée.
}
uintptr_t Serializer::serialize(Data* ptr)
{
return (reinterpret_cast<uintptr_t>(ptr));
}
Data* Serializer::deserialize(uintptr_t raw)
{
return (reinterpret_cast<Data*>(raw));
}

47
cpp06/ex01/Serializer.hpp Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Serializer.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:58:04 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 13:41:15 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SERIALIZER_HPP
#define SERIALIZER_HPP
#include "Data.hpp"
#include <stdint.h>
#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 Serializer
{
public:
// Méthodes statiques publiques pour la sérialisation et la désérialisation
static uintptr_t serialize(Data* ptr);
static Data* deserialize(uintptr_t raw);
private:
// Constructeur par défaut privé pour empêcher l'instanciation
Serializer();
// Constructeur de copie privé pour empêcher la copie
Serializer(const Serializer&);
// Opérateur d'affectation privé pour empêcher l'affectation
Serializer& operator=(const Serializer&);
// Destructeur privé
~Serializer();
};
#endif

BIN
cpp06/ex01/convert Executable file

Binary file not shown.

54
cpp06/ex01/main.cpp Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 16:06:41 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 13:46:46 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Serializer.hpp"
#include <iostream>
#include <string>
int main()
{
std::cout << YELLOW << "Enter a string to be stored in Data: " << RESET;
std::string input;
std::getline(std::cin, input); // Capture l'entrée utilisateur, incluant les espaces.
Data data; // Création d'une instance de Data.
data.dummy_str = input; // Stocke la chaîne saisie dans l'instance de Data.
std::cout << CYAN << "Original data content: " << RESET << data.dummy_str << std::endl;
// Sérialisation de l'adresse de l'instance de Data.
uintptr_t serialized = Serializer::serialize(&data);
std::cout << CYAN << "Serialized value (address as integer): " << RESET << serialized << std::endl;
// Désérialisation de la valeur sérialisée pour obtenir le pointeur vers l'instance de Data.
Data* deserialized = Serializer::deserialize(serialized);
std::cout << CYAN << "Deserialized data content: " << RESET << deserialized->dummy_str << std::endl;
// Vérification de la cohérence entre l'instance originale et l'instance désérialisée.
if (&data == deserialized)
{
std::cout << YELLOW << "Verification: Original and deserialized data point to the same instance." << RESET << std::endl;
}
else
{
std::cout << RED << "Error: Original and deserialized data do not point to the same instance." << RESET << std::endl;
}
// Logs de l'entrée standard à la fin du programme
std::cout << MAGENTA << "Final Log:" << RESET << std::endl;
std::cout << CYAN << "- Original input: " << RESET << input << std::endl;
std::cout << CYAN << "- Serialized value: " << RESET << serialized << std::endl;
std::cout << CYAN << "- Deserialized content: " << RESET << deserialized->dummy_str << std::endl;
std::cout << CYAN << "- Original and deserialized comparison: " << RESET << (&data == deserialized ? "Identical" : "Different") << std::endl;
return (0);
}

21
cpp06/ex02/A.hpp Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* A.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:24:33 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 14:48:02 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef A_HPP
#define A_HPP
#include "Base.hpp"
class A: public Base
{};
#endif

21
cpp06/ex02/B.hpp Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* B.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:26:30 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 14:48:12 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef B_HPP
#define B_HPP
#include "Base.hpp"
class B: public Base
{};
#endif

22
cpp06/ex02/Base.hpp Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Base.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:23:13 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 15:02:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BASE_HPP
#define BASE_HPP
class Base
{
public:
virtual ~Base() {};
};
#endif

21
cpp06/ex02/C.hpp Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* C.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:27:43 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 14:48:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef C_HPP
#define C_HPP
#include "Base.hpp"
class C: public Base
{};
#endif

61
cpp06/ex02/Makefile Normal file
View File

@ -0,0 +1,61 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/27 14:47:25 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 = identify
SRC = Utils.cpp \
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 "./identify" 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

52
cpp06/ex02/Utils.cpp Normal file
View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Utils.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:33:46 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 15:07:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Utils.hpp"
#include "A.hpp"
#include "B.hpp"
#include "C.hpp"
#include <iostream>
#include <cstdlib>
#include <ctime>
Base* generate(void)
{
srand(static_cast<unsigned int>(time(NULL)));
int choice = rand() % 3;
switch (choice)
{
case 0: return new A();
case 1: return new B();
case 2: return new C();
}
return (NULL); // Par défaut, retourne NULL si aucun cas n'est sélectionné.
}
void identify(Base* p)
{
if (dynamic_cast<A*>(p)) std::cout << "A\n";
else if (dynamic_cast<B*>(p)) std::cout << "B\n";
else if (dynamic_cast<C*>(p)) std::cout << "C\n";
}
void identify(Base& p)
{
try {
(void)dynamic_cast<A&>(p); std::cout << "A\n"; return;
} catch (...) {}
try {
(void)dynamic_cast<B&>(p); std::cout << "B\n"; return;
} catch (...) {}
try {
(void)dynamic_cast<C&>(p); std::cout << "C\n"; return;
} catch (...) {}
}

38
cpp06/ex02/Utils.hpp Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Utils.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:31:48 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 15:05:19 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef UTILS_HPP
#define UTILS_HPP
#include "Base.hpp"
#include "A.hpp"
#include "B.hpp"
#include "C.hpp"
#include <iostream>
#include <cstdlib>
#include <ctime>
#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"
Base* generate(void);
void identify(Base* p);
void identify(Base& p);
#endif

43
cpp06/ex02/main.cpp Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 14:44:31 by fgras-ca #+# #+# */
/* Updated: 2024/02/27 15:14:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Utils.hpp"
#include <iostream>
int main()
{
Base* base = generate();
std::cout << RED << "Identify by pointer: " << RESET;
identify(base); // Identifier par pointeur
std::cout << RED << "Identify by reference: " << RESET;
identify(*base); // Identifier par référence
delete base; // Nettoyer la mémoire
A a;
B b;
C c;
std::cout << YELLOW << "\nDirect identification:" << RESET << std::endl;
std::cout << MAGENTA << "Identify A by reference: " << RESET;
identify(a);
std::cout << MAGENTA << "Identify B by reference: " << RESET;
identify(b);
std::cout << MAGENTA << "Identify C by reference: " << RESET;
identify(c);
return (0);
}