diff --git a/CPP05/ex01/Bureaucrat.cpp b/CPP05/ex01/Bureaucrat.cpp index 846cdaf..7d9e805 100644 --- a/CPP05/ex01/Bureaucrat.cpp +++ b/CPP05/ex01/Bureaucrat.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ -/* Updated: 2022/06/26 15:59:50 by narnaud ### ########.fr */ +/* Updated: 2022/06/27 09:13:57 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { } Bureaucrat::~Bureaucrat (void) { - cout << "Bureaucrat default destructor called << endl; + cout << "Bureaucrat default destructor called" << endl; } const string Bureaucrat::getName(void) const{ @@ -74,8 +74,18 @@ const char* Bureaucrat::GradeTooLowException::what(void) const throw() { return ("Grade was too low for a bureaucrat"); } +void Bureaucrat::signForm(Form &form) const { + try { + if (!form.beSigned(*this)) + cout << _name << " succesfully signed " << form.getName() << " form." << endl; + else + cout << _name << " failed to sign " << form.getName() << ", form is already signed." << endl; + } catch (std::exception &e) { + cout << _name << "couldn't sign form because his grade is too low" << endl; + } +} + std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; return (out); } - diff --git a/CPP05/ex01/Bureaucrat.hpp b/CPP05/ex01/Bureaucrat.hpp index 816290c..76527b9 100644 --- a/CPP05/ex01/Bureaucrat.hpp +++ b/CPP05/ex01/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ -/* Updated: 2022/06/26 16:35:38 by narnaud ### ########.fr */ +/* Updated: 2022/06/27 09:36:27 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,18 +20,18 @@ using std::cout; using std::endl; using std::string; +class Form; + class Bureaucrat{ const string _name; int _grade; public: - class GradeTooHighException: virtual public std::exception - { + class GradeTooHighException: virtual public std::exception { public: const char *what(void) const throw (); }; - class GradeTooLowException: virtual public std::exception - { + class GradeTooLowException: virtual public std::exception { public: const char *what(void) const throw (); }; @@ -45,11 +45,9 @@ class Bureaucrat{ const string getName(void) const; int getGrade(void) const; - void incrGrade(int diff = 1) - throw (GradeTooHighException); - void decrGrade(int diff = 1) - throw (GradeTooLowException); - + void incrGrade(int diff = 1) throw (GradeTooHighException); + void decrGrade(int diff = 1) throw (GradeTooLowException); + void signForm(Form &form) const; }; std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex01/Form.cpp b/CPP05/ex01/Form.cpp index 01584b9..8d083ec 100644 --- a/CPP05/ex01/Form.cpp +++ b/CPP05/ex01/Form.cpp @@ -6,32 +6,77 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ -/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */ +/* Updated: 2022/06/27 09:40:11 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "Form.hpp" -Form::Form(const string name, const int signGrade, const int exeGrade) +std::ostream &operator<< (std::ostream &out, const Form &f) { + out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade() << endl; + return (out); +}; + +/* Defaults */ + +Form & Form::operator=(Form const &f) { + (void)f; + cout << "Form assignement called" << endl; + return (*this); +} + +Form::Form(Form const &f) { + (void)f; + cout << "Form copy called" << endl; +} + +Form::~Form(void) { + cout << "Form desctructor called" << endl; +} + +/* Custom constructor */ + +Form::Form(const string name, int signGrade, int exeGrade) throw (Form::GradeTooHighException, Form::GradeTooLowException): _name(name){ if (signGrade < 1 || exeGrade < 1) - throw Form::GradeTooHighException; + throw Form::GradeTooHighException(); if (signGrade > 150 || exeGrade > 150) - throw Form::GradeTooLowException; + throw Form::GradeTooLowException(); _signGrade = signGrade; _exeGrade = exeGrade; } +/* Getters */ + const string Form::getName(void) const { return (_name); } -const int Form::getSignGrade(void) const { +int Form::getSignGrade(void) const { return (_signGrade); } -const int Form::getExeGrade(void) const { +int Form::getExeGrade(void) const { return (_exeGrade); } +/* Seter */ + +bool Form::beSigned(const Bureaucrat &b) throw (Form::GradeTooLowException) { + if (_isSigned) + return (1); + if (b.getGrade() > _signGrade) + throw GradeTooLowException(); + _isSigned = 1; + return (0); +} + +/* Except */ + +const char *Form::GradeTooHighException::what(void) const throw (){ + return ("Grade was too high for a form"); +} +const char *Form::GradeTooLowException::what(void) const throw (){ + return ("Grade was too low for a form"); +} diff --git a/CPP05/ex01/Form.hpp b/CPP05/ex01/Form.hpp index d6a5ee9..74b7d32 100644 --- a/CPP05/ex01/Form.hpp +++ b/CPP05/ex01/Form.hpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ -/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */ +/* Updated: 2022/06/27 09:36:10 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,33 +20,32 @@ using std::cout; using std::endl; using std::string; +class Bureaucrat; + class Form { const string _name; - const int _signGrade; - const int _exeGrade; + int _signGrade; + int _exeGrade; bool _isSigned; public: class GradeTooHighException: virtual public std::exception { public: const char *what(void) const throw (); - } + }; class GradeTooLowException: virtual public std::exception { public: const char *what(void) const throw (); - } + }; Form(void); Form(const string name, const int signGrade,const int exeGrade) throw (GradeTooHighException, GradeTooLowException); Form & operator=(Form const &f); Form(Form const &f); - ~Form(); + ~Form(void); const string getName(void) const; - const int getSignGrade(void) const; - const int getExeGrade(void) const; - beSigned(Bureaucrat); -} + int getSignGrade(void) const; + int getExeGrade(void) const; + bool beSigned(const Bureaucrat &b) throw (GradeTooLowException); +}; -std::ostream &operator<< (std::ostream &out, const Form &f) { - out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade(); - return (out); -} +std::ostream &operator<< (std::ostream &out, const Form &f); diff --git a/CPP05/ex01/main.cpp b/CPP05/ex01/main.cpp index ef74330..13f971e 100644 --- a/CPP05/ex01/main.cpp +++ b/CPP05/ex01/main.cpp @@ -6,59 +6,25 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ -/* Updated: 2022/06/26 16:34:45 by narnaud ### ########.fr */ +/* Updated: 2022/06/27 09:40:50 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "Form.hpp" - - -void create_too_low(void) { - try { - Bureaucrat b("trash", 151); - } catch (std::exception &e) { - cout << "Grade 151 creation: " << e.what() << endl; - } -} - -void create_too_high(void) { - try { - Bureaucrat b("god", 0); - } catch (std::exception &e) { - cout << "Grade 0 creation: " << e.what() << endl; - } -} - -void first_evolve(Bureaucrat &b) { - try { - b.incrGrade(); - } catch (std::exception &e) { - cout << "Grade 1 increase: "<< e.what() << endl; - } -} - -void last_dismiss(Bureaucrat &b) { - try { - b.decrGrade(); - } catch (std::exception &e) { - cout << "Grade 150 decrease: "<< e.what() << endl; - } -} - +#include "Bureaucrat.hpp" int main(void) { - Bureaucrat first("first", 1); - Bureaucrat last("last", 150); + Bureaucrat first("first", 1); + Bureaucrat mid("midle", 75); + Bureaucrat last("last", 150); + Form garb("garbage", 75, 75); - cout << first; - cout << last; + cout << first << mid << last << garb; - create_too_low(); - create_too_high(); - first_evolve(first); - last_dismiss(last); + last.signForm(garb); + mid.signForm(garb); + first.signForm(garb); + - cout << first; - cout << last; } diff --git a/CPP05/ex02/Bureaucrat.cpp b/CPP05/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..984e0b6 --- /dev/null +++ b/CPP05/ex02/Bureaucrat.cpp @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ +/* Updated: 2022/06/27 15:03:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat (void) { + cout << "Bureaucrat default constructor called " << endl; +} + +Bureaucrat::Bureaucrat (const string name, int grade) + throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ + + if (grade < 1) + throw Bureaucrat::GradeTooHighException(); + if (grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = grade; + cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; +} + +Bureaucrat::Bureaucrat (Bureaucrat const & src) { + (void)src; + cout << "Bureaucrat copy constructor called" << endl; +} + +Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { + (void)src; + cout << "Bureaucrat assignment operator called" << endl; + return (*this); +} + +Bureaucrat::~Bureaucrat (void) { + cout << "Bureaucrat default destructor called" << endl; +} + +const string Bureaucrat::getName(void) const{ + return (_name); +} + +int Bureaucrat::getGrade(void) const{ + return (_grade); +} + +void Bureaucrat::incrGrade(int diff) + throw (Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; +} + +void Bureaucrat::decrGrade(int diff) + throw (Bureaucrat::GradeTooLowException) { + int new_grade = _grade + diff; + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = new_grade; +} + +const char* Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("Grade was too high for a bureaucrat"); +} + +const char* Bureaucrat::GradeTooLowException::what(void) const throw() { + return ("Grade was too low for a bureaucrat"); +} + +void Bureaucrat::signForm(Form &form) const { + try { + if (!form.beSigned(*this)) + cout << _name << " succesfully signed " << form.getName() << endl; + else + cout << _name << " failed to sign " << form.getName() << ": form is already signed." << endl; + } catch (std::exception &e) { + cout << _name << " failed to sign " << form.getName() << ": " << e.what() << endl; + } +} + +void Bureaucrat::executeForm(const Form &form) const { + try { + if (!form.execute(*this)) + cout << _name << " succesfully executed " << form.getName() << endl; + else + cout << _name << " failed to execute " << form.getName() << ": form is not signed" << endl; + } catch (std::exception &e) { + cout << _name << " failed to execute " << form.getName() << ": " << e.what() << endl; + } +} + +std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { + out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; + return (out); +} diff --git a/CPP05/ex02/Bureaucrat.hpp b/CPP05/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..5c7b86d --- /dev/null +++ b/CPP05/ex02/Bureaucrat.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:47:21 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Form; + +class Bureaucrat{ + const string _name; + int _grade; + public: + + class GradeTooHighException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + class GradeTooLowException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + + Bureaucrat(void); + Bureaucrat(const string name, int grade) + throw (GradeTooHighException, GradeTooLowException); + Bureaucrat(Bureaucrat const & src); + ~Bureaucrat(void); + Bureaucrat & operator= (Bureaucrat const & src); + + const string getName(void) const; + int getGrade(void) const; + void incrGrade(int diff = 1) throw (GradeTooHighException); + void decrGrade(int diff = 1) throw (GradeTooLowException); + void signForm(Form &form) const; + void executeForm(const Form &form) const; +}; + +std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex02/Form.cpp b/CPP05/ex02/Form.cpp new file mode 100644 index 0000000..685b409 --- /dev/null +++ b/CPP05/ex02/Form.cpp @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:56:54 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" + +std::ostream &operator<< (std::ostream &out, const Form &f) { + out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade() << endl; + return (out); +}; + +/* Defaults */ + +Form & Form::operator=(Form const &f) { + (void)f; + cout << "Form assignement called" << endl; + return (*this); +} + +Form::Form(Form const &f) + : _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade()) +{ + (void)f; + cout << "Form copy called" << endl; +} + +Form::~Form(void) { + cout << "Form destructor called" << endl; +} + +/* Custom constructor */ + +Form::Form(const string name, const int signGrade, const int exeGrade, const string target) + throw (Form::GradeTooHighException, Form::GradeTooLowException) + : _name(name), _signGrade(signGrade), _exeGrade(exeGrade), _target(target){ + if (signGrade < 1 || exeGrade < 1) + throw Form::GradeTooHighException(); + if (signGrade > 150 || exeGrade > 150) + throw Form::GradeTooLowException(); +} + +/* Getters */ + +const string Form::getName(void) const { + return (_name); +} + +int Form::getSignGrade(void) const { + return (_signGrade); +} + +int Form::getExeGrade(void) const { + return (_exeGrade); +} + +/* Seter */ + +int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) { + if (_isSigned) + return (-1); + if (signer.getGrade() > _signGrade) + throw GradeTooLowException(); + _isSigned = 1; + return (0); +} + +int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException) { + if (executor.getGrade() > _exeGrade) + throw Form::GradeTooLowException(); + if (!_isSigned) + return (-1); + this->run(); + return (0); +} + +/* Except */ + +const char *Form::GradeTooHighException::what(void) const throw (){ + return ("Grade was too high for a form"); +} + +const char *Form::GradeTooLowException::what(void) const throw (){ + return ("Grade was too low for a form"); +} diff --git a/CPP05/ex02/Form.hpp b/CPP05/ex02/Form.hpp new file mode 100644 index 0000000..37484a8 --- /dev/null +++ b/CPP05/ex02/Form.hpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:56:40 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Bureaucrat.hpp" +#include +#include +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Bureaucrat; + +class Form { + const string _name; + const int _signGrade; + const int _exeGrade; + bool _isSigned; + public: + class GradeTooHighException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + class GradeTooLowException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + virtual ~Form(void) = 0; + const string getName(void) const; + int getSignGrade(void) const; + int getExeGrade(void) const; + int beSigned(const Bureaucrat &signer) throw (GradeTooLowException); + int execute(Bureaucrat const &executor) const throw (GradeTooLowException); + virtual void run(void) const = 0; + protected: + Form(void); + Form(const string name, const int signGrade,const int exeGrade, const string target) + throw (GradeTooHighException, GradeTooLowException); + Form & operator=(Form const &f); + Form(Form const &f); + const string _target; +}; + +std::ostream &operator<< (std::ostream &out, const Form &f); diff --git a/CPP05/ex02/Makefile b/CPP05/ex02/Makefile new file mode 100644 index 0000000..bbad008 --- /dev/null +++ b/CPP05/ex02/Makefile @@ -0,0 +1,20 @@ +NAME = bureau +SRCS = main.cpp Bureaucrat.cpp Form.cpp ShrubberyCreationForm.cpp RobotomyRequestForm.cpp PresidentialPardonForm.cpp +OBJS= $(SRCS:.cpp=.o) + +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME) : $(OBJS) + c++ $(OBJS) -o $(NAME) + +all : $(NAME) + +clean : + rm -rf $(OBJS) + +fclean : clean + rm -rf $(NAME) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/CPP05/ex02/PresidentialPardonForm.cpp b/CPP05/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..a4752bf --- /dev/null +++ b/CPP05/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:43:58 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm (const string target): Form("PresidentialPardonForm", 25, 5, target) { + + cout << "PresidentialPardonForm parameter constructor called" << endl; +} + +PresidentialPardonForm::~PresidentialPardonForm (void) { + + cout << "PresidentialPardonForm default destructor called" << endl; +} + +void PresidentialPardonForm::run(void) const { + cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; +} diff --git a/CPP05/ex02/PresidentialPardonForm.hpp b/CPP05/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..ef52eff --- /dev/null +++ b/CPP05/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:42:14 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" + +class PresidentialPardonForm: public Form { + + public: + + PresidentialPardonForm(const string target); + ~PresidentialPardonForm(void); + void run(void) const; +}; diff --git a/CPP05/ex02/RobotomyRequestForm.cpp b/CPP05/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..3b664d8 --- /dev/null +++ b/CPP05/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:43:34 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RobotomyRequestForm.hpp" + +RobotomyRequestForm::RobotomyRequestForm (const string target): Form("RobotomyRequestForm", 72, 45, target) { + + cout << "RobotomyRequestForm parameter constructor called" << endl; +} + +RobotomyRequestForm::~RobotomyRequestForm (void) { + + cout << "RobotomyRequestForm default destructor called" << endl; +} + +void RobotomyRequestForm::run(void) const { + int r = rand(); + + cout << "ratatttaatata" << endl; + sleep(1); + cout << "ratata tatata" << endl; + sleep(1); + cout << "ratata" << endl; + sleep(1); + if (r % 2) + cout<< _target << " have been robotomized" << endl; + else + cout << _target << " robotomization failed" << endl; +} diff --git a/CPP05/ex02/RobotomyRequestForm.hpp b/CPP05/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..ada3db7 --- /dev/null +++ b/CPP05/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:45:20 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" + +class RobotomyRequestForm: public Form { + + public: + + RobotomyRequestForm(const string target); + ~RobotomyRequestForm(void); + void run(void) const; +}; diff --git a/CPP05/ex02/ShrubberyCreationForm.cpp b/CPP05/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..4abd714 --- /dev/null +++ b/CPP05/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:15 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:39:19 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" + +ShrubberyCreationForm::ShrubberyCreationForm (string target): Form("ShrubberyCreationForm", 147, 137, target) { + cout << "ShrubberyCreationForm parameter constructor called" << endl; +} + +ShrubberyCreationForm::~ShrubberyCreationForm (void) { + cout << "ShrubberyCreationForm default destructor called" << endl; +} + +void ShrubberyCreationForm::run(void) const { + std::ofstream file; + file.open(_target + "shrubbery"); + file << " ,@@@@@@@," << endl; + file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl; + file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl; + file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl; + file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl; + file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl; + file << " `&%\\ ` /%&' |.| \\ '|8'" << endl; + file << " |o| | | | |" << endl; + file << " |.| | | | |" << endl; + file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl; + file.close(); +} diff --git a/CPP05/ex02/ShrubberyCreationForm.hpp b/CPP05/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..508708a --- /dev/null +++ b/CPP05/ex02/ShrubberyCreationForm.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:14 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:40:37 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" +class ShrubberyCreationForm: public Form { + + public: + ShrubberyCreationForm(string target); + ~ShrubberyCreationForm(void); + void run(void) const; +}; diff --git a/CPP05/ex02/main.cpp b/CPP05/ex02/main.cpp new file mode 100644 index 0000000..12725ef --- /dev/null +++ b/CPP05/ex02/main.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:58:53 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "Bureaucrat.hpp" + +int main(void) +{ + Bureaucrat first("first", 1); + Bureaucrat mid("midle", 75); + Bureaucrat last("last", 150); + + ShrubberyCreationForm houseForm("House"); + PresidentialPardonForm gerardForm("Gerard"); + RobotomyRequestForm biduleForm("Bidule"); + last.signForm(houseForm); + last.signForm(gerardForm); + last.signForm(biduleForm); + first.signForm(houseForm); + first.signForm(gerardForm); + first.signForm(biduleForm); + last.executeForm(houseForm); + last.executeForm(gerardForm); + last.executeForm(biduleForm); + first.executeForm(houseForm); + first.executeForm(gerardForm); + first.executeForm(biduleForm); +} diff --git a/CPP05/ex03/Bureaucrat.cpp b/CPP05/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..984e0b6 --- /dev/null +++ b/CPP05/ex03/Bureaucrat.cpp @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ +/* Updated: 2022/06/27 15:03:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat (void) { + cout << "Bureaucrat default constructor called " << endl; +} + +Bureaucrat::Bureaucrat (const string name, int grade) + throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ + + if (grade < 1) + throw Bureaucrat::GradeTooHighException(); + if (grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = grade; + cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; +} + +Bureaucrat::Bureaucrat (Bureaucrat const & src) { + (void)src; + cout << "Bureaucrat copy constructor called" << endl; +} + +Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { + (void)src; + cout << "Bureaucrat assignment operator called" << endl; + return (*this); +} + +Bureaucrat::~Bureaucrat (void) { + cout << "Bureaucrat default destructor called" << endl; +} + +const string Bureaucrat::getName(void) const{ + return (_name); +} + +int Bureaucrat::getGrade(void) const{ + return (_grade); +} + +void Bureaucrat::incrGrade(int diff) + throw (Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; +} + +void Bureaucrat::decrGrade(int diff) + throw (Bureaucrat::GradeTooLowException) { + int new_grade = _grade + diff; + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = new_grade; +} + +const char* Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("Grade was too high for a bureaucrat"); +} + +const char* Bureaucrat::GradeTooLowException::what(void) const throw() { + return ("Grade was too low for a bureaucrat"); +} + +void Bureaucrat::signForm(Form &form) const { + try { + if (!form.beSigned(*this)) + cout << _name << " succesfully signed " << form.getName() << endl; + else + cout << _name << " failed to sign " << form.getName() << ": form is already signed." << endl; + } catch (std::exception &e) { + cout << _name << " failed to sign " << form.getName() << ": " << e.what() << endl; + } +} + +void Bureaucrat::executeForm(const Form &form) const { + try { + if (!form.execute(*this)) + cout << _name << " succesfully executed " << form.getName() << endl; + else + cout << _name << " failed to execute " << form.getName() << ": form is not signed" << endl; + } catch (std::exception &e) { + cout << _name << " failed to execute " << form.getName() << ": " << e.what() << endl; + } +} + +std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { + out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; + return (out); +} diff --git a/CPP05/ex03/Bureaucrat.hpp b/CPP05/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..5c7b86d --- /dev/null +++ b/CPP05/ex03/Bureaucrat.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:47:21 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Form; + +class Bureaucrat{ + const string _name; + int _grade; + public: + + class GradeTooHighException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + class GradeTooLowException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + + Bureaucrat(void); + Bureaucrat(const string name, int grade) + throw (GradeTooHighException, GradeTooLowException); + Bureaucrat(Bureaucrat const & src); + ~Bureaucrat(void); + Bureaucrat & operator= (Bureaucrat const & src); + + const string getName(void) const; + int getGrade(void) const; + void incrGrade(int diff = 1) throw (GradeTooHighException); + void decrGrade(int diff = 1) throw (GradeTooLowException); + void signForm(Form &form) const; + void executeForm(const Form &form) const; +}; + +std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex03/Bureaucrat.o b/CPP05/ex03/Bureaucrat.o new file mode 100644 index 0000000..7af8727 Binary files /dev/null and b/CPP05/ex03/Bureaucrat.o differ diff --git a/CPP05/ex03/Form.cpp b/CPP05/ex03/Form.cpp new file mode 100644 index 0000000..685b409 --- /dev/null +++ b/CPP05/ex03/Form.cpp @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:56:54 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" + +std::ostream &operator<< (std::ostream &out, const Form &f) { + out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade() << endl; + return (out); +}; + +/* Defaults */ + +Form & Form::operator=(Form const &f) { + (void)f; + cout << "Form assignement called" << endl; + return (*this); +} + +Form::Form(Form const &f) + : _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade()) +{ + (void)f; + cout << "Form copy called" << endl; +} + +Form::~Form(void) { + cout << "Form destructor called" << endl; +} + +/* Custom constructor */ + +Form::Form(const string name, const int signGrade, const int exeGrade, const string target) + throw (Form::GradeTooHighException, Form::GradeTooLowException) + : _name(name), _signGrade(signGrade), _exeGrade(exeGrade), _target(target){ + if (signGrade < 1 || exeGrade < 1) + throw Form::GradeTooHighException(); + if (signGrade > 150 || exeGrade > 150) + throw Form::GradeTooLowException(); +} + +/* Getters */ + +const string Form::getName(void) const { + return (_name); +} + +int Form::getSignGrade(void) const { + return (_signGrade); +} + +int Form::getExeGrade(void) const { + return (_exeGrade); +} + +/* Seter */ + +int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) { + if (_isSigned) + return (-1); + if (signer.getGrade() > _signGrade) + throw GradeTooLowException(); + _isSigned = 1; + return (0); +} + +int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException) { + if (executor.getGrade() > _exeGrade) + throw Form::GradeTooLowException(); + if (!_isSigned) + return (-1); + this->run(); + return (0); +} + +/* Except */ + +const char *Form::GradeTooHighException::what(void) const throw (){ + return ("Grade was too high for a form"); +} + +const char *Form::GradeTooLowException::what(void) const throw (){ + return ("Grade was too low for a form"); +} diff --git a/CPP05/ex03/Form.hpp b/CPP05/ex03/Form.hpp new file mode 100644 index 0000000..37484a8 --- /dev/null +++ b/CPP05/ex03/Form.hpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:56:40 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Bureaucrat.hpp" +#include +#include +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Bureaucrat; + +class Form { + const string _name; + const int _signGrade; + const int _exeGrade; + bool _isSigned; + public: + class GradeTooHighException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + class GradeTooLowException: virtual public std::exception { + public: + const char *what(void) const throw (); + }; + virtual ~Form(void) = 0; + const string getName(void) const; + int getSignGrade(void) const; + int getExeGrade(void) const; + int beSigned(const Bureaucrat &signer) throw (GradeTooLowException); + int execute(Bureaucrat const &executor) const throw (GradeTooLowException); + virtual void run(void) const = 0; + protected: + Form(void); + Form(const string name, const int signGrade,const int exeGrade, const string target) + throw (GradeTooHighException, GradeTooLowException); + Form & operator=(Form const &f); + Form(Form const &f); + const string _target; +}; + +std::ostream &operator<< (std::ostream &out, const Form &f); diff --git a/CPP05/ex03/Form.o b/CPP05/ex03/Form.o new file mode 100644 index 0000000..dafe23b Binary files /dev/null and b/CPP05/ex03/Form.o differ diff --git a/CPP05/ex03/Intern.cpp b/CPP05/ex03/Intern.cpp new file mode 100644 index 0000000..d1d6ce2 --- /dev/null +++ b/CPP05/ex03/Intern.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 15:07:10 by narnaud #+# #+# */ +/* Updated: 2022/06/27 16:28:56 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Intern.hpp" + +Intern::Intern (void) { + cout << "Intern default constructor called " << endl; +} + +Intern::Intern (Intern const & src) { + (void)src; + cout << "Intern copy constructor called" << endl; +} + +Intern & Intern::operator= (Intern const & src) { + (void)src; + cout << "Intern assignment operator called" << endl; + return (*this); +} + +Intern::~Intern (void) { + + cout << "Intern default destructor called" << endl; +} + +Form * createRobotForm(string targ){ + return (new RobotomyRequestForm(targ)); +} + +Form * createPresidentForm(string targ) { + return (new PresidentialPardonForm(targ)); +} + +Form * createShrubberyForm(string targ) { + return (new ShrubberyCreationForm(targ)); +} + +Form * Intern::makeForm(string name, string target) +{ + string subFormsName[3] = {"robotomy request", "president pardon", "shrubbery creation"}; + Form * (*subFormsCreation[3])(string targ) =\ + {createRobotForm, createPresidentForm, createShrubberyForm}; + for (int i = 0; i < 3; i++) + if (name == subFormsName[i]) + return (subFormsCreation[i](target)); + cout << "Intern fail to create " << name << ": unknow form name" << endl; + return (NULL); +} diff --git a/CPP05/ex03/Intern.hpp b/CPP05/ex03/Intern.hpp new file mode 100644 index 0000000..4c137e2 --- /dev/null +++ b/CPP05/ex03/Intern.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 15:07:10 by narnaud #+# #+# */ +/* Updated: 2022/06/27 16:28:32 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include + +#include +using std::cout; +using std::endl; +using std::string; + +class Intern{ + + public: + Intern(void); + Intern(Intern const & src); + virtual ~Intern(void); + Intern & operator= (Intern const & src); + Form * makeForm(string name, string target); + protected: +}; diff --git a/CPP05/ex03/Intern.o b/CPP05/ex03/Intern.o new file mode 100644 index 0000000..2322f5f Binary files /dev/null and b/CPP05/ex03/Intern.o differ diff --git a/CPP05/ex03/Makefile b/CPP05/ex03/Makefile new file mode 100644 index 0000000..fe92166 --- /dev/null +++ b/CPP05/ex03/Makefile @@ -0,0 +1,20 @@ +NAME = bureau +SRCS = main.cpp Bureaucrat.cpp Form.cpp ShrubberyCreationForm.cpp RobotomyRequestForm.cpp PresidentialPardonForm.cpp Intern.cpp +OBJS= $(SRCS:.cpp=.o) + +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME) : $(OBJS) + c++ $(OBJS) -o $(NAME) + +all : $(NAME) + +clean : + rm -rf $(OBJS) + +fclean : clean + rm -rf $(NAME) + +re : fclean all + +.PHONY : all clean fclean re diff --git a/CPP05/ex03/PresidentialPardonForm.cpp b/CPP05/ex03/PresidentialPardonForm.cpp new file mode 100644 index 0000000..a4752bf --- /dev/null +++ b/CPP05/ex03/PresidentialPardonForm.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:43:58 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm (const string target): Form("PresidentialPardonForm", 25, 5, target) { + + cout << "PresidentialPardonForm parameter constructor called" << endl; +} + +PresidentialPardonForm::~PresidentialPardonForm (void) { + + cout << "PresidentialPardonForm default destructor called" << endl; +} + +void PresidentialPardonForm::run(void) const { + cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; +} diff --git a/CPP05/ex03/PresidentialPardonForm.hpp b/CPP05/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..ef52eff --- /dev/null +++ b/CPP05/ex03/PresidentialPardonForm.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:42:14 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" + +class PresidentialPardonForm: public Form { + + public: + + PresidentialPardonForm(const string target); + ~PresidentialPardonForm(void); + void run(void) const; +}; diff --git a/CPP05/ex03/PresidentialPardonForm.o b/CPP05/ex03/PresidentialPardonForm.o new file mode 100644 index 0000000..c5bdb92 Binary files /dev/null and b/CPP05/ex03/PresidentialPardonForm.o differ diff --git a/CPP05/ex03/RobotomyRequestForm.cpp b/CPP05/ex03/RobotomyRequestForm.cpp new file mode 100644 index 0000000..3b664d8 --- /dev/null +++ b/CPP05/ex03/RobotomyRequestForm.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:43:34 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RobotomyRequestForm.hpp" + +RobotomyRequestForm::RobotomyRequestForm (const string target): Form("RobotomyRequestForm", 72, 45, target) { + + cout << "RobotomyRequestForm parameter constructor called" << endl; +} + +RobotomyRequestForm::~RobotomyRequestForm (void) { + + cout << "RobotomyRequestForm default destructor called" << endl; +} + +void RobotomyRequestForm::run(void) const { + int r = rand(); + + cout << "ratatttaatata" << endl; + sleep(1); + cout << "ratata tatata" << endl; + sleep(1); + cout << "ratata" << endl; + sleep(1); + if (r % 2) + cout<< _target << " have been robotomized" << endl; + else + cout << _target << " robotomization failed" << endl; +} diff --git a/CPP05/ex03/RobotomyRequestForm.hpp b/CPP05/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..ada3db7 --- /dev/null +++ b/CPP05/ex03/RobotomyRequestForm.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:45:20 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" + +class RobotomyRequestForm: public Form { + + public: + + RobotomyRequestForm(const string target); + ~RobotomyRequestForm(void); + void run(void) const; +}; diff --git a/CPP05/ex03/RobotomyRequestForm.o b/CPP05/ex03/RobotomyRequestForm.o new file mode 100644 index 0000000..810f3fc Binary files /dev/null and b/CPP05/ex03/RobotomyRequestForm.o differ diff --git a/CPP05/ex03/ShrubberyCreationForm.cpp b/CPP05/ex03/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..dc78aed --- /dev/null +++ b/CPP05/ex03/ShrubberyCreationForm.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:15 by narnaud #+# #+# */ +/* Updated: 2022/06/27 16:36:13 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" + +ShrubberyCreationForm::ShrubberyCreationForm (string target): Form("ShrubberyCreationForm", 147, 137, target) { + cout << "ShrubberyCreationForm parameter constructor called" << endl; +} + +ShrubberyCreationForm::~ShrubberyCreationForm (void) { + cout << "ShrubberyCreationForm default destructor called" << endl; +} + +void ShrubberyCreationForm::run(void) const { + std::ofstream file; + file.open(_target + "_shrubbery"); + file << " ,@@@@@@@," << endl; + file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl; + file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl; + file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl; + file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl; + file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl; + file << " `&%\\ ` /%&' |.| \\ '|8'" << endl; + file << " |o| | | | |" << endl; + file << " |.| | | | |" << endl; + file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl; + file.close(); +} diff --git a/CPP05/ex03/ShrubberyCreationForm.hpp b/CPP05/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..508708a --- /dev/null +++ b/CPP05/ex03/ShrubberyCreationForm.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/27 10:17:14 by narnaud #+# #+# */ +/* Updated: 2022/06/27 14:40:37 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Form.hpp" +class ShrubberyCreationForm: public Form { + + public: + ShrubberyCreationForm(string target); + ~ShrubberyCreationForm(void); + void run(void) const; +}; diff --git a/CPP05/ex03/ShrubberyCreationForm.o b/CPP05/ex03/ShrubberyCreationForm.o new file mode 100644 index 0000000..eb0e4b7 Binary files /dev/null and b/CPP05/ex03/ShrubberyCreationForm.o differ diff --git a/CPP05/ex03/bureau b/CPP05/ex03/bureau new file mode 100755 index 0000000..d91abf4 Binary files /dev/null and b/CPP05/ex03/bureau differ diff --git a/CPP05/ex03/house_shrubbery b/CPP05/ex03/house_shrubbery new file mode 100644 index 0000000..94ddd83 --- /dev/null +++ b/CPP05/ex03/house_shrubbery @@ -0,0 +1,10 @@ + ,@@@@@@@, + ,,,. ,@@@@@@/@@, .oo8888o. + ,&%%&%&&%,@@@@@/@@@@@@,8888\88/8o + ,%&\%&&%&&%,@@@\@@@/@@@88\88888/88' + %&&%&%&/%&&%@@\@@/ /@@@88888\88888' + %&&%/ %&%%&&@@\ V /@@' `88\8 `/88' + `&%\ ` /%&' |.| \ '|8' + |o| | | | | + |.| | | | | + \\/ ._\//_/__/ ,\_//__\\/. \_//__/_ diff --git a/CPP05/ex03/main.cpp b/CPP05/ex03/main.cpp new file mode 100644 index 0000000..6a0bc0f --- /dev/null +++ b/CPP05/ex03/main.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ +/* Updated: 2022/06/27 16:34:59 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "Bureaucrat.hpp" +#include "Intern.hpp" + +int main(void) +{ + Bureaucrat first("first", 1); + Bureaucrat mid("midle", 75); + Bureaucrat last("last", 150); + Intern rookie; + Form *form; + + form = rookie.makeForm("shrubbery creation", "house"); + last.signForm(*form); + first.signForm(*form); + last.executeForm(*form); + first.executeForm(*form); + delete form; + + form = rookie.makeForm("robotomy request", "bidule"); + last.signForm(*form); + first.signForm(*form); + last.executeForm(*form); + first.executeForm(*form); + delete form; + + form = rookie.makeForm("president pardon", "gerard"); + last.signForm(*form); + first.signForm(*form); + last.executeForm(*form); + first.executeForm(*form); + delete form; +} diff --git a/CPP05/ex03/main.o b/CPP05/ex03/main.o new file mode 100644 index 0000000..a287cb2 Binary files /dev/null and b/CPP05/ex03/main.o differ diff --git a/CPP06/.main.cpp.swp b/CPP06/.main.cpp.swp new file mode 100644 index 0000000..d52c657 Binary files /dev/null and b/CPP06/.main.cpp.swp differ diff --git a/CPP06/ex00/Converter.cpp b/CPP06/ex00/Converter.cpp new file mode 100644 index 0000000..254d4c3 --- /dev/null +++ b/CPP06/ex00/Converter.cpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Converter.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ +/* Updated: 2022/07/12 08:42:27 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Converter.hpp" + +Converter::Converter (void) { + cout << "Converter default constructor called " << endl; +} + +Converter::Converter (string str):_input(str){ + startConv(); + cout << "Converter parameter constructor called" << endl; +} + +Converter::Converter (Converter const & src) { + (void)src; + cout << "Converter copy constructor called" << endl; +} + +Converter & Converter::operator= (Converter const & src) { + (void)src; + cout << "Converter assignment operator called" << endl; + return (*this); +} + +Converter::~Converter (void) { + + cout << "Converter default destructor called" << endl; +} + + +void Converter::startConv(void) { + if (_input[0] == '\'' || _input[0] == '"') + { + _c = _input[1]; + _d = static_cast(_c); + } + else + { + _d = std::strtod(_input.c_str(), 0); + _c = static_cast(_d); + } + _i = static_cast(_d); + _f = static_cast(_d); +} + +void Converter::display(void) { + cout << "Input: " << _input << endl; + cout << "Char: " << _c << endl; + cout << "Double: " << _d << endl; + cout << "Float: " << _f << endl; + cout << "Integer: " << _i << endl; +} diff --git a/CPP06/ex00/Converter.hpp b/CPP06/ex00/Converter.hpp new file mode 100644 index 0000000..3898296 --- /dev/null +++ b/CPP06/ex00/Converter.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Converter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ +/* Updated: 2022/07/12 08:46:08 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +using std::cout; +using std::endl; +using std::string; + +class Converter{ + + string _input; + char _c; + int _i; + float _f; + double _d; + int _types[4]; + public: + Converter(void); + Converter(string str); + Converter(Converter const & src); + virtual ~Converter(void); + Converter & operator= (Converter const & src); + void startConv(void); + void display(void); +}; diff --git a/CPP06/ex00/Converter.o b/CPP06/ex00/Converter.o new file mode 100644 index 0000000..ea72cfe Binary files /dev/null and b/CPP06/ex00/Converter.o differ diff --git a/CPP06/ex00/Makefile b/CPP06/ex00/Makefile new file mode 100644 index 0000000..eed92f9 --- /dev/null +++ b/CPP06/ex00/Makefile @@ -0,0 +1,21 @@ +NAME = convert +SRCS = main.cpp Converter.cpp +OBJS= $(SRCS:.cpp=.o) + +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME) : $(OBJS) + c++ $(OBJS) -o $(NAME) + +all : $(NAME) + +clean : + rm -rf $(OBJS) + +fclean : clean + rm -rf $(NAME) + +re : fclean all + +.PHONY : all clean fclean re + diff --git a/CPP06/ex00/convert b/CPP06/ex00/convert new file mode 100755 index 0000000..da02f22 Binary files /dev/null and b/CPP06/ex00/convert differ diff --git a/CPP06/ex00/main.cpp b/CPP06/ex00/main.cpp new file mode 100644 index 0000000..7de8477 --- /dev/null +++ b/CPP06/ex00/main.cpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/28 08:44:43 by narnaud #+# #+# */ +/* Updated: 2022/07/12 08:09:02 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "Converter.hpp" + +using std::cout; +using std::endl; +using std::string; + +int main(int ac, char **av) { + + if (ac != 2) + return (EXIT_FAILURE); + string input = av[1]; + Converter raw(input); + raw.display(); + +} diff --git a/CPP06/ex00/main.o b/CPP06/ex00/main.o new file mode 100644 index 0000000..2505a0b Binary files /dev/null and b/CPP06/ex00/main.o differ