Init ex02
This commit is contained in:
118
ex02/Fixed/Fixed.cpp
Normal file
118
ex02/Fixed/Fixed.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/10 00:54:50 by tigos #+# #+# */
|
||||
/* Updated: 2025/09/10 22:31:58 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
// Default constructor
|
||||
Fixed::Fixed()
|
||||
{
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
// Constructor with constant integer
|
||||
Fixed::Fixed(const int nb)
|
||||
{
|
||||
std::cout << "Int constructor called" << std::endl;
|
||||
this->value = nb * (1 << this->fract);
|
||||
}
|
||||
|
||||
// Constructor with constant floating-point number
|
||||
Fixed::Fixed(const float nb)
|
||||
{
|
||||
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;
|
||||
this->value = other.value;
|
||||
}
|
||||
|
||||
// Copy assignment operator overload
|
||||
Fixed& Fixed::operator=(Fixed const &other)
|
||||
{
|
||||
std::cout << "Copy assignment operator called" << std::endl;
|
||||
if (this != &other)
|
||||
this->value = other.getRawBits();
|
||||
return (*this);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << "Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
// getRawBits()
|
||||
int Fixed::getRawBits( void ) const
|
||||
{
|
||||
std::cout << "getRawBits member function called" << std::endl;
|
||||
return (this->value);
|
||||
}
|
||||
|
||||
// setRawBits()
|
||||
void Fixed::setRawBits( int const raw )
|
||||
{
|
||||
this->value = raw;
|
||||
}
|
||||
|
||||
// toFloat
|
||||
float Fixed::toFloat( void ) const
|
||||
{
|
||||
return (this->value / (float)(1 << this->fract));
|
||||
}
|
||||
|
||||
// toInt
|
||||
int Fixed::toInt( void ) const
|
||||
{
|
||||
return (this->value >> this->fract);
|
||||
}
|
||||
|
||||
// · Comparison operators
|
||||
bool Fixed::operator<(const Fixed &other)
|
||||
{
|
||||
return (this->value < other->value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<=(const Fixed &other)
|
||||
{
|
||||
return (this->value <= other->value);
|
||||
}
|
||||
|
||||
bool Fixed::operator>(const Fixed &other)
|
||||
{
|
||||
return (this->value > other->value);
|
||||
}
|
||||
|
||||
bool Fixed::operator>=(const Fixed &other)
|
||||
{
|
||||
return (this->value >= other->value);
|
||||
}
|
||||
|
||||
bool Fixed::operator==(const Fixed &other)
|
||||
{
|
||||
return (this->value == other->value);
|
||||
}
|
||||
|
||||
bool Fixed::operator!=(const Fixed &other)
|
||||
{
|
||||
return (this->value != other->value);
|
||||
}
|
||||
|
||||
// << operator overload
|
||||
std::ostream& operator<<(std::ostream& os, Fixed const &other)
|
||||
{
|
||||
os << other.toFloat();
|
||||
return (os);
|
||||
}
|
||||
77
ex02/Fixed/Fixed.hpp
Normal file
77
ex02/Fixed/Fixed.hpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/10 00:54:53 by tigos #+# #+# */
|
||||
/* Updated: 2025/09/10 22:01:18 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FIXED_HPP
|
||||
|
||||
# define FIXED_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <cmath>
|
||||
|
||||
class Fixed
|
||||
{
|
||||
private:
|
||||
int value;
|
||||
static const int fract = 8;
|
||||
|
||||
public:
|
||||
// Default constructor
|
||||
Fixed();
|
||||
|
||||
// Constructor with constant integer
|
||||
Fixed(const int nb);
|
||||
|
||||
// Constructor with constant floating-point number
|
||||
Fixed(const float nb);
|
||||
|
||||
// Copy constructor
|
||||
Fixed(const Fixed &other);
|
||||
|
||||
// Copy assignment operator overload
|
||||
Fixed& operator=(Fixed const &other);
|
||||
|
||||
// Destructor
|
||||
~Fixed();
|
||||
|
||||
// getRawBits
|
||||
int getRawBits( void ) const;
|
||||
|
||||
// setRawBits
|
||||
void setRawBits( int const raw );
|
||||
|
||||
// toFloat
|
||||
float toFloat( void ) const;
|
||||
|
||||
// toInt
|
||||
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);
|
||||
|
||||
// · Arithmetic operators
|
||||
|
||||
// · Increments / decrements
|
||||
|
||||
// · Max and min
|
||||
|
||||
|
||||
};
|
||||
|
||||
// << operator overload
|
||||
std::ostream& operator<<(std::ostream& os, Fixed const &other);
|
||||
|
||||
#endif
|
||||
25
ex02/Makefile
Normal file
25
ex02/Makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
NAME = fixed
|
||||
|
||||
SRCS = main.cpp Fixed/Fixed.cpp
|
||||
OBJS=$(SRCS:.cpp=.o)
|
||||
|
||||
CC=c++
|
||||
CFLAGS=-Wall -Wextra -Werror -std=c++98
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
31
ex02/main.cpp
Normal file
31
ex02/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/10 21:57:38 by aortigos #+# #+# */
|
||||
/* Updated: 2025/09/10 22:26:59 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed/Fixed.hpp"
|
||||
|
||||
int main( void ) {
|
||||
|
||||
Fixed a;
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user