diff --git a/d02/ex01/Fixed.cpp b/d02/ex01/Fixed.cpp index 6b16de2..69954d3 100644 --- a/d02/ex01/Fixed.cpp +++ b/d02/ex01/Fixed.cpp @@ -12,8 +12,8 @@ */ int const Fixed::_frac = 8; -int const Fixed::_fmax = -1U >> (_frac +1); -int const Fixed::_fmin = ~_fmax; +int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; /* * default constructor / copy constructor / destructor @@ -42,19 +42,19 @@ Fixed::~Fixed( void ) { Fixed::Fixed(int integer) { std::cout << "Int constructor called" << '\n'; - if (integer < this->_fmin || integer > this->_fmax) + if (integer < Fixed::_min || integer > Fixed::_max) std::cout << "error: integer out of range" << '\n'; else - this->_value = integer << this->_frac; + this->_value = integer << Fixed::_frac; } Fixed::Fixed(float const floater) { std::cout << "Float constructor called" << '\n'; - if (floater < this->_fmin || floater > this->_fmax) + if (floater < Fixed::_min || floater > Fixed::_max) std::cout << "error: float out of range" << '\n'; else - this->_value = roundf(floater * (1 << this->_frac)); + this->_value = roundf(floater * (1 << Fixed::_frac)); } /* @@ -81,10 +81,10 @@ void Fixed::setRawBits( int const raw ) { } int Fixed::toInt( void ) const { - return (this->_value >> this->_frac); + return (this->_value >> Fixed::_frac); } float Fixed::toFloat( void ) const { - return ((float)this->_value / (float)(1 << this->_frac)); + return ((float)this->_value / (float)(1 << Fixed::_frac)); } /* diff --git a/d02/ex01/Fixed.hpp b/d02/ex01/Fixed.hpp index aab1d17..bd3f0f6 100644 --- a/d02/ex01/Fixed.hpp +++ b/d02/ex01/Fixed.hpp @@ -26,8 +26,8 @@ private: int _value; static int const _frac; - static int const _fmax; - static int const _fmin; + static int const _max; + static int const _min; }; diff --git a/d02/ex01/fixed b/d02/ex01/fixed new file mode 100755 index 0000000..ce8fca3 Binary files /dev/null and b/d02/ex01/fixed differ diff --git "a/d02/ex02/\032make" "b/d02/ex02/\032make" deleted file mode 100644 index e884f78..0000000 --- "a/d02/ex02/\032make" +++ /dev/null @@ -1,238 +0,0 @@ -#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); - - -/* - * 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 < ~this->_max || integer > this->_max) - std::cout << "error: integer out of range" << '\n'; - else - this->_value = integer << this->_frac; -} - -Fixed::Fixed(float const floater) { - if (floater < ~this->_max || floater > this->_max) - std::cout << "error: float out of range" << '\n'; - else - this->_value = roundf(floater * (1 << this->_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->toFloat() < rhs.toFloat(); -} -bool Fixed::operator> (Fixed const & rhs) const { - return this->toFloat() > rhs.toFloat(); -} -bool Fixed::operator<=(Fixed const & rhs) const { - return this->toFloat() <= rhs.toFloat(); -} -bool Fixed::operator>=(Fixed const & rhs) const { - return this->toFloat() >= rhs.toFloat(); -} -bool Fixed::operator==(Fixed const & rhs) const { - return this->toFloat() == rhs.toFloat(); -} -bool Fixed::operator!=(Fixed const & rhs) const { - return this->toFloat() != rhs.toFloat(); -} -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) >> 8; - return result; -} -Fixed Fixed::operator/ ( Fixed const & rhs ) const { - Fixed result(*this); - - while (result._value < (rhs._value << 8)) - result._value <<= 1; -// result._value /= rhs._value; - return result; - -// if (rhs._value >> _frac != 0) -// result._value /= rhs._value >> _frac; -// return result; - -// return Fixed( this->toFloat() / rhs.toFloat() ); - -// -// 10 / 5 = 2 -// 5 / 10 = 0.5 -// 10 / 6 = 1.666... -// 6 / 10 = 0.6 -// - -} -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 >> this->_frac); -} -float Fixed::toFloat( void ) const { - return ((float)this->_value / (float)(1 << this->_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/d02/ex02/Fixed.cpp b/d02/ex02/Fixed.cpp index 42edded..c92534a 100644 --- a/d02/ex02/Fixed.cpp +++ b/d02/ex02/Fixed.cpp @@ -48,6 +48,7 @@ std::string printBitsFloat(float num) int const Fixed::_frac = 8; int const Fixed::_max = -1U >> (_frac +1); +int const Fixed::_min = ~_max; /* @@ -73,17 +74,17 @@ Fixed::~Fixed( void ) { */ Fixed::Fixed(int integer) { - if (integer < ~this->_max || integer > this->_max) + if (integer < Fixed::_min || integer > Fixed::_max) std::cout << "error: integer out of range" << '\n'; else - this->_value = integer << this->_frac; + this->_value = integer << Fixed::_frac; } Fixed::Fixed(float const floater) { - if (floater < ~this->_max || floater > this->_max) + if (floater < Fixed::_min || floater > Fixed::_max) std::cout << "error: float out of range" << '\n'; else - this->_value = roundf(floater * (1 << this->_frac)); + this->_value = roundf(floater * (1 << Fixed::_frac)); } @@ -106,22 +107,22 @@ Fixed & Fixed::operator=( Fixed const & rhs ) { */ bool Fixed::operator< (Fixed const & rhs) const { - return this->toFloat() < rhs.toFloat(); + return this->_value < rhs._value; } bool Fixed::operator> (Fixed const & rhs) const { - return this->toFloat() > rhs.toFloat(); + return rhs < *this; } bool Fixed::operator<=(Fixed const & rhs) const { - return this->toFloat() <= rhs.toFloat(); + return !(rhs > *this); } bool Fixed::operator>=(Fixed const & rhs) const { - return this->toFloat() >= rhs.toFloat(); + return !(rhs < *this); } bool Fixed::operator==(Fixed const & rhs) const { return this->toFloat() == rhs.toFloat(); } bool Fixed::operator!=(Fixed const & rhs) const { - return this->toFloat() != rhs.toFloat(); + return !(*this == rhs); } Fixed Fixed::operator+ ( Fixed const & rhs ) const { Fixed result(*this); @@ -135,34 +136,19 @@ Fixed Fixed::operator- ( Fixed const & rhs ) const { } Fixed Fixed::operator* ( Fixed const & rhs ) const { Fixed result(*this); - result._value = ((long)result._value * (long)rhs._value) >> 8; + result._value = ((long)result._value * (long)rhs._value) >> Fixed::_frac; return result; } Fixed Fixed::operator/ ( Fixed const & rhs ) const { Fixed result(*this); - - result._value = (long)(result._value << _frac) / rhs._value; - -// if (rhs._value >> _frac != 0) -// result._value /= rhs._value >> _frac; - + result._value = (long)(result._value << Fixed::_frac) / rhs._value; return result; - -// return Fixed( this->toFloat() / rhs.toFloat() ); - -// -// 10 / 5 = 2 -// 5 / 10 = 0.5 -// 10 / 6 = 1.666... -// 6 / 10 = 0.6 -// - } -Fixed Fixed::operator++() { +Fixed & Fixed::operator++() { this->_value++; return *this; } -Fixed Fixed::operator--() { +Fixed & Fixed::operator--() { this->_value--; return *this; } @@ -217,10 +203,10 @@ void Fixed::setRawBits( int const raw ) { } int Fixed::toInt( void ) const { - return (this->_value >> this->_frac); + return (this->_value >> Fixed::_frac); } float Fixed::toFloat( void ) const { - return ((float)this->_value / (float)(1 << this->_frac)); + return ((float)this->_value / (float)(1 << Fixed::_frac)); } diff --git a/d02/ex02/Fixed.hpp b/d02/ex02/Fixed.hpp index c30657f..b63a80c 100644 --- a/d02/ex02/Fixed.hpp +++ b/d02/ex02/Fixed.hpp @@ -26,8 +26,8 @@ public: 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++(void); // prefix ++o + Fixed & operator--(void); // prefix --o Fixed operator++(int); // postfix o++ Fixed operator--(int); // postfix o-- @@ -46,6 +46,7 @@ private: int _value; static int const _frac; static int const _max; + static int const _min; }; diff --git a/d02/ex02/operators b/d02/ex02/operators index 51e3beb..0d59a29 100755 Binary files a/d02/ex02/operators and b/d02/ex02/operators differ diff --git a/d03/ex01/main.cpp b/d03/ex01/main.cpp index ca0fe1d..02a3a40 100644 --- a/d03/ex01/main.cpp +++ b/d03/ex01/main.cpp @@ -2,32 +2,44 @@ void goAttack(ClapTrap & robot1, ClapTrap & robot2) { + std::cout << "."; robot1.attack(robot2.getName()); + std::cout << " "; robot2.takeDamage(robot1.getAttack()); + std::cout << " "; robot2.beRepaired(robot1.getAttack()); } void goAttack(ScavTrap & robot1, ScavTrap & robot2) { + std::cout << "."; robot1.attack(robot2.getName()); + std::cout << " "; robot2.takeDamage(robot1.getAttack()); + std::cout << " "; robot2.beRepaired(robot1.getAttack()); } void goAttack(ClapTrap & robot1, ScavTrap & robot2) { + std::cout << "."; robot1.attack(robot2.getName()); + std::cout << " "; robot2.takeDamage(robot1.getAttack()); + std::cout << " "; robot2.beRepaired(robot1.getAttack()); } void goAttack(ScavTrap & robot1, ClapTrap & robot2) { + std::cout << "."; robot1.attack(robot2.getName()); + std::cout << " "; robot2.takeDamage(robot1.getAttack()); + std::cout << " "; robot2.beRepaired(robot1.getAttack()); } diff --git a/d03/ex01/robots b/d03/ex01/robots index 21a2f62..6e5d271 100755 Binary files a/d03/ex01/robots and b/d03/ex01/robots differ