Merge branch 'master' of bitbucket.org:hugogogo/piscine_cpp
This commit is contained in:
@@ -18,7 +18,7 @@ INCLUDES = -I$(D_HEADERS)
|
||||
D_SRCS = .
|
||||
SRCS = main.cpp \
|
||||
Zombie.cpp \
|
||||
ZombieHorde.cpp
|
||||
zombieHorde.cpp
|
||||
|
||||
D_HEADERS = .
|
||||
HEADERS = Zombie.hpp
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
BIN
d01/ex03/war
Executable file
BIN
d01/ex03/war
Executable file
Binary file not shown.
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
BIN
d01/ex04/sed
Executable file
BIN
d01/ex04/sed
Executable file
Binary file not shown.
@@ -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
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
hello
|
||||
@@ -1,5 +0,0 @@
|
||||
ce fichier
|
||||
contient
|
||||
plusieurs lignes
|
||||
les unes au dessus des autres
|
||||
youhouuu ioieux
|
||||
@@ -1,5 +0,0 @@
|
||||
ce fich++r
|
||||
cont++nt
|
||||
plus++urs lignes
|
||||
les unes au dessus des autres
|
||||
youhouuu io++ux
|
||||
@@ -1 +0,0 @@
|
||||
....................................................;
|
||||
@@ -1 +0,0 @@
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++++;
|
||||
@@ -1,2 +0,0 @@
|
||||
...................................................;
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . .;
|
||||
@@ -1,2 +0,0 @@
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++.;
|
||||
. . . . . . . . . . . . . . . . . . . . . . . . . .;
|
||||
@@ -1 +0,0 @@
|
||||
test de mmdr foubar
|
||||
@@ -1 +0,0 @@
|
||||
test de m||| foubar
|
||||
@@ -1,2 +0,0 @@
|
||||
trouve ce mot toutouille
|
||||
et celui-la toutoutouille
|
||||
@@ -1,2 +0,0 @@
|
||||
trouve ce mot ||||||||||
|
||||
et celui-la tou||||||||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Binary file not shown.
17
d02/<
17
d02/<
@@ -1,17 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ ifeq "$(TYPE)" "c"
|
||||
CC = c
|
||||
EXT = c
|
||||
else ifeq "$(TYPE)" "cpp"
|
||||
CC = clang++
|
||||
CC = c++
|
||||
EXT = cpp
|
||||
endif
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -18,25 +18,25 @@ int main() {
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << B_BLUE "cat i : " RESET;
|
||||
i->getBrain()->printIdea(0);
|
||||
dynamic_cast<Cat*>(i)->getBrain()->printIdea(0);
|
||||
std::cout << B_BLUE "cat j : " RESET;
|
||||
j->getBrain()->printIdea(0);
|
||||
dynamic_cast<Cat*>(j)->getBrain()->printIdea(0);
|
||||
|
||||
std::cout << "\n" B_BLUE "*i = *j" RESET "\n";
|
||||
*i = *j;
|
||||
*(dynamic_cast<Cat*>(i)) = *(dynamic_cast<Cat*>(j));
|
||||
|
||||
std::cout << B_BLUE "cat i : " RESET;
|
||||
i->getBrain()->printIdea(0);
|
||||
dynamic_cast<Cat*>(i)->getBrain()->printIdea(0);
|
||||
std::cout << B_BLUE "cat j : " RESET;
|
||||
j->getBrain()->printIdea(0);
|
||||
dynamic_cast<Cat*>(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<Cat*>(j)->getBrain()->putIdea(0, "I am not a cat");;
|
||||
|
||||
std::cout << B_BLUE "cat i : " RESET;
|
||||
i->getBrain()->printIdea(0);
|
||||
dynamic_cast<Cat*>(i)->getBrain()->printIdea(0);
|
||||
std::cout << B_BLUE "cat j : " RESET;
|
||||
j->getBrain()->printIdea(0);
|
||||
dynamic_cast<Cat*>(j)->getBrain()->printIdea(0);
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << B_BLUE "delete i" RESET "\n";
|
||||
|
||||
BIN
d04/ex02/pure
Executable file
BIN
d04/ex02/pure
Executable file
Binary file not shown.
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
86
d05/ex03/Makefile
Normal file
86
d05/ex03/Makefile
Normal file
@@ -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
|
||||
|
||||
54
d05/ex03/headers/AForm.hpp
Normal file
54
d05/ex03/headers/AForm.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef AFORM_HPP
|
||||
# define AFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
|
||||
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
|
||||
|
||||
48
d05/ex03/headers/Bureaucrat.hpp
Normal file
48
d05/ex03/headers/Bureaucrat.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef BUREAUCRAT_HPP
|
||||
# define BUREAUCRAT_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <stdexcept>
|
||||
|
||||
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
|
||||
|
||||
44
d05/ex03/headers/Intern.hpp
Normal file
44
d05/ex03/headers/Intern.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef INTERN_HPP
|
||||
# define INTERN_HPP
|
||||
|
||||
# include "colors.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <algorithm> // trasnform
|
||||
# include <cctype> // tolower
|
||||
|
||||
# include <AForm.hpp>
|
||||
# include <ShrubberyCreationForm.hpp>
|
||||
# include <PresidentialPardonForm.hpp>
|
||||
# include <RobotomyRequestForm.hpp>
|
||||
|
||||
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
|
||||
|
||||
28
d05/ex03/headers/PresidentialPardonForm.hpp
Normal file
28
d05/ex03/headers/PresidentialPardonForm.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef PRESIDENTIALPARDONFORM_HPP
|
||||
# define PRESIDENTIALPARDONFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <cstdlib>
|
||||
|
||||
# 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
|
||||
|
||||
28
d05/ex03/headers/RobotomyRequestForm.hpp
Normal file
28
d05/ex03/headers/RobotomyRequestForm.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef ROBOTOMYREQUESTFORM_HPP
|
||||
# define ROBOTOMYREQUESTFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <cstdlib>
|
||||
|
||||
# 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
|
||||
|
||||
28
d05/ex03/headers/ShrubberyCreationForm.hpp
Normal file
28
d05/ex03/headers/ShrubberyCreationForm.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef SHRUBBERYCREATIONFORM_HPP
|
||||
# define SHRUBBERYCREATIONFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <fstream>
|
||||
|
||||
# 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
|
||||
|
||||
25
d05/ex03/headers/color.h
Normal file
25
d05/ex03/headers/color.h
Normal file
@@ -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
|
||||
|
||||
192
d05/ex03/main.cpp
Normal file
192
d05/ex03/main.cpp
Normal file
@@ -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;
|
||||
}
|
||||
103
d05/ex03/srcs/AForm.cpp
Normal file
103
d05/ex03/srcs/AForm.cpp
Normal file
@@ -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);
|
||||
}
|
||||
117
d05/ex03/srcs/Bureaucrat.cpp
Normal file
117
d05/ex03/srcs/Bureaucrat.cpp
Normal file
@@ -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);
|
||||
}
|
||||
84
d05/ex03/srcs/Intern.cpp
Normal file
84
d05/ex03/srcs/Intern.cpp
Normal file
@@ -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);}
|
||||
|
||||
47
d05/ex03/srcs/PresidentialPardonForm.cpp
Normal file
47
d05/ex03/srcs/PresidentialPardonForm.cpp
Normal file
@@ -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";
|
||||
}
|
||||
|
||||
51
d05/ex03/srcs/RobotomyRequestForm.cpp
Normal file
51
d05/ex03/srcs/RobotomyRequestForm.cpp
Normal file
@@ -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";
|
||||
}
|
||||
|
||||
74
d05/ex03/srcs/ShrubberyCreationForm.cpp
Normal file
74
d05/ex03/srcs/ShrubberyCreationForm.cpp
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
BIN
d06/ex00/convert
Executable file
BIN
d06/ex00/convert
Executable file
Binary file not shown.
@@ -28,7 +28,6 @@ template <typename T> 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 <typename T> 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"
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -2,6 +2,12 @@
|
||||
#include <string>
|
||||
#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,
|
||||
|
||||
@@ -22,8 +22,6 @@ void toChar(T value) {
|
||||
template <typename T>
|
||||
void toInt(T value) {
|
||||
std::cout << std::setw(7) << std::left << "int";
|
||||
// if (value < std::numeric_limits<int>::min()
|
||||
// || value > std::numeric_limits<int>::max() )
|
||||
if (! (value <= std::numeric_limits<int>::max()
|
||||
&& value >= std::numeric_limits<int>::min()) )
|
||||
{
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
BIN
d06/ex01/serialize
Executable file
BIN
d06/ex01/serialize
Executable file
Binary file not shown.
@@ -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)
|
||||
|
||||
55
d07/ex00/headers/Fixed.hpp
Normal file
55
d07/ex00/headers/Fixed.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef FIXED_HPP
|
||||
# define FIXED_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
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
|
||||
25
d07/ex00/headers/colors.h
Normal file
25
d07/ex00/headers/colors.h
Normal file
@@ -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
|
||||
|
||||
@@ -2,8 +2,27 @@
|
||||
#include <string>
|
||||
#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;
|
||||
|
||||
225
d07/ex00/srcs/Fixed.cpp
Normal file
225
d07/ex00/srcs/Fixed.cpp
Normal file
@@ -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);
|
||||
}
|
||||
225
d07/ex01/Fixed.cpp
Normal file
225
d07/ex01/Fixed.cpp
Normal file
@@ -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);
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
20
d07/ex01/headers/ClassTest.hpp
Normal file
20
d07/ex01/headers/ClassTest.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef CLASSTEST_HPP
|
||||
# define CLASSTEST_HPP
|
||||
|
||||
# include <iostream>
|
||||
|
||||
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
|
||||
|
||||
55
d07/ex01/headers/Fixed.hpp
Normal file
55
d07/ex01/headers/Fixed.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef FIXED_HPP
|
||||
# define FIXED_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
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
|
||||
BIN
d07/ex01/iter
Executable file
BIN
d07/ex01/iter
Executable file
Binary file not shown.
@@ -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<int>);
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests class :" RESET "\n";
|
||||
{
|
||||
ClassTest tab[5];
|
||||
|
||||
::Iter(tab, 5, Print<ClassTest>);
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests fixed :" RESET "\n";
|
||||
{
|
||||
Fixed f[5];
|
||||
|
||||
::Iter(f, 5, Print<Fixed>);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
225
d07/ex02/Fixed.cpp
Normal file
225
d07/ex02/Fixed.cpp
Normal file
@@ -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);
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
BIN
d07/ex02/array
Executable file
BIN
d07/ex02/array
Executable file
Binary file not shown.
@@ -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);
|
||||
|
||||
55
d07/ex02/headers/Fixed.hpp
Normal file
55
d07/ex02/headers/Fixed.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef FIXED_HPP
|
||||
# define FIXED_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
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
|
||||
@@ -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<int> a(10);
|
||||
|
||||
std::cout << "a.size :" << a.size() << "\n";
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests empty array :" RESET "\n";
|
||||
{
|
||||
Array<int> 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<int> arr(5);" RESET "\n";
|
||||
Array<int> arr(5);
|
||||
std::cout << B_BLUE "create Array<int> const constarr(5);" RESET "\n";
|
||||
Array<int> 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<int>);
|
||||
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<int>);
|
||||
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<Fixed> 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<Fixed> arr(5);" RESET "\n";
|
||||
Array<Fixed> arr(5);
|
||||
std::cout << B_BLUE "create Array<Fixed> const constarr(5);" RESET "\n";
|
||||
Array<Fixed> 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<Fixed>);
|
||||
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<Fixed>);
|
||||
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<char> 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<char> arr(5);" RESET "\n";
|
||||
Array<char> arr(5);
|
||||
std::cout << B_BLUE "create Array<char> const constarr(5);" RESET "\n";
|
||||
Array<char> 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<char>);
|
||||
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<char>);
|
||||
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<int> numbers(MAX_VAL);
|
||||
|
||||
BIN
d08/correction_d08.png
Normal file
BIN
d08/correction_d08.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 425 KiB |
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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<int> container;
|
||||
standardTest(container);
|
||||
std::cout << B_BLUE "char A - L; find H :" RESET "\n";
|
||||
std::list<char> container;
|
||||
standardTest(container, 'A', 'L', 'H');
|
||||
|
||||
std::cout << B_BLUE "char A - L; find L :" RESET "\n";
|
||||
std::list<char> container1;
|
||||
standardTest(container1, 'A', 'L', 'L');
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n";
|
||||
std::list<int> container2;
|
||||
standardTest(container2, -5, 5, 4);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n";
|
||||
std::list<int> container3;
|
||||
standardTest(container3, -5, 5, 5);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n";
|
||||
std::list<int> container4;
|
||||
standardTest(container4, -5, 5, 6);
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests vector :" RESET "\n";
|
||||
{
|
||||
std::vector<int> container;
|
||||
standardTest(container);
|
||||
std::cout << B_BLUE "char A - L; find H :" RESET "\n";
|
||||
std::vector<char> container;
|
||||
standardTest(container, 'A', 'L', 'H');
|
||||
|
||||
std::cout << B_BLUE "char A - L; find L :" RESET "\n";
|
||||
std::vector<char> container1;
|
||||
standardTest(container1, 'A', 'L', 'L');
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n";
|
||||
std::vector<int> container2;
|
||||
standardTest(container2, -5, 5, 4);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n";
|
||||
std::vector<int> container3;
|
||||
standardTest(container3, -5, 5, 5);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n";
|
||||
std::vector<int> container4;
|
||||
standardTest(container4, -5, 5, 6);
|
||||
}
|
||||
|
||||
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
|
||||
<< "tests deque :" RESET "\n";
|
||||
{
|
||||
std::deque<int> container;
|
||||
standardTest(container);
|
||||
std::cout << B_BLUE "char A - L; find H :" RESET "\n";
|
||||
std::deque<char> container;
|
||||
standardTest(container, 'A', 'L', 'H');
|
||||
|
||||
std::cout << B_BLUE "char A - L; find L :" RESET "\n";
|
||||
std::deque<char> container1;
|
||||
standardTest(container1, 'A', 'L', 'L');
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 4 :" RESET "\n";
|
||||
std::deque<int> container2;
|
||||
standardTest(container2, -5, 5, 4);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 5 :" RESET "\n";
|
||||
std::deque<int> container3;
|
||||
standardTest(container3, -5, 5, 5);
|
||||
|
||||
std::cout << B_BLUE "int -5 - 5; find 6 :" RESET "\n";
|
||||
std::deque<int> container4;
|
||||
standardTest(container4, -5, 5, 6);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -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*, 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() {}
|
||||
|
||||
|
||||
@@ -17,9 +17,9 @@ public:
|
||||
Span & operator=( Span const & rhs );
|
||||
|
||||
void addNumber(int nb);
|
||||
// void addNumber(int * arr, unsigned int size);
|
||||
template <class InputIterator>
|
||||
void addNumber(InputIterator first, InputIterator last);
|
||||
// void addNumber(int * arr, unsigned int size);
|
||||
|
||||
unsigned int shortestSpan();
|
||||
unsigned int longestSpan();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "colors.h"
|
||||
#include <cstdlib> // rand
|
||||
|
||||
#include "Span.hpp"
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,22 +7,24 @@ template <typename T>
|
||||
class MutantStack : public std::stack<T> {
|
||||
|
||||
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<T>::container_type::iterator iterator;
|
||||
// typedef typename std::stack<T>::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<T>::container_type::iterator iterator;
|
||||
typedef typename std::stack<T>::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
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include <iostream>
|
||||
#include "colors.h"
|
||||
#include "MutantStack.hpp"
|
||||
#include <cstdlib> // 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<int> mstack;
|
||||
|
||||
mstack.push(-42);
|
||||
for (unsigned int i = 0; i < 126; i++)
|
||||
mstack.push(rand() % 10000000000000);
|
||||
mstack.push(42);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::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<int> mstack;
|
||||
|
||||
mstack.push(-42);
|
||||
for (unsigned int i = 0; i < 127; i++)
|
||||
mstack.push(rand() % 10000000000000);
|
||||
mstack.push(42);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::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<int> mstack;
|
||||
|
||||
mstack.push(-42);
|
||||
for (unsigned int i = 0; i < 10000; i++)
|
||||
mstack.push(rand() % 10000000000000);
|
||||
mstack.push(42);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::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<int> mstack;
|
||||
|
||||
mstack.push(7);
|
||||
mstack.push(987);
|
||||
mstack.push(9);
|
||||
mstack.push(8);
|
||||
mstack.push(34);
|
||||
mstack.push(1);
|
||||
|
||||
MutantStack<int>::const_iterator it = mstack.begin();
|
||||
MutantStack<int>::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";
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user