From 25147d5d2d8b4e457a5a7fc0e7be464bc51497cc Mon Sep 17 00:00:00 2001 From: aortigos Date: Fri, 12 Sep 2025 13:08:26 +0200 Subject: [PATCH] ex02 done --- ex02/Fixed/Fixed.cpp | 145 ++++++++++++++++++++++++++++++++++++------- ex02/Fixed/Fixed.hpp | 26 ++++++-- ex02/main.cpp | 2 +- 3 files changed, 145 insertions(+), 28 deletions(-) diff --git a/ex02/Fixed/Fixed.cpp b/ex02/Fixed/Fixed.cpp index bee77d2..b151a7d 100644 --- a/ex02/Fixed/Fixed.cpp +++ b/ex02/Fixed/Fixed.cpp @@ -6,43 +6,43 @@ /* By: aortigos value = nb * (1 << this->fract); } // Constructor with constant floating-point number Fixed::Fixed(const float nb) { - std::cout << "Float constructor called" << std::endl; + //std::cout << "Float constructor called" << std::endl; this->value = roundf(nb * (1 << this->fract)); } // Copy constructor Fixed::Fixed(const Fixed &other) { - std:: cout << "Copy constructor called" << std::endl; + //std:: cout << "Copy constructor called" << std::endl; this->value = other.value; } // Copy assignment operator overload Fixed& Fixed::operator=(Fixed const &other) { - std::cout << "Copy assignment operator called" << std::endl; + //std::cout << "Copy assignment operator called" << std::endl; if (this != &other) this->value = other.getRawBits(); return (*this); @@ -51,13 +51,13 @@ Fixed& Fixed::operator=(Fixed const &other) // Destructor Fixed::~Fixed() { - std::cout << "Destructor called" << std::endl; + //std::cout << "Destructor called" << std::endl; } // getRawBits() int Fixed::getRawBits( void ) const { - std::cout << "getRawBits member function called" << std::endl; + //std::cout << "getRawBits member function called" << std::endl; return (this->value); } @@ -80,34 +80,137 @@ int Fixed::toInt( void ) const } // · Comparison operators -bool Fixed::operator<(const Fixed &other) +bool Fixed::operator<(const Fixed &other) const { - return (this->value < other->value); + return (this->value < other.getRawBits()); } -bool Fixed::operator<=(const Fixed &other) +bool Fixed::operator<=(const Fixed &other) const { - return (this->value <= other->value); + return (this->value <= other.getRawBits()); } -bool Fixed::operator>(const Fixed &other) +bool Fixed::operator>(const Fixed &other) const { - return (this->value > other->value); + return (this->value > other.getRawBits()); } -bool Fixed::operator>=(const Fixed &other) +bool Fixed::operator>=(const Fixed &other) const { - return (this->value >= other->value); + return (this->value >= other.getRawBits()); } -bool Fixed::operator==(const Fixed &other) +bool Fixed::operator==(const Fixed &other) const { - return (this->value == other->value); + return (this->value == other.getRawBits()); } -bool Fixed::operator!=(const Fixed &other) +bool Fixed::operator!=(const Fixed &other) const { - return (this->value != other->value); + return (this->value != other.getRawBits()); +} + +// · Arithmetic operators +Fixed Fixed::operator+(Fixed const &other) const +{ + Fixed res; + + res.setRawBits(this->value + other.getRawBits()); + return (res); +} + +Fixed Fixed::operator-(Fixed const &other) const +{ + Fixed res; + + res.setRawBits(this->value - other.getRawBits()); + return (res); +} + +Fixed Fixed::operator*(Fixed const &other) const +{ + Fixed res; + long result; + + result = (long)this->value * (long)other.getRawBits(); + result = result >> this->fract; + res.setRawBits((int)result); + return (res); +} + +Fixed Fixed::operator/(Fixed const &other) const +{ + if (other.getRawBits() == 0) + { + return Fixed(); + } + long result = (long)this->value << this->fract; + result = result / other.getRawBits(); + + Fixed res; + res.setRawBits((int)result); + return (res); +} + +// · Increments / decrements + +Fixed& Fixed::operator++() +{ + this->value += 1; + return (*this); +} + +Fixed Fixed::operator++(int) +{ + Fixed temp(*this); + + this->value += 1; + return (temp); +} + +Fixed& Fixed::operator--() +{ + this->value -= 1; + return (*this); +} + +Fixed Fixed::operator--(int) +{ + Fixed temp(*this); + + this->value -= 1; + return (temp); +} + + +// · Max and min + +Fixed& Fixed::min(Fixed &a, Fixed &b) +{ + if (a.getRawBits() < b.getRawBits()) + return (a); + return (b); +} + +const Fixed& Fixed::min(const Fixed &a, const Fixed &b) +{ + if (a.getRawBits() < b.getRawBits()) + return (a); + return (b); +} + +Fixed& Fixed::max(Fixed &a, Fixed &b) +{ + if (a.getRawBits() > b.getRawBits()) + return (a); + return (b); +} + +const Fixed& Fixed::max(const Fixed &a, const Fixed &b) +{ + if (a.getRawBits() > b.getRawBits()) + return (a); + return (b); } // << operator overload diff --git a/ex02/Fixed/Fixed.hpp b/ex02/Fixed/Fixed.hpp index 725ff8a..d209e0b 100644 --- a/ex02/Fixed/Fixed.hpp +++ b/ex02/Fixed/Fixed.hpp @@ -55,19 +55,33 @@ class Fixed int toInt( void ) const; // · Comparison operators - bool Fixed::operator<(const Fixed &other); - bool Fixed::operator<=(const Fixed &other); - bool Fixed::operator>(const Fixed &other); - bool Fixed::operator>=(const Fixed &other); - bool Fixed::operator==(const Fixed &other); - bool Fixed::operator!=(const Fixed &other); + bool operator<(const Fixed &other) const; + bool operator<=(const Fixed &other) const; + bool operator>(const Fixed &other) const; + bool operator>=(const Fixed &other) const; + bool operator==(const Fixed &other) const; + bool operator!=(const Fixed &other) const; // · Arithmetic operators + Fixed operator+(Fixed const &other) const; + Fixed operator-(Fixed const &other) const; + Fixed operator*(Fixed const &other) const; + Fixed operator/(Fixed const &other) const; // · Increments / decrements + Fixed& operator++(); + Fixed operator++(int); + + Fixed& operator--(); + Fixed operator--(int); + // · Max and min + static Fixed& min(Fixed &a, Fixed &b); + static const Fixed& min(const Fixed &a, const Fixed &b); + static Fixed& max(Fixed &a, Fixed &b); + static const Fixed& max(const Fixed &a, const Fixed &b); }; diff --git a/ex02/main.cpp b/ex02/main.cpp index adbf848..05d07b4 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: aortigos