1
0

🏗️」 wip: Added all files for ex02

This commit is contained in:
2025-03-30 18:07:11 +02:00
parent 2b136d90f4
commit d56e8011de
12 changed files with 405 additions and 0 deletions

50
ex02/AForm.cpp Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/09 17:38:19 by adjoly #+# #+# */
/* Updated: 2025/03/30 15:14:17 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Form.hpp"
#include "Bureaucrat.hpp"
Form::Form(void) : _name(""), _signed(false), _minForSign(1), _minForExec(1) {
_log("", "Form", "", "default constructor called");
}
Form::Form(std::string name, uint8_t minForSign, uint8_t minForExec)
: _name(name), _signed(false), _minForSign(minForSign),
_minForExec(minForExec) {
_log("", "Form", "", "default constructor called");
}
Form::~Form(void) { _log("", "Form", "", "destructor called"); }
const char *Form::GradeTooHighException::what() const throw() {
return ("grade is too high");
}
const char *Form::GradeTooLowException::what() const throw() {
return ("grade is too low");
}
std::string Form::getName(void) const { return _name; }
bool Form::getSigned(void) const { return _signed; }
uint8_t Form::getMinForSign(void) const { return _minForSign; }
uint8_t Form::getMinForExec(void) const { return _minForExec; }
void Form::beSigned(Bureaucrat b) {
if (_minForSign > b.getGrade()) {
throw GradeTooLowException();
}
_signed = true;
}
std::ostream &operator<<(std::ostream &os, Form &val) {
os << "Form: " << val.getName() << ", minGrade to sign: " << val.getMinForSign() << ", min grade to execute: " << val.getMinForExec() << std::endl;
return (os);
}

54
ex02/AForm.hpp Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/08 20:10:59 by adjoly #+# #+# */
/* Updated: 2025/03/30 15:11:58 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <string>
void _log(std::string emoji, std::string what, std::string who,
std::string str);
typedef unsigned char uint8_t;
#include "Bureaucrat.hpp"
//class Bureaucrat;
class Form {
public:
Form(void);
Form(std::string name, uint8_t minForSign, uint8_t minForExec);
~Form(void);
class GradeTooHighException : public std::exception {
public:
virtual const char *what() const throw();
};
class GradeTooLowException : public std::exception {
public:
virtual const char *what() const throw();
};
std::string getName(void) const;
bool getSigned(void) const;
uint8_t getMinForSign(void) const;
uint8_t getMinForExec(void) const;
void beSigned(Bureaucrat);
private:
const std::string _name;
bool _signed;
const uint8_t _minForSign;
const uint8_t _minForExec;
};
std::ostream &operator<<(std::ostream &, Form &);

111
ex02/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */
/* Updated: 2025/03/30 15:13:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
#include <exception>
void _log(std::string emoji, std::string what, std::string who,
std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "" << what << ": " << str << std::endl;
else
std::cout << "" << emoji << "" << what << "(" << who << "): " << str
<< std::endl;
#else
(void)emoji, (void)what, (void)who, (void)str;
#endif
}
Bureaucrat::Bureaucrat(void) {
_log("", "Bureaucrat", "", "default constructor called");
}
Bureaucrat::Bureaucrat(const Bureaucrat &cpy) {
_log("", "Bureaucrat", "", "copy constructor called");
if (&cpy != this)
*this = cpy;
}
Bureaucrat::Bureaucrat(std::string name, uint8_t grade)
: _name(name), _grade(grade) {
_log("", "Bureaucrat", "", "constructor called");
}
const std::string &Bureaucrat::getName(void) { return _name; }
uint8_t Bureaucrat::getGrade(void) { return _grade; }
Bureaucrat::~Bureaucrat(void) {
_log("", "Bureaucrat", "", "destructor called");
}
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) {
if (&cpy == this)
return *this;
_grade = cpy._grade;
return *this;
}
Bureaucrat &Bureaucrat::operator++(void) {
_grade--;
if (_grade < MAXGRADE)
throw GradeTooHighException();
return *this;
}
Bureaucrat &Bureaucrat::operator--(void) {
_grade++;
if (_grade > MINGRADE)
throw GradeTooLowException();
return *this;
}
Bureaucrat Bureaucrat::operator++(int) {
Bureaucrat old = *this;
_grade--;
if (_grade < MAXGRADE)
throw GradeTooHighException();
return (old);
}
Bureaucrat Bureaucrat::operator--(int) {
Bureaucrat old = *this;
_grade++;
if (_grade > MINGRADE)
throw GradeTooLowException();
return old;
}
std::ostream &operator<<(std::ostream &os, Bureaucrat &val) {
os << val.getName() << ", bureaucrat grade " << (int)val.getGrade();
return os;
}
const char *Bureaucrat::GradeTooHighException::what() const throw() {
return ("Grade is too high");
}
const char *Bureaucrat::GradeTooLowException::what() const throw() {
return ("Grade is too low");
}
void Bureaucrat::signForm(Form &f) {
try {
f.beSigned(*this);
std::cout << _name << " signed " << f.getName() << std::endl;
} catch (std::exception &e) {
std::cout << _name << " counldn't sign " << f.getName() << " because " << e.what() << std::endl;
}
}

67
ex02/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */
/* Updated: 2025/03/30 15:10:46 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <exception>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
void _log(std::string emoji, std::string what, std::string who,
std::string str);
class Form;
typedef unsigned char uint8_t;
#define MAXGRADE 1
#define MINGRADE 150
class Bureaucrat {
public:
Bureaucrat(void);
Bureaucrat(const Bureaucrat &cpy);
Bureaucrat(std::string name, uint8_t grade);
~Bureaucrat(void);
const std::string &getName(void);
uint8_t getGrade(void);
class GradeTooHighException : public std::exception {
public:
virtual const char *what() const throw();
};
class GradeTooLowException : public std::exception {
public:
virtual const char *what() const throw();
};
// Copy assignement operator
Bureaucrat &operator=(Bureaucrat const &);
// Preincrement operator
Bureaucrat &operator++(void);
Bureaucrat &operator--(void);
// Post increment operator
Bureaucrat operator++(int);
Bureaucrat operator--(int);
void signForm(Form &);
private:
const std::string _name;
uint8_t _grade;
};
std::ostream &operator<<(std::ostream &, Bureaucrat &);

60
ex02/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/03/30 15:14:32 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = Form
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
ifeq ($(VERBOSE),true)
FLAGS += -D VERBOSE
endif
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

View File

View File

@ -0,0 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/30 16:25:52 by adjoly #+# #+# */
/* Updated: 2025/03/30 18:06:49 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */

View File

View File

View File

View File

51
ex02/main.cpp Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */
/* Updated: 2025/03/30 15:13:07 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
#include <iostream>
int main(void) {
Bureaucrat knl("Kanel", 10);
Bureaucrat su("Suki", 1);
knl--;
std::cout << knl << std::endl;
knl++;
std::cout << knl << std::endl;
try {
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
knl++;
} catch (const std::exception &e) {
std::cerr << "Error : " << e.what() << std::endl;
}
Form f("le contrattt", 5, 5);
su.signForm(f);
std::cout << knl << std::endl;
}