intra to gitea

This commit is contained in:
root 2025-11-15 22:09:12 +00:00
commit 5c05d04000
12 changed files with 720 additions and 0 deletions

49
ex00/Fixed.cpp Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy)
{
std::cout << "Copy constructor called\n";
*this = copy;
}
Fixed &Fixed::operator=(const Fixed &copy)
{
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;
}

36
ex00/Fixed.hpp Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
class Fixed
{
private:
int _fixed_p_n;
static const int _number_fractional_bits;
public:
Fixed();
Fixed(const Fixed &copy);
Fixed &operator=(const Fixed &copy);
~Fixed();
int getRawBits(void) const;
void setRawBits(int const raw);
};
#endif

29
ex00/Main.cpp Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/02 12:25:45 by lfirmin #+# #+# */
/* Updated: 2025/07/16 14:42:17 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#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;
}

52
ex00/Makefile Normal file
View File

@ -0,0 +1,52 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

77
ex01/Fixed.cpp Normal file
View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy)
{
std::cout << "Copy constructor called\n";
*this = copy;
}
Fixed &Fixed::operator=(const Fixed &copy)
{
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);
}

48
ex01/Fixed.hpp Normal file
View File

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#include <cmath>
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 &copy);
~Fixed();
//ol op
Fixed &operator=(const Fixed &copy);
//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

31
ex01/Main.cpp Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/02 12:25:45 by lfirmin #+# #+# */
/* Updated: 2025/07/16 14:40:41 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#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;
}

52
ex01/Makefile Normal file
View File

@ -0,0 +1,52 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

191
ex02/Fixed.cpp Normal file
View File

@ -0,0 +1,191 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy)
{
// std::cout << "Copy constructor called\n";
*this = copy;
}
Fixed &Fixed::operator=(const Fixed &copy)
{
// 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);
}

76
ex02/Fixed.hpp Normal file
View File

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#include <cmath>
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 &copy);
~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

27
ex02/Main.cpp Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/02 12:25:45 by lfirmin #+# #+# */
/* Updated: 2025/07/16 14:40:26 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#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;
}

52
ex02/Makefile Normal file
View File

@ -0,0 +1,52 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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