This commit is contained in:
Ladebeze66 2024-02-21 17:11:59 +01:00
parent 8b84a934b1
commit bce000d3ca
15 changed files with 306 additions and 131 deletions

View File

@ -57,50 +57,52 @@
"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"
},
"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"
}

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/11 13:37:27 by fgras-ca ### ########.fr #
# Updated: 2024/02/21 17:06:51 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
@ -28,14 +28,16 @@ NAME = Bureaucrat
SRC = Bureaucrat.cpp \
main.cpp \
OBJS = ${SRC:.cpp=.o}
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)

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 16:39:29 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 14:25:05 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -94,6 +94,10 @@ const char* AForm::GradeTooLowException::what() const noexcept
return ("Grade too low\n");
}
const char* AForm::NotSignedException::what() const noexcept
{
return ("The forpm is not signed.\n");
}
std::ostream& operator<<(std::ostream& os, const AForm& aform)
{
os << "AForm: " << aform.getName()

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/20 17:00:31 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 15:53:16 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,6 +27,9 @@ private:
const int gradeRequiredToSign;
const int gradeRequiredToExecute;
protected:
std::string target;
public:
//constructueur
AForm();
@ -43,6 +46,7 @@ public:
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;
@ -58,8 +62,13 @@ public:
public:
const char* what() const noexcept override;
};
};
class NotSignedException : public std::exception
{
public:
const char* what() const noexcept override;
};
};
// Surcharge de l'opérateur d'insertion
std::ostream& operator<<(std::ostream& os, const AForm& aform);

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/20 17:02:49 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 16:02:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -80,30 +80,30 @@ void Bureaucrat::decrementGrade()
++grade;
}
void Bureaucrat::signForm(Form& form)
void Bureaucrat::signForm(AForm& form)
{
try
{
form.beSigned(*this);
std::cout << this->name << " signed " << form.getName() << std::endl;
std::cout << this->name << " signed " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << std::endl;
}
catch (std::exception& e)
{
std::cout << this->name << " couldnt sign " << form.getName() << " because " << e.what() << std::endl;
std::cout << this->name << " couldnt sign " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because " << e.what() << std::endl;
}
}
void Bureaucrat::executeForm(AForm const &form)
{
try
try
{
form.execute(*this);
std::cout << name << " executed " << form.getName() << std::endl;
}
form.execute(*this);
std::cout << name << " executed " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << std::endl;
}
catch (std::exception& e)
{
std::cout << name << " couldnt execute " << form.getName() << " because: " << e.what() << std::endl;
}
std::cout << name << " couldnt execute " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because: " << e.what() << std::endl;
}
}
// Méthodes pour les exceptions

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 17:01:47 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 13:09:39 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,6 +28,8 @@
#define CYAN "\033[36m"
#define WHITE "\033[37m"
class AForm;
class Bureaucrat
{
public:
@ -53,7 +55,7 @@ public:
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(Form& form);
void signForm(AForm& form);
void executeForm(AForm const &form);
private:

View File

@ -0,0 +1 @@
ASCII trees

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/16 18:36:25 by fgras-ca ### ########.fr #
# Updated: 2024/02/21 13:06:08 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
@ -28,6 +28,9 @@ NAME = Form
SRC = Bureaucrat.cpp \
AForm.cpp \
main.cpp \
PresidentialPardonForm.cpp \
RobotomyRequestForm.cpp \
ShrubberyCreationForm.cpp \
OBJS = ${SRC:.cpp=.o}

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/21 15:18:45 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 << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl;
}
std::string PresidentialPardonForm::getTarget() const
{
return (target);
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:56:48 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 16:57:14 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 15:17:46 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,18 @@
#define PRESIDENTIALPARDONFORM_HPP
#include "AForm.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);
@ -24,6 +33,7 @@ public:
virtual ~PresidentialPardonForm();
void execute(Bureaucrat const &executor) const override;
std::string getTarget() const;
};
#endif

View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:25:21 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 16:00:03 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
RobotomyRequestForm::RobotomyRequestForm(const std::string& target)
: AForm("RobotomyRequestForm", 72, 45), target(target)
{
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 << "Drilling noises... Zzzzzz..." << std::endl;
if (std::rand() % 2 == 0)
{
std::cout << target << " has been robotomized successfully." << std::endl;
}
else
{
std::cout << "The robotomy on " << target << " failed." << std::endl;
}
}
std::string RobotomyRequestForm::getTarget() const
{
return (target); // Assurez-vous que 'target' est bien défini et accessible
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:55:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 16:56:11 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 15:58:32 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,10 +14,20 @@
#define ROBOTOMYREQUESTFORM_HPP
#include "AForm.hpp"
#include <cstdlib> // Pour rand()
#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);
@ -25,6 +35,7 @@ public:
virtual ~RobotomyRequestForm();
void execute(Bureaucrat const &executor) const override;
std::string getTarget() const override;
};
#endif

View File

@ -6,30 +6,58 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:54:52 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 19:23:43 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 15:39:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target)
: AForm("ShrubberyCreationForm", 145, 137, 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) {}
: 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)
{
AForm::operator=(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() {}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
std::cout << RED << "ShrubberyCreationForm for target: " << target << " is destroyed!" << RESET << std::endl;
}
void ShrubberyCreationForm::execute(Bureaucrat const &executor) const
{
AForm::execute(executor); // Assurez-vous que cette vérification est implémentée dans AForm ou faites-la ici.
std::ofstream ofs(getTarget() + "_shrubbery");
// Assurez-vous que ofs est ouvert correctement.
// 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
if (executor.getGrade() > getGradeRequiredToExecute())
throw GradeTooLowException();
std::ofstream ofs(target + "_shrubbery");
if (!ofs)
{
std::cerr << "Failed to create file " << target << "_shrubbery" << std::endl;
return ;
}
ofs << "ASCII trees" << std::endl;
}
std::string ShrubberyCreationForm::getTarget() const
{
return (target);
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:41:49 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 16:58:51 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 15:51:24 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,9 +15,17 @@
#include "AForm.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);
@ -25,6 +33,7 @@ public:
virtual ~ShrubberyCreationForm();
void execute(Bureaucrat const &executor) const override;
std::string getTarget() const;
};
#endif

View File

@ -6,77 +6,44 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:55:46 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 15:38:42 by fgras-ca ### ########.fr */
/* Updated: 2024/02/21 15:21:24 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
#include "ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "PresidentialPardonForm.hpp"
#include <iostream>
#include <exception>
int main()
{
try
{
// Création d'un bureaucrate avec un grade élevé
Bureaucrat highRankBureaucrat("HighRank", 2);
// Création d'un bureaucrate avec un grade bas
Bureaucrat lowRankBureaucrat("LowRank", 149);
Bureaucrat chief("Chief", 1);
Bureaucrat intern("Intern", 150);
Bureaucrat john("John", 2);
Bureaucrat doe("Doe", 150);
// Création de formulaires
Form highRequirementForm("HighRequirement", 1, 1); // Exige un grade très élevé pour signer
Form lowRequirementForm("LowRequirement", 150, 150); // Peut être signé par n'importe quel bureaucrate
Form formHigh("HighForm", 2, 5);
ShrubberyCreationForm shrub("Home");
RobotomyRequestForm robot("Robot");
PresidentialPardonForm pardon("Criminal");
// Tentative de signature des formulaires
std::cout << CYAN << "Tentative de signature par HighRank:" << RESET << std::endl;
highRankBureaucrat.signForm(highRequirementForm);
highRankBureaucrat.signForm(lowRequirementForm);
try {
john.signForm(shrub);
john.executeForm(shrub);
std::cout << CYAN << "\nTentative de signature par LowRank:" << RESET << std::endl;
lowRankBureaucrat.signForm(highRequirementForm);
lowRankBureaucrat.signForm(lowRequirementForm);
john.signForm(robot);
john.executeForm(robot);
std::cout << GREEN << "\nTenative de signature formHight par chief: " << RESET << std::endl;
chief.signForm(formHigh);
chief.signForm(highRequirementForm);
chief.signForm(lowRequirementForm);
john.signForm(pardon);
john.executeForm(pardon);
std::cout << GREEN << "\nTenative de signature formHight par intern: " << RESET << std::endl;
intern.signForm(formHigh);
intern.signForm(highRequirementForm);
intern.signForm(lowRequirementForm);
// Tester avec un bureaucrate de grade insuffisant
doe.signForm(shrub);
doe.executeForm(shrub);
}
catch (std::exception& e)
{
std::cerr << RED << "Une exception a été capturée: " << RESET << e.what() << std::endl;
std::cout << e.what() << std::endl;
}
// Création d'un formulaire avec des grades invalides
try
{
Form invalidForm("ImpossibleForm", 0, 151);
}
catch (const Form::GradeTooHighException& e)
{
std::cerr << RED << "Form creation error: " << RESET << e.what() << std::endl;
}
// Signature d'un formulaire par un bureaucrate au seuil du grade requis
try
{
Bureaucrat chief("Chief", 1);
Bureaucrat intern("Intern", 150);
Form formHigh("HighForm", 2, 5);
Form edgeCaseForm("EdgeCaseForm", 150, 150);
intern.signForm(edgeCaseForm);
// Tentative de signature d'un formulaire déjà signé
chief.signForm(formHigh);
}
catch (const std::exception& e)
{
std::cerr << RED << "Unexpected error: " << RESET << e.what() << std::endl;
}
return (0);
}