This commit is contained in:
2024-02-11 18:36:44 +01:00
commit 04480a56eb
20 changed files with 1288 additions and 0 deletions

73
cpp05/ex02/Bureaucrat.cpp Normal file
View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:50:15 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
Bureaucrat::Bureaucrat(const std::string &nom, int grade) : name(nom)
{
if (grade < 1)
{
throw GradeTooHighException();
}
else if (grade > 150)
{
throw GradeTooLowException();
}
this->grade = grade;
}
std::string Bureaucrat::getName() const
{
return (name);
}
int Bureaucrat::getGrade() const
{
return (grade);
}
void Bureaucrat::incrementGrade()
{
if (grade <= 1)
{
throw GradeTooHighException();
}
--grade;
}
void Bureaucrat::decrementGrade()
{
if (grade >= 150)
{
throw GradeTooLowException();
}
++grade;
}
void Bureaucrat::signForm(Form& form)
{
try
{
form.beSigned(*this);
std::cout << this->name << " signed " << form.getName() << std::endl;
} catch (std::exception& e)
{
std::cout << this->name << " couldn’t sign " << form.getName() << " because " << e.what() << std::endl;
}
}
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
os << bureaucrat.getName() << MAGENTA << ", bureaucrat grade " << RESET << bureaucrat.getGrade();
return (os);
}

71
cpp05/ex02/Bureaucrat.hpp Normal file
View File

@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 14:59:51 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUREAUCRAT_HPP
#define BUREAUCRAT_HPP
#include "Form.hpp"
#include <string>
#include <exception>
#include <iostream>
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
class Bureaucrat
{
public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const throw() override
{
return ("Grade trop élevé");
}
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override
{
return ("Grade trop bas");
}
};
private:
const std::string name;
int grade;
public:
Bureaucrat(const std::string &nom, int grade); // Constructeur
Bureaucrat(const Bureaucrat& other) = default; // Constructeur par copie
Bureaucrat& operator=(const Bureaucrat& other) = default; // Opérateur d'affectation
~Bureaucrat() = default; // Destructeur
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(Form& form);
};
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat);
#endif

68
cpp05/ex02/Form.cpp Normal file
View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 15:03:38 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
Form::Form(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();
}
}
std::string Form::getName() const
{
return (name);
}
bool Form::getIsSigned() const
{
return (isSigned);
}
int Form::getGradeRequiredToSign() const
{
return (gradeRequiredToSign);
}
int Form::getGradeRequiredToExecute() const
{
return (gradeRequiredToExecute);
}
void Form::beSigned(const Bureaucrat& bureaucrat)
{
if (bureaucrat.getGrade() <= gradeRequiredToSign)
{
isSigned = true;
}
else
{
throw GradeTooLowException();
}
}
std::ostream& operator<<(std::ostream& os, const Form& form)
{
os << "Form: " << form.getName()
<< ", Status: " << (form.getIsSigned() ? "Signed" : "Not signed")
<< ", Grade Required to Sign: " << form.getGradeRequiredToSign()
<< ", Grade Required to Execute: " << form.getGradeRequiredToExecute();
return os;
}

60
cpp05/ex02/Form.hpp Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/11 15:05:06 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FORM_HPP
#define FORM_HPP
#include "Bureaucrat.hpp" // Assurez-vous que cette classe est bien définie
#include <string>
#include <iostream>
class Bureaucrat;
class Form
{
private:
const std::string name;
bool isSigned;
const int gradeRequiredToSign;
const int gradeRequiredToExecute;
public:
Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
std::string getName() const;
bool getIsSigned() const;
int getGradeRequiredToSign() const;
int getGradeRequiredToExecute() const;
void beSigned(const Bureaucrat& bureaucrat);
class GradeTooHighException : public std::exception
{
public:
const char* what() const noexcept override
{
return ("Grade too high");
}
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const noexcept override
{
return ("Grade too low");
}
};
};
std::ostream& operator<<(std::ostream& os, const Form& form);
#endif

60
cpp05/ex02/Makefile Normal file
View File

@@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/11 14:58:41 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 \
Form.cpp \
main.cpp \
OBJS = ${SRC:.cpp=.o}
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
all : $(NAME)
$(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

82
cpp05/ex02/main.cpp Normal file
View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
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);
// 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);
// Tentative de signature des formulaires
std::cout << CYAN << "Tentative de signature par HighRank:" << RESET << std::endl;
highRankBureaucrat.signForm(highRequirementForm);
highRankBureaucrat.signForm(lowRequirementForm);
std::cout << CYAN << "\nTentative de signature par LowRank:" << RESET << std::endl;
lowRankBureaucrat.signForm(highRequirementForm);
lowRankBureaucrat.signForm(lowRequirementForm);
std::cout << GREEN << "\nTenative de signature formHight par chief: " << RESET << std::endl;
chief.signForm(formHigh);
chief.signForm(highRequirementForm);
chief.signForm(lowRequirementForm);
std::cout << GREEN << "\nTenative de signature formHight par intern: " << RESET << std::endl;
intern.signForm(formHigh);
intern.signForm(highRequirementForm);
intern.signForm(lowRequirementForm);
}
catch (std::exception& e)
{
std::cerr << RED << "Une exception a été capturée: " << RESET << 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);
}