diff --git a/d07/ex00/Makefile b/d07/ex00/Makefile index e474bc5..58ef36a 100644 --- a/d07/ex00/Makefile +++ b/d07/ex00/Makefile @@ -28,12 +28,14 @@ LIBS = INCLUDES = -I$(D_HEADERS) -D_SRCS = . -SRCS = main.cpp +D_SRCS = srcs +SRCS = main.cpp \ + Fixed.cpp -D_HEADERS = . +D_HEADERS = headers HEADERS = colors.h \ - Templates.hpp + Templates.hpp \ + Fixed.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d07/ex00/headers/Fixed.hpp b/d07/ex00/headers/Fixed.hpp new file mode 100644 index 0000000..b63a80c --- /dev/null +++ b/d07/ex00/headers/Fixed.hpp @@ -0,0 +1,55 @@ +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include +#include + +class Fixed { + +public: + + Fixed(void); // default/parametric constructor + Fixed(Fixed const & src); // copy constructor + ~Fixed(void); // destructor + Fixed(int integer); + Fixed(float const floater); + + Fixed & operator= (Fixed const & rhs); // assignement operator + bool operator< (Fixed const & rhs) const; + bool operator> (Fixed const & rhs) const; + bool operator<=(Fixed const & rhs) const; + bool operator>=(Fixed const & rhs) const; + bool operator==(Fixed const & rhs) const; + bool operator!=(Fixed const & rhs) const; + Fixed operator+ (Fixed const & rhs) const; + Fixed operator- (Fixed const & rhs) const; + Fixed operator* (Fixed const & rhs) const; + Fixed operator/ (Fixed const & rhs) const; + Fixed & operator++(void); // prefix ++o + Fixed & operator--(void); // prefix --o + Fixed operator++(int); // postfix o++ + Fixed operator--(int); // postfix o-- + + static const Fixed & min(Fixed const & lhs, Fixed const & rhs); + static const Fixed & max(Fixed const & lhs, Fixed const & rhs); + static Fixed & min(Fixed & lhs, Fixed & rhs); + static Fixed & max(Fixed & lhs, Fixed & rhs); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + +private: + + int _value; + static int const _frac; + static int const _max; + static int const _min; + +}; + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs); + +#endif diff --git a/d07/ex00/Templates.hpp b/d07/ex00/headers/Templates.hpp similarity index 100% rename from d07/ex00/Templates.hpp rename to d07/ex00/headers/Templates.hpp diff --git a/d07/ex00/colors.h b/d07/ex00/headers/colors.h similarity index 100% rename from d07/ex00/colors.h rename to d07/ex00/headers/colors.h diff --git a/d07/ex00/main.cpp b/d07/ex00/main.cpp index 5e9683f..9401fd9 100644 --- a/d07/ex00/main.cpp +++ b/d07/ex00/main.cpp @@ -2,8 +2,27 @@ #include #include "colors.h" #include "Templates.hpp" +#include "Fixed.hpp" -#define N_TEST "3" +#define N_TEST "5" + +class Test { +public: + Test(int n = 0) : _n(n) {} + int getN() const {return _n;} + bool operator==(Test const & rhs) const {return (this->_n == rhs._n);} + bool operator!=(Test const & rhs) const {return (this->_n != rhs._n);} + bool operator> (Test const & rhs) const {return (this->_n > rhs._n);} + bool operator< (Test const & rhs) const {return (rhs._n > this->_n);} + bool operator>=(Test const & rhs) const {return ( !(this->_n < rhs._n) );} + bool operator<=(Test const & rhs) const {return ( !(this->_n > rhs._n) );} +private: + int _n; +}; +std::ostream & operator<<(std::ostream & o, Test const & rhs){ + o << rhs.getN(); + return (o); +} int main() { int i = 0; @@ -13,12 +32,13 @@ int main() { { int a = 2; int b = 3; - - std::cout << "a = " << a << ", b = " << b << "\nswap :\n"; + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; ::swap( a, b ); std::cout << "a = " << a << ", b = " << b << "\n"; - std::cout << "min( a, b ) = " << ::min( a, b ) << "\n"; - std::cout << "max( a, b ) = " << ::max( a, b ) << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " @@ -26,12 +46,13 @@ int main() { { std::string a = "chaine1"; std::string b = "chaine2"; - - std::cout << "a = " << a << ", b = " << b << "\nswap :\n"; + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; ::swap(a, b); std::cout << "a = " << a << ", b = " << b << "\n"; - std::cout << "min( a, b ) = " << ::min( a, b ) << "\n"; - std::cout << "max( a, b ) = " << ::max( a, b ) << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " @@ -39,12 +60,42 @@ int main() { { float a = 2.42f; float b = 32.7f; - - std::cout << "a = " << a << ", b = " << b << "\nswap :\n"; + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; ::swap( a, b ); std::cout << "a = " << a << ", b = " << b << "\n"; - std::cout << "min( a, b ) = " << ::min( a, b ) << "\n"; - std::cout << "max( a, b ) = " << ::max( a, b ) << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests fixed :" RESET "\n"; + { + Fixed a(2.42f); + Fixed b(32.7f); + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; + ::swap( a, b ); + std::cout << "a = " << a << ", b = " << b << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests class :" RESET "\n"; + { + + Test a(8); + Test b(16); + + std::cout << "a = " << a << ", b = " << b << "\n" B_BLUE "swap :" RESET "\n"; + ::swap( a, b ); + std::cout << "a = " << a << ", b = " << b << "\n"; + + std::cout << B_BLUE "min( a, b ) = " RESET << ::min( a, b ) << "\n"; + std::cout << B_BLUE "max( a, b ) = " RESET << ::max( a, b ) << "\n"; } return 0; diff --git a/d07/ex00/srcs/Fixed.cpp b/d07/ex00/srcs/Fixed.cpp new file mode 100644 index 0000000..3e72ea0 --- /dev/null +++ b/d07/ex00/srcs/Fixed.cpp @@ -0,0 +1,225 @@ +#include "Fixed.hpp" + +/* + * functions to print numbers in binary + * for the float, found help from stackoverflow : + * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec + */ + +std::string printBitsInt(int num) +{ + int i = 0; + + for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) + { + std::cout << ((num & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + +std::string printBitsFloat(float num) +{ + int *p = (int *)# + int i = 0; + + for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1) + { + std::cout << ((*p & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + + +/* + * statics variables initialisation + * + * for MAX integer : + * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1)) + * <= ... >= + * 11111111 10000000 00000000 00000000 (-8388608) + * + */ + +int const Fixed::_frac = 8; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; + + +/* + * default constructor / copy constructor / destructor + */ + +Fixed::Fixed() : _value(0) { + return; +} + +Fixed::Fixed(Fixed const & src) { + *this = src; + return; +} + +Fixed::~Fixed( void ) { + return; +} + + +/* + * int and float constructors + */ + +Fixed::Fixed(int integer) { + if (integer < Fixed::_min || integer > Fixed::_max) + std::cout << "error: integer out of range" << '\n'; + else + this->_value = integer << Fixed::_frac; +} + +Fixed::Fixed(float const floater) { + if (floater < Fixed::_min || floater > Fixed::_max) + std::cout << "error: float out of range" << '\n'; + else + this->_value = roundf(floater * (1 << Fixed::_frac)); +} + + +/* + * assignement operator + */ + +Fixed & Fixed::operator=( Fixed const & rhs ) { + if ( this != &rhs ) + this->_value = rhs.getRawBits(); + return *this; +} + + +/* + * operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; -- + * ref : https://en.cppreference.com/w/cpp/language/operators + * for division, if you want to avoid floats (legitimate) : + * https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c + */ + +bool Fixed::operator< (Fixed const & rhs) const { + return this->_value < rhs._value; +} +bool Fixed::operator> (Fixed const & rhs) const { + return rhs < *this; +} +bool Fixed::operator<=(Fixed const & rhs) const { + return !(*this > rhs); +} +bool Fixed::operator>=(Fixed const & rhs) const { + return !(*this < rhs); +} +bool Fixed::operator==(Fixed const & rhs) const { + return this->_value == rhs._value; +} +bool Fixed::operator!=(Fixed const & rhs) const { + return !(*this == rhs); +} +Fixed Fixed::operator+ ( Fixed const & rhs ) const { + Fixed result(*this); + result._value += rhs._value; + return (result); +} +Fixed Fixed::operator- ( Fixed const & rhs ) const { + Fixed result(*this); + result._value -= rhs._value; + return (result); +} +Fixed Fixed::operator* ( Fixed const & rhs ) const { + Fixed result(*this); + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; + return result; +} +Fixed Fixed::operator/ ( Fixed const & rhs ) const { + Fixed result(*this); + if (rhs._value == 0) + std::cout << "!impossible division by 0¡"; + else + result._value = (long)(result._value << Fixed::_frac) / rhs._value; + return result; +} +Fixed & Fixed::operator++() { + this->_value++; + return *this; +} +Fixed & Fixed::operator--() { + this->_value--; + return *this; +} +Fixed Fixed::operator++( int ) { + Fixed old = *this; + Fixed::operator++(); + return old; +} +Fixed Fixed::operator--( int ) { + Fixed old = *this; + Fixed::operator--(); + return old; +} + + +/* + * returns min and max + */ + +Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} +Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} + + +/* + * functions that returns _value + */ + +int Fixed::getRawBits( void ) const { + return this->_value; +} + +void Fixed::setRawBits( int const raw ) { + this->_value = raw; +} + +int Fixed::toInt( void ) const { + return (this->_value >> Fixed::_frac); +} +float Fixed::toFloat( void ) const { + return ((float)this->_value / (float)(1 << Fixed::_frac)); +} + + +/* + * overload "<<" -> output fixed point in float representation + * found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp + */ + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs) +{ + o << rhs.toFloat(); + return (o); +} diff --git a/d07/ex01/Fixed.cpp b/d07/ex01/Fixed.cpp new file mode 100644 index 0000000..3e72ea0 --- /dev/null +++ b/d07/ex01/Fixed.cpp @@ -0,0 +1,225 @@ +#include "Fixed.hpp" + +/* + * functions to print numbers in binary + * for the float, found help from stackoverflow : + * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec + */ + +std::string printBitsInt(int num) +{ + int i = 0; + + for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) + { + std::cout << ((num & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + +std::string printBitsFloat(float num) +{ + int *p = (int *)# + int i = 0; + + for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1) + { + std::cout << ((*p & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + + +/* + * statics variables initialisation + * + * for MAX integer : + * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1)) + * <= ... >= + * 11111111 10000000 00000000 00000000 (-8388608) + * + */ + +int const Fixed::_frac = 8; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; + + +/* + * default constructor / copy constructor / destructor + */ + +Fixed::Fixed() : _value(0) { + return; +} + +Fixed::Fixed(Fixed const & src) { + *this = src; + return; +} + +Fixed::~Fixed( void ) { + return; +} + + +/* + * int and float constructors + */ + +Fixed::Fixed(int integer) { + if (integer < Fixed::_min || integer > Fixed::_max) + std::cout << "error: integer out of range" << '\n'; + else + this->_value = integer << Fixed::_frac; +} + +Fixed::Fixed(float const floater) { + if (floater < Fixed::_min || floater > Fixed::_max) + std::cout << "error: float out of range" << '\n'; + else + this->_value = roundf(floater * (1 << Fixed::_frac)); +} + + +/* + * assignement operator + */ + +Fixed & Fixed::operator=( Fixed const & rhs ) { + if ( this != &rhs ) + this->_value = rhs.getRawBits(); + return *this; +} + + +/* + * operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; -- + * ref : https://en.cppreference.com/w/cpp/language/operators + * for division, if you want to avoid floats (legitimate) : + * https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c + */ + +bool Fixed::operator< (Fixed const & rhs) const { + return this->_value < rhs._value; +} +bool Fixed::operator> (Fixed const & rhs) const { + return rhs < *this; +} +bool Fixed::operator<=(Fixed const & rhs) const { + return !(*this > rhs); +} +bool Fixed::operator>=(Fixed const & rhs) const { + return !(*this < rhs); +} +bool Fixed::operator==(Fixed const & rhs) const { + return this->_value == rhs._value; +} +bool Fixed::operator!=(Fixed const & rhs) const { + return !(*this == rhs); +} +Fixed Fixed::operator+ ( Fixed const & rhs ) const { + Fixed result(*this); + result._value += rhs._value; + return (result); +} +Fixed Fixed::operator- ( Fixed const & rhs ) const { + Fixed result(*this); + result._value -= rhs._value; + return (result); +} +Fixed Fixed::operator* ( Fixed const & rhs ) const { + Fixed result(*this); + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; + return result; +} +Fixed Fixed::operator/ ( Fixed const & rhs ) const { + Fixed result(*this); + if (rhs._value == 0) + std::cout << "!impossible division by 0¡"; + else + result._value = (long)(result._value << Fixed::_frac) / rhs._value; + return result; +} +Fixed & Fixed::operator++() { + this->_value++; + return *this; +} +Fixed & Fixed::operator--() { + this->_value--; + return *this; +} +Fixed Fixed::operator++( int ) { + Fixed old = *this; + Fixed::operator++(); + return old; +} +Fixed Fixed::operator--( int ) { + Fixed old = *this; + Fixed::operator--(); + return old; +} + + +/* + * returns min and max + */ + +Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} +Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} + + +/* + * functions that returns _value + */ + +int Fixed::getRawBits( void ) const { + return this->_value; +} + +void Fixed::setRawBits( int const raw ) { + this->_value = raw; +} + +int Fixed::toInt( void ) const { + return (this->_value >> Fixed::_frac); +} +float Fixed::toFloat( void ) const { + return ((float)this->_value / (float)(1 << Fixed::_frac)); +} + + +/* + * overload "<<" -> output fixed point in float representation + * found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp + */ + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs) +{ + o << rhs.toFloat(); + return (o); +} diff --git a/d07/ex01/Makefile b/d07/ex01/Makefile index 50bc55b..efc01c9 100644 --- a/d07/ex01/Makefile +++ b/d07/ex01/Makefile @@ -29,14 +29,17 @@ LIBS = INCLUDES = -I$(D_HEADERS) D_SRCS = . -SRCS = main.cpp +SRCS = main.cpp \ + Fixed.cpp D_HEADERS = headers HEADERS = colors.h \ Iter.hpp \ Print.hpp \ ToUpper.hpp \ - Decrement.hpp + Decrement.hpp \ + ClassTest.hpp \ + Fixed.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d07/ex01/headers/ClassTest.hpp b/d07/ex01/headers/ClassTest.hpp new file mode 100644 index 0000000..4e3e1fd --- /dev/null +++ b/d07/ex01/headers/ClassTest.hpp @@ -0,0 +1,20 @@ +#ifndef CLASSTEST_HPP +# define CLASSTEST_HPP + +# include + +class ClassTest { +public: + ClassTest() : _value("hello") {} + std::string getValue() const {return _value;} +private: + std::string _value; +}; + +std::ostream & operator<<(std::ostream & o, ClassTest const & rhs) { + o << rhs.getValue(); + return o; +} + +#endif + diff --git a/d07/ex01/headers/Fixed.hpp b/d07/ex01/headers/Fixed.hpp new file mode 100644 index 0000000..b63a80c --- /dev/null +++ b/d07/ex01/headers/Fixed.hpp @@ -0,0 +1,55 @@ +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include +#include + +class Fixed { + +public: + + Fixed(void); // default/parametric constructor + Fixed(Fixed const & src); // copy constructor + ~Fixed(void); // destructor + Fixed(int integer); + Fixed(float const floater); + + Fixed & operator= (Fixed const & rhs); // assignement operator + bool operator< (Fixed const & rhs) const; + bool operator> (Fixed const & rhs) const; + bool operator<=(Fixed const & rhs) const; + bool operator>=(Fixed const & rhs) const; + bool operator==(Fixed const & rhs) const; + bool operator!=(Fixed const & rhs) const; + Fixed operator+ (Fixed const & rhs) const; + Fixed operator- (Fixed const & rhs) const; + Fixed operator* (Fixed const & rhs) const; + Fixed operator/ (Fixed const & rhs) const; + Fixed & operator++(void); // prefix ++o + Fixed & operator--(void); // prefix --o + Fixed operator++(int); // postfix o++ + Fixed operator--(int); // postfix o-- + + static const Fixed & min(Fixed const & lhs, Fixed const & rhs); + static const Fixed & max(Fixed const & lhs, Fixed const & rhs); + static Fixed & min(Fixed & lhs, Fixed & rhs); + static Fixed & max(Fixed & lhs, Fixed & rhs); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + +private: + + int _value; + static int const _frac; + static int const _max; + static int const _min; + +}; + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs); + +#endif diff --git a/d07/ex01/iter b/d07/ex01/iter new file mode 100755 index 0000000..3116a3a Binary files /dev/null and b/d07/ex01/iter differ diff --git a/d07/ex01/main.cpp b/d07/ex01/main.cpp index 945b374..47c98ab 100644 --- a/d07/ex01/main.cpp +++ b/d07/ex01/main.cpp @@ -5,8 +5,10 @@ #include "Print.hpp" #include "ToUpper.hpp" #include "Decrement.hpp" +#include "ClassTest.hpp" +#include "Fixed.hpp" -#define N_TEST "2" +#define N_TEST "4" int main() { int i = 0; @@ -38,6 +40,22 @@ int main() { ::Iter(arr, len, Print); } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests class :" RESET "\n"; + { + ClassTest tab[5]; + + ::Iter(tab, 5, Print); + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests fixed :" RESET "\n"; + { + Fixed f[5]; + + ::Iter(f, 5, Print); + } + return 0; } diff --git a/d07/ex02/Fixed.cpp b/d07/ex02/Fixed.cpp new file mode 100644 index 0000000..3e72ea0 --- /dev/null +++ b/d07/ex02/Fixed.cpp @@ -0,0 +1,225 @@ +#include "Fixed.hpp" + +/* + * functions to print numbers in binary + * for the float, found help from stackoverflow : + * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec + */ + +std::string printBitsInt(int num) +{ + int i = 0; + + for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1) + { + std::cout << ((num & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + +std::string printBitsFloat(float num) +{ + int *p = (int *)# + int i = 0; + + for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1) + { + std::cout << ((*p & mask) != 0); + i++; + if (i == 1 || i == 9 || i == 24) + std::cout << ' '; + } + return ""; +} + + +/* + * statics variables initialisation + * + * for MAX integer : + * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1)) + * <= ... >= + * 11111111 10000000 00000000 00000000 (-8388608) + * + */ + +int const Fixed::_frac = 8; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; + + +/* + * default constructor / copy constructor / destructor + */ + +Fixed::Fixed() : _value(0) { + return; +} + +Fixed::Fixed(Fixed const & src) { + *this = src; + return; +} + +Fixed::~Fixed( void ) { + return; +} + + +/* + * int and float constructors + */ + +Fixed::Fixed(int integer) { + if (integer < Fixed::_min || integer > Fixed::_max) + std::cout << "error: integer out of range" << '\n'; + else + this->_value = integer << Fixed::_frac; +} + +Fixed::Fixed(float const floater) { + if (floater < Fixed::_min || floater > Fixed::_max) + std::cout << "error: float out of range" << '\n'; + else + this->_value = roundf(floater * (1 << Fixed::_frac)); +} + + +/* + * assignement operator + */ + +Fixed & Fixed::operator=( Fixed const & rhs ) { + if ( this != &rhs ) + this->_value = rhs.getRawBits(); + return *this; +} + + +/* + * operators < ; > ; <= ; == ; != ; + ; - ; * ; / ; ++ ; -- + * ref : https://en.cppreference.com/w/cpp/language/operators + * for division, if you want to avoid floats (legitimate) : + * https://stackoverflow.com/questions/8506317/fixed-point-unsigned-division-in-c + */ + +bool Fixed::operator< (Fixed const & rhs) const { + return this->_value < rhs._value; +} +bool Fixed::operator> (Fixed const & rhs) const { + return rhs < *this; +} +bool Fixed::operator<=(Fixed const & rhs) const { + return !(*this > rhs); +} +bool Fixed::operator>=(Fixed const & rhs) const { + return !(*this < rhs); +} +bool Fixed::operator==(Fixed const & rhs) const { + return this->_value == rhs._value; +} +bool Fixed::operator!=(Fixed const & rhs) const { + return !(*this == rhs); +} +Fixed Fixed::operator+ ( Fixed const & rhs ) const { + Fixed result(*this); + result._value += rhs._value; + return (result); +} +Fixed Fixed::operator- ( Fixed const & rhs ) const { + Fixed result(*this); + result._value -= rhs._value; + return (result); +} +Fixed Fixed::operator* ( Fixed const & rhs ) const { + Fixed result(*this); + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; + return result; +} +Fixed Fixed::operator/ ( Fixed const & rhs ) const { + Fixed result(*this); + if (rhs._value == 0) + std::cout << "!impossible division by 0¡"; + else + result._value = (long)(result._value << Fixed::_frac) / rhs._value; + return result; +} +Fixed & Fixed::operator++() { + this->_value++; + return *this; +} +Fixed & Fixed::operator--() { + this->_value--; + return *this; +} +Fixed Fixed::operator++( int ) { + Fixed old = *this; + Fixed::operator++(); + return old; +} +Fixed Fixed::operator--( int ) { + Fixed old = *this; + Fixed::operator--(); + return old; +} + + +/* + * returns min and max + */ + +Fixed const & Fixed::min(Fixed const & lhs, Fixed const & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed const & Fixed::max(Fixed const & lhs, Fixed const & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} +Fixed & Fixed::min(Fixed & lhs, Fixed & rhs) { + if (lhs < rhs) + return lhs; + return rhs; +} +Fixed & Fixed::max(Fixed & lhs, Fixed & rhs) { + if (lhs > rhs) + return lhs; + return rhs; +} + + +/* + * functions that returns _value + */ + +int Fixed::getRawBits( void ) const { + return this->_value; +} + +void Fixed::setRawBits( int const raw ) { + this->_value = raw; +} + +int Fixed::toInt( void ) const { + return (this->_value >> Fixed::_frac); +} +float Fixed::toFloat( void ) const { + return ((float)this->_value / (float)(1 << Fixed::_frac)); +} + + +/* + * overload "<<" -> output fixed point in float representation + * found here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp + */ + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs) +{ + o << rhs.toFloat(); + return (o); +} diff --git a/d07/ex02/Makefile b/d07/ex02/Makefile index 2f18fa0..f1c973b 100644 --- a/d07/ex02/Makefile +++ b/d07/ex02/Makefile @@ -29,14 +29,16 @@ LIBS = INCLUDES = -I$(D_HEADERS) D_SRCS = . -SRCS = main.cpp +SRCS = main.cpp \ + Fixed.cpp D_HEADERS = headers HEADERS = colors.h \ Array.hpp \ Iter.hpp \ Print.hpp \ - Fill.hpp + Fill.hpp \ + Fixed.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d07/ex02/array b/d07/ex02/array new file mode 100755 index 0000000..c5a5de6 Binary files /dev/null and b/d07/ex02/array differ diff --git a/d07/ex02/headers/Array.hpp b/d07/ex02/headers/Array.hpp index 1445e1c..804a784 100644 --- a/d07/ex02/headers/Array.hpp +++ b/d07/ex02/headers/Array.hpp @@ -6,11 +6,22 @@ template < typename T > class Array { public: - Array(unsigned int n = 0u) - : _size(n) - , _arr(new T[n]) {}; - ~Array() { delete [] _arr; }; - Array(Array const & src) : _arr(new T[0]) { *this = src; }; + Array(unsigned int n = 0U) + : _size(n) + , _arr(new T[n]){ + for (unsigned int i = 0; i < n; i++) + _arr[i] = 0; + }; + + ~Array() { + delete [] _arr; + }; + + Array(Array const & src) + : _arr(new T[0]) { + *this = src; + }; + Array &operator=(Array const & rhs) { if (this == &rhs) return (*this); diff --git a/d07/ex02/headers/Fixed.hpp b/d07/ex02/headers/Fixed.hpp new file mode 100644 index 0000000..b63a80c --- /dev/null +++ b/d07/ex02/headers/Fixed.hpp @@ -0,0 +1,55 @@ +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include +#include + +class Fixed { + +public: + + Fixed(void); // default/parametric constructor + Fixed(Fixed const & src); // copy constructor + ~Fixed(void); // destructor + Fixed(int integer); + Fixed(float const floater); + + Fixed & operator= (Fixed const & rhs); // assignement operator + bool operator< (Fixed const & rhs) const; + bool operator> (Fixed const & rhs) const; + bool operator<=(Fixed const & rhs) const; + bool operator>=(Fixed const & rhs) const; + bool operator==(Fixed const & rhs) const; + bool operator!=(Fixed const & rhs) const; + Fixed operator+ (Fixed const & rhs) const; + Fixed operator- (Fixed const & rhs) const; + Fixed operator* (Fixed const & rhs) const; + Fixed operator/ (Fixed const & rhs) const; + Fixed & operator++(void); // prefix ++o + Fixed & operator--(void); // prefix --o + Fixed operator++(int); // postfix o++ + Fixed operator--(int); // postfix o-- + + static const Fixed & min(Fixed const & lhs, Fixed const & rhs); + static const Fixed & max(Fixed const & lhs, Fixed const & rhs); + static Fixed & min(Fixed & lhs, Fixed & rhs); + static Fixed & max(Fixed & lhs, Fixed & rhs); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + +private: + + int _value; + static int const _frac; + static int const _max; + static int const _min; + +}; + +std::ostream & operator<<(std::ostream & o, Fixed const & rhs); + +#endif diff --git a/d07/ex02/main.cpp b/d07/ex02/main.cpp index 20fd472..a00d3cf 100644 --- a/d07/ex02/main.cpp +++ b/d07/ex02/main.cpp @@ -7,8 +7,9 @@ #include "Print.hpp" #include "Array.hpp" #include "Fill.hpp" +#include "Fixed.hpp" -#define N_TEST "7" +#define N_TEST "13" int main() { srand(time(NULL)); @@ -91,15 +92,134 @@ int main() { std::cout << "\n"; } - // tests luke std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " - << "tests :" RESET "\n"; + << "tests simple array :" RESET "\n"; { Array a(10); std::cout << "a.size :" << a.size() << "\n"; } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests empty array :" RESET "\n"; + { + Array arr(0); + + std::cout << "arr.size :" << arr.size() << "\n"; + try { + arr[arr.size()] = 42; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests write and read :" RESET "\n"; + { + std::cout << B_BLUE "create Array arr(5);" RESET "\n"; + Array arr(5); + std::cout << B_BLUE "create Array const constarr(5);" RESET "\n"; + Array const constarr(5); + + std::cout << "arr.size :" << arr.size() << "\n"; + std::cout << "constarr.size :" << constarr.size() << "\n"; + + try { + std::cout << B_BLUE "arr[3] = 12" RESET "\n"; + arr[3] = 12; + std::cout << B_BLUE "print arr;" RESET "\n"; + ::Iter(arr, arr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + + try { + std::cout << B_BLUE "print constarr;" RESET "\n"; + ::Iter(constarr, constarr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests empty Fixed array :" RESET "\n"; + { + Array arr(0); + + std::cout << "fixed arr.size :" << arr.size() << "\n"; + try { + arr[arr.size()] = 42; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests fixed write and read :" RESET "\n"; + { + std::cout << B_BLUE "create Array arr(5);" RESET "\n"; + Array arr(5); + std::cout << B_BLUE "create Array const constarr(5);" RESET "\n"; + Array const constarr(5); + + std::cout << "arr.size :" << arr.size() << "\n"; + std::cout << "constarr.size :" << constarr.size() << "\n"; + + try { + std::cout << B_BLUE "arr[3] = 12" RESET "\n"; + arr[3] = 12; + std::cout << B_BLUE "print arr;" RESET "\n"; + ::Iter(arr, arr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + + try { + std::cout << B_BLUE "print constarr;" RESET "\n"; + ::Iter(constarr, constarr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests empty Fixed array :" RESET "\n"; + { + Array arr(0); + + std::cout << "char arr.size :" << arr.size() << "\n"; + try { + arr[arr.size()] = 'c'; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "tests char write and read :" RESET "\n"; + { + std::cout << B_BLUE "create Array arr(5);" RESET "\n"; + Array arr(5); + std::cout << B_BLUE "create Array const constarr(5);" RESET "\n"; + Array const constarr(5); + + std::cout << "arr.size :" << arr.size() << "\n"; + std::cout << "constarr.size :" << constarr.size() << "\n"; + + try { + std::cout << B_BLUE "arr[3] = 12" RESET "\n"; + arr[3] = 12; + std::cout << B_BLUE "print arr;" RESET "\n"; + ::Iter(arr, arr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + + try { + std::cout << B_BLUE "print constarr;" RESET "\n"; + ::Iter(constarr, constarr.size(), Print); + std::cout << "\n"; } + catch(std::exception& e) { + std::cerr << e.what() << '\n'; } + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "tests index correct :" RESET "\n"; { @@ -141,7 +261,7 @@ int main() { } std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " - << "tests :" RESET "\n"; + << "tests subject :" RESET "\n"; { #define MAX_VAL 750 Array numbers(MAX_VAL);