diff --git a/CPP05/ex00/Bureaucrat.cpp b/CPP05/ex00/Bureaucrat.cpp index c30b527..53ef85c 100644 --- a/CPP05/ex00/Bureaucrat.cpp +++ b/CPP05/ex00/Bureaucrat.cpp @@ -12,70 +12,69 @@ #include "Bureaucrat.hpp" -Bureaucrat::Bureaucrat (void) { - cout << "Bureaucrat default constructor called " << endl; -} +/* Constructors */ -Bureaucrat::Bureaucrat (const string name, int grade) - throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ +Bureaucrat::Bureaucrat(void) {} - if (grade < 1) - throw Bureaucrat::GradeTooHighException(); - if (grade > 150) - throw Bureaucrat::GradeTooLowException(); - _grade = grade; - cout << "Bureaucrat " << _name << " created grade: " << _grade << 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; } -Bureaucrat::Bureaucrat (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat copy constructor called" << endl; -} +/* copy const. */ +Bureaucrat::Bureaucrat(Bureaucrat const &src) { (void)src; } -Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat assignment operator called" << endl; - return (*this); +/* assign const. */ +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) { + (void)src; + return (*this); } -Bureaucrat::~Bureaucrat (void) { - cout << "Bureaucrat default destructor called" << endl; -} +/* Destructor */ +Bureaucrat::~Bureaucrat(void) {} -const string Bureaucrat::getName(void) const{ - return (_name); -} +/* Getters */ -int Bureaucrat::getGrade(void) const{ - return (_grade); -} +const string Bureaucrat::getName(void) const { return (_name); } -void Bureaucrat::incrGrade(int diff) - throw (Bureaucrat::GradeTooHighException) { - int new_grade = _grade - diff; - if (new_grade < 1) - throw Bureaucrat::GradeTooHighException(); - _grade = new_grade; -} +int Bureaucrat::getGrade(void) const { return (_grade); } + +/* Setters */ -void Bureaucrat::decrGrade(int diff) - throw (Bureaucrat::GradeTooLowException) { - int new_grade = _grade + diff; - if (new_grade > 150) - throw Bureaucrat::GradeTooLowException(); - _grade = new_grade; +void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; + cout << _name << " grade increased." << endl; } -const char* Bureaucrat::GradeTooHighException::what(void) const throw() { - return ("Grade was too high for a bureaucrat"); +void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) { + int new_grade = _grade + diff; + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = new_grade; + cout << _name << " grade decreased." << endl; } -const char* Bureaucrat::GradeTooLowException::what(void) const throw() { - return ("Grade was too low for a bureaucrat"); +/* Exceptions */ + +const char *Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("error: grade is too high for a bureaucrat"); } -std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { - out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; - return (out); +const char *Bureaucrat::GradeTooLowException::what(void) const throw() { + return ("error: grade was too low for a bureaucrat"); } +/* Stream */ +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) { + out << b.getName() << "(" << b.getGrade() << ")"; + return (out); +} diff --git a/CPP05/ex00/Bureaucrat.hpp b/CPP05/ex00/Bureaucrat.hpp index a0f2e05..af9a1b6 100644 --- a/CPP05/ex00/Bureaucrat.hpp +++ b/CPP05/ex00/Bureaucrat.hpp @@ -12,41 +12,36 @@ #pragma once -#include #include #include +#include using std::cout; using std::endl; using std::string; -class Bureaucrat{ - 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); - virtual ~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); - +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); }; -std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex00/Makefile b/CPP05/ex00/Makefile index 0a9375a..ac1738d 100644 --- a/CPP05/ex00/Makefile +++ b/CPP05/ex00/Makefile @@ -1,20 +1,21 @@ -NAME = bureau -SRCS = main.cpp Bureaucrat.cpp -OBJS= $(SRCS:.cpp=.o) +NAME = bureau +SRCS = main.cpp Bureaucrat.cpp +OBJS = $(SRCS:.cpp=.o) -CXXFLAGS = -std=c++98 -Werror -Wextra -Wall +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall -$(NAME) : $(OBJS) - c++ $(OBJS) -o $(NAME) +$(NAME): $(OBJS) + $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME) -all : $(NAME) +all: $(NAME) -clean : - rm -rf $(OBJS) +clean: + rm -rf $(OBJS) -fclean : clean - rm -rf $(NAME) +fclean: clean + rm -rf $(NAME) -re : fclean all +re: fclean all -.PHONY : all clean fclean re +.PHONY: all clean fclean re diff --git a/CPP05/ex00/main.cpp b/CPP05/ex00/main.cpp index e76dd1a..1e277e8 100644 --- a/CPP05/ex00/main.cpp +++ b/CPP05/ex00/main.cpp @@ -12,53 +12,47 @@ #include "Bureaucrat.hpp" - -void create_too_low(void) { - try { - Bureaucrat b("trash", 151); - } catch (std::exception &e) { - cout << "Grade 151 creation: " << e.what() << endl; - } +void try_to_create(std::string name, int grade) { + try { + Bureaucrat b(name, grade); + } catch (std::exception &e) { + cout << "Creation " << name << "(" << grade << "):\n\t" << 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 evolve(Bureaucrat &b) { + try { + b.incrGrade(); + } catch (std::exception &e) { + cout << "Evolve " << b << ":\n\t" << e.what() << endl; + } } -void first_evolve(Bureaucrat &b) { - try { - b.incrGrade(); - } catch (std::exception &e) { - cout << "Grade 1 increase: "<< e.what() << endl; - } +void dismiss(Bureaucrat &b) { + try { + b.decrGrade(); + } catch (std::exception &e) { + cout << "Dismiss " << b << ":\n\t" << e.what() << endl; + } } -void last_dismiss(Bureaucrat &b) { - try { - b.decrGrade(); - } catch (std::exception &e) { - cout << "Grade 150 decrease: "<< e.what() << endl; - } -} +int main(void) { + Bureaucrat first("first", 1); + Bureaucrat last("last", 150); + // Bureaucrat trash("trash", 200); + cout << first << endl; + cout << last << endl; -int main(void) -{ - Bureaucrat first("first", 1); - Bureaucrat last("last", 150); + evolve(first); + dismiss(last); - cout << first; - cout << last; + dismiss(first); + evolve(last); - create_too_low(); - create_too_high(); - first_evolve(first); - last_dismiss(last); + cout << first << endl; + cout << last << endl; - cout << first; - cout << last; + try_to_create("God", 0); + try_to_create("Trash", 151); } diff --git a/CPP05/ex01/Bureaucrat.cpp b/CPP05/ex01/Bureaucrat.cpp index 7d9e805..5772f7c 100644 --- a/CPP05/ex01/Bureaucrat.cpp +++ b/CPP05/ex01/Bureaucrat.cpp @@ -12,80 +12,82 @@ #include "Bureaucrat.hpp" -Bureaucrat::Bureaucrat (void) { - cout << "Bureaucrat default constructor called " << endl; +/* Constructors */ +Bureaucrat::Bureaucrat(void) {} + +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; } -Bureaucrat::Bureaucrat (const string name, int grade) - throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ +/* copy const. */ +Bureaucrat::Bureaucrat(Bureaucrat const &src) { (void)src; } - if (grade < 1) - throw Bureaucrat::GradeTooHighException(); - if (grade > 150) - throw Bureaucrat::GradeTooLowException(); - _grade = grade; - cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; +/* assign const. */ +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) { + (void)src; + return (*this); } -Bureaucrat::Bureaucrat (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat copy constructor called" << endl; -} +/* Destructor */ +Bureaucrat::~Bureaucrat(void) {} -Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat assignment operator called" << endl; - return (*this); -} +/* Getters */ -Bureaucrat::~Bureaucrat (void) { - cout << "Bureaucrat default destructor called" << endl; -} +const string Bureaucrat::getName(void) const { return (_name); } -const string Bureaucrat::getName(void) const{ - return (_name); -} +int Bureaucrat::getGrade(void) const { return (_grade); } -int Bureaucrat::getGrade(void) const{ - return (_grade); -} +/* Setters */ -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::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; + cout << _name << " grade increased." << endl; } -void Bureaucrat::decrGrade(int diff) - throw (Bureaucrat::GradeTooLowException) { - int new_grade = _grade + diff; - if (new_grade > 150) - throw Bureaucrat::GradeTooLowException(); - _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; + cout << _name << " grade decreased." << endl; } -const char* Bureaucrat::GradeTooHighException::what(void) const throw() { - return ("Grade was too high for a bureaucrat"); +/* Exceptions */ + +const char *Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("error: grade is too high for a bureaucrat"); } -const char* Bureaucrat::GradeTooLowException::what(void) const throw() { - return ("Grade was too low for a bureaucrat"); +const char *Bureaucrat::GradeTooLowException::what(void) const throw() { + return ("error: grade is 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; - } +/* Externs */ + +void Bureaucrat::signForm(Form &form) const { + cout << *this << " sign " << form << ":"; + try { + if (!form.beSigned(*this)) + cout << " success."<< endl; + else + cout << " error.\n\tform is already signed." << endl; + } catch (std::exception &e) { + cout << " error.\n\tbureaucrat grade is too low." << endl; + } } -std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { - out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; - return (out); +/* Stream */ +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) { + out << b.getName() << "(" << b.getGrade() << ")"; + return (out); } diff --git a/CPP05/ex01/Bureaucrat.hpp b/CPP05/ex01/Bureaucrat.hpp index 76527b9..42be684 100644 --- a/CPP05/ex01/Bureaucrat.hpp +++ b/CPP05/ex01/Bureaucrat.hpp @@ -13,41 +13,41 @@ #pragma once #include "Form.hpp" -#include #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; +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; }; -std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex01/Form.cpp b/CPP05/ex01/Form.cpp index 8d083ec..b8c91ab 100644 --- a/CPP05/ex01/Form.cpp +++ b/CPP05/ex01/Form.cpp @@ -12,71 +12,69 @@ #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); +/* Constructors */ + +Form::Form(void) {} + +Form::Form(const string name, int signGrade, + int exeGrade) throw(Form::GradeTooHighException, + Form::GradeTooLowException) + : _name(name) { + if (signGrade < 1 || exeGrade < 1) + throw Form::GradeTooHighException(); + if (signGrade > 150 || exeGrade > 150) + throw Form::GradeTooLowException(); + _signGrade = signGrade; + _exeGrade = exeGrade; + _isSigned = 0; } -Form::Form(Form const &f) { - (void)f; - cout << "Form copy called" << endl; -} +/* copy const. */ +Form::Form(Form const &f) { (void)f; } -Form::~Form(void) { - cout << "Form desctructor called" << endl; +/* assign const. */ +Form &Form::operator=(Form const &f) { + (void)f; + return (*this); } -/* 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(); - if (signGrade > 150 || exeGrade > 150) - throw Form::GradeTooLowException(); - _signGrade = signGrade; - _exeGrade = exeGrade; -} +/* Destructor */ +Form::~Form(void) {} /* Getters */ -const string Form::getName(void) const { - return (_name); -} +const string Form::getName(void) const { return (_name); } -int Form::getSignGrade(void) const { - return (_signGrade); -} +int Form::getSignGrade(void) const { return (_signGrade); } -int Form::getExeGrade(void) const { - return (_exeGrade); -} +int Form::getExeGrade(void) const { return (_exeGrade); } -/* Seter */ +/* Setters */ -bool Form::beSigned(const Bureaucrat &b) throw (Form::GradeTooLowException) { - if (_isSigned) - return (1); - if (b.getGrade() > _signGrade) - throw GradeTooLowException(); - _isSigned = 1; - return (0); +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::GradeTooHighException::what(void) const throw() { + return ("error: grade is too high for a form"); } -const char *Form::GradeTooLowException::what(void) const throw (){ - return ("Grade was too low for a form"); +const char *Form::GradeTooLowException::what(void) const throw() { + return ("error: grade is too low for a form"); } + +/* Stream */ +std::ostream &operator<<(std::ostream &out, const Form &f) { + out << f.getName() + << "(s:" << f.getSignGrade() + << "|e:" << f.getExeGrade() + << ")"; + return (out); +}; diff --git a/CPP05/ex01/Form.hpp b/CPP05/ex01/Form.hpp index 74b7d32..affc6a0 100644 --- a/CPP05/ex01/Form.hpp +++ b/CPP05/ex01/Form.hpp @@ -13,39 +13,40 @@ #pragma once #include "Bureaucrat.hpp" -#include #include #include +#include using std::cout; using std::endl; using std::string; -class Bureaucrat; +class Bureaucrat; + +class Form { + const string _name; + int _signGrade; + int _exeGrade; + bool _isSigned; -class Form { - const string _name; - 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(void); - const string getName(void) const; - int getSignGrade(void) const; - int getExeGrade(void) const; - bool beSigned(const Bureaucrat &b) throw (GradeTooLowException); +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(void); + const string getName(void) const; + int getSignGrade(void) const; + int getExeGrade(void) const; + bool beSigned(const Bureaucrat &b) throw(GradeTooLowException); }; -std::ostream &operator<< (std::ostream &out, const Form &f); +std::ostream &operator<<(std::ostream &out, const Form &f); diff --git a/CPP05/ex01/main.cpp b/CPP05/ex01/main.cpp index 13f971e..17b2318 100644 --- a/CPP05/ex01/main.cpp +++ b/CPP05/ex01/main.cpp @@ -10,21 +10,19 @@ /* */ /* ************************************************************************** */ -#include "Form.hpp" #include "Bureaucrat.hpp" +#include "Form.hpp" -int main(void) -{ - Bureaucrat first("first", 1); - Bureaucrat mid("midle", 75); - Bureaucrat last("last", 150); - Form garb("garbage", 75, 75); - - cout << first << mid << last << garb; +int main(void) { + Bureaucrat first("first", 1); + Bureaucrat mid("midle", 75); + Bureaucrat last("last", 150); + Form garb("garbage", 75, 75); - last.signForm(garb); - mid.signForm(garb); - first.signForm(garb); - + cout << "Bureaucrats: " << first << "/" << mid << "/" << last << endl; + cout << "Forms: " << garb << endl << endl; + last.signForm(garb); + mid.signForm(garb); + first.signForm(garb); } diff --git a/CPP05/ex02/Bureaucrat.cpp b/CPP05/ex02/Bureaucrat.cpp index 984e0b6..4db847c 100644 --- a/CPP05/ex02/Bureaucrat.cpp +++ b/CPP05/ex02/Bureaucrat.cpp @@ -12,91 +12,92 @@ #include "Bureaucrat.hpp" -Bureaucrat::Bureaucrat (void) { - cout << "Bureaucrat default constructor called " << endl; +/* Constructors */ +Bureaucrat::Bureaucrat(void) {} + +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; } -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; +/* copy const. */ +Bureaucrat::Bureaucrat(Bureaucrat const &src) { + (void)src; } -Bureaucrat::Bureaucrat (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat copy constructor called" << endl; +/* assign const. */ +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) { + (void)src; + return (*this); } -Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat assignment operator called" << endl; - return (*this); +/* destructor */ +Bureaucrat::~Bureaucrat(void) { } -Bureaucrat::~Bureaucrat (void) { - cout << "Bureaucrat default destructor called" << endl; -} +/* Getters */ +const string Bureaucrat::getName(void) const { return (_name); } -const string Bureaucrat::getName(void) const{ - return (_name); -} - -int Bureaucrat::getGrade(void) const{ - return (_grade); -} +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; +/* Exception */ +const char *Bureaucrat::GradeTooHighException::what(void) const throw() { + return ("grade is too high for a bureaucrat"); } -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::GradeTooLowException::what(void) const throw() { + return ("grade is too low for a bureaucrat"); } -const char* Bureaucrat::GradeTooHighException::what(void) const throw() { - return ("Grade was too high for a bureaucrat"); +/* Methods */ +void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) { + int new_grade = _grade - diff; + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + _grade = new_grade; } -const char* Bureaucrat::GradeTooLowException::what(void) const throw() { - return ("Grade was too low for a bureaucrat"); +void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) { + int new_grade = _grade + diff; + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = new_grade; } -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::signForm(Form &form) const { + cout << *this << " sign " << form << ":"; + try { + if (!form.beSigned(*this)) + cout << " success." << endl; + else + cout << "error.\n\tform is already signed." << endl; + } catch (std::exception &e) { + cout << "error.\n\t" << 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; - } +void Bureaucrat::executeForm(const Form &form) const { + cout << *this << " execute " << form << ":"; + try { + if (!form.execute(*this)) + cout << " success." << endl; + else + cout << *this << " execute " << form.getName() + << "error.\n\tform is not signed" << endl; + } catch (std::exception &e) { + cout << "error.\n\t" << e.what() << endl; + } } -std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { - out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; - return (out); +/* Stream */ +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) { + out << b.getName() << "(" << b.getGrade() << ")"; + return (out); } diff --git a/CPP05/ex02/Bureaucrat.hpp b/CPP05/ex02/Bureaucrat.hpp index 5c7b86d..6b0cf5d 100644 --- a/CPP05/ex02/Bureaucrat.hpp +++ b/CPP05/ex02/Bureaucrat.hpp @@ -13,42 +13,42 @@ #pragma once #include "Form.hpp" -#include #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; +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); +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b); diff --git a/CPP05/ex02/Form.cpp b/CPP05/ex02/Form.cpp index 685b409..a3d4803 100644 --- a/CPP05/ex02/Form.cpp +++ b/CPP05/ex02/Form.cpp @@ -12,81 +12,75 @@ #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::operator=(Form const &f) { + (void)f; + return (*this); } Form::Form(Form const &f) - : _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade()) -{ - (void)f; - cout << "Form copy called" << endl; + : _name(f.getName()), _signGrade(f.getSignGrade()), + _exeGrade(f.getExeGrade()) { + (void)f; } -Form::~Form(void) { - cout << "Form destructor called" << endl; -} +Form::~Form(void) {} /* 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(); +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(); + _isSigned = 0; } /* Getters */ -const string Form::getName(void) const { - return (_name); -} +const string Form::getName(void) const { return (_name); } -int Form::getSignGrade(void) const { - return (_signGrade); -} +int Form::getSignGrade(void) const { return (_signGrade); } -int Form::getExeGrade(void) const { - return (_exeGrade); -} +int Form::getExeGrade(void) const { return (_exeGrade); } -/* Seter */ +/* Setters */ -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::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); +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::GradeTooHighException::what(void) const throw() { + return ("grade is too high for a form"); } -const char *Form::GradeTooLowException::what(void) const throw (){ - return ("Grade was too low for a form"); +const char *Form::GradeTooLowException::what(void) const throw() { + return ("grade is too low for a form"); } + +std::ostream &operator<<(std::ostream &out, const Form &f) { + out << f.getName() << "(s:" << f.getSignGrade() << "|e:" << f.getExeGrade() + << ")"; + return (out); +}; diff --git a/CPP05/ex02/Form.hpp b/CPP05/ex02/Form.hpp index 37484a8..5a2a3fc 100644 --- a/CPP05/ex02/Form.hpp +++ b/CPP05/ex02/Form.hpp @@ -13,45 +13,42 @@ #pragma once #include "Bureaucrat.hpp" -#include -#include #include -#include #include -using std::cout; -using std::endl; using std::string; -class Bureaucrat; +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; +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); +std::ostream &operator<<(std::ostream &out, const Form &f); diff --git a/CPP05/ex02/PresidentialPardonForm.cpp b/CPP05/ex02/PresidentialPardonForm.cpp index a4752bf..6cd5090 100644 --- a/CPP05/ex02/PresidentialPardonForm.cpp +++ b/CPP05/ex02/PresidentialPardonForm.cpp @@ -12,16 +12,11 @@ #include "PresidentialPardonForm.hpp" -PresidentialPardonForm::PresidentialPardonForm (const string target): Form("PresidentialPardonForm", 25, 5, target) { +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; -} +PresidentialPardonForm::~PresidentialPardonForm(void) {} -void PresidentialPardonForm::run(void) const { - cout << _target << "has been pardoned by Zaphod Beeblebrox" << 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 index ef52eff..2b29c05 100644 --- a/CPP05/ex02/PresidentialPardonForm.hpp +++ b/CPP05/ex02/PresidentialPardonForm.hpp @@ -14,11 +14,10 @@ #include "Form.hpp" -class PresidentialPardonForm: public Form { +class PresidentialPardonForm : public Form { - public: - - PresidentialPardonForm(const string target); - ~PresidentialPardonForm(void); - void run(void) const; +public: + PresidentialPardonForm(const string target); + ~PresidentialPardonForm(void); + void run(void) const; }; diff --git a/CPP05/ex02/RobotomyRequestForm.cpp b/CPP05/ex02/RobotomyRequestForm.cpp index 3b664d8..40b5b3c 100644 --- a/CPP05/ex02/RobotomyRequestForm.cpp +++ b/CPP05/ex02/RobotomyRequestForm.cpp @@ -12,27 +12,23 @@ #include "RobotomyRequestForm.hpp" -RobotomyRequestForm::RobotomyRequestForm (const string target): Form("RobotomyRequestForm", 72, 45, target) { +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; -} +RobotomyRequestForm::~RobotomyRequestForm(void) {} 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; + srand(time(NULL)); + 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 index ada3db7..defc8d2 100644 --- a/CPP05/ex02/RobotomyRequestForm.hpp +++ b/CPP05/ex02/RobotomyRequestForm.hpp @@ -13,12 +13,13 @@ #pragma once #include "Form.hpp" +#include +#include -class RobotomyRequestForm: public Form { +class RobotomyRequestForm : public Form { - public: - - RobotomyRequestForm(const string target); - ~RobotomyRequestForm(void); - void run(void) const; +public: + RobotomyRequestForm(const string target); + ~RobotomyRequestForm(void); + void run(void) const; }; diff --git a/CPP05/ex02/ShrubberyCreationForm.cpp b/CPP05/ex02/ShrubberyCreationForm.cpp index 4abd714..b6021b2 100644 --- a/CPP05/ex02/ShrubberyCreationForm.cpp +++ b/CPP05/ex02/ShrubberyCreationForm.cpp @@ -12,26 +12,23 @@ #include "ShrubberyCreationForm.hpp" -ShrubberyCreationForm::ShrubberyCreationForm (string target): Form("ShrubberyCreationForm", 147, 137, target) { - cout << "ShrubberyCreationForm parameter constructor called" << endl; -} +ShrubberyCreationForm::ShrubberyCreationForm(string target) + : Form("ShrubberyCreationForm", 147, 137, target) {} -ShrubberyCreationForm::~ShrubberyCreationForm (void) { - cout << "ShrubberyCreationForm default destructor called" << endl; -} +ShrubberyCreationForm::~ShrubberyCreationForm(void) {} 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(); + std::ofstream file; + file.open((_target + "shrubbery").c_str()); + 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 index 508708a..7ef4cc4 100644 --- a/CPP05/ex02/ShrubberyCreationForm.hpp +++ b/CPP05/ex02/ShrubberyCreationForm.hpp @@ -13,10 +13,10 @@ #pragma once #include "Form.hpp" -class ShrubberyCreationForm: public Form { +class ShrubberyCreationForm : public Form { - public: - ShrubberyCreationForm(string target); - ~ShrubberyCreationForm(void); - void run(void) const; +public: + ShrubberyCreationForm(string target); + ~ShrubberyCreationForm(void); + void run(void) const; }; diff --git a/CPP05/ex02/main.cpp b/CPP05/ex02/main.cpp index 12725ef..fe74a1d 100644 --- a/CPP05/ex02/main.cpp +++ b/CPP05/ex02/main.cpp @@ -10,30 +10,29 @@ /* */ /* ************************************************************************** */ -#include "ShrubberyCreationForm.hpp" -#include "RobotomyRequestForm.hpp" -#include "PresidentialPardonForm.hpp" #include "Bureaucrat.hpp" +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" -int main(void) -{ - Bureaucrat first("first", 1); - Bureaucrat mid("midle", 75); - Bureaucrat last("last", 150); +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); + 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 index 984e0b6..d89d230 100644 --- a/CPP05/ex03/Bureaucrat.cpp +++ b/CPP05/ex03/Bureaucrat.cpp @@ -12,91 +12,86 @@ #include "Bureaucrat.hpp" -Bureaucrat::Bureaucrat (void) { - cout << "Bureaucrat default constructor called " << endl; +Bureaucrat::Bureaucrat(void) { } -Bureaucrat::Bureaucrat (const string name, int grade) - throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ +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; + if (grade < 1) + throw Bureaucrat::GradeTooHighException(); + if (grade > 150) + throw Bureaucrat::GradeTooLowException(); + _grade = grade; } -Bureaucrat::Bureaucrat (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat copy constructor called" << endl; +Bureaucrat::Bureaucrat(Bureaucrat const &src) { + (void)src; } -Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { - (void)src; - cout << "Bureaucrat assignment operator called" << endl; - return (*this); +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) { + (void)src; + return (*this); } -Bureaucrat::~Bureaucrat (void) { - cout << "Bureaucrat default destructor called" << endl; +Bureaucrat::~Bureaucrat(void) { } -const string Bureaucrat::getName(void) const{ - return (_name); -} +const string Bureaucrat::getName(void) const { return (_name); } -int Bureaucrat::getGrade(void) const{ - return (_grade); -} +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::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; +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::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"); +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::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; - } +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); +std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) { + out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; + return (out); } diff --git a/CPP05/ex03/Form.cpp b/CPP05/ex03/Form.cpp index 685b409..f3eee3b 100644 --- a/CPP05/ex03/Form.cpp +++ b/CPP05/ex03/Form.cpp @@ -12,16 +12,10 @@ #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); } @@ -29,11 +23,9 @@ 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 */ @@ -45,6 +37,7 @@ Form::Form(const string name, const int signGrade, const int exeGrade, const str throw Form::GradeTooHighException(); if (signGrade > 150 || exeGrade > 150) throw Form::GradeTooLowException(); + _isSigned = 0; } /* Getters */ @@ -61,7 +54,7 @@ int Form::getExeGrade(void) const { return (_exeGrade); } -/* Seter */ +/* Setters */ int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) { if (_isSigned) @@ -84,9 +77,15 @@ int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException) /* Except */ const char *Form::GradeTooHighException::what(void) const throw (){ - return ("Grade was too high for a form"); + return ("grade is too high for a form"); } const char *Form::GradeTooLowException::what(void) const throw (){ - return ("Grade was too low for a form"); + return ("grade is too low for a form"); } + +std::ostream &operator<<(std::ostream &out, const Form &f) { + out << f.getName() << "(s:" << f.getSignGrade() << "|e:" << f.getExeGrade() + << ")"; + return (out); +}; diff --git a/CPP05/ex03/Intern.cpp b/CPP05/ex03/Intern.cpp index d1d6ce2..faf7823 100644 --- a/CPP05/ex03/Intern.cpp +++ b/CPP05/ex03/Intern.cpp @@ -12,46 +12,37 @@ #include "Intern.hpp" -Intern::Intern (void) { - cout << "Intern default constructor called " << endl; -} +Intern::Intern(void) {} -Intern::Intern (Intern const & src) { - (void)src; - cout << "Intern copy constructor called" << endl; -} +Intern::Intern(Intern const &src) { (void)src; } -Intern & Intern::operator= (Intern const & src) { - (void)src; - cout << "Intern assignment operator called" << endl; - return (*this); +Intern &Intern::operator=(Intern const &src) { + (void)src; + return (*this); } -Intern::~Intern (void) { - - cout << "Intern default destructor called" << endl; -} +Intern::~Intern(void) {} -Form * createRobotForm(string targ){ - return (new RobotomyRequestForm(targ)); +Form *createRobotForm(string targ) { + return (new RobotomyRequestForm(targ)); } -Form * createPresidentForm(string targ) { - return (new PresidentialPardonForm(targ)); +Form *createPresidentForm(string targ) { + return (new PresidentialPardonForm(targ)); } -Form * createShrubberyForm(string targ) { - return (new ShrubberyCreationForm(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); +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/PresidentialPardonForm.cpp b/CPP05/ex03/PresidentialPardonForm.cpp index a4752bf..4dfde4a 100644 --- a/CPP05/ex03/PresidentialPardonForm.cpp +++ b/CPP05/ex03/PresidentialPardonForm.cpp @@ -12,16 +12,16 @@ #include "PresidentialPardonForm.hpp" -PresidentialPardonForm::PresidentialPardonForm (const string target): Form("PresidentialPardonForm", 25, 5, target) { - - cout << "PresidentialPardonForm parameter constructor called" << endl; +PresidentialPardonForm::PresidentialPardonForm(const string target) + : Form("PresidentialPardonForm", 25, 5, target) +{ } -PresidentialPardonForm::~PresidentialPardonForm (void) { - - cout << "PresidentialPardonForm default destructor called" << endl; +PresidentialPardonForm::~PresidentialPardonForm(void) +{ } -void PresidentialPardonForm::run(void) const { - cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; +void PresidentialPardonForm::run(void) const +{ + cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; } diff --git a/CPP05/ex03/RobotomyRequestForm.cpp b/CPP05/ex03/RobotomyRequestForm.cpp index 3b664d8..15f76b0 100644 --- a/CPP05/ex03/RobotomyRequestForm.cpp +++ b/CPP05/ex03/RobotomyRequestForm.cpp @@ -12,27 +12,28 @@ #include "RobotomyRequestForm.hpp" -RobotomyRequestForm::RobotomyRequestForm (const string target): Form("RobotomyRequestForm", 72, 45, target) { - - cout << "RobotomyRequestForm parameter constructor called" << endl; +RobotomyRequestForm::RobotomyRequestForm(const string target) + : Form("RobotomyRequestForm", 72, 45, target) +{ } -RobotomyRequestForm::~RobotomyRequestForm (void) { - - cout << "RobotomyRequestForm default destructor called" << endl; +RobotomyRequestForm::~RobotomyRequestForm(void) +{ } -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; +void RobotomyRequestForm::run(void) const +{ + srand(time(NULL)); + 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 index ada3db7..adbc105 100644 --- a/CPP05/ex03/RobotomyRequestForm.hpp +++ b/CPP05/ex03/RobotomyRequestForm.hpp @@ -13,6 +13,8 @@ #pragma once #include "Form.hpp" +#include +#include class RobotomyRequestForm: public Form { diff --git a/CPP05/ex03/ShrubberyCreationForm.cpp b/CPP05/ex03/ShrubberyCreationForm.cpp index dc78aed..5c73910 100644 --- a/CPP05/ex03/ShrubberyCreationForm.cpp +++ b/CPP05/ex03/ShrubberyCreationForm.cpp @@ -12,26 +12,28 @@ #include "ShrubberyCreationForm.hpp" -ShrubberyCreationForm::ShrubberyCreationForm (string target): Form("ShrubberyCreationForm", 147, 137, target) { - cout << "ShrubberyCreationForm parameter constructor called" << endl; +ShrubberyCreationForm::ShrubberyCreationForm(string target) + : Form("ShrubberyCreationForm", 147, 137, target) +{ } -ShrubberyCreationForm::~ShrubberyCreationForm (void) { - cout << "ShrubberyCreationForm default destructor called" << endl; +ShrubberyCreationForm::~ShrubberyCreationForm(void) +{ } -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(); +void ShrubberyCreationForm::run(void) const +{ + std::ofstream file; + file.open((_target + "_shrubbery").c_str()); + 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/house_shrubbery b/CPP05/ex03/house_shrubbery deleted file mode 100644 index 94ddd83..0000000 --- a/CPP05/ex03/house_shrubbery +++ /dev/null @@ -1,10 +0,0 @@ - ,@@@@@@@, - ,,,. ,@@@@@@/@@, .oo8888o. - ,&%%&%&&%,@@@@@/@@@@@@,8888\88/8o - ,%&\%&&%&&%,@@@\@@@/@@@88\88888/88' - %&&%&%&/%&&%@@\@@/ /@@@88888\88888' - %&&%/ %&%%&&@@\ V /@@' `88\8 `/88' - `&%\ ` /%&' |.| \ '|8' - |o| | | | | - |.| | | | | - \\/ ._\//_/__/ ,\_//__\\/. \_//__/_ diff --git a/CPP06/ex00/Converter.cpp b/CPP06/ex00/Converter.cpp index 7656edf..08b6f0d 100644 --- a/CPP06/ex00/Converter.cpp +++ b/CPP06/ex00/Converter.cpp @@ -11,57 +11,64 @@ /* ************************************************************************** */ #include "Converter.hpp" +using std::cout; +using std::endl; -Converter::Converter (void) { - cout << "Converter default constructor called " << endl; +Converter::Converter(void) { + cout << "Converter default constructor called " << endl; } -Converter::Converter (string str){ - if (str[0] == '\'' || str[0] == '"') - { - _c = str[1]; - cout << "Char: " << _c << endl; - _d = static_cast(_c); - } - else - { - cout << "Char: "; - _d = std::strtod(str.c_str(), 0); - _c = static_cast(_d); - if (_d < 0 || _d > 255) - cout << "Invalid" << endl; - else if (isnan(_d)) - cout << "Impossible" << endl; - else if (!std::isprint(_c)) - cout << "Not Printable" << endl; - else - cout << "'" << _c << "'" << endl; - } - cout << "Integer: "; - _i = static_cast(_d); - if (_d < std::numeric_limits::min() || _d > std::numeric_limits::max()) - cout << "Off limits -> "; - if (isnan(_d)) - cout << "Impossible" << endl; - else - cout << _i << endl; - cout << "Double: " << _d << endl; - _f = static_cast(_d); - cout << "Float: " << _f << "f"<< endl; +Converter::Converter(string str) { + if (str[0] == '\'' || str[0] == '"') { + _c = str[1]; + cout << "Char: " << _c << endl; + _d = static_cast(_c); + } else { + cout << "Char: "; + _d = std::strtod(str.c_str(), 0); + _c = static_cast(_d); + if (_d < 0 || _d > 255) + cout << "Invalid" << endl; + else if (isnan(_d)) + cout << "Impossible" << endl; + else if (!std::isprint(_c)) + cout << "Not Printable" << endl; + else + cout << "'" << _c << "'" << endl; + } + cout << "Integer: "; + _i = static_cast(_d); + if (_d < std::numeric_limits::min() || + _d > std::numeric_limits::max()) + cout << "Off limits -> "; + if (std::isnan(_d)) + cout << "Impossible" << endl; + else + cout << _i << endl; + _f = static_cast(_d); + if (_d == 0 || _d / static_cast(_d) == 1) { + cout << "Double: " << std::setprecision(1) << std::fixed << _d<< endl; + cout << "Float: " << std::setprecision(1) << std::fixed << _f << "f" << endl; + } + else + { + cout << "Double: " << _d << endl; + cout << "Float: " << _f << "f" << endl; + } } -Converter::Converter (Converter const & src) { - (void)src; - cout << "Converter copy 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::operator=(Converter const &src) { + (void)src; + cout << "Converter assignment operator called" << endl; + return (*this); } -Converter::~Converter (void) { +Converter::~Converter(void) { - cout << "Converter default destructor called" << endl; + cout << "Converter default destructor called" << endl; } diff --git a/CPP06/ex00/Converter.hpp b/CPP06/ex00/Converter.hpp index 98829ff..9384b73 100644 --- a/CPP06/ex00/Converter.hpp +++ b/CPP06/ex00/Converter.hpp @@ -19,9 +19,8 @@ #include #include #include +#include -using std::cout; -using std::endl; using std::string; class Converter{ @@ -31,10 +30,9 @@ class Converter{ int _i; float _f; double _d; - //int _types[4]; public: - Converter(void); Converter(string str); + Converter(void); Converter(Converter const & src); virtual ~Converter(void); Converter & operator= (Converter const & src); diff --git a/CPP06/ex00/main.cpp b/CPP06/ex00/main.cpp index 5a5296d..edfcd41 100644 --- a/CPP06/ex00/main.cpp +++ b/CPP06/ex00/main.cpp @@ -13,8 +13,6 @@ #include #include "Converter.hpp" -using std::cout; -using std::endl; using std::string; int main(int ac, char **av) { diff --git a/CPP06/ex01/Data.h b/CPP06/ex01/Data.h new file mode 100644 index 0000000..81c1f9e --- /dev/null +++ b/CPP06/ex01/Data.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Data.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 09:28:26 by narnaud #+# #+# */ +/* Updated: 2022/09/29 09:28:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include + +typedef struct s_Data { + std::string content; +} t_Data; diff --git a/CPP06/ex01/Makefile b/CPP06/ex01/Makefile new file mode 100644 index 0000000..25046d0 --- /dev/null +++ b/CPP06/ex01/Makefile @@ -0,0 +1,19 @@ + +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall +CXX = c++ + +serialize: main.o + $(CXX) $(CXXFLAGS) main.o -o serialize + +all: serialize + +clean: + rm -rf main.o + +fclean: clean + rm -rf serialize + +re: fclean all + + +.PHONY: all clean fclean re diff --git a/CPP06/ex01/main.cpp b/CPP06/ex01/main.cpp new file mode 100644 index 0000000..4948d39 --- /dev/null +++ b/CPP06/ex01/main.cpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/08/04 16:48:40 by narnaud #+# #+# */ +/* Updated: 2022/08/04 16:52:56 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Data.h" +#include "iostream" +#include "stdint.h" + +uintptr_t serialize(t_Data* ptr) { + return reinterpret_cast(ptr); +} + +t_Data* deserialize(uintptr_t raw) { + return reinterpret_cast(raw); +} + +int main(void) { + t_Data * data = new t_Data; + uintptr_t raw; + t_Data * ptr; + + data->content = "Hi"; + raw = serialize(data); + ptr = deserialize(raw); + + std::cout + << "Original content: " << data->content << std::endl + << "Serialized: " << raw << std::endl + << "Deserialized: " << ptr->content << std::endl; + delete data; + return (0); +} diff --git a/CPP06/ex02/A.hpp b/CPP06/ex02/A.hpp new file mode 100644 index 0000000..b12bbe7 --- /dev/null +++ b/CPP06/ex02/A.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 10:32:10 by narnaud #+# #+# */ +/* Updated: 2022/09/29 11:08:51 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Base.hpp" + +class A:public Base { + public: + A(void); + +}; diff --git a/CPP06/ex02/B.hpp b/CPP06/ex02/B.hpp new file mode 100644 index 0000000..8aae00a --- /dev/null +++ b/CPP06/ex02/B.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 10:33:17 by narnaud #+# #+# */ +/* Updated: 2022/09/29 11:09:17 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include "Base.hpp" + +class B:public Base { + public: + B(void); +}; diff --git a/CPP06/ex02/Base.hpp b/CPP06/ex02/Base.hpp new file mode 100644 index 0000000..073bedb --- /dev/null +++ b/CPP06/ex02/Base.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 10:30:30 by narnaud #+# #+# */ +/* Updated: 2022/09/29 11:09:47 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +class Base { + public: + Base(void); + virtual ~Base(void); +}; diff --git a/CPP06/ex02/C.hpp b/CPP06/ex02/C.hpp new file mode 100644 index 0000000..1700ce1 --- /dev/null +++ b/CPP06/ex02/C.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 10:34:10 by narnaud #+# #+# */ +/* Updated: 2022/09/29 11:09:30 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include "Base.hpp" + +class C:public Base { + public: + C(void); +}; diff --git a/CPP06/ex02/Classes.cpp b/CPP06/ex02/Classes.cpp new file mode 100644 index 0000000..3119fc6 --- /dev/null +++ b/CPP06/ex02/Classes.cpp @@ -0,0 +1,11 @@ +#include "Base.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +Base::Base(void){} +Base::~Base(void){} + +A::A(void){} +B::B(void){} +C::C(void){} diff --git a/CPP06/ex02/Makefile b/CPP06/ex02/Makefile new file mode 100644 index 0000000..fc5f675 --- /dev/null +++ b/CPP06/ex02/Makefile @@ -0,0 +1,17 @@ +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +base: main.o Classes.o + $(CXX) $(CXXFLAGS) main.o Classes.o -o base + +all: base + +clean: + rm -rf main.o Classes.o + +fclean: clean + rm -rf base + +re: fclean all + +.PHONY: all clean fclean re diff --git a/CPP06/ex02/main.cpp b/CPP06/ex02/main.cpp new file mode 100644 index 0000000..c81f6ac --- /dev/null +++ b/CPP06/ex02/main.cpp @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 10:09:08 by narnaud #+# #+# */ +/* Updated: 2022/09/29 10:36:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include "Base.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +Base *generate (void) { + int rand_id; + char names[3] = {'A', 'B', 'C'}; + std::srand(time(NULL) * std::rand()); + rand_id = std::rand() % 3; + std::cout << "Generated a " << names[rand_id] << std::endl; + switch (rand_id) { + case 0: + return new A; + break; + case 1: + return new B; + break; + case 2: + return new C; + break; + default: + return NULL; + } +} + +void identify(Base *ptr) { + A *a = dynamic_cast(ptr); + if (a) + std::cout << "Identified a A" << std::endl; + B *b = dynamic_cast(ptr); + if (b) + std::cout << "Identified a B" << std::endl; + C *c = dynamic_cast(ptr); + if (c) + std::cout << "Identified a C" << std::endl; +} + +void identify(Base &ref) { + try { + A a = dynamic_cast(ref); + std::cout << "Identified a A" << std::endl; + } + catch (std::exception &e) { (void)e;} + try { + B b = dynamic_cast(ref); + std::cout << "Identified a B" << std::endl; + } + catch (std::exception &e) { (void)e;} + try { + C c = dynamic_cast(ref); + std::cout << "Identified a C" << std::endl; + } + catch (std::exception &e) { (void)e;} +} + +int main(void) { + Base *ptr; + + std::cout << "Identify from pointer:" << std::endl; + for (int i = 0; i < 5; i++) { + ptr = generate(); + identify(ptr); + delete ptr; + } + + std::cout << "Identify from reference:" << std::endl; + for (int i = 0; i < 5; i++) { + ptr = generate(); + identify(*ptr); + delete ptr; + } + +} diff --git a/CPP08/cpp08.pdf b/CPP08/cpp08.pdf new file mode 100644 index 0000000..1aff8ba Binary files /dev/null and b/CPP08/cpp08.pdf differ diff --git a/CPP08/ex00/Makefile b/CPP08/ex00/Makefile index 30d1ec0..1ecc9db 100644 --- a/CPP08/ex00/Makefile +++ b/CPP08/ex00/Makefile @@ -1,11 +1,12 @@ -NAME = easy_find -SRCS = +NAME = easyfind +SRCS = main.cpp OBJS= $(SRCS:.cpp=.o) +CXX = c++ CXXFLAGS = -std=c++98 -Werror -Wextra -Wall $(NAME) : $(OBJS) - c++ $(OBJS) -o $(NAME) + $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME) all : $(NAME) diff --git a/CPP08/ex00/easyfind.hpp b/CPP08/ex00/easyfind.hpp new file mode 100644 index 0000000..d479138 --- /dev/null +++ b/CPP08/ex00/easyfind.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* easyfind.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:06:39 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:06:42 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include + +template +typename T::iterator easyfind(T &t, const int nb) { + typename T::iterator it; + it = find(t.begin(), t.end(), nb); + if (it == t.end()) + return (t.end()); + return (it); +} + +template +void is_easyfound(T it, T end) { + if (it != end) + std::cout << "You found: " << *it << std::endl; + else + std::cout << "You didn't found it." << std::endl; +} diff --git a/CPP08/ex00/main.cpp b/CPP08/ex00/main.cpp new file mode 100644 index 0000000..f902673 --- /dev/null +++ b/CPP08/ex00/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:06:21 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:06:26 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "easyfind.hpp" +#include +#include +#include +#include + +int main(void) { + std::vector tab; + std::vector::iterator tab_it; + std::list lst; + std::list::iterator lst_it; + std::string str = "abcdefghijklmnopqrstuvwxyz\n"; + std::string::iterator str_it; + + tab.push_back(5); + tab.push_back(50); + lst.push_back(5); + lst.push_back(50); + + tab_it = easyfind(tab, 50); + lst_it = easyfind(lst, 50); + str_it = easyfind(str, 50); + + std::cout << "Array search:" << std::endl; + is_easyfound(tab_it, tab.end()); + std::cout << "List search:" << std::endl; + is_easyfound(lst_it, lst.end()); + std::cout << "String search" << std::endl; + is_easyfound(str_it, str.end()); + return (0); +} diff --git a/CPP08/ex01/Makefile b/CPP08/ex01/Makefile new file mode 100644 index 0000000..81012ce --- /dev/null +++ b/CPP08/ex01/Makefile @@ -0,0 +1,22 @@ +NAME = span + +SRCS = main.cpp Span.cpp +OBJS = $(SRCS:.cpp=.o) + +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME): $(OBJS) + $(CXX) -g $(CXXFLAGS) $(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/CPP08/ex01/Span.cpp b/CPP08/ex01/Span.cpp new file mode 100644 index 0000000..d96f46a --- /dev/null +++ b/CPP08/ex01/Span.cpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:07:10 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:07:11 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" + +Span::Span(unsigned int N) : _capacity(N) {} + +Span::~Span() {} + +Span::Span(Span const &sp) { *this = sp; } + +Span &Span::operator=(Span const &sp) { + if (this == &sp) + return *this; + _vec = sp._vec; + _capacity = sp._capacity; + return (*this); +} + +void Span::addNumber(int const nb) { + if (_vec.size() == _capacity) + throw std::runtime_error("Too much number in span container."); + _vec.push_back(nb); +} + +unsigned int Span::shortestSpan() { + std::vector::iterator it; + long ret; + + if (_vec.size() < 2) + throw std::runtime_error("Not enought number to get shortest span."); + sort(_vec.begin(), _vec.end()); + it = _vec.begin() + 1; + ret = *it - *(it - 1); + for (it = _vec.begin() + 2; it < _vec.end() && ret; it++) + if (*it - *(it - 1) < ret) + ret = *it - *(it - 1); + return (ret); +} + +unsigned int Span::longestSpan() { + if (_vec.size() < 2) + throw std::runtime_error("Not enought number to get shortest span."); + sort(_vec.begin(), _vec.end()); + return (*(_vec.end() - 1) - *_vec.begin()); +} + +void Span::addNRandom(unsigned int N) { + if (_vec.size() + N < _capacity) + throw std::runtime_error("Too much number to add into span container."); + srand((unsigned)time(NULL)); + for (unsigned int i = 0; i < N; i++) + _vec.push_back((rand() - INT_MAX / 2) * 2); +} + +void Span::addRange(std::vector::iterator begin, std::vector::iterator end) { + if (_vec.size() + end - begin > _capacity) + throw std::runtime_error("Container don't have enouth free space for range you want to add."); + std::copy(begin, end, std::back_inserter(_vec)); +} diff --git a/CPP08/ex01/Span.hpp b/CPP08/ex01/Span.hpp new file mode 100644 index 0000000..2bc001c --- /dev/null +++ b/CPP08/ex01/Span.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:07:26 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:07:31 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +class Span { + std::vector _vec; + unsigned int _capacity; +public: + Span(); + Span(unsigned int N); + Span(Span const &sp); + ~Span(); + Span &operator=(Span const &sp); + void addNumber(int nb); + unsigned int shortestSpan(); + unsigned int longestSpan(); + void addNRandom(unsigned int N); + void addRange(std::vector::iterator begin, std::vector::iterator end); +}; diff --git a/CPP08/ex01/main.cpp b/CPP08/ex01/main.cpp new file mode 100644 index 0000000..483c0a1 --- /dev/null +++ b/CPP08/ex01/main.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:06:50 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:07:01 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" +#define RAND_SIZE 10000 + +int main(void) { + Span sp(10); + sp.addNumber(5); + sp.addNumber(4); + sp.addNumber(1); + sp.addNumber(-5); + + std::cout << "5 4 1 -1" << std::endl; + std::cout << "Shortest span: " << sp.shortestSpan() << std::endl; + std::cout << "Longest span: " << sp.longestSpan() << std::endl; + + Span sp2(RAND_SIZE); + sp2.addNRandom(RAND_SIZE); + + std::cout << RAND_SIZE << " numbers between " << INT_MIN << " and " << INT_MAX + << std::endl; + std::cout << "Shortest span: " << sp2.shortestSpan() << std::endl; + std::cout << "Longest span: " << sp2.longestSpan() << std::endl; + + Span sp3(500); + std::vector nums; + for (int n = 0; n < 500; n++) nums.push_back(n); + std::random_shuffle(nums.begin(), nums.end()); + sp3.addRange(nums.begin(), nums.begin() + 20); + std::cout << "Sp3:" << std::endl; + std::cout << "Shortest span: " << sp3.shortestSpan() << std::endl; + std::cout << "Longest span: " << sp3.longestSpan() << std::endl; + + sp3.addRange(nums.begin(), nums.end()); + + return (0); +} diff --git a/CPP08/ex02/Makefile b/CPP08/ex02/Makefile new file mode 100644 index 0000000..7faf266 --- /dev/null +++ b/CPP08/ex02/Makefile @@ -0,0 +1,22 @@ +NAME = muted_stack + +SRCS = main.cpp +OBJS = $(SRCS:.cpp=.o) + +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME): $(OBJS) + $(CC) $(CXXFLAGS) $(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/CPP08/ex02/MutantStack.hpp b/CPP08/ex02/MutantStack.hpp new file mode 100644 index 0000000..e87cfbe --- /dev/null +++ b/CPP08/ex02/MutantStack.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MutantStack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 09:39:40 by narnaud #+# #+# */ +/* Updated: 2022/09/19 09:39:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include + +template class MutantStack : public std::stack { +public: + MutantStack(void) {} + MutantStack(const MutantStack &st) { *this = st; } + ~MutantStack(void) {} + MutantStack &operator=(const MutantStack &st) { + (void)st; + return *this; + } + typedef typename std::stack::container_type::iterator iterator; + iterator begin() { return this->c.begin(); } + iterator end() { return this->c.end(); } +}; diff --git a/CPP08/ex02/main.cpp b/CPP08/ex02/main.cpp new file mode 100644 index 0000000..0551d33 --- /dev/null +++ b/CPP08/ex02/main.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 09:39:50 by narnaud #+# #+# */ +/* Updated: 2022/09/19 09:40:01 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "MutantStack.hpp" + +int main() { + MutantStack mstack; + mstack.push(5); + mstack.push(17); + std::cout << mstack.top() << std::endl; + mstack.pop(); + std::cout << mstack.size() << std::endl; + mstack.push(3); + mstack.push(5); + mstack.push(737); + //[...] + mstack.push(0); + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + ++it; + --it; + while (it != ite) { + std::cout << *it << std::endl; + ++it; + } + std::stack s(mstack); + return 0; +}