From 04480a56eb3eb69c41174df711e66ca19f87374e Mon Sep 17 00:00:00 2001 From: Ladebeze66 Date: Sun, 11 Feb 2024 18:36:44 +0100 Subject: [PATCH] cd --- cpp05/.vscode/c_cpp_properties.json | 18 ++++++ cpp05/.vscode/launch.json | 24 ++++++++ cpp05/.vscode/settings.json | 62 +++++++++++++++++++ cpp05/ex00/Bureaucrat.cpp | 60 ++++++++++++++++++ cpp05/ex00/Bureaucrat.hpp | 69 +++++++++++++++++++++ cpp05/ex00/Makefile | 59 ++++++++++++++++++ cpp05/ex00/introduction.txt | 94 +++++++++++++++++++++++++++++ cpp05/ex00/main.cpp | 43 +++++++++++++ cpp05/ex01/Bureaucrat.cpp | 73 ++++++++++++++++++++++ cpp05/ex01/Bureaucrat.hpp | 71 ++++++++++++++++++++++ cpp05/ex01/Form.cpp | 87 ++++++++++++++++++++++++++ cpp05/ex01/Form.hpp | 72 ++++++++++++++++++++++ cpp05/ex01/Makefile | 60 ++++++++++++++++++ cpp05/ex01/main.cpp | 82 +++++++++++++++++++++++++ cpp05/ex02/Bureaucrat.cpp | 73 ++++++++++++++++++++++ cpp05/ex02/Bureaucrat.hpp | 71 ++++++++++++++++++++++ cpp05/ex02/Form.cpp | 68 +++++++++++++++++++++ cpp05/ex02/Form.hpp | 60 ++++++++++++++++++ cpp05/ex02/Makefile | 60 ++++++++++++++++++ cpp05/ex02/main.cpp | 82 +++++++++++++++++++++++++ 20 files changed, 1288 insertions(+) create mode 100644 cpp05/.vscode/c_cpp_properties.json create mode 100644 cpp05/.vscode/launch.json create mode 100644 cpp05/.vscode/settings.json create mode 100644 cpp05/ex00/Bureaucrat.cpp create mode 100644 cpp05/ex00/Bureaucrat.hpp create mode 100644 cpp05/ex00/Makefile create mode 100644 cpp05/ex00/introduction.txt create mode 100644 cpp05/ex00/main.cpp create mode 100644 cpp05/ex01/Bureaucrat.cpp create mode 100644 cpp05/ex01/Bureaucrat.hpp create mode 100644 cpp05/ex01/Form.cpp create mode 100644 cpp05/ex01/Form.hpp create mode 100644 cpp05/ex01/Makefile create mode 100644 cpp05/ex01/main.cpp create mode 100644 cpp05/ex02/Bureaucrat.cpp create mode 100644 cpp05/ex02/Bureaucrat.hpp create mode 100644 cpp05/ex02/Form.cpp create mode 100644 cpp05/ex02/Form.hpp create mode 100644 cpp05/ex02/Makefile create mode 100644 cpp05/ex02/main.cpp diff --git a/cpp05/.vscode/c_cpp_properties.json b/cpp05/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp05/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp05/.vscode/launch.json b/cpp05/.vscode/launch.json new file mode 100644 index 0000000..c3fd401 --- /dev/null +++ b/cpp05/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/CPP/cpp05", + "program": "/home/fgras-ca/Bureau/CPP/cpp05/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp05/.vscode/settings.json b/cpp05/.vscode/settings.json new file mode 100644 index 0000000..f6aaca9 --- /dev/null +++ b/cpp05/.vscode/settings.json @@ -0,0 +1,62 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/cpp05/ex00/Bureaucrat.cpp b/cpp05/ex00/Bureaucrat.cpp new file mode 100644 index 0000000..d67aa95 --- /dev/null +++ b/cpp05/ex00/Bureaucrat.cpp @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */ +/* Updated: 2024/02/11 13:58:43 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.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; +} + +std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat) +{ + os << bureaucrat.getName() << MAGENTA << ", bureaucrat grade " << RESET << bureaucrat.getGrade(); + return (os); +} diff --git a/cpp05/ex00/Bureaucrat.hpp b/cpp05/ex00/Bureaucrat.hpp new file mode 100644 index 0000000..1fc5d19 --- /dev/null +++ b/cpp05/ex00/Bureaucrat.hpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */ +/* Updated: 2024/02/11 14:01:58 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUREAUCRAT_HPP +#define BUREAUCRAT_HPP + +#include +#include +#include + +#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(); +}; + +std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat); + +#endif diff --git a/cpp05/ex00/Makefile b/cpp05/ex00/Makefile new file mode 100644 index 0000000..af741b7 --- /dev/null +++ b/cpp05/ex00/Makefile @@ -0,0 +1,59 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/02/11 13:37:27 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 = Bureaucrat + +SRC = Bureaucrat.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 "./Bureaucrat" 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 \ No newline at end of file diff --git a/cpp05/ex00/introduction.txt b/cpp05/ex00/introduction.txt new file mode 100644 index 0000000..92de9a0 --- /dev/null +++ b/cpp05/ex00/introduction.txt @@ -0,0 +1,94 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# introduction.txt :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/02/10 15:12:35 by fgras-ca #+# #+# # +# Updated: 2024/02/11 12:30:48 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +** Chapitre I +** Introduction +** Le C++ est un langage de programmation polyvalent créé par Bjarne Stroustrup +** comme une extension du langage de programmation C, ou "C avec Classes" +** (source : Wikipedia). +** L'objectif de ces modules est de vous introduire à la Programmation Orientée Objet. +** Ce sera le point de départ de votre voyage en C++. De nombreux langages sont recommandés +** pour apprendre la POO. Nous avons décidé de choisir le C++ puisqu'il est dérivé +** de votre vieux ami le C. +** Comme il s'agit d'un langage complexe, et afin de garder les choses simples, +** votre code sera conforme à la norme C++98. +** Nous sommes conscients que le C++ moderne est très différent sous de nombreux aspects. +** Donc, si vous voulez devenir un développeur C++ compétent, c'est à vous d'aller +** plus loin après le 42 Core Commun ! +*/ + +** Chapitre II +** Règles générales +** Compilation +** • Compilez votre code avec c++ et les drapeaux -Wall -Wextra -Werror +** • Votre code doit encore compiler si vous ajoutez le drapeau -std=c++98 +** +** Formatage et conventions de nommage +** • Les répertoires d'exercices seront nommés de cette façon : ex00, ex01, ..., exn +** • Nommez vos fichiers, classes, fonctions, fonctions membres et attributs comme +** requis dans les directives. +** • Écrivez les noms des classes en format UpperCamelCase. Les fichiers contenant +** le code des classes seront toujours nommés selon le nom de la classe. Par exemple : +** ClassName.hpp/ClassName.h, ClassName.cpp, ou ClassName.tpp. Ainsi, si vous avez +** un fichier d'en-tête contenant la définition d'une classe "BrickWall" représentant +** un mur de briques, son nom sera BrickWall.hpp. +** • Sauf indication contraire, tous les messages de sortie doivent se terminer par un +** caractère de nouvelle ligne et être affichés sur la sortie standard. +** • Adieu Norminette ! Aucun style de codage n'est imposé dans les modules C++. +** Vous pouvez suivre celui que vous préférez. Mais gardez à l'esprit qu'un code que +** vos pairs évaluateurs ne peuvent pas comprendre est un code qu'ils ne peuvent pas +** noter. Faites de votre mieux pour écrire un code propre et lisible. +** +** Autorisé/Interdit +** Vous ne codez plus en C. Il est temps de passer au C++ ! Par conséquent : +** • Vous êtes autorisé à utiliser presque tout de la bibliothèque standard. Ainsi, +** au lieu de vous en tenir à ce que vous connaissez déjà, il serait intelligent +** d'utiliser autant que possible les versions C++ des fonctions C auxquelles vous +** êtes habitué. +** • Cependant, vous ne pouvez pas utiliser d'autres bibliothèques externes. Cela +** signifie que les bibliothèques C++11 (et formes dérivées) et Boost sont interdites. +** Les fonctions suivantes sont également interdites : *printf(), *alloc() et free(). +** Si vous les utilisez, votre note sera de 0 et c'est tout. Notez que, sauf mention +** explicite du contraire, l'utilisation des mots-clés using namespace et +** friend est interdite. Sinon, votre note sera de -42. +** • Vous êtes autorisé à utiliser la STL dans les modules 08 et 09 uniquement. Cela +** signifie : pas de Conteneurs (vector/list/map/etc.) et pas d'Algorithmes (tout ce +** qui nécessite d'inclure l'en-tête ) jusqu'à ce moment. Sinon, votre +** note sera de -42. +** +** Quelques exigences de conception +** • Les fuites de mémoire se produisent aussi en C++. Lorsque vous allouez de la +** mémoire (en utilisant le mot-clé new), vous devez éviter les fuites de mémoire. +** • Du module 02 au module 09, vos classes doivent être conçues selon la Forme +** Canonique Orthodoxe, sauf indication contraire explicite. +** • Toute implémentation de fonction mise dans un fichier d'en-tête (sauf pour les +** modèles de fonction) signifie 0 pour l'exercice. +** • Vous devriez être capable d'utiliser chacun de vos en-têtes indépendamment des +** autres. Ainsi, ils doivent inclure toutes les dépendances dont ils ont besoin. +** Cependant, vous devez éviter le problème de double inclusion en ajoutant des +** gardes d'inclusion. Sinon, votre note sera de 0. +** +** Lisez-moi +** • Vous pouvez ajouter des fichiers supplémentaires si vous en avez besoin (c'est-à-dire, +** pour diviser votre code). Comme ces devoirs ne sont pas vérifiés par un programme, +** n'hésitez pas à le faire tant que vous remettez les fichiers obligatoires. +** • Parfois, les directives d'un exercice semblent courtes mais les exemples peuvent +** montrer des exigences qui ne sont pas explicitement écrites dans les instructions. +** • Lisez chaque module complètement avant de commencer ! Vraiment, faites-le. +** • Par Odin, par Thor ! Utilisez votre cerveau !!! +** Vous devrez implémenter beaucoup de classes. Cela peut sembler fastidieux, +** à moins que vous ne soyez capable de scripter votre éditeur de texte préféré. +** Une certaine liberté vous est donnée pour compléter les exercices. +** Cependant, suivez les règles obligatoires et ne soyez pas paresseux. Vous +** manqueriez beaucoup d'informations utiles ! N'hésitez pas à lire sur les +** concepts théoriques. +*/ \ No newline at end of file diff --git a/cpp05/ex00/main.cpp b/cpp05/ex00/main.cpp new file mode 100644 index 0000000..9019a8f --- /dev/null +++ b/cpp05/ex00/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 13:28:49 by fgras-ca #+# #+# */ +/* Updated: 2024/02/11 14:08:26 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +int main() +{ + try + { + Bureaucrat bob("Bob", 2); + std::cout << bob << std::endl; + bob.incrementGrade(); + std::cout << bob << std::endl; + bob.incrementGrade(); // Devrait lancer une exception + } + catch (const std::exception& e) + { + std::cerr<< RED << e.what() << RESET << std::endl; + } + + try + { + Bureaucrat jim("Jim", 149); + std::cout << jim << std::endl; + jim.decrementGrade(); + std::cout << jim << std::endl; + jim.decrementGrade(); // Devrait lancer une exception + } + catch (const std::exception& e) + { + std::cerr << RED << e.what() << RESET << std::endl; + } + return (0); +} diff --git a/cpp05/ex01/Bureaucrat.cpp b/cpp05/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..b6f86a0 --- /dev/null +++ b/cpp05/ex01/Bureaucrat.cpp @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/cpp05/ex01/Bureaucrat.hpp b/cpp05/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..350c481 --- /dev/null +++ b/cpp05/ex01/Bureaucrat.hpp @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +#include + +#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 diff --git a/cpp05/ex01/Form.cpp b/cpp05/ex01/Form.cpp new file mode 100644 index 0000000..505f113 --- /dev/null +++ b/cpp05/ex01/Form.cpp @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */ +/* Updated: 2024/02/11 16:05:55 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "Form.hpp" + +Form::Form(const std::string& name) + : name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1) +{ +} + +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(); + } +} + +Form::Form(const Form& other) + : name(other.name), isSigned(other.isSigned), gradeRequiredToSign(other.gradeRequiredToSign), gradeRequiredToExecute(other.gradeRequiredToExecute) +{ +} +Form& Form::operator=(const Form& other) +{ + (void)other; + return (*this); +} + +Form::~Form() +{ +} + +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; +} diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp new file mode 100644 index 0000000..364d6b5 --- /dev/null +++ b/cpp05/ex01/Form.hpp @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */ +/* Updated: 2024/02/11 18:03:35 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORM_HPP +#define FORM_HPP + +#include "Bureaucrat.hpp" // Assurez-vous que cette classe est bien définie +#include +#include + +class Bureaucrat; + +class AForm +{ +private: + const std::string name; + const + bool isSigned; + const int gradeRequiredToSign; + const int gradeRequiredToExecute; + +public: + //constructueur + AForm(const::std::string& name); + 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 + ~AForm(); + + + 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"); + } + }; +}; + +// Surcharge de l'opérateur d'insertion +std::ostream& operator<<(std::ostream& os, const AForm& form); + +#endif diff --git a/cpp05/ex01/Makefile b/cpp05/ex01/Makefile new file mode 100644 index 0000000..d41a303 --- /dev/null +++ b/cpp05/ex01/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 \ No newline at end of file diff --git a/cpp05/ex01/main.cpp b/cpp05/ex01/main.cpp new file mode 100644 index 0000000..bbb68bf --- /dev/null +++ b/cpp05/ex01/main.cpp @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/cpp05/ex02/Bureaucrat.cpp b/cpp05/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..b6f86a0 --- /dev/null +++ b/cpp05/ex02/Bureaucrat.cpp @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/cpp05/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..350c481 --- /dev/null +++ b/cpp05/ex02/Bureaucrat.hpp @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +#include + +#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 diff --git a/cpp05/ex02/Form.cpp b/cpp05/ex02/Form.cpp new file mode 100644 index 0000000..a632799 --- /dev/null +++ b/cpp05/ex02/Form.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} diff --git a/cpp05/ex02/Form.hpp b/cpp05/ex02/Form.hpp new file mode 100644 index 0000000..c2c1a20 --- /dev/null +++ b/cpp05/ex02/Form.hpp @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +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 diff --git a/cpp05/ex02/Makefile b/cpp05/ex02/Makefile new file mode 100644 index 0000000..d41a303 --- /dev/null +++ b/cpp05/ex02/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 \ No newline at end of file diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp new file mode 100644 index 0000000..bbb68bf --- /dev/null +++ b/cpp05/ex02/main.cpp @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}