diff --git a/ex02/Makefile b/ex02/Makefile index 6601adb..e79f471 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -1,25 +1,25 @@ CXX = c++ -CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0 SRCDIR = src OBJDIR = obj -NAME = ex02 +EXECUTABLE = fixed SRCS = $(wildcard $(SRCDIR)/*.cpp) OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) -all: $(NAME) +all: $(EXECUTABLE) $(OBJDIR)/%.o: $(SRCDIR)/%.cpp - mkdir -p obj - $(CXX) $(CPPFLAGS) -c $< -o $@ + $(CXX) $(CXXFLAGS) -c $< -o $@ -$(NAME): $(OBJS) - $(CXX) $(CPPFLAGS) $^ -o $@ +$(EXECUTABLE): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ clean: rm -rf $(OBJDIR)/*.o fclean: clean - rm -fr $(NAME) + rm -fr $(EXECUTABLE) re: fclean all + diff --git a/ex02/obj/.gitkeep b/ex02/obj/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/ex02/src/Fixed.cpp b/ex02/src/Fixed.cpp index d9f27b5..1a1c929 100644 --- a/ex02/src/Fixed.cpp +++ b/ex02/src/Fixed.cpp @@ -10,14 +10,13 @@ Fixed::~Fixed() Fixed::Fixed() { - this->_value = 0; std::cout << "Default constructor called" << std::endl; } Fixed::Fixed(const Fixed& src) { std::cout << "Copy constructor called" << std::endl; - this->_value = src.getRawBits(); + this->_value = src._value; } Fixed::Fixed(const int& src) @@ -35,7 +34,7 @@ Fixed::Fixed(const float& src) Fixed& Fixed::operator=(const Fixed& src) { std::cout << "Copy assignment operator called" << std::endl; - this->_value = src.getRawBits(); + this->_value = src._value; return (*this); } @@ -66,3 +65,107 @@ std::ostream& operator<<(std::ostream &out, const Fixed& fixed) out << (fixed.toFloat()); return (out); } + +Fixed& Fixed::operator++() +{ + this->_value++; + return (*this); +} + +Fixed& Fixed::operator--() +{ + this->_value--; + return (*this); +} + +Fixed Fixed::operator++(int) +{ + Fixed tmp(*this); + this->_value++; + return (tmp); +} + +Fixed Fixed::operator--(int) +{ + Fixed tmp(*this); + this->_value--; + return (tmp); +} + +bool Fixed::operator==(const Fixed& src) +{ + return (this->_value == src._value); +} + +bool Fixed::operator!=(const Fixed& src) +{ + return (this->_value != src._value); +} + +bool Fixed::operator>=(const Fixed& src) +{ + return (this->_value >= src._value); +} + +bool Fixed::operator<=(const Fixed& src) +{ + return (this->_value <= src._value); +} + +bool Fixed::operator>(const Fixed& src) +{ + return (this->_value > src._value); +} + +bool Fixed::operator<(const Fixed& src) +{ + return (this->_value < src._value); +} + +Fixed Fixed::operator*(const Fixed &src) +{ + return Fixed(this->toFloat() * src.toFloat()); +} + +Fixed Fixed::operator/(const Fixed &src) +{ + return Fixed(this->toFloat() / src.toFloat()); +} + +Fixed Fixed::operator+(const Fixed &src) +{ + return Fixed(this->toFloat() + src.toFloat()); +} + +Fixed Fixed::operator-(const Fixed &src) +{ + return Fixed(this->toFloat() - src.toFloat()); +} + +const Fixed& Fixed::max(const Fixed& f1, const Fixed &f2) +{ + if (f1._value > f2._value) + return (f1); + return (f2); +} + +Fixed& Fixed::max(Fixed& f1, Fixed &f2) +{ + if (f1._value > f2._value) + return (f1); + return (f2); +} + +const Fixed& Fixed::min(const Fixed& f1, const Fixed &f2) +{ + if (f1._value > f2._value) + return (f1); + return (f2); +} + +Fixed& Fixed::min(Fixed& f1, Fixed &f2) +{ + if (f1._value > f2._value) + return (f1); + return (f2); +} diff --git a/ex02/src/Fixed.hpp b/ex02/src/Fixed.hpp index 47d860d..e20d099 100644 --- a/ex02/src/Fixed.hpp +++ b/ex02/src/Fixed.hpp @@ -18,10 +18,32 @@ class Fixed Fixed &operator=(const Fixed& src); + bool operator<(const Fixed& src); + bool operator>(const Fixed& src); + bool operator>=(const Fixed& src); + bool operator<=(const Fixed& src); + bool operator==(const Fixed& src); + bool operator!=(const Fixed& src); + + Fixed operator-(const Fixed& src); + Fixed operator+(const Fixed& src); + Fixed operator*(const Fixed& src); + Fixed operator/(const Fixed& src); + + + Fixed &operator++(); + Fixed &operator--(); + Fixed operator++(int); + Fixed operator--(int); + int getRawBits( void ) const; void setRawBits( int const raw ); float toFloat(void) const ; int toInt(void) const ; + static const Fixed& max(const Fixed& f1, const Fixed& f2); + static Fixed& max(Fixed& f1, Fixed& f2); + static const Fixed& min(const Fixed& f1, const Fixed& f2); + static Fixed& min(Fixed& f1, Fixed& f2); }; std::ostream& operator << (std::ostream &out, const Fixed& fixed); diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 7065acf..3c44aa2 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -2,19 +2,14 @@ #include int main( void ) { - Fixed a; - Fixed const b( 10 ); - Fixed const c( 42.42f ); - Fixed const d( b ); - a = Fixed( 1234.4321f ); - std::cout << "a is " << a << std::endl; - std::cout << "b is " << b << std::endl; - std::cout << "c is " << c << std::endl; - std::cout << "d is " << d << std::endl; - std::cout << "a is " << a.toInt() << " as integer" << std::endl; - std::cout << "b is " << b.toInt() << " as integer" << std::endl; - std::cout << "c is " << c.toInt() << " as integer" << std::endl; - std::cout << "d is " << d.toInt() << " as integer" << std::endl; - + Fixed a(0); + Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) ); + std::cout << a << std::endl; + std::cout << ++a << std::endl; + std::cout << a << std::endl; + std::cout << a++ << std::endl; + std::cout << a << std::endl; + std::cout << b << std::endl; + std::cout << Fixed::max( a, b ) << std::endl; return 0; }