diff --git a/d01/ex01/Makefile b/d01/ex01/Makefile index 5579a85..b994b64 100644 --- a/d01/ex01/Makefile +++ b/d01/ex01/Makefile @@ -18,7 +18,7 @@ INCLUDES = -I$(D_HEADERS) D_SRCS = . SRCS = main.cpp \ Zombie.cpp \ - ZombieHorde.cpp + zombieHorde.cpp D_HEADERS = . HEADERS = Zombie.hpp diff --git a/d01/ex01/ZombieHorde.cpp b/d01/ex01/zombieHorde.cpp similarity index 100% rename from d01/ex01/ZombieHorde.cpp rename to d01/ex01/zombieHorde.cpp diff --git a/d01/ex03/HumanB.cpp b/d01/ex03/HumanB.cpp index 03c8a1f..49fb4c1 100644 --- a/d01/ex03/HumanB.cpp +++ b/d01/ex03/HumanB.cpp @@ -1,22 +1,18 @@ #include "HumanB.hpp" HumanB::HumanB( std::string name ) { - this->_name = name; - } HumanB::~HumanB() {} void HumanB::setWeapon( Weapon &weapon ) { - this->_weapon = &weapon; - } void HumanB::attack( void ) { - + if (!this->_weapon) + return; std::cout << this->_name << " attacks with their "; std::cout << this->_weapon->getType() << std::endl; - } diff --git a/d01/ex03/Weapon.cpp b/d01/ex03/Weapon.cpp index d92b812..5232fff 100644 --- a/d01/ex03/Weapon.cpp +++ b/d01/ex03/Weapon.cpp @@ -1,21 +1,15 @@ #include "Weapon.hpp" Weapon::Weapon( std::string type ) { - this->_type = type; return; - } Weapon::~Weapon() {} std::string const & Weapon::getType( void ) const { - return this->_type; - } void Weapon::setType( std::string type ) { - this->_type = type; - } diff --git a/d01/ex03/main.cpp b/d01/ex03/main.cpp index 539263c..b3ff5e0 100644 --- a/d01/ex03/main.cpp +++ b/d01/ex03/main.cpp @@ -14,6 +14,7 @@ int main() { Weapon club = Weapon("crude spiked club"); HumanB jim("Jim"); + jim.attack(); jim.setWeapon(club); jim.attack(); club.setType("some other type of club"); diff --git a/d01/ex03/war b/d01/ex03/war new file mode 100755 index 0000000..b82da62 Binary files /dev/null and b/d01/ex03/war differ diff --git a/d01/ex04/Sed.cpp b/d01/ex04/Sed.cpp index b5556e6..32736dd 100644 --- a/d01/ex04/Sed.cpp +++ b/d01/ex04/Sed.cpp @@ -16,20 +16,23 @@ void Sed::replace() { std::ofstream new_file(this->_new_file.c_str()); int len = this->_find.length(); std::string str; - char c_str[len + 1]; + char cstr[len + 1]; if (file.fail() || new_file.fail()) return; - file.get(c_str, len + 1, EOF); - str.assign(c_str); + if (len) + file.get(cstr, len + 1, EOF); + else + file.get(cstr, len + 2, EOF); + str.assign(cstr); while (!file.eof()) { - if (this->_find.compare(str) == 0) + if (len && this->_find.compare(str) == 0) { new_file << this->_replacement; - file.get(c_str, len + 1, EOF); - str.assign(c_str); + file.get(cstr, len + 1, EOF); + str.assign(cstr); continue; } else @@ -39,6 +42,4 @@ void Sed::replace() { } str.erase(str.end() - 1); new_file << str; - } - diff --git a/d01/ex04/sed b/d01/ex04/sed new file mode 100755 index 0000000..7faa8e9 Binary files /dev/null and b/d01/ex04/sed differ diff --git a/d01/ex04/unitests/test.sh b/d01/ex04/unitests/test.sh index 9031d4b..bf7fb23 100644 --- a/d01/ex04/unitests/test.sh +++ b/d01/ex04/unitests/test.sh @@ -1,10 +1,11 @@ #!/bin/bash cd $(dirname $0) source ./colors.sh -mkdir -p test_log +mkdir -p files_log make -C ../ &> /dev/null EXECUTABLE="sed" +FILES_LOG="files_log" LEAKS="" if [ $1 == "leaks" ] || [ $1 == "valgrind" ] then @@ -14,8 +15,8 @@ fi # RUN TESTS function run_tests { - touch $TESTNAME - echo "$CONTENT" > $TESTNAME + if [ -f $TESTNAME ];then + echo "$CONTENT" > $TESTNAME;fi $LEAKS ../$EXECUTABLE $TESTNAME "$FIND" "$REPLACEMENT" OUTPUT=$( cat $TESTNAME.replace ) @@ -26,11 +27,15 @@ fi echo -e $B_RED"$TESTNAME faillure"$ENDCO fi - mv $TESTNAME $TESTNAME.replace ./test_log + if [ -f $TESTNAME ];then + mv $TESTNAME ./$FILES_LOG;fi + if [ -f $TESTNAME.replace ];then + mv $TESTNAME.replace ./$FILES_LOG;fi } # TEST 1 ######################################## -TESTNAME="test1" +TESTNAME="one_line_file" +touch $TESTNAME FIND=" " REPLACEMENT="hello" CONTENT=" " @@ -38,7 +43,8 @@ RESULT="hello" run_tests # TEST 2 ######################################## -TESTNAME="test2" +TESTNAME="multi_line_file" +touch $TESTNAME FIND="ie" REPLACEMENT="++" CONTENT=$(cat << EOF @@ -60,7 +66,8 @@ EOF run_tests # TEST 3 ######################################## -TESTNAME="test3" +TESTNAME="replace_one_character" +touch $TESTNAME FIND="." REPLACEMENT="+" CONTENT=$(cat << EOF @@ -74,7 +81,8 @@ EOF run_tests # TEST 4 ######################################## -TESTNAME="test4" +TESTNAME="replace_two_characters" +touch $TESTNAME FIND=".." REPLACEMENT="++" CONTENT=$(cat << EOF @@ -90,7 +98,8 @@ EOF run_tests # TEST 5 ######################################## -TESTNAME="test5" +TESTNAME="first_letter_lure" +touch $TESTNAME FIND="mdr" REPLACEMENT="|||" CONTENT=$(cat << EOF @@ -104,7 +113,8 @@ EOF run_tests # TEST 6 ######################################## -TESTNAME="test6" +TESTNAME="half_replacement_lure" +touch $TESTNAME FIND="toutouille" REPLACEMENT="||||||||||" CONTENT=$(cat << EOF @@ -120,7 +130,8 @@ EOF run_tests # TEST 7 ######################################## -TESTNAME="test7" +TESTNAME="special_character_'" +touch $TESTNAME FIND="n't" REPLACEMENT="000" CONTENT=$(cat << EOF @@ -186,7 +197,8 @@ EOF run_tests # TEST 8 ######################################## -TESTNAME="test8" +TESTNAME="multiline_replacement" +touch $TESTNAME FIND=$(cat << EOF ry I don't @@ -213,3 +225,79 @@ EOF ) run_tests +# TEST 9 ######################################## +TESTNAME="empty_find" +touch $TESTNAME +FIND="" +REPLACEMENT="p" +CONTENT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +RESULT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +run_tests + +# TEST 10 ######################################## +TESTNAME="empty_find_and_replacement" +touch $TESTNAME +FIND="" +REPLACEMENT="" +CONTENT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +RESULT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +run_tests + +# TEST 11 ######################################## +TESTNAME="empty_replacement" +touch $TESTNAME +FIND="a" +REPLACEMENT="" +CONTENT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +RESULT=$(cat << EOF +No more ters, my hert is dry +I don't lugh nd I don't cry +I don't think bout you ll the time +But when I do - I wonder why +EOF +) +run_tests + +# TEST 12 ######################################## +TESTNAME="file_does_not_exist" +FIND="o" +REPLACEMENT="O" +CONTENT=$(cat << EOF +No more tears, my heart is dry +I don't laugh and I don't cry +I don't think about you all the time +But when I do - I wonder why +EOF +) +RESULT="" +run_tests diff --git a/d01/ex04/unitests/test_log/test1 b/d01/ex04/unitests/test_log/test1 deleted file mode 100644 index 8d1c8b6..0000000 --- a/d01/ex04/unitests/test_log/test1 +++ /dev/null @@ -1 +0,0 @@ - diff --git a/d01/ex04/unitests/test_log/test1.replace b/d01/ex04/unitests/test_log/test1.replace deleted file mode 100644 index b6fc4c6..0000000 --- a/d01/ex04/unitests/test_log/test1.replace +++ /dev/null @@ -1 +0,0 @@ -hello \ No newline at end of file diff --git a/d01/ex04/unitests/test_log/test2 b/d01/ex04/unitests/test_log/test2 deleted file mode 100644 index 23b0c96..0000000 --- a/d01/ex04/unitests/test_log/test2 +++ /dev/null @@ -1,5 +0,0 @@ -ce fichier -contient -plusieurs lignes -les unes au dessus des autres -youhouuu ioieux diff --git a/d01/ex04/unitests/test_log/test2.replace b/d01/ex04/unitests/test_log/test2.replace deleted file mode 100644 index a573c23..0000000 --- a/d01/ex04/unitests/test_log/test2.replace +++ /dev/null @@ -1,5 +0,0 @@ -ce fich++r -cont++nt -plus++urs lignes -les unes au dessus des autres -youhouuu io++ux diff --git a/d01/ex04/unitests/test_log/test3 b/d01/ex04/unitests/test_log/test3 deleted file mode 100644 index 922f88c..0000000 --- a/d01/ex04/unitests/test_log/test3 +++ /dev/null @@ -1 +0,0 @@ -....................................................; diff --git a/d01/ex04/unitests/test_log/test3.replace b/d01/ex04/unitests/test_log/test3.replace deleted file mode 100644 index d3b95af..0000000 --- a/d01/ex04/unitests/test_log/test3.replace +++ /dev/null @@ -1 +0,0 @@ -++++++++++++++++++++++++++++++++++++++++++++++++++++; diff --git a/d01/ex04/unitests/test_log/test4 b/d01/ex04/unitests/test_log/test4 deleted file mode 100644 index 0880dee..0000000 --- a/d01/ex04/unitests/test_log/test4 +++ /dev/null @@ -1,2 +0,0 @@ -...................................................; -. . . . . . . . . . . . . . . . . . . . . . . . . .; diff --git a/d01/ex04/unitests/test_log/test4.replace b/d01/ex04/unitests/test_log/test4.replace deleted file mode 100644 index 80f497e..0000000 --- a/d01/ex04/unitests/test_log/test4.replace +++ /dev/null @@ -1,2 +0,0 @@ -++++++++++++++++++++++++++++++++++++++++++++++++++.; -. . . . . . . . . . . . . . . . . . . . . . . . . .; diff --git a/d01/ex04/unitests/test_log/test5 b/d01/ex04/unitests/test_log/test5 deleted file mode 100644 index 671f5ed..0000000 --- a/d01/ex04/unitests/test_log/test5 +++ /dev/null @@ -1 +0,0 @@ -test de mmdr foubar diff --git a/d01/ex04/unitests/test_log/test5.replace b/d01/ex04/unitests/test_log/test5.replace deleted file mode 100644 index 991bdd3..0000000 --- a/d01/ex04/unitests/test_log/test5.replace +++ /dev/null @@ -1 +0,0 @@ -test de m||| foubar diff --git a/d01/ex04/unitests/test_log/test6 b/d01/ex04/unitests/test_log/test6 deleted file mode 100644 index a74f1c0..0000000 --- a/d01/ex04/unitests/test_log/test6 +++ /dev/null @@ -1,2 +0,0 @@ -trouve ce mot toutouille -et celui-la toutoutouille diff --git a/d01/ex04/unitests/test_log/test6.replace b/d01/ex04/unitests/test_log/test6.replace deleted file mode 100644 index 8072725..0000000 --- a/d01/ex04/unitests/test_log/test6.replace +++ /dev/null @@ -1,2 +0,0 @@ -trouve ce mot |||||||||| -et celui-la tou|||||||||| \ No newline at end of file diff --git a/d01/ex04/unitests/test_log/test7 b/d01/ex04/unitests/test_log/test7 deleted file mode 100644 index 05ee8e4..0000000 --- a/d01/ex04/unitests/test_log/test7 +++ /dev/null @@ -1,27 +0,0 @@ -No more tears, my heart is dry -I don't laugh and I don't cry -I don't think about you all the time -But when I do - I wonder why - -You have to go out of my door -And leave just like you did before -I know I said that I was sure -But rich men can't imagine poor. - -One day baby, we'll be old -Oh baby, we'll be old -And think of all the stories that we could have told - -Little me and little you -Kept doing all the things they do -They never really think it through -Like I can never think you're true - -Here I go again - the blame -The guilt, the pain, the hurt, the shame -The founding fathers of our plane -That's stuck in heavy clouds of rain. - -One day baby, we'll be old -Oh baby, we'll be old -And think of all the stories that we could have told diff --git a/d01/ex04/unitests/test_log/test7.replace b/d01/ex04/unitests/test_log/test7.replace deleted file mode 100644 index 7292451..0000000 --- a/d01/ex04/unitests/test_log/test7.replace +++ /dev/null @@ -1,27 +0,0 @@ -No more tears, my heart is dry -I do000 laugh and I do000 cry -I do000 think about you all the time -But when I do - I wonder why - -You have to go out of my door -And leave just like you did before -I know I said that I was sure -But rich men ca000 imagine poor. - -One day baby, we'll be old -Oh baby, we'll be old -And think of all the stories that we could have told - -Little me and little you -Kept doing all the things they do -They never really think it through -Like I can never think you're true - -Here I go again - the blame -The guilt, the pain, the hurt, the shame -The founding fathers of our plane -That's stuck in heavy clouds of rain. - -One day baby, we'll be old -Oh baby, we'll be old -And think of all the stories that we could have told diff --git a/d01/ex04/unitests/test_log/test8 b/d01/ex04/unitests/test_log/test8 deleted file mode 100644 index 652b428..0000000 --- a/d01/ex04/unitests/test_log/test8 +++ /dev/null @@ -1,4 +0,0 @@ -No more tears, my heart is dry -I don't laugh and I don't cry -I don't think about you all the time -But when I do - I wonder why diff --git a/d01/ex04/unitests/test_log/test8.replace b/d01/ex04/unitests/test_log/test8.replace deleted file mode 100644 index 3a2e850..0000000 --- a/d01/ex04/unitests/test_log/test8.replace +++ /dev/null @@ -1,4 +0,0 @@ -No more tears, my heart is doo -oooooooolaugh and I don't coo -oooooooothink about you all the time -But when I do - I wonder why diff --git a/d01/ex06/karenFilter b/d01/ex06/karenFilter deleted file mode 100755 index a029bab..0000000 Binary files a/d01/ex06/karenFilter and /dev/null differ diff --git a/d02/< b/d02/< deleted file mode 100644 index d27328e..0000000 --- a/d02/< +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include "colors.h" - -#define N_TEST "1" - -int main() { - int i = 0; - - std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " - << "tests :" RESET "\n"; - { - } - - return 0; -} - diff --git a/d04/ex02/Makefile b/d04/ex02/Makefile index 41d7f5a..f10adf4 100644 --- a/d04/ex02/Makefile +++ b/d04/ex02/Makefile @@ -13,7 +13,7 @@ ifeq "$(TYPE)" "c" CC = c EXT = c else ifeq "$(TYPE)" "cpp" - CC = clang++ + CC = c++ EXT = cpp endif diff --git a/d04/ex02/headers/Animal.hpp b/d04/ex02/headers/Animal.hpp index 8378a82..673dd6e 100644 --- a/d04/ex02/headers/Animal.hpp +++ b/d04/ex02/headers/Animal.hpp @@ -17,7 +17,7 @@ public: virtual void makeSound() const = 0; std::string getType() const; - virtual Brain * getBrain() const = 0; +// virtual Brain * getBrain() const = 0; protected: std::string type; diff --git a/d04/ex02/headers/Cat.hpp b/d04/ex02/headers/Cat.hpp index 1c69329..f4246c3 100644 --- a/d04/ex02/headers/Cat.hpp +++ b/d04/ex02/headers/Cat.hpp @@ -19,7 +19,7 @@ public: Cat( Cat const & src ); ~Cat(); Cat & operator=( Cat const & rhs ); - Cat & operator=( Animal const & rhs ); +// Cat & operator=( Animal const & rhs ); void makeSound() const; diff --git a/d04/ex02/headers/Dog.hpp b/d04/ex02/headers/Dog.hpp index 2fdc58d..b777369 100644 --- a/d04/ex02/headers/Dog.hpp +++ b/d04/ex02/headers/Dog.hpp @@ -17,7 +17,7 @@ public: Dog( Dog const & src ); ~Dog(); Dog & operator=( Dog const & rhs ); - Dog & operator=( Animal const & rhs ); +// Dog & operator=( Animal const & rhs ); void makeSound() const; Brain * getBrain() const; diff --git a/d04/ex02/main.cpp b/d04/ex02/main.cpp index e83576d..75d5414 100644 --- a/d04/ex02/main.cpp +++ b/d04/ex02/main.cpp @@ -18,25 +18,25 @@ int main() { std::cout << std::endl; std::cout << B_BLUE "cat i : " RESET; - i->getBrain()->printIdea(0); + dynamic_cast(i)->getBrain()->printIdea(0); std::cout << B_BLUE "cat j : " RESET; - j->getBrain()->printIdea(0); + dynamic_cast(j)->getBrain()->printIdea(0); std::cout << "\n" B_BLUE "*i = *j" RESET "\n"; - *i = *j; + *(dynamic_cast(i)) = *(dynamic_cast(j)); std::cout << B_BLUE "cat i : " RESET; - i->getBrain()->printIdea(0); + dynamic_cast(i)->getBrain()->printIdea(0); std::cout << B_BLUE "cat j : " RESET; - j->getBrain()->printIdea(0); + dynamic_cast(j)->getBrain()->printIdea(0); std::cout << "\n" B_BLUE "j->getBrain->putIdea(\"I am not a cat\")" RESET "\n"; - j->getBrain()->putIdea(0, "I am not a cat");; + dynamic_cast(j)->getBrain()->putIdea(0, "I am not a cat");; std::cout << B_BLUE "cat i : " RESET; - i->getBrain()->printIdea(0); + dynamic_cast(i)->getBrain()->printIdea(0); std::cout << B_BLUE "cat j : " RESET; - j->getBrain()->printIdea(0); + dynamic_cast(j)->getBrain()->printIdea(0); std::cout << std::endl; std::cout << B_BLUE "delete i" RESET "\n"; diff --git a/d04/ex02/pure b/d04/ex02/pure new file mode 100755 index 0000000..964e580 Binary files /dev/null and b/d04/ex02/pure differ diff --git a/d04/ex02/srcs/Cat.cpp b/d04/ex02/srcs/Cat.cpp index 621d138..16d32f6 100644 --- a/d04/ex02/srcs/Cat.cpp +++ b/d04/ex02/srcs/Cat.cpp @@ -67,6 +67,7 @@ Cat & Cat::operator=( Cat const & rhs ) { } // need of a second overload in case "Animal cat" = "Animal cat"; // https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended +/* Cat & Cat::operator=( Animal const & rhs ) { Animal::operator=(rhs); std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n"; @@ -74,6 +75,7 @@ Cat & Cat::operator=( Animal const & rhs ) { *_brain = *(rhs.getBrain()); return *this; } +*/ /********************************************* * PUBLIC MEMBER FUNCTIONS diff --git a/d04/ex02/srcs/Dog.cpp b/d04/ex02/srcs/Dog.cpp index cacfa69..9686c7f 100644 --- a/d04/ex02/srcs/Dog.cpp +++ b/d04/ex02/srcs/Dog.cpp @@ -56,6 +56,7 @@ Dog & Dog::operator=( Dog const & rhs ) { } // need of a second overload in case "Animal cat" = "Animal cat"; // https://stackoverflow.com/questions/68248198/why-my-virtual-assignment-operator-not-doing-as-intended +/* Dog & Dog::operator=( Animal const & rhs ) { Animal::operator=(rhs); std::cout << COPLIEN_COLOR "Cat (Animal) assignator" RESET "\n"; @@ -63,6 +64,7 @@ Dog & Dog::operator=( Animal const & rhs ) { *_brain = *(rhs.getBrain()); return *this; } +*/ /********************************************* * PUBLIC MEMBER FUNCTIONS diff --git a/d05/ex02/main.cpp b/d05/ex02/main.cpp index 002a817..d4d113d 100644 --- a/d05/ex02/main.cpp +++ b/d05/ex02/main.cpp @@ -4,7 +4,7 @@ #include "RobotomyRequestForm.hpp" #include "PresidentialPardonForm.hpp" -#define N_TEST "7" +#define N_TEST "9" int main() { int i = 0; @@ -71,6 +71,26 @@ int main() { std::cout << s2 << "\n"; } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential copy test :" RESET "\n"; + { + PresidentialPardonForm p1("turnips"); + PresidentialPardonForm p2(p1); + + std::cout << p1 << "\n"; + std::cout << p2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy copy test :" RESET "\n"; + { + RobotomyRequestForm r1("artichokes"); + RobotomyRequestForm r2(r1); + + std::cout << r1 << "\n"; + std::cout << r2 << "\n"; + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "Robotomy :" RESET "\n"; { diff --git a/d05/ex02/srcs/PresidentialPardonForm.cpp b/d05/ex02/srcs/PresidentialPardonForm.cpp index b22cffb..dfb9e15 100644 --- a/d05/ex02/srcs/PresidentialPardonForm.cpp +++ b/d05/ex02/srcs/PresidentialPardonForm.cpp @@ -13,7 +13,7 @@ PresidentialPardonForm::PresidentialPardonForm( std::string target ) } PresidentialPardonForm::PresidentialPardonForm( PresidentialPardonForm const & src ) -: AForm("presidential_creation", this->getTarget(), 25, 5) { +: AForm("presidential_creation", src.getTarget(), 25, 5) { std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n"; *this = src; return; diff --git a/d05/ex02/srcs/RobotomyRequestForm.cpp b/d05/ex02/srcs/RobotomyRequestForm.cpp index a361f4a..468f1fd 100644 --- a/d05/ex02/srcs/RobotomyRequestForm.cpp +++ b/d05/ex02/srcs/RobotomyRequestForm.cpp @@ -13,7 +13,7 @@ RobotomyRequestForm::RobotomyRequestForm( std::string target ) } RobotomyRequestForm::RobotomyRequestForm( RobotomyRequestForm const & src ) -: AForm("robotomy_creation", this->getTarget(), 72, 45) { +: AForm("robotomy_creation", src.getTarget(), 72, 45) { std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n"; *this = src; return; diff --git a/d05/ex02/srcs/ShrubberyCreationForm.cpp b/d05/ex02/srcs/ShrubberyCreationForm.cpp index f018052..264f883 100644 --- a/d05/ex02/srcs/ShrubberyCreationForm.cpp +++ b/d05/ex02/srcs/ShrubberyCreationForm.cpp @@ -13,7 +13,7 @@ ShrubberyCreationForm::ShrubberyCreationForm( std::string target ) } ShrubberyCreationForm::ShrubberyCreationForm( ShrubberyCreationForm const & src ) -: AForm("shrubbery_creation", this->getTarget(), 145, 137) { +: AForm("shrubbery_creation", src.getTarget(), 145, 137) { std::cout << COPLIEN_COLOR "ShrubberyCreationForm copy constructor" RESET "\n"; *this = src; return; diff --git a/d05/ex03/Makefile b/d05/ex03/Makefile new file mode 100644 index 0000000..7b56a70 --- /dev/null +++ b/d05/ex03/Makefile @@ -0,0 +1,86 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = interns + +#CC = gcc +CXX = c++ + +#CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +CXXFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 + +#EXT = c +EXT = cpp + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = srcs +SRCS = main.cpp \ + Bureaucrat.cpp \ + AForm.cpp \ + ShrubberyCreationForm.cpp \ + RobotomyRequestForm.cpp \ + PresidentialPardonForm.cpp \ + Intern.cpp + +D_HEADERS = headers +HEADERS = colors.h \ + Bureaucrat.hpp \ + AForm.hpp \ + ShrubberyCreationForm.hpp \ + RobotomyRequestForm.hpp \ + PresidentialPardonForm.hpp \ + Intern.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) + +ifeq "$(D_OBJS)" "." + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) +# $(CC) $(CFLAGS) -c $< -o $@ + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) +# $(CC) $(OBJS) -o $@ $(LIBS) + $(CXX) $(OBJS) -o $@ $(LIBS) + +leaks: $(NAME) + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) + +clean: + $(RM_OBJS) + rm -f *_shrubbery + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d05/ex03/headers/AForm.hpp b/d05/ex03/headers/AForm.hpp new file mode 100644 index 0000000..00646af --- /dev/null +++ b/d05/ex03/headers/AForm.hpp @@ -0,0 +1,54 @@ +#ifndef AFORM_HPP +# define AFORM_HPP + +# include "color.h" +# include +# include + +class Bureaucrat; +# include "Bureaucrat.hpp" + +class AForm { + +public: + + AForm( std::string name, std::string target, int signedGrade, int executeGrade ); + AForm( AForm const & src ); + virtual ~AForm() = 0; + AForm & operator=( AForm const & rhs ); + + std::string getName() const; + std::string getTarget() const; + bool getSigned() const; + int getSignedGrade() const; + int getExecuteGrade() const; + + void beSigned( Bureaucrat const & b ); + void execute(Bureaucrat const & executor) const; + virtual void formAction() const = 0; + +private: + + AForm(); + +protected: + + class GradeTooHighException : public std::exception { + const char * what() const throw();}; + class GradeTooLowException : public std::exception { + const char * what() const throw();}; + class NotSignedException : public std::exception { + const char * what() const throw();}; + + std::string const _name; + std::string const _target; + bool _signed; + int const _signedGrade; + int const _executeGrade; + +}; + +std::ostream & operator<<(std::ostream & o, AForm const & rhs); + +#endif + diff --git a/d05/ex03/headers/Bureaucrat.hpp b/d05/ex03/headers/Bureaucrat.hpp new file mode 100644 index 0000000..8bafb48 --- /dev/null +++ b/d05/ex03/headers/Bureaucrat.hpp @@ -0,0 +1,48 @@ +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include "color.h" +# include +# include +# include + +class AForm; +# include "AForm.hpp" + +class Bureaucrat { + +public: + + Bureaucrat(std::string name, int grade); + Bureaucrat( Bureaucrat const & src ); + ~Bureaucrat(); + Bureaucrat & operator=( Bureaucrat const & rhs ); + + std::string getName() const; + int getGrade() const; + + void gradeUp(); + void gradeDown(); + + void signForm( AForm & f ); + void executeForm( AForm const & form ); + +protected: + + std::string const _name; + int _grade; + +private: + + Bureaucrat(); + + class GradeTooHighException : public std::exception { + const char * what() const throw();}; + class GradeTooLowException : public std::exception { + const char * what() const throw();}; +}; + +std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs); + +#endif + diff --git a/d05/ex03/headers/Intern.hpp b/d05/ex03/headers/Intern.hpp new file mode 100644 index 0000000..44d462b --- /dev/null +++ b/d05/ex03/headers/Intern.hpp @@ -0,0 +1,44 @@ +#ifndef INTERN_HPP +# define INTERN_HPP + +# include "colors.h" +# include +# include +# include // trasnform +# include // tolower + +# include +# include +# include +# include + +typedef AForm * (*t_func)(std::string const & target); + +typedef struct s_formModel { + std::string const name; + t_func const create; +} t_formModel; + +class Intern { + +public: + Intern(); + Intern( Intern const & src ); + ~Intern(); + Intern & operator=( Intern const & rhs ); + + AForm * makeForm(std::string formName, std::string formTarget) const; + +private: + static const t_formModel _chooseForm[]; + + static AForm * makeShrubbery(std::string const &target); + static AForm * makePresidential(std::string const &target); + static AForm * makeRobotomy(std::string const &target); + + bool isValidForm(std::string name, unsigned int i) const; + +}; + +#endif + diff --git a/d05/ex03/headers/PresidentialPardonForm.hpp b/d05/ex03/headers/PresidentialPardonForm.hpp new file mode 100644 index 0000000..027661a --- /dev/null +++ b/d05/ex03/headers/PresidentialPardonForm.hpp @@ -0,0 +1,28 @@ +#ifndef PRESIDENTIALPARDONFORM_HPP +# define PRESIDENTIALPARDONFORM_HPP + +# include "color.h" +# include +# include +# include + +# include "AForm.hpp" + +class PresidentialPardonForm : public AForm { + +public: + + PresidentialPardonForm( std::string target ); + PresidentialPardonForm( PresidentialPardonForm const & src ); + ~PresidentialPardonForm(); + PresidentialPardonForm & operator=( PresidentialPardonForm const & rhs ); + + void formAction() const; + +private: + + PresidentialPardonForm(); +}; + +#endif + diff --git a/d05/ex03/headers/RobotomyRequestForm.hpp b/d05/ex03/headers/RobotomyRequestForm.hpp new file mode 100644 index 0000000..abeb7a9 --- /dev/null +++ b/d05/ex03/headers/RobotomyRequestForm.hpp @@ -0,0 +1,28 @@ +#ifndef ROBOTOMYREQUESTFORM_HPP +# define ROBOTOMYREQUESTFORM_HPP + +# include "color.h" +# include +# include +# include + +# include "AForm.hpp" + +class RobotomyRequestForm : public AForm { + +public: + + RobotomyRequestForm( std::string target ); + RobotomyRequestForm( RobotomyRequestForm const & src ); + ~RobotomyRequestForm(); + RobotomyRequestForm & operator=( RobotomyRequestForm const & rhs ); + + void formAction() const; + +private: + + RobotomyRequestForm(); +}; + +#endif + diff --git a/d05/ex03/headers/ShrubberyCreationForm.hpp b/d05/ex03/headers/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..0906a12 --- /dev/null +++ b/d05/ex03/headers/ShrubberyCreationForm.hpp @@ -0,0 +1,28 @@ +#ifndef SHRUBBERYCREATIONFORM_HPP +# define SHRUBBERYCREATIONFORM_HPP + +# include "color.h" +# include +# include +# include + +# include "AForm.hpp" + +class ShrubberyCreationForm : public AForm { + +public: + + ShrubberyCreationForm( std::string target ); + ShrubberyCreationForm( ShrubberyCreationForm const & src ); + ~ShrubberyCreationForm(); + ShrubberyCreationForm & operator=( ShrubberyCreationForm const & rhs ); + + void formAction() const; + +private: + + ShrubberyCreationForm(); +}; + +#endif + diff --git a/d05/ex03/headers/color.h b/d05/ex03/headers/color.h new file mode 100644 index 0000000..f40596b --- /dev/null +++ b/d05/ex03/headers/color.h @@ -0,0 +1,25 @@ +#ifndef COLOR_H +# define COLOR_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/d07/ex00/colors.h b/d05/ex03/headers/colors.h similarity index 100% rename from d07/ex00/colors.h rename to d05/ex03/headers/colors.h diff --git a/d05/ex03/main.cpp b/d05/ex03/main.cpp new file mode 100644 index 0000000..efea552 --- /dev/null +++ b/d05/ex03/main.cpp @@ -0,0 +1,192 @@ +#include "colors.h" +#include "Bureaucrat.hpp" +#include "AForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "Intern.hpp" + +#define N_TEST "10" + +int main() { + int i = 0; + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery ok :" RESET "\n"; + { + Bureaucrat b("natasha", 1); + ShrubberyCreationForm s("sekoia"); + + std::cout << s << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(s); + b.executeForm(s); + std::cout << s << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery too low sign and execute :" RESET "\n"; + { + Bureaucrat b("jordan", 150); + ShrubberyCreationForm s("chemney"); + + std::cout << s << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(s); + b.executeForm(s); + std::cout << s << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery too low execute only :" RESET "\n"; + { + Bureaucrat b("bernadette", 140); + ShrubberyCreationForm s("rutabaga"); + + std::cout << s << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(s); + b.executeForm(s); + std::cout << s << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Bureaucrat copy test :" RESET "\n"; + { + Bureaucrat b1("pantoufle", 14); + Bureaucrat b2(b1); + + std::cout << b1 << "\n"; + std::cout << b2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery copy test :" RESET "\n"; + { + ShrubberyCreationForm s1("rutabagas"); + ShrubberyCreationForm s2(s1); + + std::cout << s1 << "\n"; + std::cout << s2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential copy test :" RESET "\n"; + { + PresidentialPardonForm p1("turnips"); + PresidentialPardonForm p2(p1); + + std::cout << p1 << "\n"; + std::cout << p2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy copy test :" RESET "\n"; + { + RobotomyRequestForm r1("artichokes"); + RobotomyRequestForm r2(r1); + + std::cout << r1 << "\n"; + std::cout << r2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy :" RESET "\n"; + { + Bureaucrat b("romeo", 15); + RobotomyRequestForm r("oven"); + + std::cout << r << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + std::cout << r << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential :" RESET "\n"; + { + Bureaucrat b("sylvestre", 1); + PresidentialPardonForm p("queen"); + + std::cout << p << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(p); + b.executeForm(p); + std::cout << p << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "intern test :" RESET "\n"; + { + Intern i; + AForm * f1; + AForm * f2; + AForm * f3; + AForm * f4; + AForm * f5; + + std::cout << B_BLUE "\nintern tries to create a Shrubbery form" RESET "\n"; + f1 = i.makeForm("Shrubbery", "smoking"); + if (f1) + std::cout << *f1 << '\n'; + else + std::cout << "intern makes an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a Robotomy form" RESET "\n"; + f2 = i.makeForm("Robotomy", "building"); + if (f2) + std::cout << *f2 << '\n'; + else + std::cout << "intern makes an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a Presidential form" RESET "\n"; + f3 = i.makeForm("Presidential", "building"); + if (f3) + std::cout << *f3 << '\n'; + else + std::cout << "intern makes an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a blueberry form" RESET "\n"; + f4 = i.makeForm("blueberry", "giv_me_gold"); + if (f4) + std::cout << *f4 << '\n'; + else + std::cout << "intern makes an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a \"robotomy request\" form" RESET "\n"; + f5 = i.makeForm("robotomy request", "try_hard"); + if (f5) + std::cout << *f5 << '\n'; + else + std::cout << "intern makes an error and cannot create the form\n"; + + std::cout << '\n'; + delete f1; + delete f2; + delete f3; + delete f4; + delete f5; + } + + std::cout << "\n"; + return 0; +} diff --git a/d05/ex03/srcs/AForm.cpp b/d05/ex03/srcs/AForm.cpp new file mode 100644 index 0000000..0d6fa3b --- /dev/null +++ b/d05/ex03/srcs/AForm.cpp @@ -0,0 +1,103 @@ +#include "AForm.hpp" +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +AForm::AForm( std::string name, std::string target, int signedGrade, int executeGrade ) +: _name(name) +, _target(target) +, _signed(false) +, _signedGrade(signedGrade) +, _executeGrade(executeGrade) { + if (signedGrade > 150 || executeGrade > 150) + throw AForm::GradeTooLowException(); + if (signedGrade < 1 || executeGrade < 1) + throw AForm::GradeTooHighException(); + std::cout << COPLIEN_COLOR "AForm constructor" RESET "\n"; + return; +} + +AForm::AForm( AForm const & src ) +: _name(src.getName()) +, _signedGrade(src.getSignedGrade()) +, _executeGrade(src.getExecuteGrade()) { + std::cout << COPLIEN_COLOR "AForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +AForm::~AForm() { + std::cout << COPLIEN_COLOR "AForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +AForm & AForm::operator=( AForm const & rhs ) { + if ( this != &rhs ) { + _signed = rhs.getSigned(); + } + return *this; +} + +std::ostream & operator<<(std::ostream & o, AForm const & rhs) +{ + o << "[form name]" + << rhs.getName() << ", [target]" + << rhs.getTarget() << ", [signed]" + << rhs.getSigned() << ", [sign grade]" + << rhs.getSignedGrade() << ", [exec grade]" + << rhs.getExecuteGrade(); + return (o); +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string AForm::getName() const {return _name;} +std::string AForm::getTarget() const {return _target;} +bool AForm::getSigned() const {return _signed;} +int AForm::getSignedGrade() const {return _signedGrade;} +int AForm::getExecuteGrade() const {return _executeGrade;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void AForm::beSigned( Bureaucrat const & b) { + if (b.getGrade() < _signedGrade) + _signed = true; + else + throw AForm::GradeTooLowException(); +} + +void AForm::execute(Bureaucrat const & executor) const { + if (!_signed) + throw NotSignedException(); + if (executor.getGrade() > _executeGrade) + throw GradeTooLowException(); + formAction(); +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +const char * AForm::GradeTooHighException::what() const throw() { + return (B_RED "grade too high" RESET); +} +const char * AForm::GradeTooLowException::what() const throw() { + return (B_RED "grade too low" RESET); +} +const char * AForm::NotSignedException::what() const throw() { + return (B_RED "form is not signed" RESET); +} diff --git a/d05/ex03/srcs/Bureaucrat.cpp b/d05/ex03/srcs/Bureaucrat.cpp new file mode 100644 index 0000000..83c2b2c --- /dev/null +++ b/d05/ex03/srcs/Bureaucrat.cpp @@ -0,0 +1,117 @@ +#include "Bureaucrat.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Bureaucrat::Bureaucrat( std::string name, int grade ) +: _name(name) +, _grade(grade) { + if (grade > 150) + throw GradeTooLowException(); + if (grade < 1) + throw GradeTooHighException(); + std::cout << COPLIEN_COLOR "Bureaucrat constructor" RESET "\n"; + return; +} + +Bureaucrat::Bureaucrat( Bureaucrat const & src ) +: _name(src.getName()) { + std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Bureaucrat::~Bureaucrat() { + std::cout << COPLIEN_COLOR "Bureaucrat destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs ) { + if ( this != &rhs ) + _grade = rhs.getGrade(); + return *this; +} + +std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs) +{ + o + << rhs.getName() << ", bureaucrat grade " + << rhs.getGrade(); + return (o); +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string Bureaucrat::getName() const {return _name;} +int Bureaucrat::getGrade() const {return _grade;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Bureaucrat::gradeUp() { + if (_grade > 1) + _grade--; + else + throw GradeTooHighException(); +} + +void Bureaucrat::gradeDown() { + if (_grade < 150) + _grade++; + else + throw GradeTooLowException(); +} + +void Bureaucrat::signForm( AForm & f) { + try { + f.beSigned( *this ); + std::cout << _name << " signed " + << f.getName() << "(" + << f.getTarget() << ")\n"; + } + catch (std::exception & e) { + std::cout << _name << " couldn't sign " << f.getName() + << f.getName() << "(" + << f.getTarget() << ")" + << " because [" << e.what() << "]\n"; + } +} +void Bureaucrat::executeForm( AForm const & f ) { + try { + f.execute( *this ); + std::cout << _name << " executed " + << f.getName() << "(" + << f.getTarget() << ")\n"; + } + catch (std::exception & e) { + std::cout << _name << " couldn't execute " + << f.getName() << "(" + << f.getTarget() << ")" + << " because [" << e.what() << "]\n"; + } +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +const char * Bureaucrat::GradeTooHighException::what() const throw() { + return (B_RED "grade higher than 1" RESET); +} +const char * Bureaucrat::GradeTooLowException::what() const throw() { + return (B_RED "grade lower than 150" RESET); +} diff --git a/d05/ex03/srcs/Intern.cpp b/d05/ex03/srcs/Intern.cpp new file mode 100644 index 0000000..90d0202 --- /dev/null +++ b/d05/ex03/srcs/Intern.cpp @@ -0,0 +1,84 @@ +#include "Intern.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * STATICS + *********************************************/ + +const t_formModel Intern::_chooseForm[] = { + {"shrubbery", Intern::makeShrubbery}, + {"presidential", Intern::makePresidential}, + {"robotomy", Intern::makeRobotomy} +}; + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Intern::Intern() { + std::cout << COPLIEN_COLOR "Intern constructor" RESET "\n"; + return; +} + +Intern::Intern( Intern const & src __attribute__((unused))) { + std::cout << COPLIEN_COLOR "Intern copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Intern::~Intern() { + std::cout << COPLIEN_COLOR "Intern destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Intern & Intern::operator=( Intern const & rhs __attribute__((unused))) { + return *this; +} + +/********************************************* + * PRIVATE MEMBER FUNCTIONS + *********************************************/ + +AForm * Intern::makeForm(std::string formName, std::string formTarget) const { + unsigned int len = sizeof _chooseForm / sizeof _chooseForm[0]; + + for (unsigned int i = 0; i < len; i++) + if (isValidForm(formName, i)) + { + std::cout << "Intern creates " << formName << "\n"; + return _chooseForm[i].create(formTarget); + } + std::cout << "Intern cannot create " << formName << "\n"; + return NULL; +} + +bool Intern::isValidForm(std::string name, unsigned int i) const { + // to lowercase : + // https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + + if (name.find(_chooseForm[i].name) == std::string::npos) + return false; + return true; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +AForm * Intern::makeShrubbery(std::string const &target) { + return new ShrubberyCreationForm(target);} +AForm * Intern::makePresidential(std::string const &target) { + return new PresidentialPardonForm(target);} +AForm * Intern::makeRobotomy(std::string const &target) { + return new RobotomyRequestForm(target);} + diff --git a/d05/ex03/srcs/PresidentialPardonForm.cpp b/d05/ex03/srcs/PresidentialPardonForm.cpp new file mode 100644 index 0000000..9cc2ac6 --- /dev/null +++ b/d05/ex03/srcs/PresidentialPardonForm.cpp @@ -0,0 +1,47 @@ +#include "PresidentialPardonForm.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +PresidentialPardonForm::PresidentialPardonForm( std::string target ) +: AForm("presidential_creation", target, 25, 5){ + std::cout << COPLIEN_COLOR "RobotomyRequestForm constructor" RESET "\n"; + return; +} + +PresidentialPardonForm::PresidentialPardonForm( PresidentialPardonForm const & src ) +: AForm("presidential_creation", src.getTarget(), 25, 5) { + std::cout << COPLIEN_COLOR "PresidentialPardonForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +PresidentialPardonForm::~PresidentialPardonForm() { + std::cout << COPLIEN_COLOR "RobotomyRequestForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +PresidentialPardonForm & PresidentialPardonForm::operator=( PresidentialPardonForm const & rhs ) { + AForm::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void PresidentialPardonForm::formAction() const { + std::cout << "Zaphod Beeblebrox pardon " << _target << "\n"; +} + diff --git a/d05/ex03/srcs/RobotomyRequestForm.cpp b/d05/ex03/srcs/RobotomyRequestForm.cpp new file mode 100644 index 0000000..468f1fd --- /dev/null +++ b/d05/ex03/srcs/RobotomyRequestForm.cpp @@ -0,0 +1,51 @@ +#include "RobotomyRequestForm.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +RobotomyRequestForm::RobotomyRequestForm( std::string target ) +: AForm("robotomy_creation", target, 72, 45){ + std::cout << COPLIEN_COLOR "RobotomyRequestForm constructor" RESET "\n"; + return; +} + +RobotomyRequestForm::RobotomyRequestForm( RobotomyRequestForm const & src ) +: AForm("robotomy_creation", src.getTarget(), 72, 45) { + std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +RobotomyRequestForm::~RobotomyRequestForm() { + std::cout << COPLIEN_COLOR "RobotomyRequestForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +RobotomyRequestForm & RobotomyRequestForm::operator=( RobotomyRequestForm const & rhs ) { + AForm::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void RobotomyRequestForm::formAction() const { + std::cout << "*drill sounds*\n"; + if (std::rand() % 2) + std::cout << _target << " robotomized with success\n"; + else + std::cout << _target << " robotomization failed\n"; +} + diff --git a/d05/ex03/srcs/ShrubberyCreationForm.cpp b/d05/ex03/srcs/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..264f883 --- /dev/null +++ b/d05/ex03/srcs/ShrubberyCreationForm.cpp @@ -0,0 +1,74 @@ +#include "ShrubberyCreationForm.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +ShrubberyCreationForm::ShrubberyCreationForm( std::string target ) +: AForm("shrubbery_creation", target, 145, 137){ + std::cout << COPLIEN_COLOR "ShrubberyCreationForm constructor" RESET "\n"; + return; +} + +ShrubberyCreationForm::ShrubberyCreationForm( ShrubberyCreationForm const & src ) +: AForm("shrubbery_creation", src.getTarget(), 145, 137) { + std::cout << COPLIEN_COLOR "ShrubberyCreationForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +ShrubberyCreationForm::~ShrubberyCreationForm() { + std::cout << COPLIEN_COLOR "ShrubberyCreationForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +ShrubberyCreationForm & ShrubberyCreationForm::operator=( ShrubberyCreationForm const & rhs ) { + AForm::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void ShrubberyCreationForm::formAction() const { + std::string name = _target + "_shrubbery"; + std::ofstream ofs(name.c_str(), std::ofstream::out); + + if (!ofs) + { + std::cout << "opening Shrubbery.txt file failed\n"; + return ; + } + + ofs << " * *\n"; + ofs << " * * *\n"; + ofs << " * * * * *\n"; + ofs << " * * * * *\n"; + ofs << " * * * * * * *\n"; + ofs << " * * * * * .# * *\n"; + ofs << " * * * #. .# * *\n"; + ofs << " * \"#. #: #\" * * *\n"; + ofs << " * * * \"#. ##\" *\n"; + ofs << " * \"###\n"; + ofs << " \"##\n"; + ofs << " ##.\n"; + ofs << " .##:\n"; + ofs << " :###\n"; + ofs << " ;###\n"; + ofs << " ,####.\n"; + ofs << " /\\/\\/\\/\\/\\/.######.\\/\\/\\/\\/\\\n"; + + ofs.close(); +} + diff --git a/d06/ex00/convert b/d06/ex00/convert new file mode 100755 index 0000000..42fcb25 Binary files /dev/null and b/d06/ex00/convert differ diff --git a/d06/ex00/convert.h b/d06/ex00/convert.h index 746cc80..9df1acd 100644 --- a/d06/ex00/convert.h +++ b/d06/ex00/convert.h @@ -28,7 +28,6 @@ template void toDouble(T value); #define MAX_INT "2147483647" #define MIN_INT "-2147483648" #define MAX_INT_1 "2147483648" -#define MIN_INT_1 "-2147483649" #define INT_MAX_LENGTH 10 #define MAX_FLOAT_INT_PRECISION "16777216" #define MAX_FLOAT "340282346638528859811704183484516925440" @@ -38,6 +37,27 @@ template void toDouble(T value); #define DOUBLE_MAX_LENGTH 309 // for tests +#define MAX_I__5 "2147483642" +#define MAX_I__4 "2147483643" +#define MAX_I__3 "2147483644" +#define MAX_I__2 "2147483645" +#define MAX_I__1 "2147483646" +#define MAX_I_2 "2147483649" +#define MAX_I_3 "2147483650" +#define MAX_I_4 "2147483651" +#define MAX_I_5 "2147483652" + +#define MIN_I__5 "-2147483643" +#define MIN_I__4 "-2147483644" +#define MIN_I__3 "-2147483645" +#define MIN_I__2 "-2147483646" +#define MIN_I__1 "-2147483647" +#define MIN_I_1 "-2147483649" +#define MIN_I_2 "-2147483650" +#define MIN_I_3 "-2147483651" +#define MIN_I_4 "-2147483652" +#define MIN_I_5 "-2147483653" + #define MAX_FLOAT_INT_PREC__1 "16777215" #define MAX_FLOAT_INT_PREC__2 "16777214" #define MAX_FLOAT_INT_PREC__3 "16777213" diff --git a/d06/ex00/main.cpp b/d06/ex00/main.cpp index 41b6c1e..a794fcb 100644 --- a/d06/ex00/main.cpp +++ b/d06/ex00/main.cpp @@ -15,6 +15,8 @@ int main(int ac, char **av) { return 0; } + std::cout << "\n\n" B_GREEN "----------------------------------------------------\n" + << "\nCHAR" RESET "\n"; // char convert("!"); convert("\""); @@ -100,6 +102,9 @@ int main(int ac, char **av) { convert("|"); convert("}"); convert("~"); + + std::cout << "\n\n" B_GREEN "----------------------------------------------------\n" + << "\nINT" RESET "\n"; // int convert("0"); convert("-42"); @@ -132,10 +137,33 @@ int main(int ac, char **av) { convert("256"); convert("257"); convert("258"); - convert(MAX_INT); + + std::cout << "\n\n" B_PURPLE "[min - max int]" RESET "\n"; + convert(MIN_I_5); + convert(MIN_I_4); + convert(MIN_I_3); + convert(MIN_I_2); + convert(MIN_I_1); convert(MIN_INT); + convert(MIN_I__1); + convert(MIN_I__2); + convert(MIN_I__3); + convert(MIN_I__4); + convert(MIN_I__5); + + convert(MAX_I__5); + convert(MAX_I__4); + convert(MAX_I__3); + convert(MAX_I__2); + convert(MAX_I__1); + convert(MAX_INT); convert(MAX_INT_1); - convert(MIN_INT_1); + convert(MAX_I_2); + convert(MAX_I_3); + convert(MAX_I_4); + convert(MAX_I_5); + std::cout << "\n" B_PURPLE "[END min - max int]" RESET "\n\n"; + convert(MAX_FLOAT_INT_PRECISION); convert(MAX_FLOAT_INT_PREC__1); convert(MAX_FLOAT_INT_PREC__2); @@ -147,6 +175,9 @@ int main(int ac, char **av) { convert(MAX_FLOAT_INT_PREC_4); convert(MAX_FLOAT_INT_PREC_5); convert(MAX_FLOAT_INT_PREC_6); + + std::cout << "\n\n" B_GREEN "----------------------------------------------------\n" + << "\nFLOAT" RESET "\n"; // float convert("0.0f"); convert("-4.2f"); @@ -154,10 +185,33 @@ int main(int ac, char **av) { convert("-inff"); convert("+inff"); convert("nanf"); - convert(MAX_INT".0f"); - convert(MIN_INT".0f"); - convert(MAX_INT_1".0f"); - convert(MIN_INT_1".0f"); + + std::cout << "\n\n" B_PURPLE "[min - max int]" RESET "\n"; + convert(MIN_I_5 ".0f"); + convert(MIN_I_4 ".0f"); + convert(MIN_I_3 ".0f"); + convert(MIN_I_2 ".0f"); + convert(MIN_I_1 ".0f"); + convert(MIN_INT ".0f"); + convert(MIN_I__1 ".0f"); + convert(MIN_I__2 ".0f"); + convert(MIN_I__3 ".0f"); + convert(MIN_I__4 ".0f"); + convert(MIN_I__5 ".0f"); + + convert(MAX_I__5 ".0f"); + convert(MAX_I__4 ".0f"); + convert(MAX_I__3 ".0f"); + convert(MAX_I__2 ".0f"); + convert(MAX_I__1 ".0f"); + convert(MAX_INT ".0f"); + convert(MAX_INT_1 ".0f"); + convert(MAX_I_2 ".0f"); + convert(MAX_I_3 ".0f"); + convert(MAX_I_4 ".0f"); + convert(MAX_I_5 ".0f"); + std::cout << "\n" B_PURPLE "[END min - max int]" RESET "\n\n"; + convert(MAX_FLOAT_INT_PRECISION".0f"); convert(MAX_FLOAT_INT_PREC__1".0f"); convert(MAX_FLOAT_INT_PREC__2".0f"); @@ -181,6 +235,9 @@ int main(int ac, char **av) { convert(MAX_F_5".0f"); convert(MAX_F_6".0f"); convert(MAX_F_N".0f"); + + std::cout << "\n\n" B_GREEN "----------------------------------------------------\n" + << "\nDOUBLE" RESET "\n"; //double convert("0.0"); convert("-4.2"); @@ -188,10 +245,33 @@ int main(int ac, char **av) { convert("-inf"); convert("+inf"); convert("nan"); - convert(MAX_INT".0"); - convert(MIN_INT".0"); - convert(MAX_INT_1".0"); - convert(MIN_INT_1".0"); + + std::cout << "\n\n" B_PURPLE "[min - max int]" RESET "\n"; + convert(MIN_I_5 ".0"); + convert(MIN_I_4 ".0"); + convert(MIN_I_3 ".0"); + convert(MIN_I_2 ".0"); + convert(MIN_I_1 ".0"); + convert(MIN_INT ".0"); + convert(MIN_I__1 ".0"); + convert(MIN_I__2 ".0"); + convert(MIN_I__3 ".0"); + convert(MIN_I__4 ".0"); + convert(MIN_I__5 ".0"); + + convert(MAX_I__5 ".0"); + convert(MAX_I__4 ".0"); + convert(MAX_I__3 ".0"); + convert(MAX_I__2 ".0"); + convert(MAX_I__1 ".0"); + convert(MAX_INT ".0"); + convert(MAX_INT_1 ".0"); + convert(MAX_I_2 ".0"); + convert(MAX_I_3 ".0"); + convert(MAX_I_4 ".0"); + convert(MAX_I_5 ".0"); + std::cout << "\n" B_PURPLE "[END min - max int]" RESET "\n\n"; + convert(MAX_FLOAT_INT_PRECISION".0"); convert(MAX_FLOAT_INT_PREC__1".0"); convert(MAX_FLOAT_INT_PREC__2".0"); diff --git a/d06/ex00/srcs/checkChar.cpp b/d06/ex00/srcs/checkChar.cpp index eba45de..0f5948c 100644 --- a/d06/ex00/srcs/checkChar.cpp +++ b/d06/ex00/srcs/checkChar.cpp @@ -22,10 +22,10 @@ bool isChar(std::string str) { bool checkChar(std::string str) { char c; - if (str.length() != 1 || isdigit(str[0])) + if (!isChar(str)) return false; - c = str[0]; + c = str[0]; std::cout << B_CYAN << c << B_YELLOW " char" RESET "\n"; fromChar(c); diff --git a/d06/ex00/srcs/convert.cpp b/d06/ex00/srcs/convert.cpp index ba1c1d7..16abcb4 100644 --- a/d06/ex00/srcs/convert.cpp +++ b/d06/ex00/srcs/convert.cpp @@ -2,6 +2,12 @@ #include #include "convert.h" +// f[] : is an array +// *f[] : is an array of pointers +// (*f[])() : is an array of pointers to functions with no parameters +// (*f[])(str) : is an array of pointers to functions with parameter str +// see : +// https://stackoverflow.com/questions/31643245/declaring-an-array-of-functions-of-type-void-c bool (*checkFunc[])(std::string str) = { checkChar, diff --git a/d06/ex00/srcs/toTypes.cpp b/d06/ex00/srcs/toTypes.cpp index 180e9d5..6d6dd1c 100644 --- a/d06/ex00/srcs/toTypes.cpp +++ b/d06/ex00/srcs/toTypes.cpp @@ -22,8 +22,6 @@ void toChar(T value) { template void toInt(T value) { std::cout << std::setw(7) << std::left << "int"; -// if (value < std::numeric_limits::min() -// || value > std::numeric_limits::max() ) if (! (value <= std::numeric_limits::max() && value >= std::numeric_limits::min()) ) { diff --git a/d06/ex01/main.cpp b/d06/ex01/main.cpp index c511bd8..80786de 100644 --- a/d06/ex01/main.cpp +++ b/d06/ex01/main.cpp @@ -37,7 +37,10 @@ int main() { raw = serialize(data_ptr); std::cout << " " << raw << "\n"; - + +// unsigned long int *li = (unsigned long int *)&data_ptr; +// std::cout << " " << *li << "\n"; + data_ptr = deserialize(raw); std::cout << " " << data_ptr << "\n"; diff --git a/d06/ex01/serialize b/d06/ex01/serialize new file mode 100755 index 0000000..05589af Binary files /dev/null and b/d06/ex01/serialize differ diff --git a/d07/ex00/Makefile b/d07/ex00/Makefile index e474bc5..58ef36a 100644 --- a/d07/ex00/Makefile +++ b/d07/ex00/Makefile @@ -28,12 +28,14 @@ LIBS = INCLUDES = -I$(D_HEADERS) -D_SRCS = . -SRCS = main.cpp +D_SRCS = srcs +SRCS = main.cpp \ + Fixed.cpp -D_HEADERS = . +D_HEADERS = headers HEADERS = colors.h \ - Templates.hpp + Templates.hpp \ + Fixed.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d07/ex00/headers/Fixed.hpp b/d07/ex00/headers/Fixed.hpp new file mode 100644 index 0000000..b63a80c --- /dev/null +++ b/d07/ex00/headers/Fixed.hpp @@ -0,0 +1,55 @@ +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include +#include + +class Fixed { + +public: + + Fixed(void); // default/parametric constructor + Fixed(Fixed const & src); // copy constructor + ~Fixed(void); // destructor + Fixed(int integer); + Fixed(float const floater); + + Fixed & operator= (Fixed const & rhs); // assignement operator + bool operator< (Fixed const & rhs) const; + bool operator> (Fixed const & rhs) const; + bool operator<=(Fixed const & rhs) const; + bool operator>=(Fixed const & rhs) const; + bool operator==(Fixed const & rhs) const; + bool operator!=(Fixed const & rhs) const; + Fixed operator+ (Fixed const & rhs) const; + Fixed operator- (Fixed const & rhs) const; + Fixed operator* (Fixed const & rhs) const; + Fixed operator/ (Fixed const & rhs) const; + Fixed & operator++(void); // prefix ++o + Fixed & operator--(void); // prefix --o + Fixed operator++(int); // postfix o++ + Fixed operator--(int); // postfix o-- + + static const Fixed & min(Fixed const & lhs, Fixed const & rhs); + static const Fixed & max(Fixed const & lhs, Fixed const & rhs); + static Fixed & min(Fixed & lhs, Fixed & rhs); + static Fixed & max(Fixed & lhs, Fixed & rhs); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + +private: + + int _value; + static int const _frac; + static int const _max; + static int const _min; + +}; + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs); + +#endif diff --git a/d07/ex00/Templates.hpp b/d07/ex00/headers/Templates.hpp similarity index 100% rename from d07/ex00/Templates.hpp rename to d07/ex00/headers/Templates.hpp diff --git a/d07/ex00/headers/colors.h b/d07/ex00/headers/colors.h new file mode 100644 index 0000000..0374e42 --- /dev/null +++ b/d07/ex00/headers/colors.h @@ -0,0 +1,25 @@ +#ifndef COLORS_H +# define COLORS_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/d07/ex00/main.cpp b/d07/ex00/main.cpp index 5e9683f..9401fd9 100644 --- a/d07/ex00/main.cpp +++ b/d07/ex00/main.cpp @@ -2,8 +2,27 @@ #include #include "colors.h" #include "Templates.hpp" +#include "Fixed.hpp" -#define N_TEST "3" +#define N_TEST "5" + +class Test { +public: + Test(int n = 0) : _n(n) {} + int getN() const {return _n;} + bool operator==(Test const & rhs) const {return (this->_n == rhs._n);} + bool operator!=(Test const & rhs) const {return (this->_n != rhs._n);} + bool operator> (Test const & rhs) const {return (this->_n > rhs._n);} + bool operator< (Test const & rhs) const {return (rhs._n > this->_n);} + bool operator>=(Test const & rhs) const {return ( !(this->_n < rhs._n) );} + bool operator<=(Test const & rhs) const {return ( !(this->_n > rhs._n) );} +private: + int _n; +}; +std::ostream & operator<<(std::ostream & o, Test const & rhs){ + o << rhs.getN(); + return (o); +} int main() { int i = 0; @@ -13,12 +32,13 @@ int main() { { int a = 2; int b = 3; - - std::cout << "a = " << a << ", b = " << b << "\nswap :\n"; + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; ::swap( a, b ); std::cout << "a = " << a << ", b = " << b << "\n"; - std::cout << "min( a, b ) = " << ::min( a, b ) << "\n"; - std::cout << "max( a, b ) = " << ::max( a, b ) << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " @@ -26,12 +46,13 @@ int main() { { std::string a = "chaine1"; std::string b = "chaine2"; - - std::cout << "a = " << a << ", b = " << b << "\nswap :\n"; + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; ::swap(a, b); std::cout << "a = " << a << ", b = " << b << "\n"; - std::cout << "min( a, b ) = " << ::min( a, b ) << "\n"; - std::cout << "max( a, b ) = " << ::max( a, b ) << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " @@ -39,12 +60,42 @@ int main() { { float a = 2.42f; float b = 32.7f; - - std::cout << "a = " << a << ", b = " << b << "\nswap :\n"; + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; ::swap( a, b ); std::cout << "a = " << a << ", b = " << b << "\n"; - std::cout << "min( a, b ) = " << ::min( a, b ) << "\n"; - std::cout << "max( a, b ) = " << ::max( a, b ) << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests fixed :" RESET "\n"; + { + Fixed a(2.42f); + Fixed b(32.7f); + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; + ::swap( a, b ); + std::cout << "a = " << a << ", b = " << b << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests class :" RESET "\n"; + { + + Test a(8); + Test b(16); + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; + ::swap( a, b ); + std::cout << "a = " << a << ", b = " << b << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; } return 0; diff --git a/d07/ex00/srcs/Fixed.cpp b/d07/ex00/srcs/Fixed.cpp new file mode 100644 index 0000000..3e72ea0 --- /dev/null +++ b/d07/ex00/srcs/Fixed.cpp @@ -0,0 +1,225 @@ +#include "Fixed.hpp" + +/* + * functions to print numbers in binary + * for the float, found help from stackoverflow : + * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec + */ + +std::string printBitsInt(int num) +{ + int i = 0; + + for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) + { + std::cout << ((num & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + +std::string printBitsFloat(float num) +{ + int *p = (int *)# + int i = 0; + + for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1) + { + std::cout << ((*p & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + + +/* + * statics variables initialisation + * + * for MAX integer : + * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1)) + * <= ... >= + * 11111111 10000000 00000000 00000000 (-8388608) + * + */ + +int const Fixed::_frac = 8; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; + + +/* + * default constructor / copy constructor / destructor + */ + +Fixed::Fixed() : _value(0) { + return; +} + +Fixed::Fixed(Fixed const & src) { + *this = src; + return; +} + +Fixed::~Fixed( void ) { + return; +} + + +/* + * int and float constructors + */ + +Fixed::Fixed(int integer) { + if (integer < Fixed::_min || integer > Fixed::_max) + std::cout << "error: integer out of range" << '\n'; + else + this->_value = integer << Fixed::_frac; +} + +Fixed::Fixed(float const floater) { + if (floater < Fixed::_min || floater > Fixed::_max) + std::cout << "error: float out of range" << '\n'; + else + this->_value = roundf(floater * (1 << Fixed::_frac)); +} + + +/* + * assignement operator + */ + +Fixed & Fixed::operator=( Fixed const & rhs ) { + if ( this != &rhs ) + this->_value = rhs.getRawBits(); + return *this; +} + + +/* + * operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; -- + * ref : https://en.cppreference.com/w/cpp/language/operators + * for division, if you want to avoid floats (legitimate) : + * https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c + */ + +bool Fixed::operator< (Fixed const & rhs) const { + return this->_value < rhs._value; +} +bool Fixed::operator> (Fixed const & rhs) const { + return rhs < *this; +} +bool Fixed::operator<=(Fixed const & rhs) const { + return !(*this > rhs); +} +bool Fixed::operator>=(Fixed const & rhs) const { + return !(*this < rhs); +} +bool Fixed::operator==(Fixed const & rhs) const { + return this->_value == rhs._value; +} +bool Fixed::operator!=(Fixed const & rhs) const { + return !(*this == rhs); +} +Fixed Fixed::operator+ ( Fixed const & rhs ) const { + Fixed result(*this); + result._value += rhs._value; + return (result); +} +Fixed Fixed::operator- ( Fixed const & rhs ) const { + Fixed result(*this); + result._value -= rhs._value; + return (result); +} +Fixed Fixed::operator* ( Fixed const & rhs ) const { + Fixed result(*this); + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; + return result; +} +Fixed Fixed::operator/ ( Fixed const & rhs ) const { + Fixed result(*this); + if (rhs._value == 0) + std::cout << "!impossible division by 0¡"; + else + result._value = (long)(result._value << Fixed::_frac) / rhs._value; + return result; +} +Fixed & Fixed::operator++() { + this->_value++; + return *this; +} +Fixed & Fixed::operator--() { + this->_value--; + return *this; +} +Fixed Fixed::operator++( int ) { + Fixed old = *this; + Fixed::operator++(); + return old; +} +Fixed Fixed::operator--( int ) { + Fixed old = *this; + Fixed::operator--(); + return old; +} + + +/* + * returns min and max + */ + +Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} +Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} + + +/* + * functions that returns _value + */ + +int Fixed::getRawBits( void ) const { + return this->_value; +} + +void Fixed::setRawBits( int const raw ) { + this->_value = raw; +} + +int Fixed::toInt( void ) const { + return (this->_value >> Fixed::_frac); +} +float Fixed::toFloat( void ) const { + return ((float)this->_value / (float)(1 << Fixed::_frac)); +} + + +/* + * overload "<<" -> output fixed point in float representation + * found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp + */ + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs) +{ + o << rhs.toFloat(); + return (o); +} diff --git a/d07/ex01/Fixed.cpp b/d07/ex01/Fixed.cpp new file mode 100644 index 0000000..3e72ea0 --- /dev/null +++ b/d07/ex01/Fixed.cpp @@ -0,0 +1,225 @@ +#include "Fixed.hpp" + +/* + * functions to print numbers in binary + * for the float, found help from stackoverflow : + * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec + */ + +std::string printBitsInt(int num) +{ + int i = 0; + + for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) + { + std::cout << ((num & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + +std::string printBitsFloat(float num) +{ + int *p = (int *)# + int i = 0; + + for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1) + { + std::cout << ((*p & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + + +/* + * statics variables initialisation + * + * for MAX integer : + * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1)) + * <= ... >= + * 11111111 10000000 00000000 00000000 (-8388608) + * + */ + +int const Fixed::_frac = 8; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; + + +/* + * default constructor / copy constructor / destructor + */ + +Fixed::Fixed() : _value(0) { + return; +} + +Fixed::Fixed(Fixed const & src) { + *this = src; + return; +} + +Fixed::~Fixed( void ) { + return; +} + + +/* + * int and float constructors + */ + +Fixed::Fixed(int integer) { + if (integer < Fixed::_min || integer > Fixed::_max) + std::cout << "error: integer out of range" << '\n'; + else + this->_value = integer << Fixed::_frac; +} + +Fixed::Fixed(float const floater) { + if (floater < Fixed::_min || floater > Fixed::_max) + std::cout << "error: float out of range" << '\n'; + else + this->_value = roundf(floater * (1 << Fixed::_frac)); +} + + +/* + * assignement operator + */ + +Fixed & Fixed::operator=( Fixed const & rhs ) { + if ( this != &rhs ) + this->_value = rhs.getRawBits(); + return *this; +} + + +/* + * operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; -- + * ref : https://en.cppreference.com/w/cpp/language/operators + * for division, if you want to avoid floats (legitimate) : + * https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c + */ + +bool Fixed::operator< (Fixed const & rhs) const { + return this->_value < rhs._value; +} +bool Fixed::operator> (Fixed const & rhs) const { + return rhs < *this; +} +bool Fixed::operator<=(Fixed const & rhs) const { + return !(*this > rhs); +} +bool Fixed::operator>=(Fixed const & rhs) const { + return !(*this < rhs); +} +bool Fixed::operator==(Fixed const & rhs) const { + return this->_value == rhs._value; +} +bool Fixed::operator!=(Fixed const & rhs) const { + return !(*this == rhs); +} +Fixed Fixed::operator+ ( Fixed const & rhs ) const { + Fixed result(*this); + result._value += rhs._value; + return (result); +} +Fixed Fixed::operator- ( Fixed const & rhs ) const { + Fixed result(*this); + result._value -= rhs._value; + return (result); +} +Fixed Fixed::operator* ( Fixed const & rhs ) const { + Fixed result(*this); + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; + return result; +} +Fixed Fixed::operator/ ( Fixed const & rhs ) const { + Fixed result(*this); + if (rhs._value == 0) + std::cout << "!impossible division by 0¡"; + else + result._value = (long)(result._value << Fixed::_frac) / rhs._value; + return result; +} +Fixed & Fixed::operator++() { + this->_value++; + return *this; +} +Fixed & Fixed::operator--() { + this->_value--; + return *this; +} +Fixed Fixed::operator++( int ) { + Fixed old = *this; + Fixed::operator++(); + return old; +} +Fixed Fixed::operator--( int ) { + Fixed old = *this; + Fixed::operator--(); + return old; +} + + +/* + * returns min and max + */ + +Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} +Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} + + +/* + * functions that returns _value + */ + +int Fixed::getRawBits( void ) const { + return this->_value; +} + +void Fixed::setRawBits( int const raw ) { + this->_value = raw; +} + +int Fixed::toInt( void ) const { + return (this->_value >> Fixed::_frac); +} +float Fixed::toFloat( void ) const { + return ((float)this->_value / (float)(1 << Fixed::_frac)); +} + + +/* + * overload "<<" -> output fixed point in float representation + * found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp + */ + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs) +{ + o << rhs.toFloat(); + return (o); +} diff --git a/d07/ex01/Makefile b/d07/ex01/Makefile index 50bc55b..efc01c9 100644 --- a/d07/ex01/Makefile +++ b/d07/ex01/Makefile @@ -29,14 +29,17 @@ LIBS = INCLUDES = -I$(D_HEADERS) D_SRCS = . -SRCS = main.cpp +SRCS = main.cpp \ + Fixed.cpp D_HEADERS = headers HEADERS = colors.h \ Iter.hpp \ Print.hpp \ ToUpper.hpp \ - Decrement.hpp + Decrement.hpp \ + ClassTest.hpp \ + Fixed.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d07/ex01/headers/ClassTest.hpp b/d07/ex01/headers/ClassTest.hpp new file mode 100644 index 0000000..4e3e1fd --- /dev/null +++ b/d07/ex01/headers/ClassTest.hpp @@ -0,0 +1,20 @@ +#ifndef CLASSTEST_HPP +# define CLASSTEST_HPP + +# include + +class ClassTest { +public: + ClassTest() : _value("hello") {} + std::string getValue() const {return _value;} +private: + std::string _value; +}; + +std::ostream & operator<<(std::ostream & o, ClassTest const & rhs) { + o << rhs.getValue(); + return o; +} + +#endif + diff --git a/d07/ex01/headers/Fixed.hpp b/d07/ex01/headers/Fixed.hpp new file mode 100644 index 0000000..b63a80c --- /dev/null +++ b/d07/ex01/headers/Fixed.hpp @@ -0,0 +1,55 @@ +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include +#include + +class Fixed { + +public: + + Fixed(void); // default/parametric constructor + Fixed(Fixed const & src); // copy constructor + ~Fixed(void); // destructor + Fixed(int integer); + Fixed(float const floater); + + Fixed & operator= (Fixed const & rhs); // assignement operator + bool operator< (Fixed const & rhs) const; + bool operator> (Fixed const & rhs) const; + bool operator<=(Fixed const & rhs) const; + bool operator>=(Fixed const & rhs) const; + bool operator==(Fixed const & rhs) const; + bool operator!=(Fixed const & rhs) const; + Fixed operator+ (Fixed const & rhs) const; + Fixed operator- (Fixed const & rhs) const; + Fixed operator* (Fixed const & rhs) const; + Fixed operator/ (Fixed const & rhs) const; + Fixed & operator++(void); // prefix ++o + Fixed & operator--(void); // prefix --o + Fixed operator++(int); // postfix o++ + Fixed operator--(int); // postfix o-- + + static const Fixed & min(Fixed const & lhs, Fixed const & rhs); + static const Fixed & max(Fixed const & lhs, Fixed const & rhs); + static Fixed & min(Fixed & lhs, Fixed & rhs); + static Fixed & max(Fixed & lhs, Fixed & rhs); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + +private: + + int _value; + static int const _frac; + static int const _max; + static int const _min; + +}; + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs); + +#endif diff --git a/d07/ex01/iter b/d07/ex01/iter new file mode 100755 index 0000000..3116a3a Binary files /dev/null and b/d07/ex01/iter differ diff --git a/d07/ex01/main.cpp b/d07/ex01/main.cpp index 945b374..47c98ab 100644 --- a/d07/ex01/main.cpp +++ b/d07/ex01/main.cpp @@ -5,8 +5,10 @@ #include "Print.hpp" #include "ToUpper.hpp" #include "Decrement.hpp" +#include "ClassTest.hpp" +#include "Fixed.hpp" -#define N_TEST "2" +#define N_TEST "4" int main() { int i = 0; @@ -38,6 +40,22 @@ int main() { ::Iter(arr, len, Print); } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests class :" RESET "\n"; + { + ClassTest tab[5]; + + ::Iter(tab, 5, Print); + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests fixed :" RESET "\n"; + { + Fixed f[5]; + + ::Iter(f, 5, Print); + } + return 0; } diff --git a/d07/ex02/Fixed.cpp b/d07/ex02/Fixed.cpp new file mode 100644 index 0000000..3e72ea0 --- /dev/null +++ b/d07/ex02/Fixed.cpp @@ -0,0 +1,225 @@ +#include "Fixed.hpp" + +/* + * functions to print numbers in binary + * for the float, found help from stackoverflow : + * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec + */ + +std::string printBitsInt(int num) +{ + int i = 0; + + for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) + { + std::cout << ((num & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + +std::string printBitsFloat(float num) +{ + int *p = (int *)# + int i = 0; + + for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1) + { + std::cout << ((*p & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + + +/* + * statics variables initialisation + * + * for MAX integer : + * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1)) + * <= ... >= + * 11111111 10000000 00000000 00000000 (-8388608) + * + */ + +int const Fixed::_frac = 8; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; + + +/* + * default constructor / copy constructor / destructor + */ + +Fixed::Fixed() : _value(0) { + return; +} + +Fixed::Fixed(Fixed const & src) { + *this = src; + return; +} + +Fixed::~Fixed( void ) { + return; +} + + +/* + * int and float constructors + */ + +Fixed::Fixed(int integer) { + if (integer < Fixed::_min || integer > Fixed::_max) + std::cout << "error: integer out of range" << '\n'; + else + this->_value = integer << Fixed::_frac; +} + +Fixed::Fixed(float const floater) { + if (floater < Fixed::_min || floater > Fixed::_max) + std::cout << "error: float out of range" << '\n'; + else + this->_value = roundf(floater * (1 << Fixed::_frac)); +} + + +/* + * assignement operator + */ + +Fixed & Fixed::operator=( Fixed const & rhs ) { + if ( this != &rhs ) + this->_value = rhs.getRawBits(); + return *this; +} + + +/* + * operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; -- + * ref : https://en.cppreference.com/w/cpp/language/operators + * for division, if you want to avoid floats (legitimate) : + * https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c + */ + +bool Fixed::operator< (Fixed const & rhs) const { + return this->_value < rhs._value; +} +bool Fixed::operator> (Fixed const & rhs) const { + return rhs < *this; +} +bool Fixed::operator<=(Fixed const & rhs) const { + return !(*this > rhs); +} +bool Fixed::operator>=(Fixed const & rhs) const { + return !(*this < rhs); +} +bool Fixed::operator==(Fixed const & rhs) const { + return this->_value == rhs._value; +} +bool Fixed::operator!=(Fixed const & rhs) const { + return !(*this == rhs); +} +Fixed Fixed::operator+ ( Fixed const & rhs ) const { + Fixed result(*this); + result._value += rhs._value; + return (result); +} +Fixed Fixed::operator- ( Fixed const & rhs ) const { + Fixed result(*this); + result._value -= rhs._value; + return (result); +} +Fixed Fixed::operator* ( Fixed const & rhs ) const { + Fixed result(*this); + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; + return result; +} +Fixed Fixed::operator/ ( Fixed const & rhs ) const { + Fixed result(*this); + if (rhs._value == 0) + std::cout << "!impossible division by 0¡"; + else + result._value = (long)(result._value << Fixed::_frac) / rhs._value; + return result; +} +Fixed & Fixed::operator++() { + this->_value++; + return *this; +} +Fixed & Fixed::operator--() { + this->_value--; + return *this; +} +Fixed Fixed::operator++( int ) { + Fixed old = *this; + Fixed::operator++(); + return old; +} +Fixed Fixed::operator--( int ) { + Fixed old = *this; + Fixed::operator--(); + return old; +} + + +/* + * returns min and max + */ + +Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} +Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} + + +/* + * functions that returns _value + */ + +int Fixed::getRawBits( void ) const { + return this->_value; +} + +void Fixed::setRawBits( int const raw ) { + this->_value = raw; +} + +int Fixed::toInt( void ) const { + return (this->_value >> Fixed::_frac); +} +float Fixed::toFloat( void ) const { + return ((float)this->_value / (float)(1 << Fixed::_frac)); +} + + +/* + * overload "<<" -> output fixed point in float representation + * found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp + */ + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs) +{ + o << rhs.toFloat(); + return (o); +} diff --git a/d07/ex02/Makefile b/d07/ex02/Makefile index 2f18fa0..f1c973b 100644 --- a/d07/ex02/Makefile +++ b/d07/ex02/Makefile @@ -29,14 +29,16 @@ LIBS = INCLUDES = -I$(D_HEADERS) D_SRCS = . -SRCS = main.cpp +SRCS = main.cpp \ + Fixed.cpp D_HEADERS = headers HEADERS = colors.h \ Array.hpp \ Iter.hpp \ Print.hpp \ - Fill.hpp + Fill.hpp \ + Fixed.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d07/ex02/array b/d07/ex02/array new file mode 100755 index 0000000..c5a5de6 Binary files /dev/null and b/d07/ex02/array differ diff --git a/d07/ex02/headers/Array.hpp b/d07/ex02/headers/Array.hpp index 1445e1c..804a784 100644 --- a/d07/ex02/headers/Array.hpp +++ b/d07/ex02/headers/Array.hpp @@ -6,11 +6,22 @@ template < typename T > class Array { public: - Array(unsigned int n = 0u) - : _size(n) - , _arr(new T[n]) {}; - ~Array() { delete [] _arr; }; - Array(Array const & src) : _arr(new T[0]) { *this = src; }; + Array(unsigned int n = 0U) + : _size(n) + , _arr(new T[n]){ + for (unsigned int i = 0; i < n; i++) + _arr[i] = 0; + }; + + ~Array() { + delete [] _arr; + }; + + Array(Array const & src) + : _arr(new T[0]) { + *this = src; + }; + Array &operator=(Array const & rhs) { if (this == &rhs) return (*this); diff --git a/d07/ex02/headers/Fixed.hpp b/d07/ex02/headers/Fixed.hpp new file mode 100644 index 0000000..b63a80c --- /dev/null +++ b/d07/ex02/headers/Fixed.hpp @@ -0,0 +1,55 @@ +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include +#include + +class Fixed { + +public: + + Fixed(void); // default/parametric constructor + Fixed(Fixed const & src); // copy constructor + ~Fixed(void); // destructor + Fixed(int integer); + Fixed(float const floater); + + Fixed & operator= (Fixed const & rhs); // assignement operator + bool operator< (Fixed const & rhs) const; + bool operator> (Fixed const & rhs) const; + bool operator<=(Fixed const & rhs) const; + bool operator>=(Fixed const & rhs) const; + bool operator==(Fixed const & rhs) const; + bool operator!=(Fixed const & rhs) const; + Fixed operator+ (Fixed const & rhs) const; + Fixed operator- (Fixed const & rhs) const; + Fixed operator* (Fixed const & rhs) const; + Fixed operator/ (Fixed const & rhs) const; + Fixed & operator++(void); // prefix ++o + Fixed & operator--(void); // prefix --o + Fixed operator++(int); // postfix o++ + Fixed operator--(int); // postfix o-- + + static const Fixed & min(Fixed const & lhs, Fixed const & rhs); + static const Fixed & max(Fixed const & lhs, Fixed const & rhs); + static Fixed & min(Fixed & lhs, Fixed & rhs); + static Fixed & max(Fixed & lhs, Fixed & rhs); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + +private: + + int _value; + static int const _frac; + static int const _max; + static int const _min; + +}; + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs); + +#endif diff --git a/d07/ex02/main.cpp b/d07/ex02/main.cpp index 20fd472..a00d3cf 100644 --- a/d07/ex02/main.cpp +++ b/d07/ex02/main.cpp @@ -7,8 +7,9 @@ #include "Print.hpp" #include "Array.hpp" #include "Fill.hpp" +#include "Fixed.hpp" -#define N_TEST "7" +#define N_TEST "13" int main() { srand(time(NULL)); @@ -91,15 +92,134 @@ int main() { std::cout << "\n"; } - // tests luke std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " - << "tests :" RESET "\n"; + << "tests simple array :" RESET "\n"; { Array a(10); std::cout << "a.size :" << a.size() << "\n"; } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests empty array :" RESET "\n"; + { + Array arr(0); + + std::cout << "arr.size :" << arr.size() << "\n"; + try { + arr[arr.size()] = 42; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests write and read :" RESET "\n"; + { + std::cout << B_BLUE "create Array arr(5);" RESET "\n"; + Array arr(5); + std::cout << B_BLUE "create Array const constarr(5);" RESET "\n"; + Array const constarr(5); + + std::cout << "arr.size :" << arr.size() << "\n"; + std::cout << "constarr.size :" << constarr.size() << "\n"; + + try { + std::cout << B_BLUE "arr[3] = 12" RESET "\n"; + arr[3] = 12; + std::cout << B_BLUE "print arr;" RESET "\n"; + ::Iter(arr, arr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + + try { + std::cout << B_BLUE "print constarr;" RESET "\n"; + ::Iter(constarr, constarr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests empty Fixed array :" RESET "\n"; + { + Array arr(0); + + std::cout << "fixed arr.size :" << arr.size() << "\n"; + try { + arr[arr.size()] = 42; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests fixed write and read :" RESET "\n"; + { + std::cout << B_BLUE "create Array arr(5);" RESET "\n"; + Array arr(5); + std::cout << B_BLUE "create Array const constarr(5);" RESET "\n"; + Array const constarr(5); + + std::cout << "arr.size :" << arr.size() << "\n"; + std::cout << "constarr.size :" << constarr.size() << "\n"; + + try { + std::cout << B_BLUE "arr[3] = 12" RESET "\n"; + arr[3] = 12; + std::cout << B_BLUE "print arr;" RESET "\n"; + ::Iter(arr, arr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + + try { + std::cout << B_BLUE "print constarr;" RESET "\n"; + ::Iter(constarr, constarr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests empty Fixed array :" RESET "\n"; + { + Array arr(0); + + std::cout << "char arr.size :" << arr.size() << "\n"; + try { + arr[arr.size()] = 'c'; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests char write and read :" RESET "\n"; + { + std::cout << B_BLUE "create Array arr(5);" RESET "\n"; + Array arr(5); + std::cout << B_BLUE "create Array const constarr(5);" RESET "\n"; + Array const constarr(5); + + std::cout << "arr.size :" << arr.size() << "\n"; + std::cout << "constarr.size :" << constarr.size() << "\n"; + + try { + std::cout << B_BLUE "arr[3] = 12" RESET "\n"; + arr[3] = 12; + std::cout << B_BLUE "print arr;" RESET "\n"; + ::Iter(arr, arr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + + try { + std::cout << B_BLUE "print constarr;" RESET "\n"; + ::Iter(constarr, constarr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "tests index correct :" RESET "\n"; { @@ -141,7 +261,7 @@ int main() { } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " - << "tests :" RESET "\n"; + << "tests subject :" RESET "\n"; { #define MAX_VAL 750 Array numbers(MAX_VAL); diff --git a/d08/correction_d08.png b/d08/correction_d08.png new file mode 100644 index 0000000..47c2d45 Binary files /dev/null and b/d08/correction_d08.png differ diff --git a/d08/ex00/easyfind.hpp b/d08/ex00/easyfind.hpp index 9496691..a6a5109 100644 --- a/d08/ex00/easyfind.hpp +++ b/d08/ex00/easyfind.hpp @@ -6,7 +6,7 @@ class easyfindException : public std::exception { virtual char const *what(void) const throw() { - return "not found"; + return B_RED "not found" RESET; } }; diff --git a/d08/ex00/main.cpp b/d08/ex00/main.cpp index 1b0a966..2e0a24b 100644 --- a/d08/ex00/main.cpp +++ b/d08/ex00/main.cpp @@ -10,22 +10,20 @@ #define N_TEST "3" template < typename T > -void standardTest(T container) { +void standardTest(T container, int start, int end, int test) { typename T::const_iterator it; - int test; - for (int i = -9 ; i < 10 ; i++) + for (int i = start ; i < end ; i++) container.push_back(i); for (it = container.begin(); it != container.end(); it++) std::cout << *it << ": " << &*it << "\n"; std::cout << "\n"; - test = -3; try { it = easyfind(container, test); - std::cout << *it << ": " << &*it << "\n"; + std::cout << *it << ": " << &*it << "\n\n"; } catch (std::exception const & e) { - std::cout << test << ": " << e.what() << "\n"; + std::cout << test << ": " << e.what() << "\n\n"; } } @@ -35,22 +33,73 @@ int main() { std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "tests list :" RESET "\n"; { - std::list container; - standardTest(container); + std::cout << B_BLUE "char A - L; find H :" RESET "\n"; + std::list container; + standardTest(container, 'A', 'L', 'H'); + + std::cout << B_BLUE "char A - L; find L :" RESET "\n"; + std::list container1; + standardTest(container1, 'A', 'L', 'L'); + + std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n"; + std::list container2; + standardTest(container2, -5, 5, 4); + + std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n"; + std::list container3; + standardTest(container3, -5, 5, 5); + + std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n"; + std::list container4; + standardTest(container4, -5, 5, 6); } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "tests vector :" RESET "\n"; { - std::vector container; - standardTest(container); + std::cout << B_BLUE "char A - L; find H :" RESET "\n"; + std::vector container; + standardTest(container, 'A', 'L', 'H'); + + std::cout << B_BLUE "char A - L; find L :" RESET "\n"; + std::vector container1; + standardTest(container1, 'A', 'L', 'L'); + + std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n"; + std::vector container2; + standardTest(container2, -5, 5, 4); + + std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n"; + std::vector container3; + standardTest(container3, -5, 5, 5); + + std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n"; + std::vector container4; + standardTest(container4, -5, 5, 6); } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "tests deque :" RESET "\n"; { - std::deque container; - standardTest(container); + std::cout << B_BLUE "char A - L; find H :" RESET "\n"; + std::deque container; + standardTest(container, 'A', 'L', 'H'); + + std::cout << B_BLUE "char A - L; find L :" RESET "\n"; + std::deque container1; + standardTest(container1, 'A', 'L', 'L'); + + std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n"; + std::deque container2; + standardTest(container2, -5, 5, 4); + + std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n"; + std::deque container3; + standardTest(container3, -5, 5, 5); + + std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n"; + std::deque container4; + standardTest(container4, -5, 5, 6); } return 0; diff --git a/d08/ex01/Span.cpp b/d08/ex01/Span.cpp index 28a564b..3f32821 100644 --- a/d08/ex01/Span.cpp +++ b/d08/ex01/Span.cpp @@ -68,7 +68,7 @@ bool Span::empty() const { * PUBLIC MEMBER FUNCTIONS *********************************************/ -void Span::addNumber(int nb) { +void Span::addNumber(int nb) { if (_container.size() >= _max) throw std::out_of_range(B_RED "out of range number" RESET); _container.push_back(nb); @@ -98,8 +98,6 @@ void Span::addNumber(InputIterator first, InputIterator last) { } template void Span::addNumber(int*, int*); - - unsigned int Span::shortestSpan() { int const size = _container.size(); unsigned int shortest = longestSpan(); @@ -123,9 +121,3 @@ unsigned int Span::longestSpan() { return (_sort.back() - _sort.front()); } -/********************************************* - * NESTED CLASS - *********************************************/ - -//void Span::Class::function() {} - diff --git a/d08/ex01/Span.hpp b/d08/ex01/Span.hpp index b6de956..0a090ac 100644 --- a/d08/ex01/Span.hpp +++ b/d08/ex01/Span.hpp @@ -17,9 +17,9 @@ public: Span & operator=( Span const & rhs ); void addNumber(int nb); -// void addNumber(int * arr, unsigned int size); template void addNumber(InputIterator first, InputIterator last); +// void addNumber(int * arr, unsigned int size); unsigned int shortestSpan(); unsigned int longestSpan(); diff --git a/d08/ex01/main.cpp b/d08/ex01/main.cpp index d3b6abd..47bb891 100644 --- a/d08/ex01/main.cpp +++ b/d08/ex01/main.cpp @@ -1,6 +1,7 @@ #include #include #include "colors.h" +#include // rand #include "Span.hpp" diff --git a/d08/ex02/Makefile b/d08/ex02/Makefile index 1715a65..32e28a8 100644 --- a/d08/ex02/Makefile +++ b/d08/ex02/Makefile @@ -32,9 +32,8 @@ D_SRCS = . SRCS = main.cpp D_HEADERS = . -HEADERS = -#HEADERS = colors.h \ -# MutantStack.hpp +HEADERS = colors.h \ + MutantStack.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d08/ex02/MutantStack.hpp b/d08/ex02/MutantStack.hpp index e0c2eaf..201dae8 100644 --- a/d08/ex02/MutantStack.hpp +++ b/d08/ex02/MutantStack.hpp @@ -7,22 +7,24 @@ template class MutantStack : public std::stack { public: + /* typedef T * iterator; typedef T const * const_iterator; iterator begin() {return (&this->top() - (this->size() - 1));} - iterator end() {return (&this->top()+ 1);} + iterator end() {return (&this->top() + 1);} + const_iterator begin() const {return (&this->top() - (this->size() - 1));} - const_iterator end() const {return (&this->top()+ 1);} + const_iterator end() const {return (&this->top() + 1);} + */ -// typedef typename std::stack::container_type::iterator iterator; -// typedef typename std::stack::container_type::const_iterator const_iterator; - -// iterator begin() {return this->c.begin();} -// iterator end() {return this->c.end();} -// const_iterator begin() const {return this->c.begin();} -// const_iterator end() const {return this->c.end();} + typedef typename std::stack::container_type::iterator iterator; + typedef typename std::stack::container_type::const_iterator const_iterator; + iterator begin() {return this->c.begin();} + iterator end() {return this->c.end();} + const_iterator begin() const {return this->c.begin();} + const_iterator end() const {return this->c.end();} }; #endif diff --git a/d08/ex02/main.cpp b/d08/ex02/main.cpp index fcaae06..e089785 100644 --- a/d08/ex02/main.cpp +++ b/d08/ex02/main.cpp @@ -1,11 +1,13 @@ #include #include "colors.h" #include "MutantStack.hpp" +#include // rand -#define N_TEST "2" +#define N_TEST "3" int main() { int i = 0; + srand(time(NULL)); std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "test simple iterator :" RESET "\n"; @@ -26,6 +28,100 @@ int main() { std::cout << *it << "\n"; } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "test stack size 128:" RESET "\n"; + { + MutantStack mstack; + + mstack.push(-42); + for (unsigned int i = 0; i < 126; i++) + mstack.push(rand() % 10000000000000); + mstack.push(42); + + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + + std::cout << "[ "; + for (; it != ite; it++) + std::cout << *it << " "; + std::cout << "]\n"; + + it = mstack.begin(); + ite = mstack.end(); + + std::cout << B_BLUE "firtst and last :" RESET "\n"; + std::cout << *it << " | " << *(ite - 1) << "\n\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "test stack size 129:" RESET "\n"; + { + MutantStack mstack; + + mstack.push(-42); + for (unsigned int i = 0; i < 127; i++) + mstack.push(rand() % 10000000000000); + mstack.push(42); + + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + + std::cout << "[ "; + for (; it != ite; it++) + std::cout << *it << " "; + std::cout << "]\n"; + + it = mstack.begin(); + ite = mstack.end(); + + std::cout << B_BLUE "firtst and last :" RESET "\n"; + std::cout << *it << " | " << *(ite - 1) << "\n\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "test stack size 10000:" RESET "\n"; + { + MutantStack mstack; + + mstack.push(-42); + for (unsigned int i = 0; i < 10000; i++) + mstack.push(rand() % 10000000000000); + mstack.push(42); + + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + + std::cout << "[ "; + for (; it != ite; it++) + std::cout << *it << " "; + std::cout << "]\n"; + + it = mstack.begin(); + ite = mstack.end(); + + std::cout << B_BLUE "firtst and last :" RESET "\n"; + std::cout << *it << " | " << *(ite - 1) << "\n\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "test const iterator :" RESET "\n"; + { + MutantStack mstack; + + mstack.push(7); + mstack.push(987); + mstack.push(9); + mstack.push(8); + mstack.push(34); + mstack.push(1); + + MutantStack::const_iterator it = mstack.begin(); + MutantStack::const_iterator ite = mstack.end(); + + for (; it != ite; it++) + std::cout << *it << "\n"; + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "tests subject :" RESET "\n"; {