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

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 << " couldn’t sign " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because " << e.what() << std::endl;
std::cout << RED << this->name << " couldn’t 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 << " couldn’t execute " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because: " << e.what() << std::endl;
std::cout << RED << name << " couldn’t 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 */
/* */
/* ************************************************************************** */
@@ -62,15 +62,15 @@ 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);
}