Browse Source

finish

master
narnaud 2 years ago
parent
commit
e5a85d6ed9
  1. 15
      CPP00/ex00/Makefile
  2. 2
      CPP00/ex00/main.cpp
  3. BIN
      CPP00/ex00/megaphone
  4. 36
      CPP00/ex01/Contact.cpp
  5. 15
      CPP00/ex01/Contact.hpp
  6. 20
      CPP00/ex01/Makefile
  7. 64
      CPP00/ex01/PhoneBook.cpp
  8. 16
      CPP00/ex01/PhoneBook.hpp
  9. 6
      CPP00/ex01/main.cpp
  10. 13
      CPP01/ex00/Zombie.cpp
  11. 1
      CPP01/ex00/Zombie.hpp
  12. 1
      CPP01/ex00/main.cpp
  13. 2
      CPP01/ex01/Makefile
  14. 3
      CPP01/ex01/Zombie.cpp
  15. 2
      CPP01/ex01/Zombie.hpp
  16. BIN
      CPP01/ex01/apocalypse
  17. 9
      CPP01/ex01/main.cpp
  18. 8
      CPP01/ex01/zombieHorde.cpp
  19. 16
      CPP01/ex02/Makefile
  20. 4
      CPP01/ex02/main.cpp
  21. 10
      CPP01/ex03/HumanA.hpp
  22. 9
      CPP01/ex03/HumanB.cpp
  23. 10
      CPP01/ex03/HumanB.hpp
  24. 12
      CPP01/ex03/Weapon.cpp
  25. 7
      CPP01/ex03/Weapon.hpp
  26. 16
      CPP01/ex04/Makefile
  27. 30
      CPP01/ex04/main.cpp
  28. 11
      CPP01/ex05/Harl.cpp
  29. 56
      CPP01/ex06/Harl.cpp
  30. 32
      CPP01/ex06/Harl.hpp
  31. 20
      CPP01/ex06/Makefile
  32. 20
      CPP01/ex06/main.cpp
  33. 22
      CPP02/ex01/Fixed.cpp
  34. 12
      CPP02/ex01/main.cpp
  35. 3
      CPP03/ex03/DiamondTrap.cpp
  36. 4
      CPP04/ex00/Animal.cpp
  37. 4
      CPP04/ex00/WrongAnimal.cpp
  38. 6
      CPP04/ex00/main.cpp
  39. 8
      CPP04/ex01/Brain.cpp
  40. 8
      CPP04/ex02/Brain.cpp
  41. 4
      CPP04/ex02/Brain.hpp
  42. 95
      CPP05/ex00/Bureaucrat.cpp
  43. 55
      CPP05/ex00/Bureaucrat.hpp
  44. 27
      CPP05/ex00/Makefile
  45. 70
      CPP05/ex00/main.cpp
  46. 108
      CPP05/ex01/Bureaucrat.cpp
  47. 64
      CPP05/ex01/Bureaucrat.hpp
  48. 73
      CPP05/ex01/Form.cpp
  49. 62
      CPP05/ex01/Form.hpp
  50. 60
      CPP05/ex01/main.cpp
  51. 103
      CPP05/ex02/Bureaucrat.cpp
  52. 54
      CPP05/ex02/Bureaucrat.hpp
  53. 86
      CPP05/ex02/Form.cpp
  54. 54
      CPP05/ex02/Form.hpp
  55. 20
      CPP05/ex02/Makefile
  56. 22
      CPP05/ex02/PresidentialPardonForm.cpp
  57. 23
      CPP05/ex02/PresidentialPardonForm.hpp
  58. 34
      CPP05/ex02/RobotomyRequestForm.cpp
  59. 25
      CPP05/ex02/RobotomyRequestForm.hpp
  60. 34
      CPP05/ex02/ShrubberyCreationForm.cpp
  61. 22
      CPP05/ex02/ShrubberyCreationForm.hpp
  62. 38
      CPP05/ex02/main.cpp
  63. 97
      CPP05/ex03/Bureaucrat.cpp
  64. 54
      CPP05/ex03/Bureaucrat.hpp
  65. 91
      CPP05/ex03/Form.cpp
  66. 57
      CPP05/ex03/Form.hpp
  67. 48
      CPP05/ex03/Intern.cpp
  68. 35
      CPP05/ex03/Intern.hpp
  69. 20
      CPP05/ex03/Makefile
  70. 27
      CPP05/ex03/PresidentialPardonForm.cpp
  71. 24
      CPP05/ex03/PresidentialPardonForm.hpp
  72. 39
      CPP05/ex03/RobotomyRequestForm.cpp
  73. 26
      CPP05/ex03/RobotomyRequestForm.hpp
  74. 39
      CPP05/ex03/ShrubberyCreationForm.cpp
  75. 22
      CPP05/ex03/ShrubberyCreationForm.hpp
  76. 47
      CPP05/ex03/main.cpp
  77. 74
      CPP06/ex00/Converter.cpp
  78. 39
      CPP06/ex00/Converter.hpp
  79. 21
      CPP06/ex00/Makefile
  80. 24
      CPP06/ex00/main.cpp
  81. 18
      CPP06/ex01/Data.h
  82. 19
      CPP06/ex01/Makefile
  83. 40
      CPP06/ex01/main.cpp
  84. 21
      CPP06/ex02/A.hpp
  85. 19
      CPP06/ex02/B.hpp
  86. 19
      CPP06/ex02/Base.hpp
  87. 19
      CPP06/ex02/C.hpp
  88. 11
      CPP06/ex02/Classes.cpp
  89. 17
      CPP06/ex02/Makefile
  90. 90
      CPP06/ex02/main.cpp
  91. 20
      CPP07/ex00/Makefile
  92. 32
      CPP07/ex00/main.cpp
  93. 29
      CPP07/ex00/main.hpp
  94. 21
      CPP07/ex01/Makefile
  95. 19
      CPP07/ex01/iter.hpp
  96. 33
      CPP07/ex01/main.cpp
  97. 61
      CPP07/ex02/Array.hpp
  98. 20
      CPP07/ex02/Makefile
  99. 32
      CPP07/ex02/main.cpp
  100. BIN
      CPP08/cpp08.pdf

15
CPP00/ex00/Makefile

@ -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

2
CPP00/ex00/main.cpp

@ -11,7 +11,6 @@
/* ************************************************************************** */
# include <iostream>
# include <locale.h>
int main(int argc, char **argv)
{
@ -30,5 +29,4 @@ int main(int argc, char **argv)
}
}
std::cout << std::endl;
return (EXIT_SUCCESS);
}

BIN
CPP00/ex00/megaphone

Binary file not shown.

36
CPP00/ex01/Contact.cpp

@ -15,41 +15,45 @@ Contact::Contact(){}
void Contact::set(int id)
{
this->id = id;
_id = id;
std::cout << "Enter new contact first name:\n";
std::cin >> this->datas[FIRST];
while (_datas[FIRST] == "")
std::cin >> _datas[FIRST];
std::cout << "Enter new contact last name:\n";
std::cin >> this->datas[LAST];
while (_datas[LAST] == "")
std::cin >> _datas[LAST];
std::cout << "Enter new contact nickname:\n";
std::cin >> this->datas[NICK];
while (_datas[NICK] == "")
std::cin >> _datas[NICK];
std::cout << "Enter new contact number:\n";
std::cin >> this->datas[NUMBER];
while (_datas[NUMBER] == "")
std::cin >> _datas[NUMBER];
std::cout << "Enter new contact secret:\n";
std::cin >> this->datas[SECRET];
std::cout << std::endl;
while (_datas[SECRET] == "")
std::cin >> _datas[SECRET];
}
void Contact::summary()
{
std::cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
std::cout << "|";
std::cout << std::setw(2) << this->id;
std::cout << std::setw(2) << _id;
std::cout << "|";
for (int i = 0; i <= SECRET; i++)
{
if (this->datas[i].length() < 10)
std::cout << std::setw(10) << this->datas[i] << "|";
if (_datas[i].length() < 10)
std::cout << std::setw(10) << _datas[i] << "|";
else
std::cout << this->datas[i].substr(0, 9) << "." << "|";
std::cout << _datas[i].substr(0, 9) << "." << "|";
}
std::cout << "\nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n";
}
void Contact::get()
{
std::cout <<"First name : " << this->datas[FIRST] << std::endl;
std::cout <<"Last name : " << this->datas[LAST] << std::endl;
std::cout <<"Nick name : " << this->datas[NICK] << std::endl;
std::cout <<"Phone nb : " << this->datas[NUMBER] << std::endl;
std::cout <<"Dark secret : " << this->datas[SECRET] << std::endl;
std::cout <<"First name : " << _datas[FIRST] << std::endl;
std::cout <<"Last name : " << _datas[LAST] << std::endl;
std::cout <<"Nick name : " << _datas[NICK] << std::endl;
std::cout <<"Phone nb : " << _datas[NUMBER] << std::endl;
std::cout <<"Dark secret : " << _datas[SECRET] << std::endl;
}

15
CPP00/ex01/Contact.hpp

@ -9,12 +9,10 @@
/* Updated: 2022/06/13 16:35:43 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CONTACT_HPP
# define CONTACT_HPP
# include <iostream>
# include <string>
# include <iomanip>
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
enum e_datas
{
@ -33,7 +31,6 @@ class Contact
void summary();
void get();
private:
int id;
std::string datas[5];
int _id;
std::string _datas[5];
};
#endif

20
CPP00/ex01/Makefile

@ -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

64
CPP00/ex01/PhoneBook.cpp

@ -12,15 +12,13 @@
#include "PhoneBook.hpp"
PhoneBook::PhoneBook()
{
this->id = 0;
PhoneBook::PhoneBook() {
_id = 0;
}
void PhoneBook::add()
{
int id = this->id % CONTACTS_AMOUNT;
if (this->id > 7)
void PhoneBook::add() {
int id = _id % CONTACTS_AMOUNT;
if (_id > CONTACTS_AMOUNT - 1)
{
std::cout << "You gonna remove the contact n." << id;
std::cout << "\nTo continue, type 'y': ";
@ -28,45 +26,37 @@ void PhoneBook::add()
std::cin >> confirm;
if (confirm != "y")
{
std::cout << "\nAbort.\n";
std::cout << std::endl << "Abort." << std::endl;
return ;
}
std::cout << std::endl;
}
this->contacts[id].set(id);
this->id++;
_contacts[id].set(id);
_id++;
}
void PhoneBook::search()
{
int id = 0;
Contact ctct;
ctct = this->contacts[0];
while (id++ < this->id % CONTACTS_AMOUNT)
{
ctct.summary();
ctct = this->contacts[id];
}
void PhoneBook::search() {
for (int id = 0; id < _id % CONTACTS_AMOUNT; id++)
_contacts[id].summary();
std::string sid;
std::cout << "Which contact do you wanna see [id?]:";
std::cin >> sid;
id = std::stoi(sid);
ctct = this->contacts[id];
ctct.get();
_contacts[(int)std::strtod(sid.c_str(), 0)].get();
}
int PhoneBook::run()
{
std::cout << "PhoneBook.\n" << "Commands available : ADD, SEARCH, EXIT.\n";
std::cout << "What do you want?\n";
std::cin >> this->cmd;
if (cmd == "EXIT")
return (EXIT_FAILURE);
if (cmd == "SEARCH")
this->search();
else if (cmd == "ADD")
this->add();
else
std::cout << "Unknown command.\n";
return (EXIT_SUCCESS);
void PhoneBook::run() {
std::string cmd;
while (cmd != "EXIT" && cmd != "3")
{
std::cout << "=== PHONEBOOK ===" << std::endl;
std::cout << "Commands available : 1-ADD, 2-SEARCH, 3-EXIT.\n";
std::cout << "What do you want? ";
std::cin >> cmd;
if (cmd == "ADD" || cmd == "1")
this->add();
else if (cmd == "SEARCH" || cmd == "2")
this->search();
else if (cmd != "EXIT" && cmd != "3")
std::cout << "Unknown command." << std::endl;
}
}

16
CPP00/ex01/PhoneBook.hpp

@ -9,21 +9,19 @@
/* Updated: 2022/06/13 15:35:30 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHONEBOOK_HPP
# define PHONEBOOK_HPP
# include "Contact.hpp"
# define CONTACTS_AMOUNT 8
#pragma once
#include "Contact.hpp"
#include <cstdlib>
#define CONTACTS_AMOUNT 8
class PhoneBook
{
public:
PhoneBook();
int run();
void run();
private:
Contact contacts[CONTACTS_AMOUNT];
std::string cmd;
int id;
Contact _contacts[CONTACTS_AMOUNT];
int _id;
void add();
void search();
};
#endif

6
CPP00/ex01/main.cpp

@ -14,9 +14,7 @@
int main(void)
{
PhoneBook pb;
PhoneBook phone;
while (pb.run() == EXIT_SUCCESS)
;
return (EXIT_SUCCESS);
phone.run();
}

13
CPP01/ex00/Zombie.cpp

@ -12,17 +12,14 @@
#include "Zombie.hpp"
Zombie::Zombie(string name):_name(name)
{
Zombie::Zombie(string name):_name(name) {
cout << "*" << name << " died.*\n";
}
Zombie::~Zombie(void)
{
cout << "*" << this->_name << " really died.*\n";
Zombie::~Zombie(void) {
cout << "*Zombie " << _name << " really died.*\n";
}
void Zombie::announce(void) const
{
cout << this->_name << ": BraiiiiiiinnnzzzZ...\n";
void Zombie::announce(void) const {
cout << _name << ": BraiiiiiiinnnzzzZ...\n";
}

1
CPP01/ex00/Zombie.hpp

@ -17,6 +17,7 @@
# endif
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
using std::string;

1
CPP01/ex00/main.cpp

@ -20,5 +20,4 @@ int main(void)
randomChump("Aurel");
if (DEBUG)
system("leaks apocalypse");
return (EXIT_SUCCESS);
}

2
CPP01/ex01/Makefile

@ -1,7 +1,7 @@
NAME = apocalypse
SRCS = main.cpp Zombie.cpp zombieHorde.cpp
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -g -std=c++98 -Werror -Wextra -Wall
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME): $(OBJS)
c++ $(OBJS) -o $(NAME)

3
CPP01/ex01/Zombie.cpp

@ -21,7 +21,7 @@ Zombie::Zombie(string name):_name(name)
Zombie::~Zombie(void)
{
cout << "*" << _name << " really died.*\n";
cout << "*Zombie " << _name << " really died.*\n";
}
void Zombie::announce(void) const
@ -31,5 +31,6 @@ void Zombie::announce(void) const
void Zombie::setName(string name)
{
cout << "*" << name << " died.*\n";
_name = name;
}

2
CPP01/ex01/Zombie.hpp

@ -18,6 +18,8 @@
# define ZB_AMOUNT 8
#include <iostream>
#include <sstream>
#include <cstdlib>
using std::cin;
using std::cout;
using std::string;

BIN
CPP01/ex01/apocalypse

Binary file not shown.

9
CPP01/ex01/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 07:59:29 by narnaud #+# #+# */
/* Updated: 2022/06/16 12:44:26 by narnaud ### ########.fr */
/* Updated: 2022/07/18 08:19:08 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,11 +14,10 @@
int main(void)
{
Zombie *zombs = zombieHorde(ZB_AMOUNT, "Zombie");
int i = 0;
Zombie *zombs = zombieHorde(ZB_AMOUNT, "Z");
while (i < ZB_AMOUNT)
zombs[i++].announce();
for (int i = 0; i < ZB_AMOUNT; i++)
zombs[i].announce();
delete [] zombs;
if (DEBUG)
system("leaks apocalypse");

8
CPP01/ex01/zombieHorde.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 08:17:02 by narnaud #+# #+# */
/* Updated: 2022/06/16 12:47:20 by narnaud ### ########.fr */
/* Updated: 2022/07/18 08:20:03 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,11 @@ Zombie *zombieHorde(int N, string name)
int n = 0;
while (n < N)
{
ret[n].setName(name + std::to_string(n));
std::stringstream ss;
string s;
ss << name << n;
ss >> s;
ret[n].setName(s);
n++;
}
return (ret);

16
CPP01/ex02/Makefile

@ -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

4
CPP01/ex02/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 11:01:07 by narnaud #+# #+# */
/* Updated: 2022/06/14 11:08:21 by narnaud ### ########.fr */
/* Updated: 2022/07/18 08:31:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,9 +23,11 @@ int main(void)
cout << &str << endl;
cout << stringPTR << endl;
//cout << &stringPTR << endl;
cout << &stringREF << endl;
cout << str << endl;
cout << *stringPTR << endl;
//cout << stringPTR << endl;
cout << stringREF << endl;
}

10
CPP01/ex03/HumanA.hpp

@ -6,14 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 12:52:11 by narnaud #+# #+# */
/* Updated: 2022/06/16 10:33:40 by narnaud ### ########.fr */
/* Updated: 2022/07/18 10:06:45 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HUMANA_HPP
# define HUMANA_HPP
# include "Weapon.hpp"
#pragma once
#include "Weapon.hpp"
class HumanA
{
@ -24,5 +22,3 @@ class HumanA
string _name;
Weapon &_weapon;
};
#endif

9
CPP01/ex03/HumanB.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/16 09:13:28 by narnaud #+# #+# */
/* Updated: 2022/06/16 10:12:56 by narnaud ### ########.fr */
/* Updated: 2022/07/18 10:05:32 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,9 +17,7 @@ HumanB::HumanB(string name):_name(name), _weapon(NULL){}
HumanB::HumanB(string name, Weapon &weap):_name(name), _weapon(&weap){}
void HumanB::attack(void) const
{
void HumanB::attack(void) const {
std::cout << _name << " attacks with their ";
if (_weapon)
cout << _weapon->getType() << endl;
@ -27,7 +25,6 @@ void HumanB::attack(void) const
cout << "fists\n";
}
void HumanB::setWeapon(Weapon &weap)
{
void HumanB::setWeapon(Weapon &weap) {
_weapon = &weap;
}

10
CPP01/ex03/HumanB.hpp

@ -6,14 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 12:54:11 by narnaud #+# #+# */
/* Updated: 2022/06/16 10:33:28 by narnaud ### ########.fr */
/* Updated: 2022/07/18 10:07:11 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HUMANB_HPP
# define HUMANB_HPP
# include "Weapon.hpp"
#pragma once
#include "Weapon.hpp"
class HumanB
{
@ -26,5 +24,3 @@ class HumanB
string _name;
Weapon *_weapon;
};
#endif

12
CPP01/ex03/Weapon.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 12:51:23 by narnaud #+# #+# */
/* Updated: 2022/06/16 10:08:02 by narnaud ### ########.fr */
/* Updated: 2022/07/18 10:05:10 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,13 +14,11 @@
Weapon::Weapon(string type):_type(type){}
const string &Weapon::getType(void) const
{
return (this->_type);
const string &Weapon::getType(void) const {
return (_type);
}
void Weapon::setType(string type)
{
this->_type = type;
void Weapon::setType(string type) {
_type = type;
}

7
CPP01/ex03/Weapon.hpp

@ -6,12 +6,11 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 12:50:31 by narnaud #+# #+# */
/* Updated: 2022/06/16 10:04:50 by narnaud ### ########.fr */
/* Updated: 2022/07/18 10:07:39 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef WEAPON_HPP
# define WEAPON_HPP
#pragma once
# include <iostream>
using std::string;
@ -29,5 +28,3 @@ class Weapon
private:
string _type;
};
#endif

16
CPP01/ex04/Makefile

@ -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

30
CPP01/ex04/main.cpp

@ -1,14 +1,14 @@
/*
* my_sed(argc,argv)
* filename = argv[1];
* s1 = argv[2];
* s2 = argv[3];
* input::ifstream ifs;
* input = open(filename, std::ifstream::in);
*
*
*
* */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 10:27:42 by narnaud #+# #+# */
/* Updated: 2022/07/18 12:46:16 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
@ -31,10 +31,16 @@ int main(int argc, char **argv)
if (argc != 4)
return (EXIT_FAILURE);
ifstream input(argv[1]);
if (!input.is_open()) {
std::cout << "Invalid file." << std::endl;
return (EXIT_FAILURE);
}
oldStr = argv[2];
newStr = argv[3];
while (getline(input, buffer))
while (getline(input, buffer)) {
text += buffer;
text += '\n';
}
input.close();
fileLen = text.length();
oldLen = oldStr.length();

11
CPP01/ex05/Harl.cpp

@ -6,20 +6,15 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/17 11:14:29 by narnaud #+# #+# */
/* Updated: 2022/06/20 09:30:23 by narnaud ### ########.fr */
/* Updated: 2022/07/18 13:00:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Harl.hpp"
Harl::Harl (void) {
std::cout << "Harl default constructor called " << std::endl;
}
Harl::~Harl (void) {
Harl::Harl (void) {}
std::cout << "Harl default destructor called" << std::endl;
}
Harl::~Harl (void) {}
void Harl::_debug(void)
{

56
CPP01/ex06/Harl.cpp

@ -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();
}
}

32
CPP01/ex06/Harl.hpp

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

20
CPP01/ex06/Makefile

@ -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

20
CPP01/ex06/main.cpp

@ -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]);
}

22
CPP02/ex01/Fixed.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 10:07:44 by narnaud #+# #+# */
/* Updated: 2022/06/20 14:15:11 by narnaud ### ########.fr */
/* Updated: 2022/07/18 14:00:06 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,43 +22,37 @@ Fixed::Fixed (void) {
Fixed::Fixed (const int init_int) {
std::cout << "Fixed constructor from interger called " << std::endl;
std::cout << "Integer constructor called " << std::endl;
_raw_bits = init_int * (1 << _fract_bits);
}
Fixed::Fixed (const float init_float) {
std::cout << "Fixed constructor from float called " << std::endl;
std::cout << "Float constructor called " << std::endl;
_raw_bits = std::roundf(init_float * (1 << _fract_bits));
}
Fixed::Fixed (Fixed const & src) {
std::cout << "Fixed copy constructor called" << std::endl;
_raw_bits = src.getRawBits();
std::cout << "Copy constructor called" << std::endl;
*this = src;
}
Fixed & Fixed::operator= (Fixed const & src) {
std::cout << "Fixed assignment operator called" << std::endl;
std::cout << "Copy assignment operator called" << std::endl;
_raw_bits = src.getRawBits();
return (*this);
}
Fixed::~Fixed (void) {
std::cout << "Fixed default destructor called" << std::endl;
}
int Fixed::getRawBits(void) const
{
std::cout << "getRawBits member function called" << std::endl;
int Fixed::getRawBits(void) const {
return (_raw_bits);
}
void Fixed::setRawBits(int const raw)
{
std::cout << "setRawBits member function called" << std::endl;
void Fixed::setRawBits(int const raw) {
_raw_bits = raw;
}

12
CPP02/ex01/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 10:07:23 by narnaud #+# #+# */
/* Updated: 2022/06/20 13:35:26 by narnaud ### ########.fr */
/* Updated: 2022/07/18 13:51:53 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,14 +19,14 @@ int main(void)
Fixed const b( 10 );
Fixed const c( 42.42f );
Fixed const d( b );
Fixed e;
e = d;
//Fixed e;
//e = d;
a = Fixed( 1234.4321f );
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
//std::cout << a.getRawBits() << std::endl;
//std::cout << b.getRawBits() << std::endl;
//std::cout << c.getRawBits() << std::endl;
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;

3
CPP03/ex03/DiamondTrap.cpp

@ -6,13 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/23 15:19:54 by narnaud #+# #+# */
/* Updated: 2022/07/19 02:45:31 by narnaud ### ########.fr */
/* Updated: 2022/08/03 10:30:43 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "DiamondTrap.hpp"
DiamondTrap::DiamondTrap (std::string name): ScavTrap(name), FragTrap(name){
cout << ScavTrap::_energy << endl;
ClapTrap::_name = name + "_clap_name";
_name = name;
_health = FragTrap::_health;

4
CPP04/ex00/Animal.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:22:10 by narnaud ### ########.fr */
/* Updated: 2022/07/19 12:42:41 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,5 +39,5 @@ string Animal::getType(void) const {
return (_type);
}
void Animal::makeSound(void) const {
cout << "..." << endl;
cout << "Strange noise" << endl;
}

4
CPP04/ex00/WrongAnimal.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 09:02:11 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:22:03 by narnaud ### ########.fr */
/* Updated: 2022/07/19 12:43:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -38,5 +38,5 @@ string WrongAnimal::getType(void) const {
return (_type);
}
void WrongAnimal::makeSound(void) const {
cout << "..." << endl;
cout << "Strange noise" << endl;
}

6
CPP04/ex00/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 08:28:40 by narnaud #+# #+# */
/* Updated: 2022/06/24 10:16:31 by narnaud ### ########.fr */
/* Updated: 2022/07/19 12:40:35 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,5 +28,9 @@ int main(void)
meta->makeSound();
k->makeSound();
delete meta;
delete i;
delete j;
delete k;
return (0);
}

8
CPP04/ex01/Brain.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:57:44 by narnaud ### ########.fr */
/* Updated: 2022/07/19 14:16:32 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,13 +17,15 @@ Brain::Brain (void) {
}
Brain::Brain (Brain const & src) {
(void)src;
for (int i = 0; i < 100; i++)
_idea[i] = src._idea[i];
cout << "Brain copy constructor called" << endl;
}
Brain & Brain::operator= (Brain const & src) {
(void)src;
cout << "Brain assignment operator called" << endl;
for (int i = 0; i < 100; i++)
_idea[i] = src._idea[i];
return (*this);
}

8
CPP04/ex02/Brain.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:57:44 by narnaud ### ########.fr */
/* Updated: 2022/07/19 14:22:38 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,12 +17,14 @@ Brain::Brain (void) {
}
Brain::Brain (Brain const & src) {
(void)src;
for (int i = 0; i < 100; i++)
_ideas[i] = src._ideas[i];
cout << "Brain copy constructor called" << endl;
}
Brain & Brain::operator= (Brain const & src) {
(void)src;
for (int i = 0; i < 100; i++)
_ideas[i] = src._ideas[i];
cout << "Brain assignment operator called" << endl;
return (*this);
}

4
CPP04/ex02/Brain.hpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 11:24:53 by narnaud #+# #+# */
/* Updated: 2022/06/24 11:56:58 by narnaud ### ########.fr */
/* Updated: 2022/07/19 14:23:14 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@ using std::endl;
using std::string;
class Brain{
string ideas[100];
string _ideas[100];
public:
Brain(void);
Brain(Brain const & src);

95
CPP05/ex00/Bureaucrat.cpp

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

55
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;
int _grade;
public:
class GradeTooHighException: virtual public std::exception
{
public:
const char *what(void) const throw ();
};
class GradeTooLowException: virtual public std::exception
{
public:
const char *what(void) const throw ();
};
Bureaucrat(void);
Bureaucrat(const string name, int grade)
throw (GradeTooHighException, GradeTooLowException);
Bureaucrat(Bureaucrat const & src);
virtual ~Bureaucrat(void);
Bureaucrat & operator= (Bureaucrat const & src);
const string getName(void) const;
int getGrade(void) const;
void incrGrade(int diff = 1)
throw (GradeTooHighException);
void decrGrade(int diff = 1)
throw (GradeTooLowException);
class Bureaucrat {
const string _name;
int _grade;
public:
class GradeTooHighException : virtual public std::exception {
public:
const char *what(void) const throw();
};
class GradeTooLowException : virtual public std::exception {
public:
const char *what(void) const throw();
};
Bureaucrat(void);
Bureaucrat(const string name, int grade) throw(GradeTooHighException,
GradeTooLowException);
Bureaucrat(Bureaucrat const &src);
~Bureaucrat(void);
Bureaucrat &operator=(Bureaucrat const &src);
const string getName(void) const;
int getGrade(void) const;
void incrGrade(int diff = 1) throw(GradeTooHighException);
void decrGrade(int diff = 1) throw(GradeTooLowException);
};
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b);
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b);

27
CPP05/ex00/Makefile

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

70
CPP05/ex00/main.cpp

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

108
CPP05/ex01/Bureaucrat.cpp

@ -6,76 +6,88 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */
/* Updated: 2022/06/26 15:59:50 by narnaud ### ########.fr */
/* Updated: 2022/06/27 09:13:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat (void) {
cout << "Bureaucrat default constructor called " << endl;
/* Constructors */
Bureaucrat::Bureaucrat(void) {}
Bureaucrat::Bureaucrat(const string name,
int grade) throw(Bureaucrat::GradeTooHighException,
Bureaucrat::GradeTooLowException)
: _name(name) {
if (grade < 1)
throw Bureaucrat::GradeTooHighException();
if (grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = grade;
}
Bureaucrat::Bureaucrat (const string name, int grade)
throw (Bureaucrat::GradeTooHighException, Bureaucrat::GradeTooLowException): _name(name){
/* copy const. */
Bureaucrat::Bureaucrat(Bureaucrat const &src) { (void)src; }
if (grade < 1)
throw Bureaucrat::GradeTooHighException();
if (grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = grade;
cout << "Bureaucrat " << _name << " created grade: " << _grade << endl;
/* assign const. */
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
(void)src;
return (*this);
}
Bureaucrat::Bureaucrat (Bureaucrat const & src) {
(void)src;
cout << "Bureaucrat copy constructor called" << endl;
}
/* Destructor */
Bureaucrat::~Bureaucrat(void) {}
Bureaucrat & Bureaucrat::operator= (Bureaucrat const & src) {
(void)src;
cout << "Bureaucrat assignment operator called" << endl;
return (*this);
}
/* Getters */
Bureaucrat::~Bureaucrat (void) {
cout << "Bureaucrat default destructor called << endl;
}
const string Bureaucrat::getName(void) const { return (_name); }
const string Bureaucrat::getName(void) const{
return (_name);
}
int Bureaucrat::getGrade(void) const { return (_grade); }
int Bureaucrat::getGrade(void) const{
return (_grade);
}
/* Setters */
void Bureaucrat::incrGrade(int diff)
throw (Bureaucrat::GradeTooHighException) {
int new_grade = _grade - diff;
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
_grade = new_grade;
void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) {
int new_grade = _grade - diff;
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
_grade = new_grade;
cout << _name << " grade increased." << endl;
}
void Bureaucrat::decrGrade(int diff)
throw (Bureaucrat::GradeTooLowException) {
int new_grade = _grade + diff;
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = new_grade;
void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
int new_grade = _grade + diff;
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = new_grade;
cout << _name << " grade decreased." << endl;
}
const char* Bureaucrat::GradeTooHighException::what(void) const throw() {
return ("Grade was too high for a bureaucrat");
/* Exceptions */
const char *Bureaucrat::GradeTooHighException::what(void) const throw() {
return ("error: grade is too high for a bureaucrat");
}
const char* Bureaucrat::GradeTooLowException::what(void) const throw() {
return ("Grade was too low for a bureaucrat");
const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
return ("error: grade is too low for a bureaucrat");
}
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b) {
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl;
return (out);
/* Externs */
void Bureaucrat::signForm(Form &form) const {
cout << *this << " sign " << form << ":";
try {
if (!form.beSigned(*this))
cout << " success."<< endl;
else
cout << " error.\n\tform is already signed." << endl;
} catch (std::exception &e) {
cout << " error.\n\tbureaucrat grade is too low." << endl;
}
}
/* Stream */
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
out << b.getName() << "(" << b.getGrade() << ")";
return (out);
}

64
CPP05/ex01/Bureaucrat.hpp

@ -6,50 +6,48 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */
/* Updated: 2022/06/26 16:35:38 by narnaud ### ########.fr */
/* Updated: 2022/06/27 09:36:27 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Form.hpp"
#include <string>
#include <iostream>
#include <stdexcept>
#include <string>
using std::cout;
using std::endl;
using std::string;
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);
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;
};
std::ostream &operator<< (std::ostream &out, Bureaucrat const &b);
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b);

73
CPP05/ex01/Form.cpp

@ -6,32 +6,75 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/26 16:12:05 by narnaud #+# #+# */
/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */
/* Updated: 2022/06/27 09:40:11 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Form.hpp"
Form::Form(const string name, const int signGrade, const 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;
/* Constructors */
Form::Form(void) {}
Form::Form(const string name, int signGrade,
int exeGrade) throw(Form::GradeTooHighException,
Form::GradeTooLowException)
: _name(name) {
if (signGrade < 1 || exeGrade < 1)
throw Form::GradeTooHighException();
if (signGrade > 150 || exeGrade > 150)
throw Form::GradeTooLowException();
_signGrade = signGrade;
_exeGrade = exeGrade;
_isSigned = 0;
}
const string Form::getName(void) const {
return (_name);
/* copy const. */
Form::Form(Form const &f) { (void)f; }
/* assign const. */
Form &Form::operator=(Form const &f) {
(void)f;
return (*this);
}
const int Form::getSignGrade(void) const {
return (_signGrade);
/* Destructor */
Form::~Form(void) {}
/* Getters */
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)
return (1);
if (b.getGrade() > _signGrade)
throw GradeTooLowException();
_isSigned = 1;
return (0);
}
const int Form::getExeGrade(void) const {
return (_exeGrade);
/* Except */
const char *Form::GradeTooHighException::what(void) const throw() {
return ("error: grade is too high for a form");
}
const char *Form::GradeTooLowException::what(void) const throw() {
return ("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);
};

62
CPP05/ex01/Form.hpp

@ -6,47 +6,47 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/26 15:47:12 by narnaud #+# #+# */
/* Updated: 2022/06/27 01:05:27 by narnaud ### ########.fr */
/* Updated: 2022/06/27 09:36:10 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Bureaucrat.hpp"
#include <string>
#include <iostream>
#include <stdexcept>
#include <string>
using std::cout;
using std::endl;
using std::string;
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 ();
}
Form(void);
Form(const string name, const int signGrade,const int exeGrade)
throw (GradeTooHighException, GradeTooLowException);
Form & operator=(Form const &f);
Form(Form const &f);
~Form();
const string getName(void) const;
const int getSignGrade(void) const;
const int getExeGrade(void) const;
beSigned(Bureaucrat);
}
class Bureaucrat;
class Form {
const string _name;
int _signGrade;
int _exeGrade;
bool _isSigned;
public:
class GradeTooHighException : virtual public std::exception {
public:
const char *what(void) const throw();
};
class GradeTooLowException : virtual public std::exception {
public:
const char *what(void) const throw();
};
Form(void);
Form(const string name, const int signGrade,
const int exeGrade) throw(GradeTooHighException, GradeTooLowException);
Form &operator=(Form const &f);
Form(Form const &f);
~Form(void);
const string getName(void) const;
int getSignGrade(void) const;
int getExeGrade(void) const;
bool beSigned(const Bureaucrat &b) throw(GradeTooLowException);
};
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();
return (out);
}
std::ostream &operator<<(std::ostream &out, const Form &f);

60
CPP05/ex01/main.cpp

@ -6,59 +6,23 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:16:27 by narnaud #+# #+# */
/* Updated: 2022/06/26 16:34:45 by narnaud ### ########.fr */
/* Updated: 2022/06/27 09:40:50 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
int main(void) {
Bureaucrat first("first", 1);
Bureaucrat mid("midle", 75);
Bureaucrat last("last", 150);
Form garb("garbage", 75, 75);
void create_too_low(void) {
try {
Bureaucrat b("trash", 151);
} catch (std::exception &e) {
cout << "Grade 151 creation: " << 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) {
try {
b.incrGrade();
} catch (std::exception &e) {
cout << "Grade 1 increase: "<< e.what() << endl;
}
}
void last_dismiss(Bureaucrat &b) {
try {
b.decrGrade();
} catch (std::exception &e) {
cout << "Grade 150 decrease: "<< e.what() << endl;
}
}
int main(void)
{
Bureaucrat first("first", 1);
Bureaucrat last("last", 150);
cout << first;
cout << last;
create_too_low();
create_too_high();
first_evolve(first);
last_dismiss(last);
cout << "Bureaucrats: " << first << "/" << mid << "/" << last << endl;
cout << "Forms: " << garb << endl << endl;
cout << first;
cout << last;
last.signForm(garb);
mid.signForm(garb);
first.signForm(garb);
}

103
CPP05/ex02/Bureaucrat.cpp

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

54
CPP05/ex02/Bureaucrat.hpp

@ -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);

86
CPP05/ex02/Form.cpp

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

54
CPP05/ex02/Form.hpp

@ -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);

20
CPP05/ex02/Makefile

@ -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

22
CPP05/ex02/PresidentialPardonForm.cpp

@ -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;
}

23
CPP05/ex02/PresidentialPardonForm.hpp

@ -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;
};

34
CPP05/ex02/RobotomyRequestForm.cpp

@ -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;
}

25
CPP05/ex02/RobotomyRequestForm.hpp

@ -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;
};

34
CPP05/ex02/ShrubberyCreationForm.cpp

@ -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();
}

22
CPP05/ex02/ShrubberyCreationForm.hpp

@ -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;
};

38
CPP05/ex02/main.cpp

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

97
CPP05/ex03/Bureaucrat.cpp

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

54
CPP05/ex03/Bureaucrat.hpp

@ -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);

91
CPP05/ex03/Form.cpp

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

57
CPP05/ex03/Form.hpp

@ -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);

48
CPP05/ex03/Intern.cpp

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

35
CPP05/ex03/Intern.hpp

@ -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:
};

20
CPP05/ex03/Makefile

@ -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

27
CPP05/ex03/PresidentialPardonForm.cpp

@ -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;
}

24
CPP05/ex03/PresidentialPardonForm.hpp

@ -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;
};

39
CPP05/ex03/RobotomyRequestForm.cpp

@ -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;
}

26
CPP05/ex03/RobotomyRequestForm.hpp

@ -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;
};

39
CPP05/ex03/ShrubberyCreationForm.cpp

@ -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();
}

22
CPP05/ex03/ShrubberyCreationForm.hpp

@ -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;
};

47
CPP05/ex03/main.cpp

@ -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;
}

74
CPP06/ex00/Converter.cpp

@ -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;
}

39
CPP06/ex00/Converter.hpp

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

21
CPP06/ex00/Makefile

@ -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

24
CPP06/ex00/main.cpp

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

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

20
CPP07/ex00/Makefile

@ -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

32
CPP07/ex00/main.cpp

@ -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;
}

29
CPP07/ex00/main.hpp

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

21
CPP07/ex01/Makefile

@ -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

19
CPP07/ex01/iter.hpp

@ -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]);
}
}

33
CPP07/ex01/main.cpp

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

61
CPP07/ex02/Array.hpp

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

20
CPP07/ex02/Makefile

@ -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

32
CPP07/ex02/main.cpp

@ -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;
}
}

BIN
CPP08/cpp08.pdf

Binary file not shown.

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save