This commit is contained in:
Ladebeze66 2024-02-25 18:12:02 +01:00
parent 9aa5e8f9c5
commit 57e94d216a
20 changed files with 1045 additions and 119 deletions

View File

@ -57,52 +57,53 @@
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"files.associations": {
"iostream": "cpp",
"ostream": "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",
"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",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"ctime": "cpp",
"fstream": "cpp"
},
"C_Cpp.errorSquiggles": "disabled"
"iostream": "cpp",
"ostream": "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",
"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",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"ctime": "cpp",
"fstream": "cpp"
},
"C_Cpp.errorSquiggles": "disabled",
"CodeGPT.apiKey": "CodeGPT Plus Beta"
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 15:24:37 by fgras-ca ### ########.fr */
/* Updated: 2024/02/25 13:27:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -85,11 +85,11 @@ void Bureaucrat::signForm(AForm& form)
try
{
form.beSigned(*this);
std::cout << this->name << " signed " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << std::endl;
std::cout << YELLOW << this->name << " signed " << form.getName() << RESET << "\n" << std::endl;
}
catch (std::exception& e)
{
std::cout << this->name << " couldnt sign " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because " << e.what() << std::endl;
std::cout << RED << this->name << " couldnt sign " << form.getName() << " because: " << e.what() << RESET << std::endl;
}
}
@ -98,18 +98,18 @@ void Bureaucrat::executeForm(AForm const &form)
try
{
form.execute(*this);
std::cout << name << " executed " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << std::endl;
std::cout << YELLOW << name << " executed " << form.getName() << RESET << "\n" << std::endl;
}
catch (std::exception& e)
{
std::cerr << name << " couldnt execute " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because: " << e.what() << std::endl;
std::cout << RED << name << " couldnt execute " << form.getName() << " because: " << e.what() << RESET << std::endl;
}
}
// Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too high\n");
return ("Grade too hight\n");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()

16
cpp05/ex02/Home_shrubbery Normal file
View File

@ -0,0 +1,16 @@
. 🅻🅰🅳🅴🅱🅴🆉🅴 . +
. . . # .
. . ### . .
. . .#:. .:##°##:. .:#. . .
. . ####°###°#### .
. #:. .:#.###.#:. .:# . .
#########°######### . .
. #:. ####°###°#### .:# . .
. . °#######°°##°##°°#######° .
.°##°#####°#####°##° . .
. #:. ... .:##°###°###°##:. ... .:# .
. #######°##°#####°##°####### . .
. . #####°°#######°°##### . .
. 000 . .
. . . 000 . . .
. ..................O000O.................

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:31:13 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 15:42:00 by fgras-ca ### ########.fr */
/* Updated: 2024/02/25 13:29:09 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,7 +50,7 @@ void PresidentialPardonForm::execute(Bureaucrat const &executor) const
{
throw AForm::GradeTooLowException();
}
std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl;
std::cout << MAGENTA << getTarget() << " has been pardoned by Zaphod Beeblebrox.\n" << RESET << std::endl;
}
std::string PresidentialPardonForm::getTarget() const

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:25:21 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 16:13:37 by fgras-ca ### ########.fr */
/* Updated: 2024/02/25 13:15:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,14 +63,14 @@ void RobotomyRequestForm::execute(Bureaucrat const &executor) const
throw GradeTooLowException();
}
std::cout << "Drilling noises... Zzzzzz..." << std::endl;
std::cout << "\nDrilling noises... Zzzzzz..." << std::endl;
if (std::rand() % 2 == 0)
{
std::cout << GREEN << target << " has been robotomized successfully." << RESET << std::endl;
std::cout << GREEN << target << " has been robotomized successfully.\n" << RESET << std::endl;
}
else
{
std::cout << RED << "The robotomy on " << target << " failed." << RESET << std::endl;
std::cout << RED << "The robotomy on " << target << " failed.\n" << RESET << std::endl;
}
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:55:46 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 15:54:24 by fgras-ca ### ########.fr */
/* Updated: 2024/02/25 13:18:30 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
int main()
{
std::cout << CYAN << "\n--- Creation ---\n\n" << RESET;
// Création des bureaucrates
Bureaucrat john("John", 2);
Bureaucrat doe("Doe", 150);
@ -29,7 +30,8 @@ int main()
PresidentialPardonForm pardon("Criminal");
// Tentatives de signature et d'exécution des formulaires par John
std::cout << "\n--- John tries to sign and execute forms ---\n";
std::cout << CYAN << "\n--- John tries to sign and execute forms ---\n\n" << RESET;
john.signForm(shrub);
john.executeForm(shrub);
@ -40,68 +42,25 @@ int main()
john.executeForm(pardon);
// Tentatives de signature et d'exécution des formulaires par Doe
std::cout << "\n--- Doe tries to sign and execute forms ---\n";
try
{
doe.signForm(shrub);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
std::cout << CYAN << "\n--- Doe tries to sign and execute forms ---\n\n" << RESET;
try
{
doe.executeForm(shrub);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
try
{
doe.executeForm(robot);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
doe.signForm(shrub);
doe.executeForm(shrub);
doe.executeForm(robot);
// Tentatives de signature et d'exécution des formulaires avec gestion des exceptions
std::cout << "\n--- Additional tests with exception handling ---\n";
try
{
doe.signForm(robot);
doe.executeForm(robot);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
std::cout << CYAN << "\n--- Additional tests with exception handling ---\n\n" << RESET;
try
{
doe.signForm(pardon);
doe.executeForm(pardon);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
doe.signForm(robot);
doe.executeForm(robot);
doe.signForm(pardon);
doe.executeForm(pardon);
// Test avec un formulaire déjà signé
std::cout << "\n--- Testing with already signed form ---\n";
try
{
john.signForm(shrub); // shrub est déjà signé par John plus tôt
john.executeForm(shrub);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
std::cout << CYAN << "\n--- Testing with already signed form ---\n\n" << RESET;
john.signForm(shrub); // shrub est déjà signé par John plus tôt
john.executeForm(shrub);
return (0);
}

103
cpp05/ex03/AForm.cpp Normal file
View File

@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 14:36:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "AForm.hpp"
AForm::AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute)
: name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute)
{
if (gradeRequiredToSign < 1 || gradeRequiredToExecute < 1)
{
throw GradeTooHighException();
}
else if (gradeRequiredToSign > 150 || gradeRequiredToExecute > 150)
{
throw GradeTooLowException();
}
}
AForm::AForm(const AForm& other)
: name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute)
{
std::cout << CYAN << "Copy constructor called for AForm " << this->name << RESET << std::endl;
}
AForm& AForm::operator=(const AForm& other)
{
if (this != &other)
{
this->isSigned = other.isSigned;
}
return (*this);
}
AForm::~AForm()
{
std::cout << RED << "AForm " << this->name << " is destroyed!" << RESET << std::endl;
}
std::string AForm::getName() const
{
return (name);
}
bool AForm::getIsSigned() const
{
return (isSigned);
}
int AForm::getGradeRequiredToSign() const
{
return (gradeRequiredToSign);
}
int AForm::getGradeRequiredToExecute() const
{
return (gradeRequiredToExecute);
}
void AForm::beSigned(const Bureaucrat& bureaucrat)
{
if (bureaucrat.getGrade() <= gradeRequiredToSign)
{
isSigned = true;
}
else
{
throw GradeTooLowException();
}
}
const char* AForm::GradeTooHighException::what() const throw()
{
return ("Grade too high\n");
}
const char* AForm::GradeTooLowException::what() const throw()
{
return ("Grade too low\n");
}
const char* AForm::NotSignedException::what() const throw()
{
return ("The form is not signed.\n");
}
std::ostream& operator<<(std::ostream& os, const AForm& aform)
{
os << "AForm: " << aform.getName()
<< ", Status: " << (aform.getIsSigned() ? "Signed" : "Not signed")
<< ", Grade Required to Sign: " << aform.getGradeRequiredToSign()
<< ", Grade Required to Execute: " << aform.getGradeRequiredToExecute();
return (os);
}

74
cpp05/ex03/AForm.hpp Normal file
View File

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 13:49:48 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AFORM_HPP
#define AFORM_HPP
#include "Bureaucrat.hpp" // Assurez-vous que cette classe est bien définie
#include <string>
#include <iostream>
class Bureaucrat;
class AForm
{
private:
const std::string name;
bool isSigned;
const int gradeRequiredToSign;
const int gradeRequiredToExecute;
protected:
std::string target;
public:
//constructueur
AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie
AForm(const AForm& other);
// Opérateur d'affectation
AForm& operator=(const AForm& other);
// Destructeur
virtual ~AForm();
std::string getName() const;
bool getIsSigned() const;
int getGradeRequiredToSign() const;
int getGradeRequiredToExecute() const;
virtual std::string getTarget() const {return target;}
virtual void beSigned(const Bureaucrat& bureaucrat);
virtual void execute(Bureaucrat const &executor) const = 0;
class GradeTooHighException : public std::exception
{
public:
virtual const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
virtual const char* what() const throw();
};
class NotSignedException : public std::exception
{
public:
virtual const char* what() const throw();
};
};
// Surcharge de l'opérateur d'insertion
std::ostream& operator<<(std::ostream& os, const AForm& aform);
#endif

124
cpp05/ex03/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,124 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 13:27:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
// Constructeur par défaut
Bureaucrat::Bureaucrat() : name("Default"), grade(150)
{
std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl;
}
// Constructeur avec paramètres
Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade)
{
if (grade < 1)
{
throw GradeTooHighException();
}
else if (grade > 150)
{
throw GradeTooLowException();
}
this->grade = grade;
std::cout << GREEN << "Bureaucrat " << this->name << " constructor called with grade " << this->grade << RESET << std::endl;
}
// Constructeur de copie
Bureaucrat::Bureaucrat(const Bureaucrat& other) : name(other.name), grade(other.grade)
{
std::cout << CYAN << "Copy constructor called for Bureaucrat " << this->name << RESET << std::endl;
}
// Opérateur d'affectation
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other)
{
if (this != &other)
{
// L'attribut name étant const, nous ne pouvons pas le modifier. Seul grade est copié.
this->grade = other.grade;
}
return (*this);
}
// Destructeur
Bureaucrat::~Bureaucrat()
{
std::cout << RED << "Bureaucrat " << this->name << " is destroyed!" << RESET << std::endl;
}
// Getter pour le nom
std::string Bureaucrat::getName() const
{
return (this->name);
}
// Getter pour le grade
int Bureaucrat::getGrade() const
{
return (this->grade);
}
// Incrémente le grade
void Bureaucrat::incrementGrade()
{
if (grade <= 1)
{
throw GradeTooHighException();
}
--grade;
}
// Décrémente le grade
void Bureaucrat::decrementGrade()
{
if (grade >= 150)
{
throw GradeTooLowException();
}
++grade;
}
void Bureaucrat::signForm(AForm& form)
{
try
{
form.beSigned(*this);
std::cout << YELLOW << this->name << " signed " << form.getName() << RESET << "\n" << std::endl;
}
catch (std::exception& e)
{
std::cout << RED << this->name << " couldnt sign " << form.getName() << " because: " << e.what() << RESET << std::endl;
}
}
void Bureaucrat::executeForm(AForm const &form)
{
try
{
form.execute(*this);
std::cout << YELLOW << name << " executed " << form.getName() << RESET << "\n" << std::endl;
}
catch (std::exception& e)
{
std::cout << RED << name << " couldnt execute " << form.getName() << " because: " << e.what() << RESET << std::endl;
}
}
// Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too hight\n");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low\n");
}
// Surcharge de l'opérateur <<
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
{
os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade();
return (os);
}

68
cpp05/ex03/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 16:18:19 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUREAUCRAT_HPP
#define BUREAUCRAT_HPP
#include <string>
#include <exception>
#include <iostream>
#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"
class AForm;
class Bureaucrat
{
public:
class GradeTooHighException : public std::exception
{
public:
virtual const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
virtual const char* what() const throw();
};
Bureaucrat();
Bureaucrat(const std::string& name, int grade);
Bureaucrat(const Bureaucrat& other);
Bureaucrat& operator=(const Bureaucrat& other);
~Bureaucrat();
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(AForm& form);
virtual void executeForm(AForm const &form);
private:
const std::string name;
int grade;
};
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);
#endif

60
cpp05/ex03/Intern.cpp Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Intern.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/25 16:27:11 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 18:05:57 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Intern.hpp"
Intern::Intern()
{
std::cout << GREEN << "Intern default constructor called!" << RESET << std::endl;
}
Intern::Intern(const Intern& other)
{
*this = other;
std::cout << CYAN << "Copy constructor called for Intern." << RESET << std::endl;
}
Intern& Intern::operator=(const Intern& other)
{
(void)other;
return (*this);
}
Intern::~Intern()
{
std::cout << RED << "Intern is destroyed!" << RESET << std::endl;
}
AForm* Intern::makeForm(const std::string& formName, const std::string& target) const
{
AForm* form = NULL;
if (formName == "shrubbery creation")
{
form = new ShrubberyCreationForm(target);
std::cout << BLUE << "Intern creates ShruberryCreationForm" << RESET << std::endl;
}
else if (formName == "robotomy request")
{
form = new RobotomyRequestForm(target);
std::cout << BLUE << "Intern creates RobotomyRequestForm" << RESET << std::endl;
}
else if (formName == "presidential pardon")
{
form = new PresidentialPardonForm(target);
std::cout << BLUE << "Intern creates PresidentialPardonForm" << RESET << std::endl;
}
else
{
std::cerr << RED << "Error: Request form " << formName << " is unknown: " << target << RESET << std::endl;
}
return (form);
}

34
cpp05/ex03/Intern.hpp Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Intern.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/25 16:16:41 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 16:26:38 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INTERN_HPP
#define INTERN_HPP
#include "AForm.hpp"
#include "ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "PresidentialPardonForm.hpp"
#include <string>
#include <iostream>
class Intern
{
public:
Intern();
Intern(const Intern& other);
Intern& operator=(const Intern& other);
~Intern();
AForm* makeForm(const std::string& formName, const std::string& target) const;
};
#endif

66
cpp05/ex03/Makefile Normal file
View File

@ -0,0 +1,66 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/25 17:24:06 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 = Form
SRC = Bureaucrat.cpp \
AForm.cpp \
main.cpp \
PresidentialPardonForm.cpp \
RobotomyRequestForm.cpp \
ShrubberyCreationForm.cpp \
Intern.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 "./Form" 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

View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:31:13 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 13:29:09 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "PresidentialPardonForm.hpp"
PresidentialPardonForm::PresidentialPardonForm(const std::string& target)
: AForm("PresidentialPardonForm", 25, 5), target(target)
{
std::cout << GREEN << "PresidentialPardonForm constructed for target: " << target << RESET << std::endl;
}
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other)
: AForm(other), target(other.target)
{
std::cout << "PresidentialPardonForm copy constructed for target: " << target << std::endl;
}
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other)
{
if (this != &other)
{
AForm::operator=(other);
target = other.target;
std::cout << "PresidentialPardonForm assigned from another form for target: " << target << std::endl;
}
return (*this);
}
PresidentialPardonForm::~PresidentialPardonForm()
{
std::cout << RED << "PresidentialPardonForm destroyed for target: " << target << RESET << std::endl;
}
void PresidentialPardonForm::execute(Bureaucrat const &executor) const
{
if (!getIsSigned())
{
throw AForm::NotSignedException();
}
if (executor.getGrade() > getGradeRequiredToExecute())
{
throw AForm::GradeTooLowException();
}
std::cout << MAGENTA << getTarget() << " has been pardoned by Zaphod Beeblebrox.\n" << RESET << std::endl;
}
std::string PresidentialPardonForm::getTarget() const
{
return (target);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:56:48 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 15:39:59 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRESIDENTIALPARDONFORM_HPP
#define PRESIDENTIALPARDONFORM_HPP
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <iostream>
#define RESET "\033[0m"
#define GREEN "\033[32m"
#define CYAN "\033[36m"
#define RED "\033[31m"
class PresidentialPardonForm : public AForm
{
private:
std::string target;
public:
PresidentialPardonForm(const std::string& target);
PresidentialPardonForm(const PresidentialPardonForm& other);
PresidentialPardonForm& operator=(const PresidentialPardonForm& other);
virtual ~PresidentialPardonForm();
void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
};
#endif

View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:25:21 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 13:15:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
void RobotomyRequestForm::initializeRandomSeed()
{
static bool seeded = false;
if (!seeded)
{
std::srand(static_cast<unsigned int>(std::time(NULL))); // Utilisez NULL au lieu de nullptr
seeded = true;
}
}
RobotomyRequestForm::RobotomyRequestForm(const std::string& target)
: AForm("RobotomyRequestForm", 72, 45), target(target)
{
initializeRandomSeed();
std::cout << GREEN << "RobotomyRequestForm constructor called for target: " << target << RESET << std::endl;
}
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other)
: AForm(other), target(other.target)
{
std::cout << CYAN << "Copy constructor called for RobotomyRequestForm with target: " << target << RESET << std::endl;
}
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& other)
{
if (this != &other)
{
AForm::operator=(other); // Note: Ceci n'aura aucun effet car AForm::operator= est supprimé ou non implémenté.
target = other.target;
// Log pour l'opération d'affectation
std::cout << CYAN << "Assignment operator called for RobotomyRequestForm with target: " << target << RESET << std::endl;
}
return (*this);
}
RobotomyRequestForm::~RobotomyRequestForm()
{
std::cout << RED << "RobotomyRequestForm for target: " << target << " is destroyed!" << RESET << std::endl;
}
void RobotomyRequestForm::execute(Bureaucrat const &executor) const
{
if (!getIsSigned())
{
throw NotSignedException();
}
if (executor.getGrade() > getGradeRequiredToExecute())
{
throw GradeTooLowException();
}
std::cout << "\nDrilling noises... Zzzzzz..." << std::endl;
if (std::rand() % 2 == 0)
{
std::cout << GREEN << target << " has been robotomized successfully.\n" << RESET << std::endl;
}
else
{
std::cout << RED << "The robotomy on " << target << " failed.\n" << RESET << std::endl;
}
}
std::string RobotomyRequestForm::getTarget() const
{
return (target); // Assurez-vous que 'target' est bien défini et accessible
}

View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:55:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 16:10:22 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROBOTOMYREQUESTFORM_HPP
#define ROBOTOMYREQUESTFORM_HPP
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <iostream>
#include <cstdlib> // Pour std::rand() et std::srand()
#include <ctime> // Pour std::time()
// Assurez-vous d'avoir ces définitions quelque part, ou remplacez-les par vos propres codes de couleur.
#define RESET "\033[0m"
#define GREEN "\033[32m"
#define CYAN "\033[36m"
#define RED "\033[31m"
class RobotomyRequestForm : public AForm
{
private:
std::string target;
public:
RobotomyRequestForm(const std::string& target);
RobotomyRequestForm(const RobotomyRequestForm& other);
RobotomyRequestForm& operator=(const RobotomyRequestForm& other);
virtual ~RobotomyRequestForm();
static void initializeRandomSeed();
void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
};
#endif

View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:54:52 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 16:33:23 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target)
: AForm("ShrubberyCreationForm", 145, 137), target(target)
{
std::cout << GREEN << "ShrubberyCreationForm constructor called for target: " << target << RESET << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& other)
: AForm(other), target(other.target)
{
std::cout << CYAN << "Copy constructor called for ShrubberyCreationForm with target: " << target << RESET << std::endl;
}
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm& other)
{
if (this != &other)
{
AForm::operator=(other); // Cette opération n'a pas d'effet réel si AForm n'implémente pas operator= pour les champs non statiques.
target = other.target;
std::cout << CYAN << "Assignment operator called for ShrubberyCreationForm with target: " << target << RESET << std::endl;
}
return (*this);
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
std::cout << RED << "ShrubberyCreationForm for target: " << target << " is destroyed!" << RESET << std::endl;
}
void ShrubberyCreationForm::execute(Bureaucrat const &executor) const
{
// Vérification si le formulaire est signé et si le grade de l'exécuteur est suffisant
if (!getIsSigned())
throw NotSignedException(); // Assurez-vous que cette exception est correctement définie dans AForm
else if (executor.getGrade() > this->getGradeRequiredToExecute())
throw GradeTooLowException();
else
{
std::ofstream ofs((target + "_shrubbery").c_str());
if (!ofs)
{
std::cerr << "Failed to create file " << target << "_shrubbery" << std::endl;
return ;
}
ofs << " . 🅻🅰🅳🅴🅱🅴🆉🅴 . +" << "\n"
<<" . . . # ." << "\n"
<<" . . ### . ." << "\n"
<<" . . .#:. .:##°##:. .:#. . ." << "\n"
<<" . . ####°###°#### ." << "\n"
<<" . #:. .:#.###.#:. .:# . ." << "\n"
<<" #########°######### . ." << "\n"
<<" . #:. ####°###°#### .:# . ." << "\n"
<<" . . °#######°°##°##°°#######° ." << "\n"
<<" .°##°#####°#####°##° . ." << "\n"
<<". #:. ... .:##°###°###°##:. ... .:# ." << "\n"
<<" . #######°##°#####°##°####### . ." << "\n"
<<". . #####°°#######°°##### . ." << "\n"
<<" . 000 . ." << "\n"
<<" . . . 000 . . ." << "\n"
<<". ..................O000O................." << std::endl;
}
}
std::string ShrubberyCreationForm::getTarget() const
{
return (target);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:41:49 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 15:35:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SHRUBBERYCREATIONFORM_HPP
#define SHRUBBERYCREATIONFORM_HPP
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <fstream>
#include <iostream>
#define RESET "\033[0m"
#define GREEN "\033[32m"
#define CYAN "\033[36m"
#define RED "\033[31m"
class ShrubberyCreationForm : public AForm
{
private:
std::string target;
public:
ShrubberyCreationForm(const std::string& target);
ShrubberyCreationForm(const ShrubberyCreationForm& other);
ShrubberyCreationForm& operator=(const ShrubberyCreationForm& other);
virtual ~ShrubberyCreationForm();
void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
};
#endif

79
cpp05/ex03/main.cpp Normal file
View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:55:46 by fgras-ca #+# #+# */
/* Updated: 2024/02/25 18:03:43 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Intern.hpp"
#include <iostream>
#include <exception>
int main()
{
std::cout << CYAN << "\n--- Creation ---\n\n" << RESET;
// Création des bureaucrates
Bureaucrat john("John", 2);
Bureaucrat doe("Doe", 150);
// Création d'un intern
Intern someRandomIntern;
// Utilisation de l'intern pour créer des formulaires
AForm* shrub = someRandomIntern.makeForm("shrubbery creation", "Home");
AForm* robot = someRandomIntern.makeForm("robotomy request", "Robot");
AForm* pardon = someRandomIntern.makeForm("presidential pardon", "Criminal");
AForm* unknown = someRandomIntern.makeForm("unknown form", "Nowhere"); // Test d'un formulaire inconnu
// Tentatives de signature et d'exécution des formulaires par John
std::cout << CYAN << "\n--- John tries to sign and execute forms ---\n\n" << RESET;
if (shrub)
{
john.signForm(*shrub);
john.executeForm(*shrub);
}
if (robot)
{
john.signForm(*robot);
john.executeForm(*robot);
}
if (pardon)
{
john.signForm(*pardon);
john.executeForm(*pardon);
}
// Tentatives de signature et d'exécution des formulaires par Doe
std::cout << CYAN << "\n--- Doe tries to sign and execute forms ---\n\n" << RESET;
if (shrub)
{
doe.signForm(*shrub);
doe.executeForm(*shrub);
}
if (robot)
{
doe.executeForm(*robot);
}
// Test avec un formulaire déjà signé
std::cout << CYAN << "\n--- Testing with already signed form ---\n\n" << RESET;
if (shrub)
{
john.signForm(*shrub); // shrub est déjà signé par John plus tôt
john.executeForm(*shrub);
}
// Nettoyage des formulaires créés
delete shrub;
delete robot;
delete pardon;
delete unknown;
return (0);
}