diff --git a/CPP00/ex00/Makefile b/CPP00/ex00/Makefile index c1de22d..458e89f 100644 --- a/CPP00/ex00/Makefile +++ b/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 diff --git a/CPP00/ex00/main.cpp b/CPP00/ex00/main.cpp index 54d37c2..71b422c 100644 --- a/CPP00/ex00/main.cpp +++ b/CPP00/ex00/main.cpp @@ -11,7 +11,6 @@ /* ************************************************************************** */ # include -# include int main(int argc, char **argv) { @@ -30,5 +29,4 @@ int main(int argc, char **argv) } } std::cout << std::endl; - return (EXIT_SUCCESS); } diff --git a/CPP00/ex00/megaphone b/CPP00/ex00/megaphone deleted file mode 100755 index 2f79b4d..0000000 Binary files a/CPP00/ex00/megaphone and /dev/null differ diff --git a/CPP00/ex01/Contact.cpp b/CPP00/ex01/Contact.cpp index 5f82dd0..cd9ffee 100644 --- a/CPP00/ex01/Contact.cpp +++ b/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; } diff --git a/CPP00/ex01/Contact.hpp b/CPP00/ex01/Contact.hpp index 5d7665c..35f1526 100644 --- a/CPP00/ex01/Contact.hpp +++ b/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 -# include -# include +#pragma once +#include +#include +#include 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 diff --git a/CPP00/ex01/Makefile b/CPP00/ex01/Makefile new file mode 100644 index 0000000..8fc3237 --- /dev/null +++ b/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 diff --git a/CPP00/ex01/PhoneBook.cpp b/CPP00/ex01/PhoneBook.cpp index f2e798d..6871ab3 100644 --- a/CPP00/ex01/PhoneBook.cpp +++ b/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; + } } diff --git a/CPP00/ex01/PhoneBook.hpp b/CPP00/ex01/PhoneBook.hpp index 88ce766..85d3030 100644 --- a/CPP00/ex01/PhoneBook.hpp +++ b/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 +#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 diff --git a/CPP00/ex01/main.cpp b/CPP00/ex01/main.cpp index f7a6382..762764a 100644 --- a/CPP00/ex01/main.cpp +++ b/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(); } diff --git a/CPP01/ex00/Zombie.cpp b/CPP01/ex00/Zombie.cpp index fbe34f7..a29d168 100644 --- a/CPP01/ex00/Zombie.cpp +++ b/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"; } diff --git a/CPP01/ex00/Zombie.hpp b/CPP01/ex00/Zombie.hpp index c57e189..e4e2b5e 100644 --- a/CPP01/ex00/Zombie.hpp +++ b/CPP01/ex00/Zombie.hpp @@ -17,6 +17,7 @@ # endif #include +#include using std::cin; using std::cout; using std::string; diff --git a/CPP01/ex00/main.cpp b/CPP01/ex00/main.cpp index 14ddbb6..1066874 100644 --- a/CPP01/ex00/main.cpp +++ b/CPP01/ex00/main.cpp @@ -20,5 +20,4 @@ int main(void) randomChump("Aurel"); if (DEBUG) system("leaks apocalypse"); - return (EXIT_SUCCESS); } diff --git a/CPP01/ex01/Makefile b/CPP01/ex01/Makefile index 27da46f..0c31381 100644 --- a/CPP01/ex01/Makefile +++ b/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) diff --git a/CPP01/ex01/Zombie.cpp b/CPP01/ex01/Zombie.cpp index 7cc1cf8..d4716d2 100644 --- a/CPP01/ex01/Zombie.cpp +++ b/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; } diff --git a/CPP01/ex01/Zombie.hpp b/CPP01/ex01/Zombie.hpp index d115ed5..bb48858 100644 --- a/CPP01/ex01/Zombie.hpp +++ b/CPP01/ex01/Zombie.hpp @@ -18,6 +18,8 @@ # define ZB_AMOUNT 8 #include +#include +#include using std::cin; using std::cout; using std::string; diff --git a/CPP01/ex01/apocalypse b/CPP01/ex01/apocalypse deleted file mode 100755 index cc578a7..0000000 Binary files a/CPP01/ex01/apocalypse and /dev/null differ diff --git a/CPP01/ex01/main.cpp b/CPP01/ex01/main.cpp index 43bcb50..4bc10b3 100644 --- a/CPP01/ex01/main.cpp +++ b/CPP01/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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"); diff --git a/CPP01/ex01/zombieHorde.cpp b/CPP01/ex01/zombieHorde.cpp index 18c7511..85faf50 100644 --- a/CPP01/ex01/zombieHorde.cpp +++ b/CPP01/ex01/zombieHorde.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/CPP01/ex02/Makefile b/CPP01/ex02/Makefile new file mode 100644 index 0000000..1b3c76f --- /dev/null +++ b/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 diff --git a/CPP01/ex02/main.cpp b/CPP01/ex02/main.cpp index 9626a81..d56b7fa 100644 --- a/CPP01/ex02/main.cpp +++ b/CPP01/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } diff --git a/CPP01/ex03/HumanA.hpp b/CPP01/ex03/HumanA.hpp index ce7ad11..dca0290 100644 --- a/CPP01/ex03/HumanA.hpp +++ b/CPP01/ex03/HumanA.hpp @@ -6,14 +6,12 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/CPP01/ex03/HumanB.cpp b/CPP01/ex03/HumanB.cpp index 6555eba..e530d1c 100644 --- a/CPP01/ex03/HumanB.cpp +++ b/CPP01/ex03/HumanB.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } diff --git a/CPP01/ex03/HumanB.hpp b/CPP01/ex03/HumanB.hpp index b4bef57..d93c6ad 100644 --- a/CPP01/ex03/HumanB.hpp +++ b/CPP01/ex03/HumanB.hpp @@ -6,14 +6,12 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/CPP01/ex03/Weapon.cpp b/CPP01/ex03/Weapon.cpp index e52f882..ba1d073 100644 --- a/CPP01/ex03/Weapon.cpp +++ b/CPP01/ex03/Weapon.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } diff --git a/CPP01/ex03/Weapon.hpp b/CPP01/ex03/Weapon.hpp index 52a2064..80dedfb 100644 --- a/CPP01/ex03/Weapon.hpp +++ b/CPP01/ex03/Weapon.hpp @@ -6,12 +6,11 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 using std::string; @@ -29,5 +28,3 @@ class Weapon private: string _type; }; - -#endif diff --git a/CPP01/ex04/Makefile b/CPP01/ex04/Makefile new file mode 100644 index 0000000..87472aa --- /dev/null +++ b/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 diff --git a/CPP01/ex04/main.cpp b/CPP01/ex04/main.cpp index b86caf8..4dff08e 100644 --- a/CPP01/ex04/main.cpp +++ b/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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/18 10:27:42 by narnaud #+# #+# */ +/* Updated: 2022/07/18 12:46:16 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ #include @@ -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(); diff --git a/CPP01/ex05/Harl.cpp b/CPP01/ex05/Harl.cpp index 151aa22..7861201 100644 --- a/CPP01/ex05/Harl.cpp +++ b/CPP01/ex05/Harl.cpp @@ -6,20 +6,15 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { diff --git a/CPP01/ex06/Harl.cpp b/CPP01/ex06/Harl.cpp new file mode 100644 index 0000000..42a996e --- /dev/null +++ b/CPP01/ex06/Harl.cpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(); + } +} + diff --git a/CPP01/ex06/Harl.hpp b/CPP01/ex06/Harl.hpp new file mode 100644 index 0000000..96baed2 --- /dev/null +++ b/CPP01/ex06/Harl.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/17 11:13:50 by narnaud #+# #+# */ +/* Updated: 2022/06/20 09:30:38 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +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); + +}; diff --git a/CPP01/ex06/Makefile b/CPP01/ex06/Makefile new file mode 100644 index 0000000..bbc9a22 --- /dev/null +++ b/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 diff --git a/CPP01/ex06/main.cpp b/CPP01/ex06/main.cpp new file mode 100644 index 0000000..6c91b5a --- /dev/null +++ b/CPP01/ex06/main.cpp @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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]); +} diff --git a/CPP02/ex01/Fixed.cpp b/CPP02/ex01/Fixed.cpp index 6658e4c..70e1cc1 100644 --- a/CPP02/ex01/Fixed.cpp +++ b/CPP02/ex01/Fixed.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } diff --git a/CPP02/ex01/main.cpp b/CPP02/ex01/main.cpp index 944f370..67c29d7 100644 --- a/CPP02/ex01/main.cpp +++ b/CPP02/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/CPP03/ex03/DiamondTrap.cpp b/CPP03/ex03/DiamondTrap.cpp index c09efc3..d120025 100644 --- a/CPP03/ex03/DiamondTrap.cpp +++ b/CPP03/ex03/DiamondTrap.cpp @@ -6,13 +6,14 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/CPP04/ex00/Animal.cpp b/CPP04/ex00/Animal.cpp index 71a3e72..d17f56c 100644 --- a/CPP04/ex00/Animal.cpp +++ b/CPP04/ex00/Animal.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } diff --git a/CPP04/ex00/WrongAnimal.cpp b/CPP04/ex00/WrongAnimal.cpp index 2c07637..b5ae921 100644 --- a/CPP04/ex00/WrongAnimal.cpp +++ b/CPP04/ex00/WrongAnimal.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; } diff --git a/CPP04/ex00/main.cpp b/CPP04/ex00/main.cpp index 9882379..48ba373 100644 --- a/CPP04/ex00/main.cpp +++ b/CPP04/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/CPP04/ex01/Brain.cpp b/CPP04/ex01/Brain.cpp index 2d69c0b..5c84ea5 100644 --- a/CPP04/ex01/Brain.cpp +++ b/CPP04/ex01/Brain.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/CPP04/ex02/Brain.cpp b/CPP04/ex02/Brain.cpp index 2d69c0b..60e66b6 100644 --- a/CPP04/ex02/Brain.cpp +++ b/CPP04/ex02/Brain.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/CPP04/ex02/Brain.hpp b/CPP04/ex02/Brain.hpp index 4598a81..9108054 100644 --- a/CPP04/ex02/Brain.hpp +++ b/CPP04/ex02/Brain.hpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/CPP05/ex00/Bureaucrat.cpp b/CPP05/ex00/Bureaucrat.cpp index c30b527..53ef85c 100644 --- a/CPP05/ex00/Bureaucrat.cpp +++ b/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); +} diff --git a/CPP05/ex00/Bureaucrat.hpp b/CPP05/ex00/Bureaucrat.hpp index a0f2e05..af9a1b6 100644 --- a/CPP05/ex00/Bureaucrat.hpp +++ b/CPP05/ex00/Bureaucrat.hpp @@ -12,41 +12,36 @@ #pragma once -#include #include #include +#include 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); diff --git a/CPP05/ex00/Makefile b/CPP05/ex00/Makefile index 0a9375a..ac1738d 100644 --- a/CPP05/ex00/Makefile +++ b/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 diff --git a/CPP05/ex00/main.cpp b/CPP05/ex00/main.cpp index e76dd1a..1e277e8 100644 --- a/CPP05/ex00/main.cpp +++ b/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); } diff --git a/CPP05/ex01/Bureaucrat.cpp b/CPP05/ex01/Bureaucrat.cpp index 846cdaf..5772f7c 100644 --- a/CPP05/ex01/Bureaucrat.cpp +++ b/CPP05/ex01/Bureaucrat.cpp @@ -6,76 +6,88 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} diff --git a/CPP05/ex01/Bureaucrat.hpp b/CPP05/ex01/Bureaucrat.hpp index 816290c..42be684 100644 --- a/CPP05/ex01/Bureaucrat.hpp +++ b/CPP05/ex01/Bureaucrat.hpp @@ -6,50 +6,48 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include #include +#include 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); diff --git a/CPP05/ex01/Form.cpp b/CPP05/ex01/Form.cpp index 01584b9..b8c91ab 100644 --- a/CPP05/ex01/Form.cpp +++ b/CPP05/ex01/Form.cpp @@ -6,32 +6,75 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +}; diff --git a/CPP05/ex01/Form.hpp b/CPP05/ex01/Form.hpp index d6a5ee9..affc6a0 100644 --- a/CPP05/ex01/Form.hpp +++ b/CPP05/ex01/Form.hpp @@ -6,47 +6,47 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include #include +#include 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); diff --git a/CPP05/ex01/main.cpp b/CPP05/ex01/main.cpp index ef74330..17b2318 100644 --- a/CPP05/ex01/main.cpp +++ b/CPP05/ex01/main.cpp @@ -6,59 +6,23 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/CPP05/ex02/Bureaucrat.cpp b/CPP05/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..4db847c --- /dev/null +++ b/CPP05/ex02/Bureaucrat.cpp @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/CPP05/ex02/Bureaucrat.hpp b/CPP05/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..6b0cf5d --- /dev/null +++ b/CPP05/ex02/Bureaucrat.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +#include +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); diff --git a/CPP05/ex02/Form.cpp b/CPP05/ex02/Form.cpp new file mode 100644 index 0000000..a3d4803 --- /dev/null +++ b/CPP05/ex02/Form.cpp @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}; diff --git a/CPP05/ex02/Form.hpp b/CPP05/ex02/Form.hpp new file mode 100644 index 0000000..5a2a3fc --- /dev/null +++ b/CPP05/ex02/Form.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +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); diff --git a/CPP05/ex02/Makefile b/CPP05/ex02/Makefile new file mode 100644 index 0000000..bbad008 --- /dev/null +++ b/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 diff --git a/CPP05/ex02/PresidentialPardonForm.cpp b/CPP05/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..6cd5090 --- /dev/null +++ b/CPP05/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} diff --git a/CPP05/ex02/PresidentialPardonForm.hpp b/CPP05/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..2b29c05 --- /dev/null +++ b/CPP05/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +}; diff --git a/CPP05/ex02/RobotomyRequestForm.cpp b/CPP05/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..40b5b3c --- /dev/null +++ b/CPP05/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} diff --git a/CPP05/ex02/RobotomyRequestForm.hpp b/CPP05/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..defc8d2 --- /dev/null +++ b/CPP05/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +class RobotomyRequestForm : public Form { + +public: + RobotomyRequestForm(const string target); + ~RobotomyRequestForm(void); + void run(void) const; +}; diff --git a/CPP05/ex02/ShrubberyCreationForm.cpp b/CPP05/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..b6021b2 --- /dev/null +++ b/CPP05/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(); +} diff --git a/CPP05/ex02/ShrubberyCreationForm.hpp b/CPP05/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..7ef4cc4 --- /dev/null +++ b/CPP05/ex02/ShrubberyCreationForm.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +}; diff --git a/CPP05/ex02/main.cpp b/CPP05/ex02/main.cpp new file mode 100644 index 0000000..fe74a1d --- /dev/null +++ b/CPP05/ex02/main.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/CPP05/ex03/Bureaucrat.cpp b/CPP05/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..d89d230 --- /dev/null +++ b/CPP05/ex03/Bureaucrat.cpp @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/CPP05/ex03/Bureaucrat.hpp b/CPP05/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..5c7b86d --- /dev/null +++ b/CPP05/ex03/Bureaucrat.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +#include +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); diff --git a/CPP05/ex03/Form.cpp b/CPP05/ex03/Form.cpp new file mode 100644 index 0000000..f3eee3b --- /dev/null +++ b/CPP05/ex03/Form.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}; diff --git a/CPP05/ex03/Form.hpp b/CPP05/ex03/Form.hpp new file mode 100644 index 0000000..37484a8 --- /dev/null +++ b/CPP05/ex03/Form.hpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +#include +#include +#include +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); diff --git a/CPP05/ex03/Intern.cpp b/CPP05/ex03/Intern.cpp new file mode 100644 index 0000000..faf7823 --- /dev/null +++ b/CPP05/ex03/Intern.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/CPP05/ex03/Intern.hpp b/CPP05/ex03/Intern.hpp new file mode 100644 index 0000000..4c137e2 --- /dev/null +++ b/CPP05/ex03/Intern.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +#include +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: +}; diff --git a/CPP05/ex03/Makefile b/CPP05/ex03/Makefile new file mode 100644 index 0000000..fe92166 --- /dev/null +++ b/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 diff --git a/CPP05/ex03/PresidentialPardonForm.cpp b/CPP05/ex03/PresidentialPardonForm.cpp new file mode 100644 index 0000000..4dfde4a --- /dev/null +++ b/CPP05/ex03/PresidentialPardonForm.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} diff --git a/CPP05/ex03/PresidentialPardonForm.hpp b/CPP05/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..ef52eff --- /dev/null +++ b/CPP05/ex03/PresidentialPardonForm.hpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +}; diff --git a/CPP05/ex03/RobotomyRequestForm.cpp b/CPP05/ex03/RobotomyRequestForm.cpp new file mode 100644 index 0000000..15f76b0 --- /dev/null +++ b/CPP05/ex03/RobotomyRequestForm.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} diff --git a/CPP05/ex03/RobotomyRequestForm.hpp b/CPP05/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..adbc105 --- /dev/null +++ b/CPP05/ex03/RobotomyRequestForm.hpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +class RobotomyRequestForm: public Form { + + public: + + RobotomyRequestForm(const string target); + ~RobotomyRequestForm(void); + void run(void) const; +}; diff --git a/CPP05/ex03/ShrubberyCreationForm.cpp b/CPP05/ex03/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..5c73910 --- /dev/null +++ b/CPP05/ex03/ShrubberyCreationForm.cpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(); +} diff --git a/CPP05/ex03/ShrubberyCreationForm.hpp b/CPP05/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..508708a --- /dev/null +++ b/CPP05/ex03/ShrubberyCreationForm.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +}; diff --git a/CPP05/ex03/main.cpp b/CPP05/ex03/main.cpp new file mode 100644 index 0000000..6a0bc0f --- /dev/null +++ b/CPP05/ex03/main.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} diff --git a/CPP06/ex00/Converter.cpp b/CPP06/ex00/Converter.cpp new file mode 100644 index 0000000..08b6f0d --- /dev/null +++ b/CPP06/ex00/Converter.cpp @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Converter.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(_c); + } else { + cout << "Char: "; + _d = std::strtod(str.c_str(), 0); + _c = static_cast(_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(_d); + if (_d < std::numeric_limits::min() || + _d > std::numeric_limits::max()) + cout << "Off limits -> "; + if (std::isnan(_d)) + cout << "Impossible" << endl; + else + cout << _i << endl; + _f = static_cast(_d); + if (_d == 0 || _d / static_cast(_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; +} diff --git a/CPP06/ex00/Converter.hpp b/CPP06/ex00/Converter.hpp new file mode 100644 index 0000000..9384b73 --- /dev/null +++ b/CPP06/ex00/Converter.hpp @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Converter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/30 08:45:12 by narnaud #+# #+# */ +/* Updated: 2022/08/18 08:45:46 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +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); +}; diff --git a/CPP06/ex00/Makefile b/CPP06/ex00/Makefile new file mode 100644 index 0000000..eed92f9 --- /dev/null +++ b/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 + diff --git a/CPP06/ex00/main.cpp b/CPP06/ex00/main.cpp new file mode 100644 index 0000000..edfcd41 --- /dev/null +++ b/CPP06/ex00/main.cpp @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/28 08:44:43 by narnaud #+# #+# */ +/* Updated: 2022/07/21 12:16:42 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#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); +} diff --git a/CPP06/ex01/Data.h b/CPP06/ex01/Data.h new file mode 100644 index 0000000..81c1f9e --- /dev/null +++ b/CPP06/ex01/Data.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Data.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 09:28:26 by narnaud #+# #+# */ +/* Updated: 2022/09/29 09:28:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once +#include + +typedef struct s_Data { + std::string content; +} t_Data; diff --git a/CPP06/ex01/Makefile b/CPP06/ex01/Makefile new file mode 100644 index 0000000..25046d0 --- /dev/null +++ b/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 diff --git a/CPP06/ex01/main.cpp b/CPP06/ex01/main.cpp new file mode 100644 index 0000000..4948d39 --- /dev/null +++ b/CPP06/ex01/main.cpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: 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(ptr); +} + +t_Data* deserialize(uintptr_t raw) { + return reinterpret_cast(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); +} diff --git a/CPP06/ex02/A.hpp b/CPP06/ex02/A.hpp new file mode 100644 index 0000000..b12bbe7 --- /dev/null +++ b/CPP06/ex02/A.hpp @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); + +}; diff --git a/CPP06/ex02/B.hpp b/CPP06/ex02/B.hpp new file mode 100644 index 0000000..8aae00a --- /dev/null +++ b/CPP06/ex02/B.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}; diff --git a/CPP06/ex02/Base.hpp b/CPP06/ex02/Base.hpp new file mode 100644 index 0000000..073bedb --- /dev/null +++ b/CPP06/ex02/Base.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}; diff --git a/CPP06/ex02/C.hpp b/CPP06/ex02/C.hpp new file mode 100644 index 0000000..1700ce1 --- /dev/null +++ b/CPP06/ex02/C.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}; diff --git a/CPP06/ex02/Classes.cpp b/CPP06/ex02/Classes.cpp new file mode 100644 index 0000000..3119fc6 --- /dev/null +++ b/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){} diff --git a/CPP06/ex02/Makefile b/CPP06/ex02/Makefile new file mode 100644 index 0000000..fc5f675 --- /dev/null +++ b/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 diff --git a/CPP06/ex02/main.cpp b/CPP06/ex02/main.cpp new file mode 100644 index 0000000..c81f6ac --- /dev/null +++ b/CPP06/ex02/main.cpp @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 10:09:08 by narnaud #+# #+# */ +/* Updated: 2022/09/29 10:36:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#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(ptr); + if (a) + std::cout << "Identified a A" << std::endl; + B *b = dynamic_cast(ptr); + if (b) + std::cout << "Identified a B" << std::endl; + C *c = dynamic_cast(ptr); + if (c) + std::cout << "Identified a C" << std::endl; +} + +void identify(Base &ref) { + try { + A a = dynamic_cast(ref); + std::cout << "Identified a A" << std::endl; + } + catch (std::exception &e) { (void)e;} + try { + B b = dynamic_cast(ref); + std::cout << "Identified a B" << std::endl; + } + catch (std::exception &e) { (void)e;} + try { + C c = dynamic_cast(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; + } + +} diff --git a/CPP07/ex00/Makefile b/CPP07/ex00/Makefile new file mode 100644 index 0000000..be24ac4 --- /dev/null +++ b/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 diff --git a/CPP07/ex00/main.cpp b/CPP07/ex00/main.cpp new file mode 100644 index 0000000..31790cb --- /dev/null +++ b/CPP07/ex00/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:05:29 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:13:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main.hpp" +#include + +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; +} + diff --git a/CPP07/ex00/main.hpp b/CPP07/ex00/main.hpp new file mode 100644 index 0000000..659bb67 --- /dev/null +++ b/CPP07/ex00/main.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:05:52 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:13:08 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +template +void swap (T &t1, T &t2) { + T tmp; + tmp = t1; + t1 = t2; + t2 = tmp; +} + +template +T min (T &t1, T &t2) { + return (t1 < t2 ? t1 : t2); +} + +template +T max (T &t1, T &t2) { + return (t1 > t2 ? t1 : t2); +} diff --git a/CPP07/ex01/Makefile b/CPP07/ex01/Makefile new file mode 100644 index 0000000..cec2b40 --- /dev/null +++ b/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 + diff --git a/CPP07/ex01/iter.hpp b/CPP07/ex01/iter.hpp new file mode 100644 index 0000000..43d1d4f --- /dev/null +++ b/CPP07/ex01/iter.hpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:14:42 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:33:06 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +template +void iter(T *arr, int length, void (*fct)(T)) { + for (int i = 0; i < length; i++) { + fct(arr[i]); + } +} diff --git a/CPP07/ex01/main.cpp b/CPP07/ex01/main.cpp new file mode 100644 index 0000000..e4e1561 --- /dev/null +++ b/CPP07/ex01/main.cpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:14:45 by narnaud #+# #+# */ +/* Updated: 2022/09/05 08:43:03 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "iter.hpp" + +template +void put_t(T t) { + std::cout << t << std::endl; +} + +template +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); +} diff --git a/CPP07/ex02/Array.hpp b/CPP07/ex02/Array.hpp new file mode 100644 index 0000000..188d4c6 --- /dev/null +++ b/CPP07/ex02/Array.hpp @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Array.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/05 08:50:55 by narnaud #+# #+# */ +/* Updated: 2022/09/05 11:23:19 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +template class Array { + T *tab; + int nbr; + public: + Array() { + tab = new T[0]; + }; + + Array(int n) { + tab = new T[nbr = n]; + }; + + Array(const Array &a) { + tab = new T[nbr = a.size()]; + for (int i = 0; i < nbr; i++) { + tab[i] = a[i]; + } + }; + + ~Array() { + delete [] tab; + }; + + Array &operator= (const Array &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); + }; +}; diff --git a/CPP07/ex02/Makefile b/CPP07/ex02/Makefile new file mode 100644 index 0000000..6c35345 --- /dev/null +++ b/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 diff --git a/CPP07/ex02/main.cpp b/CPP07/ex02/main.cpp new file mode 100644 index 0000000..1dbb026 --- /dev/null +++ b/CPP07/ex02/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 a; + Array b(10); + Array 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; + } +} diff --git a/CPP08/cpp08.pdf b/CPP08/cpp08.pdf new file mode 100644 index 0000000..1aff8ba Binary files /dev/null and b/CPP08/cpp08.pdf differ diff --git a/CPP08/ex00/Makefile b/CPP08/ex00/Makefile new file mode 100644 index 0000000..1ecc9db --- /dev/null +++ b/CPP08/ex00/Makefile @@ -0,0 +1,22 @@ +NAME = easyfind +SRCS = main.cpp +OBJS= $(SRCS:.cpp=.o) + +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME) : $(OBJS) + $(CXX) $(CXXFLAGS) $(OBJS) -o $(NAME) + +all : $(NAME) + +clean : + rm -rf $(OBJS) + +fclean : clean + rm -rf $(NAME) + +re : fclean all + +.PHONY : all clean fclean re + diff --git a/CPP08/ex00/easyfind.hpp b/CPP08/ex00/easyfind.hpp new file mode 100644 index 0000000..d479138 --- /dev/null +++ b/CPP08/ex00/easyfind.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* easyfind.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:06:39 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:06:42 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include + +template +typename T::iterator easyfind(T &t, const int nb) { + typename T::iterator it; + it = find(t.begin(), t.end(), nb); + if (it == t.end()) + return (t.end()); + return (it); +} + +template +void is_easyfound(T it, T end) { + if (it != end) + std::cout << "You found: " << *it << std::endl; + else + std::cout << "You didn't found it." << std::endl; +} diff --git a/CPP08/ex00/main.cpp b/CPP08/ex00/main.cpp new file mode 100644 index 0000000..f902673 --- /dev/null +++ b/CPP08/ex00/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:06:21 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:06:26 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "easyfind.hpp" +#include +#include +#include +#include + +int main(void) { + std::vector tab; + std::vector::iterator tab_it; + std::list lst; + std::list::iterator lst_it; + std::string str = "abcdefghijklmnopqrstuvwxyz\n"; + std::string::iterator str_it; + + tab.push_back(5); + tab.push_back(50); + lst.push_back(5); + lst.push_back(50); + + tab_it = easyfind(tab, 50); + lst_it = easyfind(lst, 50); + str_it = easyfind(str, 50); + + std::cout << "Array search:" << std::endl; + is_easyfound(tab_it, tab.end()); + std::cout << "List search:" << std::endl; + is_easyfound(lst_it, lst.end()); + std::cout << "String search" << std::endl; + is_easyfound(str_it, str.end()); + return (0); +} diff --git a/CPP08/ex01/Makefile b/CPP08/ex01/Makefile new file mode 100644 index 0000000..81012ce --- /dev/null +++ b/CPP08/ex01/Makefile @@ -0,0 +1,22 @@ +NAME = span + +SRCS = main.cpp Span.cpp +OBJS = $(SRCS:.cpp=.o) + +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME): $(OBJS) + $(CXX) -g $(CXXFLAGS) $(OBJS) -o $(NAME) + +all: $(NAME) + +clean: + rm -rf $(OBJS) + +fclean: clean + rm -rf $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/CPP08/ex01/Span.cpp b/CPP08/ex01/Span.cpp new file mode 100644 index 0000000..d96f46a --- /dev/null +++ b/CPP08/ex01/Span.cpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:07:10 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:07:11 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" + +Span::Span(unsigned int N) : _capacity(N) {} + +Span::~Span() {} + +Span::Span(Span const &sp) { *this = sp; } + +Span &Span::operator=(Span const &sp) { + if (this == &sp) + return *this; + _vec = sp._vec; + _capacity = sp._capacity; + return (*this); +} + +void Span::addNumber(int const nb) { + if (_vec.size() == _capacity) + throw std::runtime_error("Too much number in span container."); + _vec.push_back(nb); +} + +unsigned int Span::shortestSpan() { + std::vector::iterator it; + long ret; + + if (_vec.size() < 2) + throw std::runtime_error("Not enought number to get shortest span."); + sort(_vec.begin(), _vec.end()); + it = _vec.begin() + 1; + ret = *it - *(it - 1); + for (it = _vec.begin() + 2; it < _vec.end() && ret; it++) + if (*it - *(it - 1) < ret) + ret = *it - *(it - 1); + return (ret); +} + +unsigned int Span::longestSpan() { + if (_vec.size() < 2) + throw std::runtime_error("Not enought number to get shortest span."); + sort(_vec.begin(), _vec.end()); + return (*(_vec.end() - 1) - *_vec.begin()); +} + +void Span::addNRandom(unsigned int N) { + if (_vec.size() + N < _capacity) + throw std::runtime_error("Too much number to add into span container."); + srand((unsigned)time(NULL)); + for (unsigned int i = 0; i < N; i++) + _vec.push_back((rand() - INT_MAX / 2) * 2); +} + +void Span::addRange(std::vector::iterator begin, std::vector::iterator end) { + if (_vec.size() + end - begin > _capacity) + throw std::runtime_error("Container don't have enouth free space for range you want to add."); + std::copy(begin, end, std::back_inserter(_vec)); +} diff --git a/CPP08/ex01/Span.hpp b/CPP08/ex01/Span.hpp new file mode 100644 index 0000000..2bc001c --- /dev/null +++ b/CPP08/ex01/Span.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Span.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:07:26 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:07:31 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +class Span { + std::vector _vec; + unsigned int _capacity; +public: + Span(); + Span(unsigned int N); + Span(Span const &sp); + ~Span(); + Span &operator=(Span const &sp); + void addNumber(int nb); + unsigned int shortestSpan(); + unsigned int longestSpan(); + void addNRandom(unsigned int N); + void addRange(std::vector::iterator begin, std::vector::iterator end); +}; diff --git a/CPP08/ex01/main.cpp b/CPP08/ex01/main.cpp new file mode 100644 index 0000000..483c0a1 --- /dev/null +++ b/CPP08/ex01/main.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 08:06:50 by narnaud #+# #+# */ +/* Updated: 2022/09/19 08:07:01 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Span.hpp" +#define RAND_SIZE 10000 + +int main(void) { + Span sp(10); + sp.addNumber(5); + sp.addNumber(4); + sp.addNumber(1); + sp.addNumber(-5); + + std::cout << "5 4 1 -1" << std::endl; + std::cout << "Shortest span: " << sp.shortestSpan() << std::endl; + std::cout << "Longest span: " << sp.longestSpan() << std::endl; + + Span sp2(RAND_SIZE); + sp2.addNRandom(RAND_SIZE); + + std::cout << RAND_SIZE << " numbers between " << INT_MIN << " and " << INT_MAX + << std::endl; + std::cout << "Shortest span: " << sp2.shortestSpan() << std::endl; + std::cout << "Longest span: " << sp2.longestSpan() << std::endl; + + Span sp3(500); + std::vector nums; + for (int n = 0; n < 500; n++) nums.push_back(n); + std::random_shuffle(nums.begin(), nums.end()); + sp3.addRange(nums.begin(), nums.begin() + 20); + std::cout << "Sp3:" << std::endl; + std::cout << "Shortest span: " << sp3.shortestSpan() << std::endl; + std::cout << "Longest span: " << sp3.longestSpan() << std::endl; + + sp3.addRange(nums.begin(), nums.end()); + + return (0); +} diff --git a/CPP08/ex02/Makefile b/CPP08/ex02/Makefile new file mode 100644 index 0000000..7faf266 --- /dev/null +++ b/CPP08/ex02/Makefile @@ -0,0 +1,22 @@ +NAME = muted_stack + +SRCS = main.cpp +OBJS = $(SRCS:.cpp=.o) + +CXX = c++ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME): $(OBJS) + $(CC) $(CXXFLAGS) $(OBJS) -o $(NAME) + +all: $(NAME) + +clean: + rm -rf $(OBJS) + +fclean: clean + rm -rf $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/CPP08/ex02/MutantStack.hpp b/CPP08/ex02/MutantStack.hpp new file mode 100644 index 0000000..e87cfbe --- /dev/null +++ b/CPP08/ex02/MutantStack.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* MutantStack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 09:39:40 by narnaud #+# #+# */ +/* Updated: 2022/09/19 09:39:45 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include + +template class MutantStack : public std::stack { +public: + MutantStack(void) {} + MutantStack(const MutantStack &st) { *this = st; } + ~MutantStack(void) {} + MutantStack &operator=(const MutantStack &st) { + (void)st; + return *this; + } + typedef typename std::stack::container_type::iterator iterator; + iterator begin() { return this->c.begin(); } + iterator end() { return this->c.end(); } +}; diff --git a/CPP08/ex02/main.cpp b/CPP08/ex02/main.cpp new file mode 100644 index 0000000..0551d33 --- /dev/null +++ b/CPP08/ex02/main.cpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/19 09:39:50 by narnaud #+# #+# */ +/* Updated: 2022/09/19 09:40:01 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "MutantStack.hpp" + +int main() { + MutantStack mstack; + mstack.push(5); + mstack.push(17); + std::cout << mstack.top() << std::endl; + mstack.pop(); + std::cout << mstack.size() << std::endl; + mstack.push(3); + mstack.push(5); + mstack.push(737); + //[...] + mstack.push(0); + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + ++it; + --it; + while (it != ite) { + std::cout << *it << std::endl; + ++it; + } + std::stack s(mstack); + return 0; +}