narnaud
2 years ago
110 changed files with 2863 additions and 491 deletions
@ -1,16 +1,21 @@ |
|||
NAME = megaphone |
|||
SRC = main.cpp |
|||
OBJ = main.o |
|||
|
|||
CC = c++ |
|||
CFLAGS = -std=c++98 -Wall -Werror -Wextra |
|||
CXXFLAGS = -std=c++98 -Wall -Werror -Wextra |
|||
RM = rm -rf |
|||
|
|||
$(NAME): $(SRC) |
|||
$(CC) $(CFLAGS) $(SRC) -o $(NAME) |
|||
$(NAME): $(OBJ) |
|||
c++ $(OBJ) -o $(NAME) |
|||
|
|||
all: $(NAME) |
|||
|
|||
clean: |
|||
$(RM) $(OBJ) |
|||
|
|||
fclean: clean |
|||
$(RM) $(NAME) |
|||
|
|||
.PHONY: all re |
|||
re: fclean all |
|||
|
|||
.PHONY: all clean fclean re |
|||
|
Binary file not shown.
@ -0,0 +1,20 @@ |
|||
NAME = phonebook |
|||
SRCS = main.cpp PhoneBook.cpp Contact.cpp |
|||
OBJS = $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME): $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all: $(NAME) |
|||
|
|||
clean: |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean: clean |
|||
rm -rf $(NAME) |
|||
|
|||
re: fclean all |
|||
|
|||
.PHONY: all clean fclean re |
Binary file not shown.
@ -0,0 +1,16 @@ |
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
brain: main.o |
|||
c++ main.o -o brain |
|||
|
|||
all: brain |
|||
|
|||
clean: |
|||
rm -rf main.o |
|||
|
|||
fclean: clean |
|||
rm -rf brain |
|||
|
|||
re: fclean all |
|||
|
|||
.PHONY: brain all clean fclean re |
@ -0,0 +1,16 @@ |
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
replaceby: main.o |
|||
c++ main.o -o replaceby |
|||
|
|||
all: replaceby |
|||
|
|||
clean: |
|||
rm -rf main.o |
|||
|
|||
fclean: clean |
|||
rm -rf replaceby |
|||
|
|||
re: fclean all |
|||
|
|||
.PHONY: replaceby all clean fclean re |
@ -0,0 +1,56 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Harl.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/17 11:14:29 by narnaud #+# #+# */ |
|||
/* Updated: 2022/07/18 13:00:18 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Harl.hpp" |
|||
|
|||
Harl::Harl (void) {} |
|||
|
|||
Harl::~Harl (void) {} |
|||
|
|||
void Harl::_debug(void) |
|||
{ |
|||
std::cout << "Debug : ..." << std::endl; |
|||
} |
|||
|
|||
void Harl::_info(void) |
|||
{ |
|||
std::cout << "Info: ..." << std::endl; |
|||
} |
|||
|
|||
void Harl::_warning(void) |
|||
{ |
|||
std::cout << "Warning: ..." << std::endl; |
|||
} |
|||
|
|||
void Harl::_error(void) |
|||
{ |
|||
std::cout << "Error: ..." << std::endl; |
|||
} |
|||
|
|||
void Harl::_complain(string level) { |
|||
|
|||
string ids = "DIWE"; |
|||
int i = 0; |
|||
while (ids[i] && ids[i] != level[0]) |
|||
i++; |
|||
switch (i) { |
|||
case 0: |
|||
this->_debug(); |
|||
case 1: |
|||
this->_info(); |
|||
case 2: |
|||
this->_warning(); |
|||
case 3: |
|||
this->_error(); |
|||
} |
|||
} |
|||
|
@ -0,0 +1,32 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Harl.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/17 11:13:50 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/20 09:30:38 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include <iostream> |
|||
using std::string; |
|||
|
|||
class Harl { |
|||
|
|||
public: |
|||
|
|||
Harl (void); |
|||
~Harl (void); |
|||
void _complain(string level); |
|||
|
|||
private: |
|||
void _debug(void); |
|||
void _info(void); |
|||
void _warning(void); |
|||
void _error(void); |
|||
|
|||
}; |
@ -0,0 +1,20 @@ |
|||
NAME = harlFilter |
|||
SRCS = main.cpp Harl.cpp |
|||
OBJS = ${SRCS:.cpp=.o} |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME): $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all: $(NAME) |
|||
|
|||
clean: |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean: clean |
|||
rm -rf $(NAME) |
|||
|
|||
re: fclean all |
|||
|
|||
.PHONY: all clean fclean re |
@ -0,0 +1,20 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/17 11:37:33 by narnaud #+# #+# */ |
|||
/* Updated: 2022/07/18 12:59:24 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Harl.hpp" |
|||
|
|||
int main(int argc, char **argv) |
|||
{ |
|||
Harl bot; |
|||
if (argc == 2) |
|||
bot._complain(argv[1]); |
|||
} |
@ -1,20 +1,21 @@ |
|||
NAME = bureau |
|||
SRCS = main.cpp Bureaucrat.cpp |
|||
OBJS= $(SRCS:.cpp=.o) |
|||
NAME = bureau |
|||
SRCS = main.cpp Bureaucrat.cpp |
|||
OBJS = $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
CXX = c++ |
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
$(NAME): $(OBJS) |
|||
$(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
all: $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
clean: |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
fclean: clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
re: fclean all |
|||
|
|||
.PHONY : all clean fclean re |
|||
.PHONY: all clean fclean re |
|||
|
@ -0,0 +1,103 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Bureaucrat.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 15:03:06 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
|
|||
/* 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; |
|||
} |
|||
|
|||
/* copy const. */ |
|||
Bureaucrat::Bureaucrat(Bureaucrat const &src) { |
|||
(void)src; |
|||
} |
|||
|
|||
/* assign const. */ |
|||
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) { |
|||
(void)src; |
|||
return (*this); |
|||
} |
|||
|
|||
/* destructor */ |
|||
Bureaucrat::~Bureaucrat(void) { |
|||
} |
|||
|
|||
/* 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"); |
|||
} |
|||
|
|||
const char *Bureaucrat::GradeTooLowException::what(void) const throw() { |
|||
return ("grade is too low for a bureaucrat"); |
|||
} |
|||
|
|||
/* Methods */ |
|||
void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) { |
|||
int new_grade = _grade - diff; |
|||
if (new_grade < 1) |
|||
throw Bureaucrat::GradeTooHighException(); |
|||
_grade = new_grade; |
|||
} |
|||
|
|||
void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) { |
|||
int new_grade = _grade + diff; |
|||
if (new_grade > 150) |
|||
throw Bureaucrat::GradeTooLowException(); |
|||
_grade = new_grade; |
|||
} |
|||
|
|||
void Bureaucrat::signForm(Form &form) const { |
|||
cout << *this << " sign " << form << ":"; |
|||
try { |
|||
if (!form.beSigned(*this)) |
|||
cout << " success." << endl; |
|||
else |
|||
cout << "error.\n\tform is already signed." << endl; |
|||
} catch (std::exception &e) { |
|||
cout << "error.\n\t" << e.what() << endl; |
|||
} |
|||
} |
|||
|
|||
void Bureaucrat::executeForm(const Form &form) const { |
|||
cout << *this << " execute " << form << ":"; |
|||
try { |
|||
if (!form.execute(*this)) |
|||
cout << " success." << endl; |
|||
else |
|||
cout << *this << " execute " << form.getName() |
|||
<< "error.\n\tform is not signed" << endl; |
|||
} catch (std::exception &e) { |
|||
cout << "error.\n\t" << e.what() << endl; |
|||
} |
|||
} |
|||
|
|||
/* Stream */ |
|||
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) { |
|||
out << b.getName() << "(" << b.getGrade() << ")"; |
|||
return (out); |
|||
} |
@ -0,0 +1,54 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Bureaucrat.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:47:21 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
#include <iostream> |
|||
#include <stdexcept> |
|||
#include <string> |
|||
using std::cout; |
|||
using std::endl; |
|||
using std::string; |
|||
|
|||
class Form; |
|||
|
|||
class Bureaucrat { |
|||
const string _name; |
|||
int _grade; |
|||
|
|||
public: |
|||
class GradeTooHighException : virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw(); |
|||
}; |
|||
class GradeTooLowException : virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw(); |
|||
}; |
|||
|
|||
Bureaucrat(void); |
|||
Bureaucrat(const string name, int grade) throw(GradeTooHighException, |
|||
GradeTooLowException); |
|||
Bureaucrat(Bureaucrat const &src); |
|||
~Bureaucrat(void); |
|||
Bureaucrat &operator=(Bureaucrat const &src); |
|||
|
|||
const string getName(void) const; |
|||
int getGrade(void) const; |
|||
void incrGrade(int diff = 1) throw(GradeTooHighException); |
|||
void decrGrade(int diff = 1) throw(GradeTooLowException); |
|||
void signForm(Form &form) const; |
|||
void executeForm(const Form &form) const; |
|||
}; |
|||
|
|||
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b); |
@ -0,0 +1,86 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Form.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:56:54 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Form.hpp" |
|||
|
|||
/* Defaults */ |
|||
|
|||
Form &Form::operator=(Form const &f) { |
|||
(void)f; |
|||
return (*this); |
|||
} |
|||
|
|||
Form::Form(Form const &f) |
|||
: _name(f.getName()), _signGrade(f.getSignGrade()), |
|||
_exeGrade(f.getExeGrade()) { |
|||
(void)f; |
|||
} |
|||
|
|||
Form::~Form(void) {} |
|||
|
|||
/* Custom constructor */ |
|||
|
|||
Form::Form(const string name, const int signGrade, const int exeGrade, |
|||
const string target) throw(Form::GradeTooHighException, |
|||
Form::GradeTooLowException) |
|||
: _name(name), _signGrade(signGrade), _exeGrade(exeGrade), _target(target) { |
|||
if (signGrade < 1 || exeGrade < 1) |
|||
throw Form::GradeTooHighException(); |
|||
if (signGrade > 150 || exeGrade > 150) |
|||
throw Form::GradeTooLowException(); |
|||
_isSigned = 0; |
|||
} |
|||
|
|||
/* Getters */ |
|||
|
|||
const string Form::getName(void) const { return (_name); } |
|||
|
|||
int Form::getSignGrade(void) const { return (_signGrade); } |
|||
|
|||
int Form::getExeGrade(void) const { return (_exeGrade); } |
|||
|
|||
/* Setters */ |
|||
|
|||
int Form::beSigned(const Bureaucrat &signer) throw(Form::GradeTooLowException) { |
|||
if (_isSigned) |
|||
return (-1); |
|||
if (signer.getGrade() > _signGrade) |
|||
throw GradeTooLowException(); |
|||
_isSigned = 1; |
|||
return (0); |
|||
} |
|||
|
|||
int Form::execute(Bureaucrat const &executor) const |
|||
throw(GradeTooLowException) { |
|||
if (executor.getGrade() > _exeGrade) |
|||
throw Form::GradeTooLowException(); |
|||
if (!_isSigned) |
|||
return (-1); |
|||
this->run(); |
|||
return (0); |
|||
} |
|||
|
|||
/* Except */ |
|||
|
|||
const char *Form::GradeTooHighException::what(void) const throw() { |
|||
return ("grade is too high for a form"); |
|||
} |
|||
|
|||
const char *Form::GradeTooLowException::what(void) const throw() { |
|||
return ("grade is too low for a form"); |
|||
} |
|||
|
|||
std::ostream &operator<<(std::ostream &out, const Form &f) { |
|||
out << f.getName() << "(s:" << f.getSignGrade() << "|e:" << f.getExeGrade() |
|||
<< ")"; |
|||
return (out); |
|||
}; |
@ -0,0 +1,54 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Form.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:56:40 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
#include <fstream> |
|||
#include <unistd.h> |
|||
using std::string; |
|||
|
|||
class Bureaucrat; |
|||
|
|||
class Form { |
|||
const string _name; |
|||
const int _signGrade; |
|||
const int _exeGrade; |
|||
bool _isSigned; |
|||
|
|||
public: |
|||
class GradeTooHighException : virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw(); |
|||
}; |
|||
class GradeTooLowException : virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw(); |
|||
}; |
|||
virtual ~Form(void) = 0; |
|||
const string getName(void) const; |
|||
int getSignGrade(void) const; |
|||
int getExeGrade(void) const; |
|||
int beSigned(const Bureaucrat &signer) throw(GradeTooLowException); |
|||
int execute(Bureaucrat const &executor) const throw(GradeTooLowException); |
|||
virtual void run(void) const = 0; |
|||
|
|||
protected: |
|||
Form(void); |
|||
Form(const string name, const int signGrade, const int exeGrade, |
|||
const string target) throw(GradeTooHighException, GradeTooLowException); |
|||
Form &operator=(Form const &f); |
|||
Form(Form const &f); |
|||
const string _target; |
|||
}; |
|||
|
|||
std::ostream &operator<<(std::ostream &out, const Form &f); |
@ -0,0 +1,20 @@ |
|||
NAME = bureau |
|||
SRCS = main.cpp Bureaucrat.cpp Form.cpp ShrubberyCreationForm.cpp RobotomyRequestForm.cpp PresidentialPardonForm.cpp |
|||
OBJS= $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* PresidentialPardonForm.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:43:58 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "PresidentialPardonForm.hpp" |
|||
|
|||
PresidentialPardonForm::PresidentialPardonForm(const string target) |
|||
: Form("PresidentialPardonForm", 25, 5, target) {} |
|||
|
|||
PresidentialPardonForm::~PresidentialPardonForm(void) {} |
|||
|
|||
void PresidentialPardonForm::run(void) const { |
|||
cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; |
|||
} |
@ -0,0 +1,23 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* PresidentialPardonForm.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:42:14 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
|
|||
class PresidentialPardonForm : public Form { |
|||
|
|||
public: |
|||
PresidentialPardonForm(const string target); |
|||
~PresidentialPardonForm(void); |
|||
void run(void) const; |
|||
}; |
@ -0,0 +1,34 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* RobotomyRequestForm.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:43:34 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "RobotomyRequestForm.hpp" |
|||
|
|||
RobotomyRequestForm::RobotomyRequestForm(const string target) |
|||
: Form("RobotomyRequestForm", 72, 45, target) {} |
|||
|
|||
RobotomyRequestForm::~RobotomyRequestForm(void) {} |
|||
|
|||
void RobotomyRequestForm::run(void) const { |
|||
srand(time(NULL)); |
|||
int r = rand(); |
|||
|
|||
cout << "ratatttaatata" << endl; |
|||
sleep(1); |
|||
cout << "ratata tatata" << endl; |
|||
sleep(1); |
|||
cout << "ratata" << endl; |
|||
sleep(1); |
|||
if (r % 2) |
|||
cout << _target << " have been robotomized" << endl; |
|||
else |
|||
cout << _target << " robotomization failed" << endl; |
|||
} |
@ -0,0 +1,25 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* RobotomyRequestForm.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:45:20 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#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; |
|||
}; |
@ -0,0 +1,34 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ShrubberyCreationForm.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:15 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:39:19 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "ShrubberyCreationForm.hpp" |
|||
|
|||
ShrubberyCreationForm::ShrubberyCreationForm(string target) |
|||
: Form("ShrubberyCreationForm", 147, 137, target) {} |
|||
|
|||
ShrubberyCreationForm::~ShrubberyCreationForm(void) {} |
|||
|
|||
void ShrubberyCreationForm::run(void) const { |
|||
std::ofstream file; |
|||
file.open((_target + "shrubbery").c_str()); |
|||
file << " ,@@@@@@@," << endl; |
|||
file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl; |
|||
file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl; |
|||
file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl; |
|||
file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl; |
|||
file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl; |
|||
file << " `&%\\ ` /%&' |.| \\ '|8'" << endl; |
|||
file << " |o| | | | |" << endl; |
|||
file << " |.| | | | |" << endl; |
|||
file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl; |
|||
file.close(); |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ShrubberyCreationForm.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:14 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:40:37 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
class ShrubberyCreationForm : public Form { |
|||
|
|||
public: |
|||
ShrubberyCreationForm(string target); |
|||
~ShrubberyCreationForm(void); |
|||
void run(void) const; |
|||
}; |
@ -0,0 +1,38 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:58:53 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
#include "PresidentialPardonForm.hpp" |
|||
#include "RobotomyRequestForm.hpp" |
|||
#include "ShrubberyCreationForm.hpp" |
|||
|
|||
int main(void) { |
|||
Bureaucrat first("first", 1); |
|||
Bureaucrat mid("midle", 75); |
|||
Bureaucrat last("last", 150); |
|||
|
|||
ShrubberyCreationForm houseForm("House"); |
|||
PresidentialPardonForm gerardForm("Gerard"); |
|||
RobotomyRequestForm biduleForm("Bidule"); |
|||
last.signForm(houseForm); |
|||
last.signForm(gerardForm); |
|||
last.signForm(biduleForm); |
|||
first.signForm(houseForm); |
|||
first.signForm(gerardForm); |
|||
first.signForm(biduleForm); |
|||
last.executeForm(houseForm); |
|||
last.executeForm(gerardForm); |
|||
last.executeForm(biduleForm); |
|||
first.executeForm(houseForm); |
|||
first.executeForm(gerardForm); |
|||
first.executeForm(biduleForm); |
|||
} |
@ -0,0 +1,97 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Bureaucrat.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 15:03:06 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
|
|||
Bureaucrat::Bureaucrat(void) { |
|||
} |
|||
|
|||
Bureaucrat::Bureaucrat(const string name, |
|||
int grade) throw(Bureaucrat::GradeTooHighException, |
|||
Bureaucrat::GradeTooLowException) |
|||
: _name(name) { |
|||
|
|||
if (grade < 1) |
|||
throw Bureaucrat::GradeTooHighException(); |
|||
if (grade > 150) |
|||
throw Bureaucrat::GradeTooLowException(); |
|||
_grade = grade; |
|||
} |
|||
|
|||
Bureaucrat::Bureaucrat(Bureaucrat const &src) { |
|||
(void)src; |
|||
} |
|||
|
|||
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) { |
|||
(void)src; |
|||
return (*this); |
|||
} |
|||
|
|||
Bureaucrat::~Bureaucrat(void) { |
|||
} |
|||
|
|||
const string Bureaucrat::getName(void) const { return (_name); } |
|||
|
|||
int Bureaucrat::getGrade(void) const { return (_grade); } |
|||
|
|||
void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) { |
|||
int new_grade = _grade - diff; |
|||
if (new_grade < 1) |
|||
throw Bureaucrat::GradeTooHighException(); |
|||
_grade = new_grade; |
|||
} |
|||
|
|||
void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) { |
|||
int new_grade = _grade + diff; |
|||
if (new_grade > 150) |
|||
throw Bureaucrat::GradeTooLowException(); |
|||
_grade = new_grade; |
|||
} |
|||
|
|||
const char *Bureaucrat::GradeTooHighException::what(void) const throw() { |
|||
return ("Grade was too high for a bureaucrat"); |
|||
} |
|||
|
|||
const char *Bureaucrat::GradeTooLowException::what(void) const throw() { |
|||
return ("Grade was too low for a bureaucrat"); |
|||
} |
|||
|
|||
void Bureaucrat::signForm(Form &form) const { |
|||
try { |
|||
if (!form.beSigned(*this)) |
|||
cout << _name << " succesfully signed " << form.getName() << endl; |
|||
else |
|||
cout << _name << " failed to sign " << form.getName() |
|||
<< ": form is already signed." << endl; |
|||
} catch (std::exception &e) { |
|||
cout << _name << " failed to sign " << form.getName() << ": " << e.what() |
|||
<< endl; |
|||
} |
|||
} |
|||
|
|||
void Bureaucrat::executeForm(const Form &form) const { |
|||
try { |
|||
if (!form.execute(*this)) |
|||
cout << _name << " succesfully executed " << form.getName() << endl; |
|||
else |
|||
cout << _name << " failed to execute " << form.getName() |
|||
<< ": form is not signed" << endl; |
|||
} catch (std::exception &e) { |
|||
cout << _name << " failed to execute " << form.getName() << ": " << e.what() |
|||
<< endl; |
|||
} |
|||
} |
|||
|
|||
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) { |
|||
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl; |
|||
return (out); |
|||
} |
@ -0,0 +1,54 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Bureaucrat.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:47:21 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
#include <string> |
|||
#include <iostream> |
|||
#include <stdexcept> |
|||
using std::cout; |
|||
using std::endl; |
|||
using std::string; |
|||
|
|||
class Form; |
|||
|
|||
class Bureaucrat{ |
|||
const string _name; |
|||
int _grade; |
|||
public: |
|||
|
|||
class GradeTooHighException: virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw (); |
|||
}; |
|||
class GradeTooLowException: virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw (); |
|||
}; |
|||
|
|||
Bureaucrat(void); |
|||
Bureaucrat(const string name, int grade) |
|||
throw (GradeTooHighException, GradeTooLowException); |
|||
Bureaucrat(Bureaucrat const & src); |
|||
~Bureaucrat(void); |
|||
Bureaucrat & operator= (Bureaucrat const & src); |
|||
|
|||
const string getName(void) const; |
|||
int getGrade(void) const; |
|||
void incrGrade(int diff = 1) throw (GradeTooHighException); |
|||
void decrGrade(int diff = 1) throw (GradeTooLowException); |
|||
void signForm(Form &form) const; |
|||
void executeForm(const Form &form) const; |
|||
}; |
|||
|
|||
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b); |
@ -0,0 +1,91 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Form.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:56:54 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Form.hpp" |
|||
|
|||
/* Defaults */ |
|||
|
|||
Form & Form::operator=(Form const &f) { |
|||
(void)f; |
|||
return (*this); |
|||
} |
|||
|
|||
Form::Form(Form const &f) |
|||
: _name(f.getName()), _signGrade(f.getSignGrade()), _exeGrade(f.getExeGrade()) |
|||
{ |
|||
(void)f; |
|||
} |
|||
|
|||
Form::~Form(void) { |
|||
} |
|||
|
|||
/* Custom constructor */ |
|||
|
|||
Form::Form(const string name, const int signGrade, const int exeGrade, const string target) |
|||
throw (Form::GradeTooHighException, Form::GradeTooLowException) |
|||
: _name(name), _signGrade(signGrade), _exeGrade(exeGrade), _target(target){ |
|||
if (signGrade < 1 || exeGrade < 1) |
|||
throw Form::GradeTooHighException(); |
|||
if (signGrade > 150 || exeGrade > 150) |
|||
throw Form::GradeTooLowException(); |
|||
_isSigned = 0; |
|||
} |
|||
|
|||
/* Getters */ |
|||
|
|||
const string Form::getName(void) const { |
|||
return (_name); |
|||
} |
|||
|
|||
int Form::getSignGrade(void) const { |
|||
return (_signGrade); |
|||
} |
|||
|
|||
int Form::getExeGrade(void) const { |
|||
return (_exeGrade); |
|||
} |
|||
|
|||
/* Setters */ |
|||
|
|||
int Form::beSigned(const Bureaucrat &signer) throw (Form::GradeTooLowException) { |
|||
if (_isSigned) |
|||
return (-1); |
|||
if (signer.getGrade() > _signGrade) |
|||
throw GradeTooLowException(); |
|||
_isSigned = 1; |
|||
return (0); |
|||
} |
|||
|
|||
int Form::execute(Bureaucrat const &executor) const throw (GradeTooLowException) { |
|||
if (executor.getGrade() > _exeGrade) |
|||
throw Form::GradeTooLowException(); |
|||
if (!_isSigned) |
|||
return (-1); |
|||
this->run(); |
|||
return (0); |
|||
} |
|||
|
|||
/* Except */ |
|||
|
|||
const char *Form::GradeTooHighException::what(void) const throw (){ |
|||
return ("grade is too high for a form"); |
|||
} |
|||
|
|||
const char *Form::GradeTooLowException::what(void) const throw (){ |
|||
return ("grade is too low for a form"); |
|||
} |
|||
|
|||
std::ostream &operator<<(std::ostream &out, const Form &f) { |
|||
out << f.getName() << "(s:" << f.getSignGrade() << "|e:" << f.getExeGrade() |
|||
<< ")"; |
|||
return (out); |
|||
}; |
@ -0,0 +1,57 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Form.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:56:40 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Bureaucrat.hpp" |
|||
#include <string> |
|||
#include <iostream> |
|||
#include <fstream> |
|||
#include <stdexcept> |
|||
#include <unistd.h> |
|||
using std::cout; |
|||
using std::endl; |
|||
using std::string; |
|||
|
|||
class Bureaucrat; |
|||
|
|||
class Form { |
|||
const string _name; |
|||
const int _signGrade; |
|||
const int _exeGrade; |
|||
bool _isSigned; |
|||
public: |
|||
class GradeTooHighException: virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw (); |
|||
}; |
|||
class GradeTooLowException: virtual public std::exception { |
|||
public: |
|||
const char *what(void) const throw (); |
|||
}; |
|||
virtual ~Form(void) = 0; |
|||
const string getName(void) const; |
|||
int getSignGrade(void) const; |
|||
int getExeGrade(void) const; |
|||
int beSigned(const Bureaucrat &signer) throw (GradeTooLowException); |
|||
int execute(Bureaucrat const &executor) const throw (GradeTooLowException); |
|||
virtual void run(void) const = 0; |
|||
protected: |
|||
Form(void); |
|||
Form(const string name, const int signGrade,const int exeGrade, const string target) |
|||
throw (GradeTooHighException, GradeTooLowException); |
|||
Form & operator=(Form const &f); |
|||
Form(Form const &f); |
|||
const string _target; |
|||
}; |
|||
|
|||
std::ostream &operator<< (std::ostream &out, const Form &f); |
@ -0,0 +1,48 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Intern.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 15:07:10 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 16:28:56 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Intern.hpp" |
|||
|
|||
Intern::Intern(void) {} |
|||
|
|||
Intern::Intern(Intern const &src) { (void)src; } |
|||
|
|||
Intern &Intern::operator=(Intern const &src) { |
|||
(void)src; |
|||
return (*this); |
|||
} |
|||
|
|||
Intern::~Intern(void) {} |
|||
|
|||
Form *createRobotForm(string targ) { |
|||
return (new RobotomyRequestForm(targ)); |
|||
} |
|||
|
|||
Form *createPresidentForm(string targ) { |
|||
return (new PresidentialPardonForm(targ)); |
|||
} |
|||
|
|||
Form *createShrubberyForm(string targ) { |
|||
return (new ShrubberyCreationForm(targ)); |
|||
} |
|||
|
|||
Form *Intern::makeForm(string name, string target) { |
|||
string subFormsName[3] = {"robotomy request", "president pardon", |
|||
"shrubbery creation"}; |
|||
Form *(*subFormsCreation[3])(string targ) = { |
|||
createRobotForm, createPresidentForm, createShrubberyForm}; |
|||
for (int i = 0; i < 3; i++) |
|||
if (name == subFormsName[i]) |
|||
return (subFormsCreation[i](target)); |
|||
cout << "Intern fail to create " << name << ": unknow form name" << endl; |
|||
return (NULL); |
|||
} |
@ -0,0 +1,35 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Intern.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 15:07:10 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 16:28:32 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
#include "PresidentialPardonForm.hpp" |
|||
#include "RobotomyRequestForm.hpp" |
|||
#include "ShrubberyCreationForm.hpp" |
|||
#include <string> |
|||
|
|||
#include <iostream> |
|||
using std::cout; |
|||
using std::endl; |
|||
using std::string; |
|||
|
|||
class Intern{ |
|||
|
|||
public: |
|||
Intern(void); |
|||
Intern(Intern const & src); |
|||
virtual ~Intern(void); |
|||
Intern & operator= (Intern const & src); |
|||
Form * makeForm(string name, string target); |
|||
protected: |
|||
}; |
@ -0,0 +1,20 @@ |
|||
NAME = bureau |
|||
SRCS = main.cpp Bureaucrat.cpp Form.cpp ShrubberyCreationForm.cpp RobotomyRequestForm.cpp PresidentialPardonForm.cpp Intern.cpp |
|||
OBJS= $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
@ -0,0 +1,27 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* PresidentialPardonForm.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:43:58 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "PresidentialPardonForm.hpp" |
|||
|
|||
PresidentialPardonForm::PresidentialPardonForm(const string target) |
|||
: Form("PresidentialPardonForm", 25, 5, target) |
|||
{ |
|||
} |
|||
|
|||
PresidentialPardonForm::~PresidentialPardonForm(void) |
|||
{ |
|||
} |
|||
|
|||
void PresidentialPardonForm::run(void) const |
|||
{ |
|||
cout << _target << "has been pardoned by Zaphod Beeblebrox" << endl; |
|||
} |
@ -0,0 +1,24 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* PresidentialPardonForm.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:18:34 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:42:14 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
|
|||
class PresidentialPardonForm: public Form { |
|||
|
|||
public: |
|||
|
|||
PresidentialPardonForm(const string target); |
|||
~PresidentialPardonForm(void); |
|||
void run(void) const; |
|||
}; |
@ -0,0 +1,39 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* RobotomyRequestForm.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:43:34 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "RobotomyRequestForm.hpp" |
|||
|
|||
RobotomyRequestForm::RobotomyRequestForm(const string target) |
|||
: Form("RobotomyRequestForm", 72, 45, target) |
|||
{ |
|||
} |
|||
|
|||
RobotomyRequestForm::~RobotomyRequestForm(void) |
|||
{ |
|||
} |
|||
|
|||
void RobotomyRequestForm::run(void) const |
|||
{ |
|||
srand(time(NULL)); |
|||
int r = rand(); |
|||
|
|||
cout << "ratatttaatata" << endl; |
|||
sleep(1); |
|||
cout << "ratata tatata" << endl; |
|||
sleep(1); |
|||
cout << "ratata" << endl; |
|||
sleep(1); |
|||
if (r % 2) |
|||
cout << _target << " have been robotomized" << endl; |
|||
else |
|||
cout << _target << " robotomization failed" << endl; |
|||
} |
@ -0,0 +1,26 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* RobotomyRequestForm.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:52 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:45:20 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#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; |
|||
}; |
@ -0,0 +1,39 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ShrubberyCreationForm.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:15 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 16:36:13 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "ShrubberyCreationForm.hpp" |
|||
|
|||
ShrubberyCreationForm::ShrubberyCreationForm(string target) |
|||
: Form("ShrubberyCreationForm", 147, 137, target) |
|||
{ |
|||
} |
|||
|
|||
ShrubberyCreationForm::~ShrubberyCreationForm(void) |
|||
{ |
|||
} |
|||
|
|||
void ShrubberyCreationForm::run(void) const |
|||
{ |
|||
std::ofstream file; |
|||
file.open((_target + "_shrubbery").c_str()); |
|||
file << " ,@@@@@@@," << endl; |
|||
file << " ,,,. ,@@@@@@/@@, .oo8888o." << endl; |
|||
file << " ,&\%%&%&&%,@@@@@/@@@@@@,8888\\88/8o" << endl; |
|||
file << " ,%&\\%&&%&&%,@@@\\@@@/@@@88\\88888/88'" << endl; |
|||
file << " %&&%&%&/%&&%@@\\@@/ /@@@88888\\88888'" << endl; |
|||
file << " %&&%/ %&\%%&&@@\\ V /@@' `88\\8 `/88'" << endl; |
|||
file << " `&%\\ ` /%&' |.| \\ '|8'" << endl; |
|||
file << " |o| | | | |" << endl; |
|||
file << " |.| | | | |" << endl; |
|||
file << " \\\\/ ._\\//_/__/ ,\\_//__\\\\/. \\_//__/_" << endl; |
|||
file.close(); |
|||
} |
@ -0,0 +1,22 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* ShrubberyCreationForm.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/27 10:17:14 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 14:40:37 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include "Form.hpp" |
|||
class ShrubberyCreationForm: public Form { |
|||
|
|||
public: |
|||
ShrubberyCreationForm(string target); |
|||
~ShrubberyCreationForm(void); |
|||
void run(void) const; |
|||
}; |
@ -0,0 +1,47 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */ |
|||
/* Updated: 2022/06/27 16:34:59 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "ShrubberyCreationForm.hpp" |
|||
#include "RobotomyRequestForm.hpp" |
|||
#include "PresidentialPardonForm.hpp" |
|||
#include "Bureaucrat.hpp" |
|||
#include "Intern.hpp" |
|||
|
|||
int main(void) |
|||
{ |
|||
Bureaucrat first("first", 1); |
|||
Bureaucrat mid("midle", 75); |
|||
Bureaucrat last("last", 150); |
|||
Intern rookie; |
|||
Form *form; |
|||
|
|||
form = rookie.makeForm("shrubbery creation", "house"); |
|||
last.signForm(*form); |
|||
first.signForm(*form); |
|||
last.executeForm(*form); |
|||
first.executeForm(*form); |
|||
delete form; |
|||
|
|||
form = rookie.makeForm("robotomy request", "bidule"); |
|||
last.signForm(*form); |
|||
first.signForm(*form); |
|||
last.executeForm(*form); |
|||
first.executeForm(*form); |
|||
delete form; |
|||
|
|||
form = rookie.makeForm("president pardon", "gerard"); |
|||
last.signForm(*form); |
|||
first.signForm(*form); |
|||
last.executeForm(*form); |
|||
first.executeForm(*form); |
|||
delete form; |
|||
} |
@ -0,0 +1,74 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Converter.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ |
|||
/* Updated: 2022/08/18 08:54:04 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#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] == '"') { |
|||
_c = str[1]; |
|||
cout << "Char: " << _c << endl; |
|||
_d = static_cast<double>(_c); |
|||
} else { |
|||
cout << "Char: "; |
|||
_d = std::strtod(str.c_str(), 0); |
|||
_c = static_cast<char>(_d); |
|||
if (_d < 0 || _d > 255) |
|||
cout << "Invalid" << endl; |
|||
else if (isnan(_d)) |
|||
cout << "Impossible" << endl; |
|||
else if (!std::isprint(_c)) |
|||
cout << "Not Printable" << endl; |
|||
else |
|||
cout << "'" << _c << "'" << endl; |
|||
} |
|||
cout << "Integer: "; |
|||
_i = static_cast<int>(_d); |
|||
if (_d < std::numeric_limits<int>::min() || |
|||
_d > std::numeric_limits<int>::max()) |
|||
cout << "Off limits -> "; |
|||
if (std::isnan(_d)) |
|||
cout << "Impossible" << endl; |
|||
else |
|||
cout << _i << 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; |
|||
cout << "Converter copy constructor called" << endl; |
|||
} |
|||
|
|||
Converter &Converter::operator=(Converter const &src) { |
|||
(void)src; |
|||
cout << "Converter assignment operator called" << endl; |
|||
return (*this); |
|||
} |
|||
|
|||
Converter::~Converter(void) { |
|||
|
|||
cout << "Converter default destructor called" << endl; |
|||
} |
@ -0,0 +1,39 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Converter.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ |
|||
/* Updated: 2022/08/18 08:45:46 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <iostream> |
|||
#include <cstdlib> |
|||
#include <stdexcept> |
|||
#include <cctype> |
|||
#include <limits> |
|||
#include <cmath> |
|||
#include <iomanip> |
|||
|
|||
using std::string; |
|||
|
|||
class Converter{ |
|||
|
|||
string _input; |
|||
char _c; |
|||
int _i; |
|||
float _f; |
|||
double _d; |
|||
public: |
|||
Converter(string str); |
|||
Converter(void); |
|||
Converter(Converter const & src); |
|||
virtual ~Converter(void); |
|||
Converter & operator= (Converter const & src); |
|||
}; |
@ -0,0 +1,21 @@ |
|||
NAME = convert |
|||
SRCS = main.cpp Converter.cpp |
|||
OBJS= $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
|||
|
@ -0,0 +1,24 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/06/28 08:44:43 by narnaud #+# #+# */ |
|||
/* Updated: 2022/07/21 12:16:42 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include <iostream> |
|||
#include "Converter.hpp" |
|||
|
|||
using std::string; |
|||
|
|||
int main(int ac, char **av) { |
|||
|
|||
if (ac != 2) |
|||
return (EXIT_FAILURE); |
|||
string input = av[1]; |
|||
Converter raw(input); |
|||
} |
@ -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; |
@ -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 |
@ -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); |
|||
} |
@ -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); |
|||
|
|||
}; |
@ -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); |
|||
}; |
@ -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); |
|||
}; |
@ -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); |
|||
}; |
@ -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){} |
@ -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 |
@ -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; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,20 @@ |
|||
NAME = templates |
|||
SRCS = main.cpp |
|||
OBJS = $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
@ -0,0 +1,32 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/09/05 08:05:29 by narnaud #+# #+# */ |
|||
/* Updated: 2022/09/05 08:13:45 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "main.hpp" |
|||
#include <iostream> |
|||
|
|||
int main( void ) { |
|||
int a = 2; |
|||
int b = 3; |
|||
std::cout << "a = " << a << ", b = " << b << std::endl; |
|||
::swap( a, b ); |
|||
std::cout << "a = " << a << ", b = " << b << std::endl; |
|||
std::cout << "min( a, b ) = " << ::min( a, b ) << std::endl; |
|||
std::cout << "max( a, b ) = " << ::max( a, b ) << std::endl; |
|||
std::string c = "chaine1"; |
|||
std::string d = "chaine2"; |
|||
::swap(c, d); |
|||
std::cout << "c = " << c << ", d = " << d << std::endl; |
|||
std::cout << "min( c, d ) = " << ::min( c, d ) << std::endl; |
|||
std::cout << "max( c, d ) = " << ::max( c, d ) << std::endl; |
|||
return 0; |
|||
} |
|||
|
@ -0,0 +1,29 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/09/05 08:05:52 by narnaud #+# #+# */ |
|||
/* Updated: 2022/09/05 08:13:08 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
template <class T> |
|||
void swap (T &t1, T &t2) { |
|||
T tmp; |
|||
tmp = t1; |
|||
t1 = t2; |
|||
t2 = tmp; |
|||
} |
|||
|
|||
template <class T> |
|||
T min (T &t1, T &t2) { |
|||
return (t1 < t2 ? t1 : t2); |
|||
} |
|||
|
|||
template <class T> |
|||
T max (T &t1, T &t2) { |
|||
return (t1 > t2 ? t1 : t2); |
|||
} |
@ -0,0 +1,21 @@ |
|||
NAME = iter |
|||
SRCS = main.cpp |
|||
OBJS = $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
|||
|
@ -0,0 +1,19 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* iter.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/09/05 08:14:42 by narnaud #+# #+# */ |
|||
/* Updated: 2022/09/05 08:33:06 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
|
|||
template <class T> |
|||
void iter(T *arr, int length, void (*fct)(T)) { |
|||
for (int i = 0; i < length; i++) { |
|||
fct(arr[i]); |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/09/05 08:14:45 by narnaud #+# #+# */ |
|||
/* Updated: 2022/09/05 08:43:03 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include <iostream> |
|||
#include "iter.hpp" |
|||
|
|||
template <class T> |
|||
void put_t(T t) { |
|||
std::cout << t << std::endl; |
|||
} |
|||
|
|||
template <class T> |
|||
void put_square(T t) { |
|||
put_t(t * t); |
|||
} |
|||
|
|||
int main(void) { |
|||
int nbrs[5] = {1, 2, 3 , 4, 5}; |
|||
char words[5][4] = {"the", "one", "two", "get", "any"}; |
|||
|
|||
iter(nbrs, 5, put_t); |
|||
iter(nbrs, 3, put_square); |
|||
iter(words, 5, put_t); |
|||
} |
@ -0,0 +1,61 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* Array.hpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/09/05 08:50:55 by narnaud #+# #+# */ |
|||
/* Updated: 2022/09/05 11:23:19 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include <iostream> |
|||
|
|||
template <class T> class Array { |
|||
T *tab; |
|||
int nbr; |
|||
public: |
|||
Array() { |
|||
tab = new T[0]; |
|||
}; |
|||
|
|||
Array(int n) { |
|||
tab = new T[nbr = n]; |
|||
}; |
|||
|
|||
Array(const Array<T> &a) { |
|||
tab = new T[nbr = a.size()]; |
|||
for (int i = 0; i < nbr; i++) { |
|||
tab[i] = a[i]; |
|||
} |
|||
}; |
|||
|
|||
~Array() { |
|||
delete [] tab; |
|||
}; |
|||
|
|||
Array<T> &operator= (const Array<T> &a) { |
|||
delete [] tab; |
|||
nbr = a.size(); |
|||
tab = new T[nbr]; |
|||
for (int i = 0; i < nbr; i++) { |
|||
tab[i] = a[i]; |
|||
} |
|||
return (*this); |
|||
}; |
|||
|
|||
T &operator[] (int i) { |
|||
if (i >= nbr) throw (std::runtime_error("error: Array index out of bound.")); |
|||
return (tab[i]); |
|||
}; |
|||
|
|||
T operator[] (int i) const { |
|||
if (i >= nbr) throw (std::runtime_error("error: Array index out of bound.")); |
|||
return (tab[i]); |
|||
}; |
|||
|
|||
int size() const{ |
|||
return (nbr); |
|||
}; |
|||
}; |
@ -0,0 +1,20 @@ |
|||
NAME = array |
|||
SRCS = main.cpp |
|||
OBJS = $(SRCS:.cpp=.o) |
|||
|
|||
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall |
|||
|
|||
$(NAME) : $(OBJS) |
|||
c++ $(OBJS) -o $(NAME) |
|||
|
|||
all : $(NAME) |
|||
|
|||
clean : |
|||
rm -rf $(OBJS) |
|||
|
|||
fclean : clean |
|||
rm -rf $(NAME) |
|||
|
|||
re : fclean all |
|||
|
|||
.PHONY : all clean fclean re |
@ -0,0 +1,32 @@ |
|||
/* ************************************************************************** */ |
|||
/* */ |
|||
/* ::: :::::::: */ |
|||
/* main.cpp :+: :+: :+: */ |
|||
/* +:+ +:+ +:+ */ |
|||
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|||
/* +#+#+#+#+#+ +#+ */ |
|||
/* Created: 2022/09/05 08:49:45 by narnaud #+# #+# */ |
|||
/* Updated: 2022/09/05 11:00:32 by narnaud ### ########.fr */ |
|||
/* */ |
|||
/* ************************************************************************** */ |
|||
|
|||
#include "Array.hpp" |
|||
|
|||
int main(void) { |
|||
Array<char> a; |
|||
Array<char> b(10); |
|||
Array<char> c(b); |
|||
|
|||
b[3] = 'b'; |
|||
std::cout << b[3] << std::endl; |
|||
std::cout << b.size() << std::endl; |
|||
std::cout << c[3] << std::endl; |
|||
c = b; |
|||
std::cout << c[3] << std::endl; |
|||
|
|||
try { |
|||
std::cout << b[12] << std::endl; |
|||
} catch (std::exception &e) { |
|||
std::cout << e.what() << std::endl; |
|||
} |
|||
} |
Binary file not shown.
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue