Browse Source

finish?

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

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

25
CPP05/ex00/Bureaucrat.hpp

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

3
CPP05/ex00/Makefile

@ -2,10 +2,11 @@ NAME = bureau
SRCS = main.cpp Bureaucrat.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)

48
CPP05/ex00/main.cpp

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

64
CPP05/ex01/Bureaucrat.cpp

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

8
CPP05/ex01/Bureaucrat.hpp

@ -13,9 +13,9 @@
#pragma once
#include "Form.hpp"
#include <string>
#include <iostream>
#include <stdexcept>
#include <string>
using std::cout;
using std::endl;
using std::string;
@ -25,8 +25,8 @@ class Form;
class Bureaucrat {
const string _name;
int _grade;
public:
public:
class GradeTooHighException : virtual public std::exception {
public:
const char *what(void) const throw();
@ -37,8 +37,8 @@ class Bureaucrat{
};
Bureaucrat(void);
Bureaucrat(const string name, int grade)
throw (GradeTooHighException, GradeTooLowException);
Bureaucrat(const string name, int grade) throw(GradeTooHighException,
GradeTooLowException);
Bureaucrat(Bureaucrat const &src);
~Bureaucrat(void);
Bureaucrat &operator=(Bureaucrat const &src);

70
CPP05/ex01/Form.cpp

@ -12,55 +12,44 @@
#include "Form.hpp"
std::ostream &operator<< (std::ostream &out, const Form &f) {
out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade() << endl;
return (out);
};
/* Defaults */
Form & Form::operator=(Form const &f) {
(void)f;
cout << "Form assignement called" << endl;
return (*this);
}
Form::Form(Form const &f) {
(void)f;
cout << "Form copy called" << endl;
}
Form::~Form(void) {
cout << "Form desctructor called" << endl;
}
/* Constructors */
/* Custom constructor */
Form::Form(void) {}
Form::Form(const string name, int signGrade, int exeGrade)
throw (Form::GradeTooHighException, Form::GradeTooLowException): _name(name){
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;
}
/* Getters */
/* copy const. */
Form::Form(Form const &f) { (void)f; }
const string Form::getName(void) const {
return (_name);
/* assign const. */
Form &Form::operator=(Form const &f) {
(void)f;
return (*this);
}
int Form::getSignGrade(void) const {
return (_signGrade);
}
/* Destructor */
Form::~Form(void) {}
int Form::getExeGrade(void) const {
return (_exeGrade);
}
/* Getters */
/* Seter */
const string Form::getName(void) const { return (_name); }
int Form::getSignGrade(void) const { return (_signGrade); }
int Form::getExeGrade(void) const { return (_exeGrade); }
/* Setters */
bool Form::beSigned(const Bureaucrat &b) throw(Form::GradeTooLowException) {
if (_isSigned)
@ -74,9 +63,18 @@ bool Form::beSigned(const Bureaucrat &b) throw (Form::GradeTooLowException) {
/* Except */
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() {
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);
};

7
CPP05/ex01/Form.hpp

@ -13,9 +13,9 @@
#pragma once
#include "Bureaucrat.hpp"
#include <string>
#include <iostream>
#include <stdexcept>
#include <string>
using std::cout;
using std::endl;
using std::string;
@ -27,6 +27,7 @@ class Form {
int _signGrade;
int _exeGrade;
bool _isSigned;
public:
class GradeTooHighException : virtual public std::exception {
public:
@ -37,8 +38,8 @@ class Form {
const char *what(void) const throw();
};
Form(void);
Form(const string name, const int signGrade,const int exeGrade)
throw (GradeTooHighException, GradeTooLowException);
Form(const string name, const int signGrade,
const int exeGrade) throw(GradeTooHighException, GradeTooLowException);
Form &operator=(Form const &f);
Form(Form const &f);
~Form(void);

10
CPP05/ex01/main.cpp

@ -10,21 +10,19 @@
/* */
/* ************************************************************************** */
#include "Form.hpp"
#include "Bureaucrat.hpp"
#include "Form.hpp"
int main(void)
{
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;
cout << "Bureaucrats: " << first << "/" << mid << "/" << last << endl;
cout << "Forms: " << garb << endl << endl;
last.signForm(garb);
mid.signForm(garb);
first.signForm(garb);
}

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

8
CPP05/ex02/Bureaucrat.hpp

@ -13,9 +13,9 @@
#pragma once
#include "Form.hpp"
#include <string>
#include <iostream>
#include <stdexcept>
#include <string>
using std::cout;
using std::endl;
using std::string;
@ -25,8 +25,8 @@ class Form;
class Bureaucrat {
const string _name;
int _grade;
public:
public:
class GradeTooHighException : virtual public std::exception {
public:
const char *what(void) const throw();
@ -37,8 +37,8 @@ class Bureaucrat{
};
Bureaucrat(void);
Bureaucrat(const string name, int grade)
throw (GradeTooHighException, GradeTooLowException);
Bureaucrat(const string name, int grade) throw(GradeTooHighException,
GradeTooLowException);
Bureaucrat(Bureaucrat const &src);
~Bureaucrat(void);
Bureaucrat &operator=(Bureaucrat const &src);

48
CPP05/ex02/Form.cpp

@ -12,56 +12,43 @@
#include "Form.hpp"
std::ostream &operator<< (std::ostream &out, const Form &f) {
out << "Form: " << f.getName() << " - Required grade to sign: " << f.getSignGrade() << " - Required grade to execute: " << f.getExeGrade() << endl;
return (out);
};
/* Defaults */
Form &Form::operator=(Form const &f) {
(void)f;
cout << "Form assignement called" << endl;
return (*this);
}
Form::Form(Form const &f)
: _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade())
{
: _name(f.getName()), _signGrade(f.getSignGrade()),
_exeGrade(f.getExeGrade()) {
(void)f;
cout << "Form copy called" << endl;
}
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)
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)
@ -72,7 +59,8 @@ int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException)
return (0);
}
int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException) {
int Form::execute(Bureaucrat const &executor) const
throw(GradeTooLowException) {
if (executor.getGrade() > _exeGrade)
throw Form::GradeTooLowException();
if (!_isSigned)
@ -84,9 +72,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);
};

11
CPP05/ex02/Form.hpp

@ -13,13 +13,8 @@
#pragma once
#include "Bureaucrat.hpp"
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <unistd.h>
using std::cout;
using std::endl;
using std::string;
class Bureaucrat;
@ -29,6 +24,7 @@ class Form {
const int _signGrade;
const int _exeGrade;
bool _isSigned;
public:
class GradeTooHighException : virtual public std::exception {
public:
@ -45,10 +41,11 @@ class Form {
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(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;

11
CPP05/ex02/PresidentialPardonForm.cpp

@ -12,15 +12,10 @@
#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;

1
CPP05/ex02/PresidentialPardonForm.hpp

@ -17,7 +17,6 @@
class PresidentialPardonForm : public Form {
public:
PresidentialPardonForm(const string target);
~PresidentialPardonForm(void);
void run(void) const;

12
CPP05/ex02/RobotomyRequestForm.cpp

@ -12,17 +12,13 @@
#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 {
srand(time(NULL));
int r = rand();
cout << "ratatttaatata" << endl;

3
CPP05/ex02/RobotomyRequestForm.hpp

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

11
CPP05/ex02/ShrubberyCreationForm.cpp

@ -12,17 +12,14 @@
#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.open((_target + "shrubbery").c_str());
file << " ,@@@@@@@," << endl;
file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl;
file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl;

9
CPP05/ex02/main.cpp

@ -10,13 +10,12 @@
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "Bureaucrat.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"
int main(void)
{
int main(void) {
Bureaucrat first("first", 1);
Bureaucrat mid("midle", 75);
Bureaucrat last("last", 150);

37
CPP05/ex03/Bureaucrat.cpp

@ -13,53 +13,44 @@
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat(void) {
cout << "Bureaucrat default constructor called " << endl;
}
Bureaucrat::Bureaucrat (const string name, int grade)
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){
Bureaucrat::Bureaucrat(const string name,
int grade) throw(Bureaucrat::GradeTooHighException,
Bureaucrat::GradeTooLowException)
: _name(name) {
if (grade < 1)
throw Bureaucrat::GradeTooHighException();
if (grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = grade;
cout << "Bureaucrat " << _name << " created grade: " << _grade << endl;
}
Bureaucrat::Bureaucrat(Bureaucrat const &src) {
(void)src;
cout << "Bureaucrat copy constructor called" << endl;
}
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
(void)src;
cout << "Bureaucrat assignment operator called" << endl;
return (*this);
}
Bureaucrat::~Bureaucrat(void) {
cout << "Bureaucrat default destructor called" << endl;
}
const string Bureaucrat::getName(void) const{
return (_name);
}
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) {
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) {
void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
int new_grade = _grade + diff;
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
@ -79,9 +70,11 @@ void Bureaucrat::signForm(Form &form) const {
if (!form.beSigned(*this))
cout << _name << " succesfully signed " << form.getName() << endl;
else
cout << _name << " failed to sign " << form.getName() << ": form is already signed." << endl;
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;
cout << _name << " failed to sign " << form.getName() << ": " << e.what()
<< endl;
}
}
@ -90,9 +83,11 @@ void Bureaucrat::executeForm(const Form &form) const {
if (!form.execute(*this))
cout << _name << " succesfully executed " << form.getName() << endl;
else
cout << _name << " failed to execute " << form.getName() << ": form is not signed" << endl;
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;
cout << _name << " failed to execute " << form.getName() << ": " << e.what()
<< endl;
}
}

21
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);
};

25
CPP05/ex03/Intern.cpp

@ -12,25 +12,16 @@
#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 (void) {
cout << "Intern default destructor called" << endl;
}
Intern::~Intern(void) {}
Form *createRobotForm(string targ) {
return (new RobotomyRequestForm(targ));
@ -44,11 +35,11 @@ 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};
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));

14
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 {
void PresidentialPardonForm::run(void) const
{
cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl;
}

15
CPP05/ex03/RobotomyRequestForm.cpp

@ -12,17 +12,18 @@
#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 {
void RobotomyRequestForm::run(void) const
{
srand(time(NULL));
int r = rand();
cout << "ratatttaatata" << endl;

2
CPP05/ex03/RobotomyRequestForm.hpp

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

14
CPP05/ex03/ShrubberyCreationForm.cpp

@ -12,17 +12,19 @@
#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 {
void ShrubberyCreationForm::run(void) const
{
std::ofstream file;
file.open(_target + "_shrubbery");
file.open((_target + "_shrubbery").c_str());
file << " ,@@@@@@@," << endl;
file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl;
file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl;

10
CPP05/ex03/house_shrubbery

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

23
CPP06/ex00/Converter.cpp

@ -11,20 +11,19 @@
/* ************************************************************************** */
#include "Converter.hpp"
using std::cout;
using std::endl;
Converter::Converter(void) {
cout << "Converter default constructor called " << endl;
}
Converter::Converter(string str) {
if (str[0] == '\'' || str[0] == '"')
{
if (str[0] == '\'' || str[0] == '"') {
_c = str[1];
cout << "Char: " << _c << endl;
_d = static_cast<double>(_c);
}
else
{
} else {
cout << "Char: ";
_d = std::strtod(str.c_str(), 0);
_c = static_cast<char>(_d);
@ -39,16 +38,24 @@ Converter::Converter (string str){
}
cout << "Integer: ";
_i = static_cast<int>(_d);
if (_d < std::numeric_limits<int>::min() || _d > std::numeric_limits<int>::max())
if (_d < std::numeric_limits<int>::min() ||
_d > std::numeric_limits<int>::max())
cout << "Off limits -> ";
if (isnan(_d))
if (std::isnan(_d))
cout << "Impossible" << endl;
else
cout << _i << endl;
cout << "Double: " << _d << endl;
_f = static_cast<float>(_d);
if (_d == 0 || _d / static_cast<int>(_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;

6
CPP06/ex00/Converter.hpp

@ -19,9 +19,8 @@
#include <cctype>
#include <limits>
#include <cmath>
#include <iomanip>
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);

2
CPP06/ex00/main.cpp

@ -13,8 +13,6 @@
#include <iostream>
#include "Converter.hpp"
using std::cout;
using std::endl;
using std::string;
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
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)

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