Browse Source

finish?

master^2
narnaud 2 years ago
parent
commit
3c8634649b
  1. 95
      CPP05/ex00/Bureaucrat.cpp
  2. 55
      CPP05/ex00/Bureaucrat.hpp
  3. 27
      CPP05/ex00/Makefile
  4. 70
      CPP05/ex00/main.cpp
  5. 114
      CPP05/ex01/Bureaucrat.cpp
  6. 56
      CPP05/ex01/Bureaucrat.hpp
  7. 96
      CPP05/ex01/Form.cpp
  8. 55
      CPP05/ex01/Form.hpp
  9. 24
      CPP05/ex01/main.cpp
  10. 131
      CPP05/ex02/Bureaucrat.cpp
  11. 58
      CPP05/ex02/Bureaucrat.hpp
  12. 96
      CPP05/ex02/Form.cpp
  13. 67
      CPP05/ex02/Form.hpp
  14. 15
      CPP05/ex02/PresidentialPardonForm.cpp
  15. 11
      CPP05/ex02/PresidentialPardonForm.hpp
  16. 36
      CPP05/ex02/RobotomyRequestForm.cpp
  17. 13
      CPP05/ex02/RobotomyRequestForm.hpp
  18. 35
      CPP05/ex02/ShrubberyCreationForm.cpp
  19. 10
      CPP05/ex02/ShrubberyCreationForm.hpp
  20. 45
      CPP05/ex02/main.cpp
  21. 119
      CPP05/ex03/Bureaucrat.cpp
  22. 21
      CPP05/ex03/Form.cpp
  23. 53
      CPP05/ex03/Intern.cpp
  24. 16
      CPP05/ex03/PresidentialPardonForm.cpp
  25. 39
      CPP05/ex03/RobotomyRequestForm.cpp
  26. 2
      CPP05/ex03/RobotomyRequestForm.hpp
  27. 38
      CPP05/ex03/ShrubberyCreationForm.cpp
  28. 10
      CPP05/ex03/house_shrubbery
  29. 93
      CPP06/ex00/Converter.cpp
  30. 6
      CPP06/ex00/Converter.hpp
  31. 2
      CPP06/ex00/main.cpp
  32. 18
      CPP06/ex01/Data.h
  33. 19
      CPP06/ex01/Makefile
  34. 40
      CPP06/ex01/main.cpp
  35. 21
      CPP06/ex02/A.hpp
  36. 19
      CPP06/ex02/B.hpp
  37. 19
      CPP06/ex02/Base.hpp
  38. 19
      CPP06/ex02/C.hpp
  39. 11
      CPP06/ex02/Classes.cpp
  40. 17
      CPP06/ex02/Makefile
  41. 90
      CPP06/ex02/main.cpp
  42. BIN
      CPP08/cpp08.pdf
  43. 7
      CPP08/ex00/Makefile
  44. 35
      CPP08/ex00/easyfind.hpp
  45. 43
      CPP08/ex00/main.cpp
  46. 22
      CPP08/ex01/Makefile
  47. 69
      CPP08/ex01/Span.cpp
  48. 37
      CPP08/ex01/Span.hpp
  49. 47
      CPP08/ex01/main.cpp
  50. 22
      CPP08/ex02/Makefile
  51. 30
      CPP08/ex02/MutantStack.hpp
  52. 37
      CPP08/ex02/main.cpp

95
CPP05/ex00/Bureaucrat.cpp

@ -12,70 +12,69 @@
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat (void) { /* Constructors */
cout << "Bureaucrat default constructor called " << endl;
}
Bureaucrat::Bureaucrat (const string name, int grade) Bureaucrat::Bureaucrat(void) {}
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){
if (grade < 1) Bureaucrat::Bureaucrat(const string name,
throw Bureaucrat::GradeTooHighException(); int grade) throw(Bureaucrat::GradeTooHighException,
if (grade > 150) Bureaucrat::GradeTooLowException)
throw Bureaucrat::GradeTooLowException(); : _name(name) {
_grade = grade; if (grade < 1)
cout << "Bureaucrat " << _name << " created grade: " << _grade << endl; throw Bureaucrat::GradeTooHighException();
if (grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = grade;
} }
Bureaucrat::Bureaucrat (Bureaucrat const & src) { /* copy const. */
(void)src; Bureaucrat::Bureaucrat(Bureaucrat const &src) { (void)src; }
cout << "Bureaucrat copy constructor called" << endl;
}
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { /* assign const. */
(void)src; Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
cout << "Bureaucrat assignment operator called" << endl; (void)src;
return (*this); return (*this);
} }
Bureaucrat::~Bureaucrat (void) { /* Destructor */
cout << "Bureaucrat default destructor called" << endl; Bureaucrat::~Bureaucrat(void) {}
}
const string Bureaucrat::getName(void) const{ /* Getters */
return (_name);
}
int Bureaucrat::getGrade(void) const{ const string Bureaucrat::getName(void) const { return (_name); }
return (_grade);
}
void Bureaucrat::incrGrade(int diff) int Bureaucrat::getGrade(void) const { return (_grade); }
throw (Bureaucrat::GradeTooHighException) {
int new_grade = _grade - diff; /* Setters */
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
_grade = new_grade;
}
void Bureaucrat::decrGrade(int diff) void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) {
throw (Bureaucrat::GradeTooLowException) { int new_grade = _grade - diff;
int new_grade = _grade + diff; if (new_grade < 1)
if (new_grade > 150) throw Bureaucrat::GradeTooHighException();
throw Bureaucrat::GradeTooLowException(); _grade = new_grade;
_grade = new_grade; cout << _name << " grade increased." << endl;
} }
const char* Bureaucrat::GradeTooHighException::what(void) const throw() { void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
return ("Grade was too high for a bureaucrat"); 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() { /* Exceptions */
return ("Grade was too low for a bureaucrat");
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) { const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; return ("error: grade was too low for a bureaucrat");
return (out);
} }
/* Stream */
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
out << b.getName() << "(" << b.getGrade() << ")";
return (out);
}

55
CPP05/ex00/Bureaucrat.hpp

@ -12,41 +12,36 @@
#pragma once #pragma once
#include <string>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string>
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::string; using std::string;
class Bureaucrat{ class Bureaucrat {
string _name; const string _name;
int _grade; int _grade;
public:
public:
class GradeTooHighException: virtual public std::exception class GradeTooHighException : virtual public std::exception {
{ public:
public: const char *what(void) const throw();
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();
public: };
const char *what(void) const throw (); Bureaucrat(void);
}; Bureaucrat(const string name, int grade) throw(GradeTooHighException,
Bureaucrat(void); GradeTooLowException);
Bureaucrat(const string name, int grade) Bureaucrat(Bureaucrat const &src);
throw (GradeTooHighException, GradeTooLowException); ~Bureaucrat(void);
Bureaucrat(Bureaucrat const & src); Bureaucrat &operator=(Bureaucrat const &src);
virtual ~Bureaucrat(void); const string getName(void) const;
Bureaucrat & operator= (Bureaucrat const & src); int getGrade(void) const;
const string getName(void) const; void incrGrade(int diff = 1) throw(GradeTooHighException);
int getGrade(void) const; void decrGrade(int diff = 1) throw(GradeTooLowException);
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);

27
CPP05/ex00/Makefile

@ -1,20 +1,21 @@
NAME = bureau NAME = bureau
SRCS = main.cpp Bureaucrat.cpp SRCS = main.cpp Bureaucrat.cpp
OBJS= $(SRCS:.cpp=.o) OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall CXX = c++
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME) : $(OBJS) $(NAME): $(OBJS)
c++ $(OBJS) -o $(NAME) $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME)
all : $(NAME) all: $(NAME)
clean : clean:
rm -rf $(OBJS) rm -rf $(OBJS)
fclean : clean fclean: clean
rm -rf $(NAME) rm -rf $(NAME)
re : fclean all re: fclean all
.PHONY : all clean fclean re .PHONY: all clean fclean re

70
CPP05/ex00/main.cpp

@ -12,53 +12,47 @@
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
void try_to_create(std::string name, int grade) {
void create_too_low(void) { try {
try { Bureaucrat b(name, grade);
Bureaucrat b("trash", 151); } catch (std::exception &e) {
} catch (std::exception &e) { cout << "Creation " << name << "(" << grade << "):\n\t" << e.what() << endl;
cout << "Grade 151 creation: " << e.what() << endl; }
}
} }
void create_too_high(void) { void evolve(Bureaucrat &b) {
try { try {
Bureaucrat b("god", 0); b.incrGrade();
} catch (std::exception &e) { } catch (std::exception &e) {
cout << "Grade 0 creation: " << e.what() << endl; cout << "Evolve " << b << ":\n\t" << e.what() << endl;
} }
} }
void first_evolve(Bureaucrat &b) { void dismiss(Bureaucrat &b) {
try { try {
b.incrGrade(); b.decrGrade();
} catch (std::exception &e) { } catch (std::exception &e) {
cout << "Grade 1 increase: "<< e.what() << endl; cout << "Dismiss " << b << ":\n\t" << e.what() << endl;
} }
} }
void last_dismiss(Bureaucrat &b) { int main(void) {
try { Bureaucrat first("first", 1);
b.decrGrade(); Bureaucrat last("last", 150);
} catch (std::exception &e) { // Bureaucrat trash("trash", 200);
cout << "Grade 150 decrease: "<< e.what() << endl;
}
}
cout << first << endl;
cout << last << endl;
int main(void) evolve(first);
{ dismiss(last);
Bureaucrat first("first", 1);
Bureaucrat last("last", 150);
cout << first; dismiss(first);
cout << last; evolve(last);
create_too_low(); cout << first << endl;
create_too_high(); cout << last << endl;
first_evolve(first);
last_dismiss(last);
cout << first; try_to_create("God", 0);
cout << last; try_to_create("Trash", 151);
} }

114
CPP05/ex01/Bureaucrat.cpp

@ -12,80 +12,82 @@
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat (void) { /* Constructors */
cout << "Bureaucrat default constructor called " << endl; 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) /* copy const. */
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ Bureaucrat::Bureaucrat(Bureaucrat const &src) { (void)src; }
if (grade < 1) /* assign const. */
throw Bureaucrat::GradeTooHighException(); Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
if (grade > 150) (void)src;
throw Bureaucrat::GradeTooLowException(); return (*this);
_grade = grade;
cout << "Bureaucrat " << _name << " created grade: " << _grade << endl;
} }
Bureaucrat::Bureaucrat (Bureaucrat const & src) { /* Destructor */
(void)src; Bureaucrat::~Bureaucrat(void) {}
cout << "Bureaucrat copy constructor called" << endl;
}
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { /* Getters */
(void)src;
cout << "Bureaucrat assignment operator called" << endl;
return (*this);
}
Bureaucrat::~Bureaucrat (void) { const string Bureaucrat::getName(void) const { return (_name); }
cout << "Bureaucrat default destructor called" << endl;
}
const string Bureaucrat::getName(void) const{ int Bureaucrat::getGrade(void) const { return (_grade); }
return (_name);
}
int Bureaucrat::getGrade(void) const{ /* Setters */
return (_grade);
}
void Bureaucrat::incrGrade(int diff) void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) {
throw (Bureaucrat::GradeTooHighException) { int new_grade = _grade - diff;
int new_grade = _grade - diff; if (new_grade < 1)
if (new_grade < 1) throw Bureaucrat::GradeTooHighException();
throw Bureaucrat::GradeTooHighException(); _grade = new_grade;
_grade = new_grade; cout << _name << " grade increased." << endl;
} }
void Bureaucrat::decrGrade(int diff) void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
throw (Bureaucrat::GradeTooLowException) { int new_grade = _grade + diff;
int new_grade = _grade + diff; if (new_grade > 150)
if (new_grade > 150) throw Bureaucrat::GradeTooLowException();
throw Bureaucrat::GradeTooLowException(); _grade = new_grade;
_grade = new_grade; cout << _name << " grade decreased." << endl;
} }
const char* Bureaucrat::GradeTooHighException::what(void) const throw() { /* Exceptions */
return ("Grade was too high for a bureaucrat");
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() { const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
return ("Grade was too low for a bureaucrat"); return ("error: grade is too low for a bureaucrat");
} }
void Bureaucrat::signForm(Form &form) const { /* Externs */
try {
if (!form.beSigned(*this)) void Bureaucrat::signForm(Form &form) const {
cout << _name << " succesfully signed " << form.getName() << " form." << endl; cout << *this << " sign " << form << ":";
else try {
cout << _name << " failed to sign " << form.getName() << ", form is already signed." << endl; if (!form.beSigned(*this))
} catch (std::exception &e) { cout << " success."<< endl;
cout << _name << "couldn't sign form because his grade is too low" << 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) { /* Stream */
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
return (out); out << b.getName() << "(" << b.getGrade() << ")";
return (out);
} }

56
CPP05/ex01/Bureaucrat.hpp

@ -13,41 +13,41 @@
#pragma once #pragma once
#include "Form.hpp" #include "Form.hpp"
#include <string>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string>
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::string; using std::string;
class Form; class Form;
class Bureaucrat{ class Bureaucrat {
const string _name; const string _name;
int _grade; int _grade;
public:
public:
class GradeTooHighException: virtual public std::exception { class GradeTooHighException : virtual public std::exception {
public: public:
const char *what(void) const throw (); const char *what(void) const throw();
}; };
class GradeTooLowException: virtual public std::exception { class GradeTooLowException : virtual public std::exception {
public: public:
const char *what(void) const throw (); const char *what(void) const throw();
}; };
Bureaucrat(void); Bureaucrat(void);
Bureaucrat(const string name, int grade) Bureaucrat(const string name, int grade) throw(GradeTooHighException,
throw (GradeTooHighException, GradeTooLowException); GradeTooLowException);
Bureaucrat(Bureaucrat const & src); Bureaucrat(Bureaucrat const &src);
~Bureaucrat(void); ~Bureaucrat(void);
Bureaucrat & operator= (Bureaucrat const & src); Bureaucrat &operator=(Bureaucrat const &src);
const string getName(void) const; const string getName(void) const;
int getGrade(void) const; int getGrade(void) const;
void incrGrade(int diff = 1) throw (GradeTooHighException); void incrGrade(int diff = 1) throw(GradeTooHighException);
void decrGrade(int diff = 1) throw (GradeTooLowException); void decrGrade(int diff = 1) throw(GradeTooLowException);
void signForm(Form &form) const; void signForm(Form &form) const;
}; };
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); std::ostream &operator<<(std::ostream &out, Bureaucrat const &b);

96
CPP05/ex01/Form.cpp

@ -12,71 +12,69 @@
#include "Form.hpp" #include "Form.hpp"
std::ostream &operator<< (std::ostream &out, const Form &f) { /* Constructors */
out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade() << endl;
return (out); Form::Form(void) {}
};
Form::Form(const string name, int signGrade,
/* Defaults */ int exeGrade) throw(Form::GradeTooHighException,
Form::GradeTooLowException)
Form & Form::operator=(Form const &f) { : _name(name) {
(void)f; if (signGrade < 1 || exeGrade < 1)
cout << "Form assignement called" << endl; throw Form::GradeTooHighException();
return (*this); if (signGrade > 150 || exeGrade > 150)
throw Form::GradeTooLowException();
_signGrade = signGrade;
_exeGrade = exeGrade;
_isSigned = 0;
} }
Form::Form(Form const &f) { /* copy const. */
(void)f; Form::Form(Form const &f) { (void)f; }
cout << "Form copy called" << endl;
}
Form::~Form(void) { /* assign const. */
cout << "Form desctructor called" << endl; Form &Form::operator=(Form const &f) {
(void)f;
return (*this);
} }
/* Custom constructor */ /* Destructor */
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;
}
/* Getters */ /* Getters */
const string Form::getName(void) const { const string Form::getName(void) const { return (_name); }
return (_name);
}
int Form::getSignGrade(void) const { int Form::getSignGrade(void) const { return (_signGrade); }
return (_signGrade);
}
int Form::getExeGrade(void) const { int Form::getExeGrade(void) const { return (_exeGrade); }
return (_exeGrade);
}
/* Seter */ /* Setters */
bool Form::beSigned(const Bureaucrat &b) throw (Form::GradeTooLowException) { bool Form::beSigned(const Bureaucrat &b) throw(Form::GradeTooLowException) {
if (_isSigned) if (_isSigned)
return (1); return (1);
if (b.getGrade() > _signGrade) if (b.getGrade() > _signGrade)
throw GradeTooLowException(); throw GradeTooLowException();
_isSigned = 1; _isSigned = 1;
return (0); return (0);
} }
/* Except */ /* Except */
const char *Form::GradeTooHighException::what(void) const throw (){ const char *Form::GradeTooHighException::what(void) const throw() {
return ("Grade was too high for a form"); return ("error: grade is too high for a form");
} }
const char *Form::GradeTooLowException::what(void) const throw (){ const char *Form::GradeTooLowException::what(void) const throw() {
return ("Grade was too low for a form"); 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);
};

55
CPP05/ex01/Form.hpp

@ -13,39 +13,40 @@
#pragma once #pragma once
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include <string>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string>
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::string; using std::string;
class Bureaucrat; class Bureaucrat;
class Form {
const string _name;
int _signGrade;
int _exeGrade;
bool _isSigned;
class Form { public:
const string _name; class GradeTooHighException : virtual public std::exception {
int _signGrade; public:
int _exeGrade; const char *what(void) const throw();
bool _isSigned; };
public: class GradeTooLowException : virtual public std::exception {
class GradeTooHighException: virtual public std::exception { public:
public: const char *what(void) const throw();
const char *what(void) const throw (); };
}; Form(void);
class GradeTooLowException: virtual public std::exception { Form(const string name, const int signGrade,
public: const int exeGrade) throw(GradeTooHighException, GradeTooLowException);
const char *what(void) const throw (); Form &operator=(Form const &f);
}; Form(Form const &f);
Form(void); ~Form(void);
Form(const string name, const int signGrade,const int exeGrade) const string getName(void) const;
throw (GradeTooHighException, GradeTooLowException); int getSignGrade(void) const;
Form & operator=(Form const &f); int getExeGrade(void) const;
Form(Form const &f); bool beSigned(const Bureaucrat &b) throw(GradeTooLowException);
~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);

24
CPP05/ex01/main.cpp

@ -10,21 +10,19 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Form.hpp"
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include "Form.hpp"
int main(void) int main(void) {
{ Bureaucrat first("first", 1);
Bureaucrat first("first", 1); Bureaucrat mid("midle", 75);
Bureaucrat mid("midle", 75); Bureaucrat last("last", 150);
Bureaucrat last("last", 150); Form garb("garbage", 75, 75);
Form garb("garbage", 75, 75);
cout << first << mid << last << garb;
last.signForm(garb); cout << "Bureaucrats: " << first << "/" << mid << "/" << last << endl;
mid.signForm(garb); cout << "Forms: " << garb << endl << endl;
first.signForm(garb);
last.signForm(garb);
mid.signForm(garb);
first.signForm(garb);
} }

131
CPP05/ex02/Bureaucrat.cpp

@ -12,91 +12,92 @@
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat (void) { /* Constructors */
cout << "Bureaucrat default constructor called " << endl; 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) /* copy const. */
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ 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;
} }
Bureaucrat::Bureaucrat (Bureaucrat const & src) { /* assign const. */
(void)src; Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
cout << "Bureaucrat copy constructor called" << endl; (void)src;
return (*this);
} }
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { /* destructor */
(void)src; Bureaucrat::~Bureaucrat(void) {
cout << "Bureaucrat assignment operator called" << endl;
return (*this);
} }
Bureaucrat::~Bureaucrat (void) { /* Getters */
cout << "Bureaucrat default destructor called" << endl; const string Bureaucrat::getName(void) const { return (_name); }
}
const string Bureaucrat::getName(void) const{ int Bureaucrat::getGrade(void) const { return (_grade); }
return (_name);
}
int Bureaucrat::getGrade(void) const{
return (_grade);
}
void Bureaucrat::incrGrade(int diff) /* Exception */
throw (Bureaucrat::GradeTooHighException) { const char *Bureaucrat::GradeTooHighException::what(void) const throw() {
int new_grade = _grade - diff; return ("grade is too high for a bureaucrat");
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
_grade = new_grade;
} }
void Bureaucrat::decrGrade(int diff) const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
throw (Bureaucrat::GradeTooLowException) { return ("grade is too low for a bureaucrat");
int new_grade = _grade + diff;
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = new_grade;
} }
const char* Bureaucrat::GradeTooHighException::what(void) const throw() { /* Methods */
return ("Grade was too high for a bureaucrat"); 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() { void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
return ("Grade was too low for a bureaucrat"); int new_grade = _grade + diff;
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = new_grade;
} }
void Bureaucrat::signForm(Form &form) const { void Bureaucrat::signForm(Form &form) const {
try { cout << *this << " sign " << form << ":";
if (!form.beSigned(*this)) try {
cout << _name << " succesfully signed " << form.getName() << endl; if (!form.beSigned(*this))
else cout << " success." << endl;
cout << _name << " failed to sign " << form.getName() << ": form is already signed." << endl; else
} catch (std::exception &e) { cout << "error.\n\tform is already signed." << endl;
cout << _name << " failed to sign " << form.getName() << ": " << e.what() << endl; } catch (std::exception &e) {
} cout << "error.\n\t" << e.what() << endl;
}
} }
void Bureaucrat::executeForm(const Form &form) const { void Bureaucrat::executeForm(const Form &form) const {
try { cout << *this << " execute " << form << ":";
if (!form.execute(*this)) try {
cout << _name << " succesfully executed " << form.getName() << endl; if (!form.execute(*this))
else cout << " success." << endl;
cout << _name << " failed to execute " << form.getName() << ": form is not signed" << endl; else
} catch (std::exception &e) { cout << *this << " execute " << form.getName()
cout << _name << " failed to execute " << form.getName() << ": " << e.what() << endl; << "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) { /* Stream */
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
return (out); out << b.getName() << "(" << b.getGrade() << ")";
return (out);
} }

58
CPP05/ex02/Bureaucrat.hpp

@ -13,42 +13,42 @@
#pragma once #pragma once
#include "Form.hpp" #include "Form.hpp"
#include <string>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string>
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::string; using std::string;
class Form; class Form;
class Bureaucrat{ class Bureaucrat {
const string _name; const string _name;
int _grade; int _grade;
public:
public:
class GradeTooHighException: virtual public std::exception { class GradeTooHighException : virtual public std::exception {
public: public:
const char *what(void) const throw (); const char *what(void) const throw();
}; };
class GradeTooLowException: virtual public std::exception { class GradeTooLowException : virtual public std::exception {
public: public:
const char *what(void) const throw (); const char *what(void) const throw();
}; };
Bureaucrat(void); Bureaucrat(void);
Bureaucrat(const string name, int grade) Bureaucrat(const string name, int grade) throw(GradeTooHighException,
throw (GradeTooHighException, GradeTooLowException); GradeTooLowException);
Bureaucrat(Bureaucrat const & src); Bureaucrat(Bureaucrat const &src);
~Bureaucrat(void); ~Bureaucrat(void);
Bureaucrat & operator= (Bureaucrat const & src); Bureaucrat &operator=(Bureaucrat const &src);
const string getName(void) const; const string getName(void) const;
int getGrade(void) const; int getGrade(void) const;
void incrGrade(int diff = 1) throw (GradeTooHighException); void incrGrade(int diff = 1) throw(GradeTooHighException);
void decrGrade(int diff = 1) throw (GradeTooLowException); void decrGrade(int diff = 1) throw(GradeTooLowException);
void signForm(Form &form) const; void signForm(Form &form) const;
void executeForm(const 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);

96
CPP05/ex02/Form.cpp

@ -12,81 +12,75 @@
#include "Form.hpp" #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 */ /* Defaults */
Form & Form::operator=(Form const &f) { Form &Form::operator=(Form const &f) {
(void)f; (void)f;
cout << "Form assignement called" << endl; return (*this);
return (*this);
} }
Form::Form(Form const &f) Form::Form(Form const &f)
: _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade()) : _name(f.getName()), _signGrade(f.getSignGrade()),
{ _exeGrade(f.getExeGrade()) {
(void)f; (void)f;
cout << "Form copy called" << endl;
} }
Form::~Form(void) { Form::~Form(void) {}
cout << "Form destructor called" << endl;
}
/* Custom constructor */ /* Custom constructor */
Form::Form(const string name, const int signGrade, const int exeGrade, const string target) Form::Form(const string name, const int signGrade, const int exeGrade,
throw (Form::GradeTooHighException, Form::GradeTooLowException) const string target) throw(Form::GradeTooHighException,
: _name(name), _signGrade(signGrade), _exeGrade(exeGrade), _target(target){ Form::GradeTooLowException)
if (signGrade < 1 || exeGrade < 1) : _name(name), _signGrade(signGrade), _exeGrade(exeGrade), _target(target) {
throw Form::GradeTooHighException(); if (signGrade < 1 || exeGrade < 1)
if (signGrade > 150 || exeGrade > 150) throw Form::GradeTooHighException();
throw Form::GradeTooLowException(); if (signGrade > 150 || exeGrade > 150)
throw Form::GradeTooLowException();
_isSigned = 0;
} }
/* Getters */ /* Getters */
const string Form::getName(void) const { const string Form::getName(void) const { return (_name); }
return (_name);
}
int Form::getSignGrade(void) const { int Form::getSignGrade(void) const { return (_signGrade); }
return (_signGrade);
}
int Form::getExeGrade(void) const { int Form::getExeGrade(void) const { return (_exeGrade); }
return (_exeGrade);
}
/* Seter */ /* Setters */
int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) { int Form::beSigned(const Bureaucrat &signer) throw(Form::GradeTooLowException) {
if (_isSigned) if (_isSigned)
return (-1); return (-1);
if (signer.getGrade() > _signGrade) if (signer.getGrade() > _signGrade)
throw GradeTooLowException(); throw GradeTooLowException();
_isSigned = 1; _isSigned = 1;
return (0); return (0);
} }
int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException) { int Form::execute(Bureaucrat const &executor) const
if (executor.getGrade() > _exeGrade) throw(GradeTooLowException) {
throw Form::GradeTooLowException(); if (executor.getGrade() > _exeGrade)
if (!_isSigned) throw Form::GradeTooLowException();
return (-1); if (!_isSigned)
this->run(); return (-1);
return (0); this->run();
return (0);
} }
/* Except */ /* Except */
const char *Form::GradeTooHighException::what(void) const throw (){ 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 (){ 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);
};

67
CPP05/ex02/Form.hpp

@ -13,45 +13,42 @@
#pragma once #pragma once
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include <string>
#include <iostream>
#include <fstream> #include <fstream>
#include <stdexcept>
#include <unistd.h> #include <unistd.h>
using std::cout;
using std::endl;
using std::string; using std::string;
class Bureaucrat; class Bureaucrat;
class Form { class Form {
const string _name; const string _name;
const int _signGrade; const int _signGrade;
const int _exeGrade; const int _exeGrade;
bool _isSigned; bool _isSigned;
public:
class GradeTooHighException: virtual public std::exception { public:
public: class GradeTooHighException : virtual public std::exception {
const char *what(void) const throw (); public:
}; const char *what(void) const throw();
class GradeTooLowException: virtual public std::exception { };
public: class GradeTooLowException : virtual public std::exception {
const char *what(void) const throw (); public:
}; const char *what(void) const throw();
virtual ~Form(void) = 0; };
const string getName(void) const; virtual ~Form(void) = 0;
int getSignGrade(void) const; const string getName(void) const;
int getExeGrade(void) const; int getSignGrade(void) const;
int beSigned(const Bureaucrat &signer) throw (GradeTooLowException); int getExeGrade(void) const;
int execute(Bureaucrat const &executor) const throw (GradeTooLowException); int beSigned(const Bureaucrat &signer) throw(GradeTooLowException);
virtual void run(void) const = 0; int execute(Bureaucrat const &executor) const throw(GradeTooLowException);
protected: virtual void run(void) const = 0;
Form(void);
Form(const string name, const int signGrade,const int exeGrade, const string target) protected:
throw (GradeTooHighException, GradeTooLowException); Form(void);
Form & operator=(Form const &f); Form(const string name, const int signGrade, const int exeGrade,
Form(Form const &f); const string target) throw(GradeTooHighException, GradeTooLowException);
const string _target; 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);

15
CPP05/ex02/PresidentialPardonForm.cpp

@ -12,16 +12,11 @@
#include "PresidentialPardonForm.hpp" #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) {}
}
PresidentialPardonForm::~PresidentialPardonForm (void) {
cout << "PresidentialPardonForm default destructor called" << endl;
}
void PresidentialPardonForm::run(void) const { void PresidentialPardonForm::run(void) const {
cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl;
} }

11
CPP05/ex02/PresidentialPardonForm.hpp

@ -14,11 +14,10 @@
#include "Form.hpp" #include "Form.hpp"
class PresidentialPardonForm: public Form { class PresidentialPardonForm : public Form {
public: public:
PresidentialPardonForm(const string target);
PresidentialPardonForm(const string target); ~PresidentialPardonForm(void);
~PresidentialPardonForm(void); void run(void) const;
void run(void) const;
}; };

36
CPP05/ex02/RobotomyRequestForm.cpp

@ -12,27 +12,23 @@
#include "RobotomyRequestForm.hpp" #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) {}
}
RobotomyRequestForm::~RobotomyRequestForm (void) {
cout << "RobotomyRequestForm default destructor called" << endl;
}
void RobotomyRequestForm::run(void) const { void RobotomyRequestForm::run(void) const {
int r = rand(); srand(time(NULL));
int r = rand();
cout << "ratatttaatata" << endl;
sleep(1); cout << "ratatttaatata" << endl;
cout << "ratata tatata" << endl; sleep(1);
sleep(1); cout << "ratata tatata" << endl;
cout << "ratata" << endl; sleep(1);
sleep(1); cout << "ratata" << endl;
if (r % 2) sleep(1);
cout<< _target << " have been robotomized" << endl; if (r % 2)
else cout << _target << " have been robotomized" << endl;
cout << _target << " robotomization failed" << endl; else
cout << _target << " robotomization failed" << endl;
} }

13
CPP05/ex02/RobotomyRequestForm.hpp

@ -13,12 +13,13 @@
#pragma once #pragma once
#include "Form.hpp" #include "Form.hpp"
#include <time.h>
#include <algorithm>
class RobotomyRequestForm: public Form { class RobotomyRequestForm : public Form {
public: public:
RobotomyRequestForm(const string target);
RobotomyRequestForm(const string target); ~RobotomyRequestForm(void);
~RobotomyRequestForm(void); void run(void) const;
void run(void) const;
}; };

35
CPP05/ex02/ShrubberyCreationForm.cpp

@ -12,26 +12,23 @@
#include "ShrubberyCreationForm.hpp" #include "ShrubberyCreationForm.hpp"
ShrubberyCreationForm::ShrubberyCreationForm (string target): Form("ShrubberyCreationForm", 147, 137, target) { ShrubberyCreationForm::ShrubberyCreationForm(string target)
cout << "ShrubberyCreationForm parameter constructor called" << endl; : Form("ShrubberyCreationForm", 147, 137, target) {}
}
ShrubberyCreationForm::~ShrubberyCreationForm (void) { ShrubberyCreationForm::~ShrubberyCreationForm(void) {}
cout << "ShrubberyCreationForm default destructor called" << endl;
}
void ShrubberyCreationForm::run(void) const { void ShrubberyCreationForm::run(void) const {
std::ofstream file; std::ofstream file;
file.open(_target + "shrubbery"); file.open((_target + "shrubbery").c_str());
file << " ,@@@@@@@," << endl; file << " ,@@@@@@@," << endl;
file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl; file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl;
file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl; file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl;
file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl; file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl;
file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl; file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl;
file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl; file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl;
file << " `&%\\ ` /%&' |.| \\ '|8'" << endl; file << " `&%\\ ` /%&' |.| \\ '|8'" << endl;
file << " |o| | | | |" << endl; file << " |o| | | | |" << endl;
file << " |.| | | | |" << endl; file << " |.| | | | |" << endl;
file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl; file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl;
file.close(); file.close();
} }

10
CPP05/ex02/ShrubberyCreationForm.hpp

@ -13,10 +13,10 @@
#pragma once #pragma once
#include "Form.hpp" #include "Form.hpp"
class ShrubberyCreationForm: public Form { class ShrubberyCreationForm : public Form {
public: public:
ShrubberyCreationForm(string target); ShrubberyCreationForm(string target);
~ShrubberyCreationForm(void); ~ShrubberyCreationForm(void);
void run(void) const; void run(void) const;
}; };

45
CPP05/ex02/main.cpp

@ -10,30 +10,29 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"
int main(void) int main(void) {
{ Bureaucrat first("first", 1);
Bureaucrat first("first", 1); Bureaucrat mid("midle", 75);
Bureaucrat mid("midle", 75); Bureaucrat last("last", 150);
Bureaucrat last("last", 150);
ShrubberyCreationForm houseForm("House"); ShrubberyCreationForm houseForm("House");
PresidentialPardonForm gerardForm("Gerard"); PresidentialPardonForm gerardForm("Gerard");
RobotomyRequestForm biduleForm("Bidule"); RobotomyRequestForm biduleForm("Bidule");
last.signForm(houseForm); last.signForm(houseForm);
last.signForm(gerardForm); last.signForm(gerardForm);
last.signForm(biduleForm); last.signForm(biduleForm);
first.signForm(houseForm); first.signForm(houseForm);
first.signForm(gerardForm); first.signForm(gerardForm);
first.signForm(biduleForm); first.signForm(biduleForm);
last.executeForm(houseForm); last.executeForm(houseForm);
last.executeForm(gerardForm); last.executeForm(gerardForm);
last.executeForm(biduleForm); last.executeForm(biduleForm);
first.executeForm(houseForm); first.executeForm(houseForm);
first.executeForm(gerardForm); first.executeForm(gerardForm);
first.executeForm(biduleForm); first.executeForm(biduleForm);
} }

119
CPP05/ex03/Bureaucrat.cpp

@ -12,91 +12,86 @@
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat (void) { Bureaucrat::Bureaucrat(void) {
cout << "Bureaucrat default constructor called " << endl;
} }
Bureaucrat::Bureaucrat (const string name, int grade) Bureaucrat::Bureaucrat(const string name,
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){ int grade) throw(Bureaucrat::GradeTooHighException,
Bureaucrat::GradeTooLowException)
: _name(name) {
if (grade < 1) if (grade < 1)
throw Bureaucrat::GradeTooHighException(); throw Bureaucrat::GradeTooHighException();
if (grade > 150) if (grade > 150)
throw Bureaucrat::GradeTooLowException(); throw Bureaucrat::GradeTooLowException();
_grade = grade; _grade = grade;
cout << "Bureaucrat " << _name << " created grade: " << _grade << endl;
} }
Bureaucrat::Bureaucrat (Bureaucrat const & src) { Bureaucrat::Bureaucrat(Bureaucrat const &src) {
(void)src; (void)src;
cout << "Bureaucrat copy constructor called" << endl;
} }
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) { Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
(void)src; (void)src;
cout << "Bureaucrat assignment operator called" << endl; return (*this);
return (*this);
} }
Bureaucrat::~Bureaucrat (void) { Bureaucrat::~Bureaucrat(void) {
cout << "Bureaucrat default destructor called" << endl;
} }
const string Bureaucrat::getName(void) const{ const string Bureaucrat::getName(void) const { return (_name); }
return (_name);
}
int Bureaucrat::getGrade(void) const{ int Bureaucrat::getGrade(void) const { return (_grade); }
return (_grade);
}
void Bureaucrat::incrGrade(int diff) void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) {
throw (Bureaucrat::GradeTooHighException) { int new_grade = _grade - diff;
int new_grade = _grade - diff; if (new_grade < 1)
if (new_grade < 1) throw Bureaucrat::GradeTooHighException();
throw Bureaucrat::GradeTooHighException(); _grade = new_grade;
_grade = new_grade;
} }
void Bureaucrat::decrGrade(int diff) void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
throw (Bureaucrat::GradeTooLowException) { int new_grade = _grade + diff;
int new_grade = _grade + diff; if (new_grade > 150)
if (new_grade > 150) throw Bureaucrat::GradeTooLowException();
throw Bureaucrat::GradeTooLowException(); _grade = new_grade;
_grade = new_grade;
} }
const char* Bureaucrat::GradeTooHighException::what(void) const throw() { const char *Bureaucrat::GradeTooHighException::what(void) const throw() {
return ("Grade was too high for a bureaucrat"); return ("Grade was too high for a bureaucrat");
} }
const char* Bureaucrat::GradeTooLowException::what(void) const throw() { const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
return ("Grade was too low for a bureaucrat"); return ("Grade was too low for a bureaucrat");
} }
void Bureaucrat::signForm(Form &form) const { void Bureaucrat::signForm(Form &form) const {
try { try {
if (!form.beSigned(*this)) if (!form.beSigned(*this))
cout << _name << " succesfully signed " << form.getName() << endl; cout << _name << " succesfully signed " << form.getName() << endl;
else else
cout << _name << " failed to sign " << form.getName() << ": form is already signed." << endl; cout << _name << " failed to sign " << form.getName()
} catch (std::exception &e) { << ": form is already signed." << endl;
cout << _name << " failed to sign " << form.getName() << ": " << e.what() << endl; } catch (std::exception &e) {
} cout << _name << " failed to sign " << form.getName() << ": " << e.what()
<< endl;
}
} }
void Bureaucrat::executeForm(const Form &form) const { void Bureaucrat::executeForm(const Form &form) const {
try { try {
if (!form.execute(*this)) if (!form.execute(*this))
cout << _name << " succesfully executed " << form.getName() << endl; cout << _name << " succesfully executed " << form.getName() << endl;
else else
cout << _name << " failed to execute " << form.getName() << ": form is not signed" << endl; cout << _name << " failed to execute " << form.getName()
} catch (std::exception &e) { << ": form is not signed" << endl;
cout << _name << " failed to execute " << form.getName() << ": " << e.what() << endl; } catch (std::exception &e) {
} cout << _name << " failed to execute " << form.getName() << ": " << e.what()
<< endl;
}
} }
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) { std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl;
return (out); return (out);
} }

21
CPP05/ex03/Form.cpp

@ -12,16 +12,10 @@
#include "Form.hpp" #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 */ /* Defaults */
Form & Form::operator=(Form const &f) { Form & Form::operator=(Form const &f) {
(void)f; (void)f;
cout << "Form assignement called" << endl;
return (*this); return (*this);
} }
@ -29,11 +23,9 @@ Form::Form(Form const &f)
: _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade()) : _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade())
{ {
(void)f; (void)f;
cout << "Form copy called" << endl;
} }
Form::~Form(void) { Form::~Form(void) {
cout << "Form destructor called" << endl;
} }
/* Custom constructor */ /* Custom constructor */
@ -45,6 +37,7 @@ Form::Form(const string name, const int signGrade, const int exeGrade, const str
throw Form::GradeTooHighException(); throw Form::GradeTooHighException();
if (signGrade > 150 || exeGrade > 150) if (signGrade > 150 || exeGrade > 150)
throw Form::GradeTooLowException(); throw Form::GradeTooLowException();
_isSigned = 0;
} }
/* Getters */ /* Getters */
@ -61,7 +54,7 @@ int Form::getExeGrade(void) const {
return (_exeGrade); return (_exeGrade);
} }
/* Seter */ /* Setters */
int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) { int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) {
if (_isSigned) if (_isSigned)
@ -84,9 +77,15 @@ int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException)
/* Except */ /* Except */
const char *Form::GradeTooHighException::what(void) const throw (){ 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 (){ 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);
};

53
CPP05/ex03/Intern.cpp

@ -12,46 +12,37 @@
#include "Intern.hpp" #include "Intern.hpp"
Intern::Intern (void) { Intern::Intern(void) {}
cout << "Intern default constructor called " << endl;
}
Intern::Intern (Intern const & src) { Intern::Intern(Intern const &src) { (void)src; }
(void)src;
cout << "Intern copy constructor called" << endl;
}
Intern & Intern::operator= (Intern const & src) { Intern &Intern::operator=(Intern const &src) {
(void)src; (void)src;
cout << "Intern assignment operator called" << endl; return (*this);
return (*this);
} }
Intern::~Intern (void) { Intern::~Intern(void) {}
cout << "Intern default destructor called" << endl;
}
Form * createRobotForm(string targ){ Form *createRobotForm(string targ) {
return (new RobotomyRequestForm(targ)); return (new RobotomyRequestForm(targ));
} }
Form * createPresidentForm(string targ) { Form *createPresidentForm(string targ) {
return (new PresidentialPardonForm(targ)); return (new PresidentialPardonForm(targ));
} }
Form * createShrubberyForm(string targ) { Form *createShrubberyForm(string targ) {
return (new ShrubberyCreationForm(targ)); return (new ShrubberyCreationForm(targ));
} }
Form * Intern::makeForm(string name, string target) Form *Intern::makeForm(string name, string target) {
{ string subFormsName[3] = {"robotomy request", "president pardon",
string subFormsName[3] = {"robotomy request", "president pardon", "shrubbery creation"}; "shrubbery creation"};
Form * (*subFormsCreation[3])(string targ) =\ Form *(*subFormsCreation[3])(string targ) = {
{createRobotForm, createPresidentForm, createShrubberyForm}; createRobotForm, createPresidentForm, createShrubberyForm};
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
if (name == subFormsName[i]) if (name == subFormsName[i])
return (subFormsCreation[i](target)); return (subFormsCreation[i](target));
cout << "Intern fail to create " << name << ": unknow form name" << endl; cout << "Intern fail to create " << name << ": unknow form name" << endl;
return (NULL); return (NULL);
} }

16
CPP05/ex03/PresidentialPardonForm.cpp

@ -12,16 +12,16 @@
#include "PresidentialPardonForm.hpp" #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) { PresidentialPardonForm::~PresidentialPardonForm(void)
{
cout << "PresidentialPardonForm default destructor called" << endl;
} }
void PresidentialPardonForm::run(void) const { void PresidentialPardonForm::run(void) const
cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; {
cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl;
} }

39
CPP05/ex03/RobotomyRequestForm.cpp

@ -12,27 +12,28 @@
#include "RobotomyRequestForm.hpp" #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) { RobotomyRequestForm::~RobotomyRequestForm(void)
{
cout << "RobotomyRequestForm default destructor called" << endl;
} }
void RobotomyRequestForm::run(void) const { void RobotomyRequestForm::run(void) const
int r = rand(); {
srand(time(NULL));
cout << "ratatttaatata" << endl; int r = rand();
sleep(1);
cout << "ratata tatata" << endl; cout << "ratatttaatata" << endl;
sleep(1); sleep(1);
cout << "ratata" << endl; cout << "ratata tatata" << endl;
sleep(1); sleep(1);
if (r % 2) cout << "ratata" << endl;
cout<< _target << " have been robotomized" << endl; sleep(1);
else if (r % 2)
cout << _target << " robotomization failed" << endl; cout << _target << " have been robotomized" << endl;
else
cout << _target << " robotomization failed" << endl;
} }

2
CPP05/ex03/RobotomyRequestForm.hpp

@ -13,6 +13,8 @@
#pragma once #pragma once
#include "Form.hpp" #include "Form.hpp"
#include <time.h>
#include <algorithm>
class RobotomyRequestForm: public Form { class RobotomyRequestForm: public Form {

38
CPP05/ex03/ShrubberyCreationForm.cpp

@ -12,26 +12,28 @@
#include "ShrubberyCreationForm.hpp" #include "ShrubberyCreationForm.hpp"
ShrubberyCreationForm::ShrubberyCreationForm (string target): Form("ShrubberyCreationForm", 147, 137, target) { ShrubberyCreationForm::ShrubberyCreationForm(string target)
cout << "ShrubberyCreationForm parameter constructor called" << endl; : Form("ShrubberyCreationForm", 147, 137, target)
{
} }
ShrubberyCreationForm::~ShrubberyCreationForm (void) { ShrubberyCreationForm::~ShrubberyCreationForm(void)
cout << "ShrubberyCreationForm default destructor called" << endl; {
} }
void ShrubberyCreationForm::run(void) const { void ShrubberyCreationForm::run(void) const
std::ofstream file; {
file.open(_target + "_shrubbery"); std::ofstream file;
file << " ,@@@@@@@," << endl; file.open((_target + "_shrubbery").c_str());
file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl; file << " ,@@@@@@@," << endl;
file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl; file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl;
file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl; file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl;
file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl; file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl;
file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl; file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl;
file << " `&%\\ ` /%&' |.| \\ '|8'" << endl; file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl;
file << " |o| | | | |" << endl; file << " `&%\\ ` /%&' |.| \\ '|8'" << endl;
file << " |.| | | | |" << endl; file << " |o| | | | |" << endl;
file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl; file << " |.| | | | |" << endl;
file.close(); file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl;
file.close();
} }

10
CPP05/ex03/house_shrubbery

@ -1,10 +0,0 @@
,@@@@@@@,
,,,. ,@@@@@@/@@, .oo8888o.
,&%%&%&&%,@@@@@/@@@@@@,8888\88/8o
,%&\%&&%&&%,@@@\@@@/@@@88\88888/88'
%&&%&%&/%&&%@@\@@/ /@@@88888\88888'
%&&%/ %&%%&&@@\ V /@@' `88\8 `/88'
`&%\ ` /%&' |.| \ '|8'
|o| | | | |
|.| | | | |
\\/ ._\//_/__/ ,\_//__\\/. \_//__/_

93
CPP06/ex00/Converter.cpp

@ -11,57 +11,64 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "Converter.hpp" #include "Converter.hpp"
using std::cout;
using std::endl;
Converter::Converter (void) { Converter::Converter(void) {
cout << "Converter default constructor called " << endl; cout << "Converter default constructor called " << endl;
} }
Converter::Converter (string str){ Converter::Converter(string str) {
if (str[0] == '\'' || str[0] == '"') if (str[0] == '\'' || str[0] == '"') {
{ _c = str[1];
_c = str[1]; cout << "Char: " << _c << endl;
cout << "Char: " << _c << endl; _d = static_cast<double>(_c);
_d = static_cast<double>(_c); } else {
} cout << "Char: ";
else _d = std::strtod(str.c_str(), 0);
{ _c = static_cast<char>(_d);
cout << "Char: "; if (_d < 0 || _d > 255)
_d = std::strtod(str.c_str(), 0); cout << "Invalid" << endl;
_c = static_cast<char>(_d); else if (isnan(_d))
if (_d < 0 || _d > 255) cout << "Impossible" << endl;
cout << "Invalid" << endl; else if (!std::isprint(_c))
else if (isnan(_d)) cout << "Not Printable" << endl;
cout << "Impossible" << endl; else
else if (!std::isprint(_c)) cout << "'" << _c << "'" << endl;
cout << "Not Printable" << endl; }
else cout << "Integer: ";
cout << "'" << _c << "'" << endl; _i = static_cast<int>(_d);
} if (_d < std::numeric_limits<int>::min() ||
cout << "Integer: "; _d > std::numeric_limits<int>::max())
_i = static_cast<int>(_d); cout << "Off limits -> ";
if (_d < std::numeric_limits<int>::min() || _d > std::numeric_limits<int>::max()) if (std::isnan(_d))
cout << "Off limits -> "; cout << "Impossible" << endl;
if (isnan(_d)) else
cout << "Impossible" << endl; cout << _i << endl;
else _f = static_cast<float>(_d);
cout << _i << endl; if (_d == 0 || _d / static_cast<int>(_d) == 1) {
cout << "Double: " << _d << endl; cout << "Double: " << std::setprecision(1) << std::fixed << _d<< endl;
_f = static_cast<float>(_d); cout << "Float: " << std::setprecision(1) << std::fixed << _f << "f" << endl;
cout << "Float: " << _f << "f"<< endl; }
else
{
cout << "Double: " << _d << endl;
cout << "Float: " << _f << "f" << endl;
}
} }
Converter::Converter (Converter const & src) { Converter::Converter(Converter const &src) {
(void)src; (void)src;
cout << "Converter copy constructor called" << endl; cout << "Converter copy constructor called" << endl;
} }
Converter & Converter::operator= (Converter const & src) { Converter &Converter::operator=(Converter const &src) {
(void)src; (void)src;
cout << "Converter assignment operator called" << endl; cout << "Converter assignment operator called" << endl;
return (*this); return (*this);
} }
Converter::~Converter (void) { Converter::~Converter(void) {
cout << "Converter default destructor called" << endl; cout << "Converter default destructor called" << endl;
} }

6
CPP06/ex00/Converter.hpp

@ -19,9 +19,8 @@
#include <cctype> #include <cctype>
#include <limits> #include <limits>
#include <cmath> #include <cmath>
#include <iomanip>
using std::cout;
using std::endl;
using std::string; using std::string;
class Converter{ class Converter{
@ -31,10 +30,9 @@ class Converter{
int _i; int _i;
float _f; float _f;
double _d; double _d;
//int _types[4];
public: public:
Converter(void);
Converter(string str); Converter(string str);
Converter(void);
Converter(Converter const & src); Converter(Converter const & src);
virtual ~Converter(void); virtual ~Converter(void);
Converter & operator= (Converter const & src); Converter & operator= (Converter const & src);

2
CPP06/ex00/main.cpp

@ -13,8 +13,6 @@
#include <iostream> #include <iostream>
#include "Converter.hpp" #include "Converter.hpp"
using std::cout;
using std::endl;
using std::string; using std::string;
int main(int ac, char **av) { int main(int ac, char **av) {

18
CPP06/ex01/Data.h

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Data.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 09:28:26 by narnaud #+# #+# */
/* Updated: 2022/09/29 09:28:45 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
typedef struct s_Data {
std::string content;
} t_Data;

19
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

40
CPP06/ex01/main.cpp

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud </var/spool/mail/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<uintptr_t>(ptr);
}
t_Data* deserialize(uintptr_t raw) {
return reinterpret_cast<t_Data *>(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);
}

21
CPP06/ex02/A.hpp

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* A.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
};

19
CPP06/ex02/B.hpp

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* B.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
};

19
CPP06/ex02/Base.hpp

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Base.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
};

19
CPP06/ex02/C.hpp

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* C.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
};

11
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){}

17
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

90
CPP06/ex02/main.cpp

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 10:09:08 by narnaud #+# #+# */
/* Updated: 2022/09/29 10:36:06 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <time.h>
#include <unistd.h>
#include <cstdlib>
#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<A *>(ptr);
if (a)
std::cout << "Identified a A" << std::endl;
B *b = dynamic_cast<B *>(ptr);
if (b)
std::cout << "Identified a B" << std::endl;
C *c = dynamic_cast<C *>(ptr);
if (c)
std::cout << "Identified a C" << std::endl;
}
void identify(Base &ref) {
try {
A a = dynamic_cast<A&>(ref);
std::cout << "Identified a A" << std::endl;
}
catch (std::exception &e) { (void)e;}
try {
B b = dynamic_cast<B &>(ref);
std::cout << "Identified a B" << std::endl;
}
catch (std::exception &e) { (void)e;}
try {
C c = dynamic_cast<C &>(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;
}
}

BIN
CPP08/cpp08.pdf

Binary file not shown.

7
CPP08/ex00/Makefile

@ -1,11 +1,12 @@
NAME = easy_find NAME = easyfind
SRCS = SRCS = main.cpp
OBJS= $(SRCS:.cpp=.o) OBJS= $(SRCS:.cpp=.o)
CXX = c++
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME) : $(OBJS) $(NAME) : $(OBJS)
c++ $(OBJS) -o $(NAME) $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME)
all : $(NAME) all : $(NAME)

35
CPP08/ex00/easyfind.hpp

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* easyfind.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 08:06:39 by narnaud #+# #+# */
/* Updated: 2022/09/19 08:06:42 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <algorithm>
#include <exception>
#include <iostream>
#include <iterator>
template <class T>
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 <class T>
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;
}

43
CPP08/ex00/main.cpp

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 08:06:21 by narnaud #+# #+# */
/* Updated: 2022/09/19 08:06:26 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "easyfind.hpp"
#include <iostream>
#include <list>
#include <string>
#include <vector>
int main(void) {
std::vector<int> tab;
std::vector<int>::iterator tab_it;
std::list<int> lst;
std::list<int>::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);
}

22
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

69
CPP08/ex01/Span.cpp

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<int>::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<int>::iterator begin, std::vector<int>::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));
}

37
CPP08/ex01/Span.hpp

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Span.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 08:07:26 by narnaud #+# #+# */
/* Updated: 2022/09/19 08:07:31 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <exception>
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <time.h>
#include <numeric>
class Span {
std::vector<int> _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<int>::iterator begin, std::vector<int>::iterator end);
};

47
CPP08/ex01/main.cpp

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<int> 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);
}

22
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

30
CPP08/ex02/MutantStack.hpp

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MutantStack.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 09:39:40 by narnaud #+# #+# */
/* Updated: 2022/09/19 09:39:45 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#include <stack>
template <class T> class MutantStack : public std::stack<T> {
public:
MutantStack<T>(void) {}
MutantStack<T>(const MutantStack<T> &st) { *this = st; }
~MutantStack<T>(void) {}
MutantStack<T> &operator=(const MutantStack<T> &st) {
(void)st;
return *this;
}
typedef typename std::stack<T>::container_type::iterator iterator;
iterator begin() { return this->c.begin(); }
iterator end() { return this->c.end(); }
};

37
CPP08/ex02/main.cpp

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<int> 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<int>::iterator it = mstack.begin();
MutantStack<int>::iterator ite = mstack.end();
++it;
--it;
while (it != ite) {
std::cout << *it << std::endl;
++it;
}
std::stack<int> s(mstack);
return 0;
}
Loading…
Cancel
Save