mirror of
https://github.com/Ladebeze66/cpp-partie-2.git
synced 2025-12-16 05:57:57 +01:00
modif
This commit is contained in:
parent
8b84a934b1
commit
bce000d3ca
92
cpp05/.vscode/settings.json
vendored
92
cpp05/.vscode/settings.json
vendored
@ -57,50 +57,52 @@
|
|||||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||||
"C_Cpp_Runner.msvcSecureNoWarnings": false,
|
"C_Cpp_Runner.msvcSecureNoWarnings": false,
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"iostream": "cpp",
|
"iostream": "cpp",
|
||||||
"ostream": "cpp",
|
"ostream": "cpp",
|
||||||
"array": "cpp",
|
"array": "cpp",
|
||||||
"atomic": "cpp",
|
"atomic": "cpp",
|
||||||
"bit": "cpp",
|
"bit": "cpp",
|
||||||
"*.tcc": "cpp",
|
"*.tcc": "cpp",
|
||||||
"cctype": "cpp",
|
"cctype": "cpp",
|
||||||
"clocale": "cpp",
|
"clocale": "cpp",
|
||||||
"cmath": "cpp",
|
"cmath": "cpp",
|
||||||
"compare": "cpp",
|
"compare": "cpp",
|
||||||
"concepts": "cpp",
|
"concepts": "cpp",
|
||||||
"cstdarg": "cpp",
|
"cstdarg": "cpp",
|
||||||
"cstddef": "cpp",
|
"cstddef": "cpp",
|
||||||
"cstdint": "cpp",
|
"cstdint": "cpp",
|
||||||
"cstdio": "cpp",
|
"cstdio": "cpp",
|
||||||
"cstdlib": "cpp",
|
"cstdlib": "cpp",
|
||||||
"cwchar": "cpp",
|
"cwchar": "cpp",
|
||||||
"cwctype": "cpp",
|
"cwctype": "cpp",
|
||||||
"deque": "cpp",
|
"deque": "cpp",
|
||||||
"string": "cpp",
|
"string": "cpp",
|
||||||
"unordered_map": "cpp",
|
"unordered_map": "cpp",
|
||||||
"vector": "cpp",
|
"vector": "cpp",
|
||||||
"exception": "cpp",
|
"exception": "cpp",
|
||||||
"algorithm": "cpp",
|
"algorithm": "cpp",
|
||||||
"functional": "cpp",
|
"functional": "cpp",
|
||||||
"iterator": "cpp",
|
"iterator": "cpp",
|
||||||
"memory": "cpp",
|
"memory": "cpp",
|
||||||
"memory_resource": "cpp",
|
"memory_resource": "cpp",
|
||||||
"numeric": "cpp",
|
"numeric": "cpp",
|
||||||
"random": "cpp",
|
"random": "cpp",
|
||||||
"string_view": "cpp",
|
"string_view": "cpp",
|
||||||
"system_error": "cpp",
|
"system_error": "cpp",
|
||||||
"tuple": "cpp",
|
"tuple": "cpp",
|
||||||
"type_traits": "cpp",
|
"type_traits": "cpp",
|
||||||
"utility": "cpp",
|
"utility": "cpp",
|
||||||
"initializer_list": "cpp",
|
"initializer_list": "cpp",
|
||||||
"iosfwd": "cpp",
|
"iosfwd": "cpp",
|
||||||
"istream": "cpp",
|
"istream": "cpp",
|
||||||
"limits": "cpp",
|
"limits": "cpp",
|
||||||
"new": "cpp",
|
"new": "cpp",
|
||||||
"numbers": "cpp",
|
"numbers": "cpp",
|
||||||
"stdexcept": "cpp",
|
"stdexcept": "cpp",
|
||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"typeinfo": "cpp"
|
"typeinfo": "cpp",
|
||||||
},
|
"ctime": "cpp",
|
||||||
|
"fstream": "cpp"
|
||||||
|
},
|
||||||
"C_Cpp.errorSquiggles": "disabled"
|
"C_Cpp.errorSquiggles": "disabled"
|
||||||
}
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
# 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 \
|
SRC = Bureaucrat.cpp \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
|
|
||||||
OBJS = ${SRC:.cpp=.o}
|
|
||||||
|
|
||||||
CC = c++
|
CC = c++
|
||||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||||
RM = rm -f
|
RM = rm -f
|
||||||
|
OBJS = ${SRC:.cpp=.o}
|
||||||
|
|
||||||
all : $(NAME)
|
all : $(NAME)
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
$(NAME) : $(OBJS)
|
$(NAME) : $(OBJS)
|
||||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||||
|
|||||||
@ -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/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");
|
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)
|
std::ostream& operator<<(std::ostream& os, const AForm& aform)
|
||||||
{
|
{
|
||||||
os << "AForm: " << aform.getName()
|
os << "AForm: " << aform.getName()
|
||||||
|
|||||||
@ -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:37:14 by fgras-ca #+# #+# */
|
/* 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 gradeRequiredToSign;
|
||||||
const int gradeRequiredToExecute;
|
const int gradeRequiredToExecute;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string target;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//constructueur
|
//constructueur
|
||||||
AForm();
|
AForm();
|
||||||
@ -43,6 +46,7 @@ public:
|
|||||||
bool getIsSigned() const;
|
bool getIsSigned() const;
|
||||||
int getGradeRequiredToSign() const;
|
int getGradeRequiredToSign() const;
|
||||||
int getGradeRequiredToExecute() const;
|
int getGradeRequiredToExecute() const;
|
||||||
|
virtual std::string getTarget() const {return target;}
|
||||||
|
|
||||||
virtual void beSigned(const Bureaucrat& bureaucrat);
|
virtual void beSigned(const Bureaucrat& bureaucrat);
|
||||||
virtual void execute(Bureaucrat const &executor) const = 0;
|
virtual void execute(Bureaucrat const &executor) const = 0;
|
||||||
@ -58,8 +62,13 @@ public:
|
|||||||
public:
|
public:
|
||||||
const char* what() const noexcept override;
|
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
|
// Surcharge de l'opérateur d'insertion
|
||||||
std::ostream& operator<<(std::ostream& os, const AForm& aform);
|
std::ostream& operator<<(std::ostream& os, const AForm& aform);
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
|
/* 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;
|
++grade;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bureaucrat::signForm(Form& form)
|
void Bureaucrat::signForm(AForm& form)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
form.beSigned(*this);
|
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)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
std::cout << this->name << " couldn’t sign " << form.getName() << " because " << e.what() << std::endl;
|
std::cout << this->name << " couldn’t sign " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bureaucrat::executeForm(AForm const &form)
|
void Bureaucrat::executeForm(AForm const &form)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
form.execute(*this);
|
form.execute(*this);
|
||||||
std::cout << name << " executed " << form.getName() << std::endl;
|
std::cout << name << " executed " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << std::endl;
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
std::cout << name << " couldn’t execute " << form.getName() << " because: " << e.what() << std::endl;
|
std::cout << name << " couldn’t execute " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Méthodes pour les exceptions
|
// Méthodes pour les exceptions
|
||||||
|
|||||||
@ -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/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 CYAN "\033[36m"
|
||||||
#define WHITE "\033[37m"
|
#define WHITE "\033[37m"
|
||||||
|
|
||||||
|
class AForm;
|
||||||
|
|
||||||
class Bureaucrat
|
class Bureaucrat
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -53,7 +55,7 @@ public:
|
|||||||
int getGrade() const;
|
int getGrade() const;
|
||||||
void incrementGrade();
|
void incrementGrade();
|
||||||
void decrementGrade();
|
void decrementGrade();
|
||||||
void signForm(Form& form);
|
void signForm(AForm& form);
|
||||||
void executeForm(AForm const &form);
|
void executeForm(AForm const &form);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
1
cpp05/ex02/Home_shrubbery
Normal file
1
cpp05/ex02/Home_shrubbery
Normal file
@ -0,0 +1 @@
|
|||||||
|
ASCII trees
|
||||||
@ -6,7 +6,7 @@
|
|||||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
# 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 \
|
SRC = Bureaucrat.cpp \
|
||||||
AForm.cpp \
|
AForm.cpp \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
|
PresidentialPardonForm.cpp \
|
||||||
|
RobotomyRequestForm.cpp \
|
||||||
|
ShrubberyCreationForm.cpp \
|
||||||
|
|
||||||
OBJS = ${SRC:.cpp=.o}
|
OBJS = ${SRC:.cpp=.o}
|
||||||
|
|
||||||
|
|||||||
59
cpp05/ex02/PresidentialPardonForm.cpp
Normal file
59
cpp05/ex02/PresidentialPardonForm.cpp
Normal 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);
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/20 16:56:48 by fgras-ca #+# #+# */
|
/* 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
|
#define PRESIDENTIALPARDONFORM_HPP
|
||||||
|
|
||||||
#include "AForm.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
|
class PresidentialPardonForm : public AForm
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
std::string target;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PresidentialPardonForm(const std::string& target);
|
PresidentialPardonForm(const std::string& target);
|
||||||
PresidentialPardonForm(const PresidentialPardonForm& other);
|
PresidentialPardonForm(const PresidentialPardonForm& other);
|
||||||
@ -24,6 +33,7 @@ public:
|
|||||||
virtual ~PresidentialPardonForm();
|
virtual ~PresidentialPardonForm();
|
||||||
|
|
||||||
void execute(Bureaucrat const &executor) const override;
|
void execute(Bureaucrat const &executor) const override;
|
||||||
|
std::string getTarget() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
68
cpp05/ex02/RobotomyRequestForm.cpp
Normal file
68
cpp05/ex02/RobotomyRequestForm.cpp
Normal 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
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/20 16:55:26 by fgras-ca #+# #+# */
|
/* 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
|
#define ROBOTOMYREQUESTFORM_HPP
|
||||||
|
|
||||||
#include "AForm.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
|
class RobotomyRequestForm : public AForm
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
std::string target;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RobotomyRequestForm(const std::string& target);
|
RobotomyRequestForm(const std::string& target);
|
||||||
RobotomyRequestForm(const RobotomyRequestForm& other);
|
RobotomyRequestForm(const RobotomyRequestForm& other);
|
||||||
@ -25,6 +35,7 @@ public:
|
|||||||
virtual ~RobotomyRequestForm();
|
virtual ~RobotomyRequestForm();
|
||||||
|
|
||||||
void execute(Bureaucrat const &executor) const override;
|
void execute(Bureaucrat const &executor) const override;
|
||||||
|
std::string getTarget() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,30 +6,58 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/20 16:54:52 by fgras-ca #+# #+# */
|
/* 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"
|
#include "ShrubberyCreationForm.hpp"
|
||||||
|
|
||||||
ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target)
|
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)
|
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)
|
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);
|
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
|
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.
|
// Vérification si le formulaire est signé et si le grade de l'exécuteur est suffisant
|
||||||
std::ofstream ofs(getTarget() + "_shrubbery");
|
if (!getIsSigned())
|
||||||
// Assurez-vous que ofs est ouvert correctement.
|
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;
|
ofs << "ASCII trees" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string ShrubberyCreationForm::getTarget() const
|
||||||
|
{
|
||||||
|
return (target);
|
||||||
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/20 16:41:49 by fgras-ca #+# #+# */
|
/* 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 "AForm.hpp"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#define RESET "\033[0m"
|
||||||
|
#define GREEN "\033[32m"
|
||||||
|
#define CYAN "\033[36m"
|
||||||
|
#define RED "\033[31m"
|
||||||
class ShrubberyCreationForm : public AForm
|
class ShrubberyCreationForm : public AForm
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
std::string target;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ShrubberyCreationForm(const std::string& target);
|
ShrubberyCreationForm(const std::string& target);
|
||||||
ShrubberyCreationForm(const ShrubberyCreationForm& other);
|
ShrubberyCreationForm(const ShrubberyCreationForm& other);
|
||||||
@ -25,6 +33,7 @@ public:
|
|||||||
virtual ~ShrubberyCreationForm();
|
virtual ~ShrubberyCreationForm();
|
||||||
|
|
||||||
void execute(Bureaucrat const &executor) const override;
|
void execute(Bureaucrat const &executor) const override;
|
||||||
|
std::string getTarget() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -6,77 +6,44 @@
|
|||||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/11 14:55:46 by fgras-ca #+# #+# */
|
/* 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 "Bureaucrat.hpp"
|
||||||
#include "Form.hpp"
|
#include "ShrubberyCreationForm.hpp"
|
||||||
|
#include "RobotomyRequestForm.hpp"
|
||||||
|
#include "PresidentialPardonForm.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
try
|
Bureaucrat john("John", 2);
|
||||||
{
|
Bureaucrat doe("Doe", 150);
|
||||||
// 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);
|
|
||||||
|
|
||||||
// Création de formulaires
|
ShrubberyCreationForm shrub("Home");
|
||||||
Form highRequirementForm("HighRequirement", 1, 1); // Exige un grade très élevé pour signer
|
RobotomyRequestForm robot("Robot");
|
||||||
Form lowRequirementForm("LowRequirement", 150, 150); // Peut être signé par n'importe quel bureaucrate
|
PresidentialPardonForm pardon("Criminal");
|
||||||
Form formHigh("HighForm", 2, 5);
|
|
||||||
|
|
||||||
// Tentative de signature des formulaires
|
try {
|
||||||
std::cout << CYAN << "Tentative de signature par HighRank:" << RESET << std::endl;
|
john.signForm(shrub);
|
||||||
highRankBureaucrat.signForm(highRequirementForm);
|
john.executeForm(shrub);
|
||||||
highRankBureaucrat.signForm(lowRequirementForm);
|
|
||||||
|
|
||||||
std::cout << CYAN << "\nTentative de signature par LowRank:" << RESET << std::endl;
|
john.signForm(robot);
|
||||||
lowRankBureaucrat.signForm(highRequirementForm);
|
john.executeForm(robot);
|
||||||
lowRankBureaucrat.signForm(lowRequirementForm);
|
|
||||||
|
|
||||||
std::cout << GREEN << "\nTenative de signature formHight par chief: " << RESET << std::endl;
|
john.signForm(pardon);
|
||||||
chief.signForm(formHigh);
|
john.executeForm(pardon);
|
||||||
chief.signForm(highRequirementForm);
|
|
||||||
chief.signForm(lowRequirementForm);
|
|
||||||
|
|
||||||
std::cout << GREEN << "\nTenative de signature formHight par intern: " << RESET << std::endl;
|
// Tester avec un bureaucrate de grade insuffisant
|
||||||
intern.signForm(formHigh);
|
doe.signForm(shrub);
|
||||||
intern.signForm(highRequirementForm);
|
doe.executeForm(shrub);
|
||||||
intern.signForm(lowRequirementForm);
|
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user