commit 5c05d0400080664f3f55295e68fd2ccdc1bf5919 Author: root Date: Sat Nov 15 22:09:12 2025 +0000 intra to gitea diff --git a/ex00/Fixed.cpp b/ex00/Fixed.cpp new file mode 100644 index 0000000..ae3aea8 --- /dev/null +++ b/ex00/Fixed.cpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:46 by lfirmin #+# #+# */ +/* Updated: 2025/07/02 12:33:48 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +const int Fixed::_number_fractional_bits = 8; + +Fixed::Fixed() : _fixed_p_n(0) +{ + std::cout << "Default constructor called\n"; +} + +Fixed::Fixed(const Fixed ©) +{ + std::cout << "Copy constructor called\n"; + *this = copy; +} + +Fixed &Fixed::operator=(const Fixed ©) +{ + std::cout << "Copy assignment constructor called\n"; + setRawBits(copy.getRawBits()); + return (*this); +} + +Fixed::~Fixed() +{ + std::cout << "Deconstructor called\n"; +} + +int Fixed::getRawBits(void) const +{ + std::cout << "getRawBits member function called\n"; + return (_fixed_p_n); +} + +void Fixed::setRawBits(int const raw) +{ + _fixed_p_n = raw; +} diff --git a/ex00/Fixed.hpp b/ex00/Fixed.hpp new file mode 100644 index 0000000..4273355 --- /dev/null +++ b/ex00/Fixed.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:41 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:42:26 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP +# define FIXED_HPP + +# include + +class Fixed +{ + private: + int _fixed_p_n; + static const int _number_fractional_bits; + + public: + + Fixed(); + Fixed(const Fixed ©); + Fixed &operator=(const Fixed ©); + ~Fixed(); + + + int getRawBits(void) const; + void setRawBits(int const raw); +}; + +#endif diff --git a/ex00/Main.cpp b/ex00/Main.cpp new file mode 100644 index 0000000..0c4d739 --- /dev/null +++ b/ex00/Main.cpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:45 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:42:17 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +int main( void ) +{ + Fixed a; + Fixed b( a ); + Fixed c; + + c = b; + + std::cout << a.getRawBits() << std::endl; + std::cout << b.getRawBits() << std::endl; + std::cout << c.getRawBits() << std::endl; + + return 0; +} diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..e62ae14 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,52 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# # +# Updated: 2025/07/02 12:32:58 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +C++ = c++ +C++_FLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +INFILES = Main.cpp\ + Fixed.cpp\ + + +OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o)) + +NAME = Fixed + +all: $(NAME) + +$(OBJDIR): + @echo "๐Ÿ“ Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "๐Ÿง  Compiling $< ..." + @$(C++) $(C++_FLAGS) -c $< -o $@ + @echo "โœ… $@ ready!" + +$(NAME): $(OBJFILES) + @echo "๐Ÿ”— Linking $(NAME) ..." + @$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME) + @echo "๐ŸŽ‰ $(NAME) is ready !" + +clean: + @echo "๐Ÿงน Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "โœจ Objects cleaned!" + +fclean: clean + @echo "๐Ÿ—‘๏ธ Removing $(NAME)..." + @rm -f $(NAME) + @echo "๐Ÿ’€ Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex01/Fixed.cpp b/ex01/Fixed.cpp new file mode 100644 index 0000000..9a2588b --- /dev/null +++ b/ex01/Fixed.cpp @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:46 by lfirmin #+# #+# */ +/* Updated: 2025/07/02 12:33:48 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +const int Fixed::_number_fractional_bits = 8; + +Fixed::Fixed() : _fixed_p_n(0) +{ + std::cout << "Default constructor called\n"; +} + +Fixed::Fixed(const int input) +{ + std::cout << "Fixed Int Constructor called" << std::endl; + this->_fixed_p_n = input << this->_number_fractional_bits; +} + +Fixed::Fixed(const float input) +{ + std::cout << "Fixed Float Constructor called" << std::endl; + this->_fixed_p_n = roundf(input * (1 << this->_number_fractional_bits)); +} + +Fixed::Fixed(const Fixed ©) +{ + std::cout << "Copy constructor called\n"; + *this = copy; +} + +Fixed &Fixed::operator=(const Fixed ©) +{ + std::cout << "Copy assignment constructor called\n"; + setRawBits(copy.getRawBits()); + return (*this); +} + +Fixed::~Fixed() +{ + std::cout << "Deconstructor called\n"; +} + +int Fixed::getRawBits(void) const +{ + std::cout << "getRawBits member function called\n"; + return (_fixed_p_n); +} + +void Fixed::setRawBits(int const raw) +{ + _fixed_p_n = raw; +} + +int Fixed::toInt(void)const +{ + return (this->_fixed_p_n>> this->_number_fractional_bits); +} + +float Fixed::toFloat(void)const +{ + return ((float)this->_fixed_p_n/ (float)(1 << this->_number_fractional_bits)); +} + +std::ostream &operator<<(std::ostream &o, Fixed const &fixed) +{ + o << fixed.toFloat(); + return (o); +} diff --git a/ex01/Fixed.hpp b/ex01/Fixed.hpp new file mode 100644 index 0000000..9f7a4dd --- /dev/null +++ b/ex01/Fixed.hpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:41 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:41:02 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include + + +class Fixed +{ + private: + int _fixed_p_n; + static const int _number_fractional_bits; + + public: + //const/dest + Fixed(); + Fixed(const int input); + Fixed(const float input); + Fixed(const Fixed ©); + ~Fixed(); + + //ol op + Fixed &operator=(const Fixed ©); + + //pub method + float toFloat(void)const; + int toInt(void)const; + + //get/set + int getRawBits(void) const; + void setRawBits(int const raw); +}; + +std::ostream &operator<<(std::ostream &o, Fixed const &fixed); + +#endif diff --git a/ex01/Main.cpp b/ex01/Main.cpp new file mode 100644 index 0000000..da83321 --- /dev/null +++ b/ex01/Main.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:45 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:40:41 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +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; + return 0; +} diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..e62ae14 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,52 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# # +# Updated: 2025/07/02 12:32:58 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +C++ = c++ +C++_FLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +INFILES = Main.cpp\ + Fixed.cpp\ + + +OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o)) + +NAME = Fixed + +all: $(NAME) + +$(OBJDIR): + @echo "๐Ÿ“ Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "๐Ÿง  Compiling $< ..." + @$(C++) $(C++_FLAGS) -c $< -o $@ + @echo "โœ… $@ ready!" + +$(NAME): $(OBJFILES) + @echo "๐Ÿ”— Linking $(NAME) ..." + @$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME) + @echo "๐ŸŽ‰ $(NAME) is ready !" + +clean: + @echo "๐Ÿงน Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "โœจ Objects cleaned!" + +fclean: clean + @echo "๐Ÿ—‘๏ธ Removing $(NAME)..." + @rm -f $(NAME) + @echo "๐Ÿ’€ Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex02/Fixed.cpp b/ex02/Fixed.cpp new file mode 100644 index 0000000..8a3ad91 --- /dev/null +++ b/ex02/Fixed.cpp @@ -0,0 +1,191 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:46 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:41:49 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" + +const int Fixed::_number_fractional_bits = 8; + +Fixed::Fixed() : _fixed_p_n(0) +{ + // std::cout << "Default constructor called\n"; +} + +Fixed::Fixed(const int input) +{ + // std::cout << "Fixed Int Constructor called" << std::endl; + this->_fixed_p_n = input << this->_number_fractional_bits; +} + +Fixed::Fixed(const float input) +{ + // std::cout << "Fixed Float Constructor called" << std::endl; + this->_fixed_p_n = roundf(input * (1 << this->_number_fractional_bits)); +} + +Fixed::Fixed(const Fixed ©) +{ + // std::cout << "Copy constructor called\n"; + *this = copy; +} + +Fixed &Fixed::operator=(const Fixed ©) +{ + // std::cout << "Copy assignment constructor called\n"; + setRawBits(copy.getRawBits()); + return (*this); +} + +Fixed::~Fixed() +{ + // std::cout << "Deconstructor called\n"; +} + +int Fixed::getRawBits(void) const +{ + // std::cout << "getRawBits member function called\n"; + return (_fixed_p_n); +} + +void Fixed::setRawBits(int const raw) +{ + _fixed_p_n = raw; +} + +int Fixed::toInt(void)const +{ + return (this->_fixed_p_n>> this->_number_fractional_bits); +} + +float Fixed::toFloat(void)const +{ + return ((float)this->_fixed_p_n/ (float)(1 << this->_number_fractional_bits)); +} + +std::ostream &operator<<(std::ostream &o, Fixed const &fixed) +{ + o << fixed.toFloat(); + return (o); +} + +// Overloaded comparation Operators +bool Fixed::operator>(Fixed fixed) const +{ + return (this->toFloat() > fixed.toFloat()); +} + +bool Fixed::operator<(Fixed fixed) const +{ + return (this->toFloat() < fixed.toFloat()); +} + +bool Fixed::operator>=(Fixed fixed) const +{ + return (this->toFloat() >= fixed.toFloat()); +} + +bool Fixed::operator<=(Fixed fixed) const +{ + return (this->toFloat() <= fixed.toFloat()); +} + +bool Fixed::operator==(Fixed fixed) const +{ + return (this->toFloat() == fixed.toFloat()); +} + +bool Fixed::operator!=(Fixed fixed) const +{ + return (this->toFloat() != fixed.toFloat()); +} +///// + +// Overloaded Arithmetic Operators +float Fixed::operator+(Fixed fixed) const +{ + return (this->toFloat() + fixed.toFloat()); +} + +float Fixed::operator-(Fixed fixed) const +{ + return (this->toFloat() - fixed.toFloat()); +} + +float Fixed::operator*(Fixed fixed) const +{ + return (this->toFloat() * fixed.toFloat()); +} + +float Fixed::operator/(Fixed fixed) const +{ + return (this->toFloat() / fixed.toFloat()); +} +///// + +// Overloaded pre-increment Operators +Fixed Fixed::operator++() +{ + this->_fixed_p_n++; + return (*this); +} + +Fixed Fixed::operator--() +{ + this->_fixed_p_n--; + return (*this); +} + +Fixed Fixed::operator++(int) +{ + Fixed tmp = *this; + ++this->_fixed_p_n; + return (tmp); +} + +Fixed Fixed::operator--(int) +{ + Fixed tmp = *this; + --this->_fixed_p_n; + return (tmp); +} +////// + +Fixed &Fixed::min(Fixed &first, Fixed &second) +{ + if (first.toFloat() <= second.toFloat()) + return (first); + else + return (second); +} + +const Fixed &Fixed::min(const Fixed &first, const Fixed &second) +{ + if (first.toFloat() <= second.toFloat()) + return (first); + else + return (second); +} + +Fixed &Fixed::max(Fixed &first, Fixed &second) +{ + if (first.toFloat() >= second.toFloat()) + return (first); + else + return (second); +} + +const Fixed &Fixed::max(const Fixed &first, const Fixed &second) +{ + if (first.toFloat() >= second.toFloat()) + return (first); + else + return (second); +} diff --git a/ex02/Fixed.hpp b/ex02/Fixed.hpp new file mode 100644 index 0000000..a7b9054 --- /dev/null +++ b/ex02/Fixed.hpp @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:41 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:41:55 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP +# define FIXED_HPP + +#include +#include + + +class Fixed +{ + private: + int _fixed_p_n; + static const int _number_fractional_bits; + + public: + //const/dest + Fixed(); + Fixed(const int input); + Fixed(const float input); + Fixed(const Fixed ©); + ~Fixed(); + + // Overloaded Operators + Fixed &operator=(const Fixed &src); + + // Comparison Operators + bool operator>(Fixed fixed)const; + bool operator<(Fixed fixed)const; + bool operator>=(Fixed fixed)const; + bool operator<=(Fixed fixed)const; + bool operator==(Fixed fixed)const; + bool operator!=(Fixed fixed)const; + + // Arithmetic Operators + float operator+(Fixed fixed)const; + float operator-(Fixed fixed)const; + float operator*(Fixed fixed)const; + float operator/(Fixed fixed)const; + + // pre-increment Operators + Fixed operator++(); + Fixed operator--(); + // post-increment Operators + Fixed operator++(int); + Fixed operator--(int); + + + //pub method + float toFloat(void)const; + int toInt(void)const; + + //get/set + int getRawBits(void) const; + void setRawBits(int const raw); + + static Fixed &min(Fixed &first, Fixed &second); + static const Fixed &min(Fixed const &first, Fixed const &second); + static Fixed &max(Fixed &first, Fixed &second); + static const Fixed &max(Fixed const &first, const Fixed &second); + +}; + +std::ostream &operator<<(std::ostream &o, Fixed const &fixed); + +#endif diff --git a/ex02/Main.cpp b/ex02/Main.cpp new file mode 100644 index 0000000..f3b37e5 --- /dev/null +++ b/ex02/Main.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/02 12:25:45 by lfirmin #+# #+# */ +/* Updated: 2025/07/16 14:40:26 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "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; +} diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..e62ae14 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,52 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/07/01 13:54:05 by lfirmin #+# #+# # +# Updated: 2025/07/02 12:32:58 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +C++ = c++ +C++_FLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +INFILES = Main.cpp\ + Fixed.cpp\ + + +OBJFILES = $(addprefix $(OBJDIR)/, $(INFILES:.cpp=.o)) + +NAME = Fixed + +all: $(NAME) + +$(OBJDIR): + @echo "๐Ÿ“ Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "๐Ÿง  Compiling $< ..." + @$(C++) $(C++_FLAGS) -c $< -o $@ + @echo "โœ… $@ ready!" + +$(NAME): $(OBJFILES) + @echo "๐Ÿ”— Linking $(NAME) ..." + @$(C++) $(C++_FLAGS) $(OBJFILES) -o $(NAME) + @echo "๐ŸŽ‰ $(NAME) is ready !" + +clean: + @echo "๐Ÿงน Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "โœจ Objects cleaned!" + +fclean: clean + @echo "๐Ÿ—‘๏ธ Removing $(NAME)..." + @rm -f $(NAME) + @echo "๐Ÿ’€ Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file