feat: adding the ex02

This commit is contained in:
Raphael 2025-02-15 19:19:18 +01:00
parent edbcc22519
commit 921b3951d9
4 changed files with 465 additions and 0 deletions

143
cpp02/ex02/Fixed.cpp Normal file
View file

@ -0,0 +1,143 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:52:44 by rparodi #+# #+# */
/* Updated: 2025/02/15 19:16:27 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <cmath>
#include <iostream>
Fixed::Fixed() {
std::cout << "Default constructor called" << std::endl;
_fixed_value = 0;
}
Fixed::Fixed(const Fixed &copy) {
std::cout << "Copy constructor called" << std::endl;
this->_fixed_value = copy._fixed_value;
}
Fixed::Fixed(const int &integer) {
this->_fixed_value = integer << this->_fractionnal;
}
Fixed::Fixed(const float &floating) {
this->_fixed_value = (int)(roundf(floating * (1 << this->_fractionnal)));
}
Fixed::~Fixed() {
std::cout << "Destructor called" << std::endl;
}
Fixed& Fixed::operator=(Fixed const &value) {
this->_fixed_value = value._fixed_value;
return *this;
}
Fixed Fixed::operator+(Fixed const &other) const {
return Fixed(this->toFloat() + other.toFloat());
}
Fixed Fixed::operator-(Fixed const &other) const {
return Fixed(this->toFloat() - other.toFloat());
}
Fixed Fixed::operator*(Fixed const &other) const {
return Fixed(this->toFloat() * other.toFloat());
}
Fixed Fixed::operator/(Fixed const &other) const {
return Fixed(this->toFloat() / other.toFloat());
}
bool Fixed::operator==(Fixed const &other) const {
return this->_fixed_value == other._fixed_value ? true : false;
}
bool Fixed::operator!=(Fixed const &other) const {
return this->_fixed_value != other._fixed_value ? true : false;
}
bool Fixed::operator>(Fixed const &other) const {
return this->_fixed_value > other._fixed_value ? true : false;
}
bool Fixed::operator<(Fixed const &other) const {
return this->_fixed_value < other._fixed_value ? true : false;
}
bool Fixed::operator>=(Fixed const &other) const {
return this->_fixed_value < other._fixed_value ? false : true;
}
bool Fixed::operator<=(Fixed const &other) const {
return this->_fixed_value < other._fixed_value ? false : true;
}
Fixed & Fixed::operator++() {
this->_fixed_value += 1;
return *this;
}
Fixed Fixed::operator ++ (int) {
Fixed tmp(*this);
++(*this);
return tmp;
}
Fixed & Fixed::operator -- () {
this->_fixed_value -= 1;
return *this;
}
Fixed Fixed::operator -- (int) {
Fixed tmp(*this);
--(*this);
return tmp;
}
std::ostream& operator<<(std::ostream &output, const Fixed &value) {
output << value.toFloat();
return (output);
}
int Fixed::toInt() const {
return ((int)this->_fixed_value >> this->_fractionnal);
}
float Fixed::toFloat() const {
return ((float)this->_fixed_value / ( 1 << this->_fractionnal ));
}
int Fixed::getRawBits() const {
std::cout << "getRawBits member function called" << std::endl;
return this->_fixed_value;
}
void Fixed::setRawBits(const int value) {
std::cout << "setRawBits member function called" << std::endl;
this->_fixed_value = value;
}
Fixed & Fixed::min(Fixed &a, Fixed &b) {
return a < b ? a : b;
}
Fixed & Fixed::max(Fixed &a, Fixed &b) {
return a > b ? a : b;
}
const Fixed & Fixed::min(const Fixed &a, const Fixed &b) {
return a < b ? a : b;
}
const Fixed & Fixed::max(const Fixed &a, const Fixed &b) {
return a > b ? a : b;
}

95
cpp02/ex02/Fixed.hpp Normal file
View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:30:23 by rparodi #+# #+# */
/* Updated: 2025/02/15 19:14:59 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FIXED_HPP
#define FIXED_HPP
#include <iostream>
class Fixed {
public:
Fixed();
Fixed(const Fixed &copy);
Fixed(const int &integer);
Fixed(const float &floating);
~Fixed();
int getRawBits() const;
void setRawBits(const int value);
Fixed & operator = (const Fixed &value);
Fixed operator * (const Fixed &value) const;
Fixed operator + (const Fixed &value) const;
Fixed operator - (const Fixed &value) const;
Fixed operator / (const Fixed &value) const;
bool operator == (const Fixed &value) const;
bool operator != (const Fixed &value) const;
bool operator > (const Fixed &value) const;
bool operator < (const Fixed &value) const;
bool operator <= (const Fixed &value) const;
bool operator >= (const Fixed &value) const;
Fixed & operator ++ ();
Fixed operator ++ (int);
Fixed & operator -- ();
Fixed operator -- (int);
int toInt() const;
float toFloat() const;
static Fixed & min(Fixed &a, Fixed &b);
static Fixed & max(Fixed &a, Fixed &b);
static const Fixed & min(const Fixed &a, const Fixed &b);
static const Fixed & max(const Fixed &a, const Fixed &b);
private:
int _fixed_value;
static const int _fractionnal = 8;
};
std::ostream & operator << (std::ostream& output, const Fixed &value);
// Color (Just aesthetic)
#define CLR_RESET "\033[0m"
#define CLR_BLACK "\033[0;30m"
#define CLR_RED "\033[0;31m"
#define CLR_GREEN "\033[0;32m"
#define CLR_YELLOW "\033[0;33m"
#define CLR_BLUE "\033[0;34m"
#define CLR_MAGENTA "\033[0;35m"
#define CLR_CYAN "\033[0;36m"
#define CLR_WHITE "\033[0;37m"
#define CLR_GOLD "\033[38;5;220m"
#define CLR_GREY "\033[38;5;240m"
#define CLR_LIGHT_BLACK "\033[0;90m"
#define CLR_LIGHT_RED "\033[0;91m"
#define CLR_LIGHT_GREEN "\033[0;92m"
#define CLR_LIGHT_YELLOW "\033[0;93m"
#define CLR_LIGHT_BLUE "\033[0;94m"
#define CLR_LIGHT_MAGENTA "\033[0;95m"
#define CLR_LIGHT_CYAN "\033[0;96m"
#define CLR_LIGHT_WHITE "\033[0;97m"
#define CLR_BOLD_BLACK "\033[1;30m"
#define CLR_BOLD_RED "\033[1;31m"
#define CLR_BOLD_GREEN "\033[1;32m"
#define CLR_BOLD_YELLOW "\033[1;33m"
#define CLR_BOLD_BLUE "\033[1;34m"
#define CLR_BOLD_MAGENTA "\033[1;35m"
#define CLR_BOLD_CYAN "\033[1;36m"
#define CLR_BOLD_WHITE "\033[1;37m"
#endif

124
cpp02/ex02/Makefile Normal file
View file

@ -0,0 +1,124 @@
# **************************************************************************** #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/12/21 19:50:58 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = Fixed
# Commands
CXX = c++
RM = rm -rf
# Flags
# Mandatory flags for 42 cpp
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
# Flags to debug and have the dependences (can be removed for correction)
CXXFLAGS += -MMD -g3
# Flag to debug (TO REMOVE)
# CXXFLAGS += -D DEBUG=1
# Sources
SRC = Fixed.cpp \
main.cpp
# Objects
OBJDIRNAME = ./build
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
# Colors
GREEN = \033[32m
GREY = \033[0;90m
RED = \033[0;31m
GOLD = \033[38;5;220m
END = \033[0m
# Rules
# All (make all)
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) footer
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Clean (make clean)
clean:
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
@$(RM) $(OBJDIRNAME)
# Clean (make fclean)
fclean: clean
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
@$(RM) $(NAME)
@echo ""
# Restart (make re)
re: header fclean all
# Dependences for all
$(NAME): $(OBJ)
@mkdir -p $(OBJDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
# Creating the objects
$(OBJDIRNAME)/%.o: %.cpp
@mkdir -p $(dir $@)
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
@$(CXX) $(CXXFLAGS) -o $@ -c $<
# Header
header:
@clear
@printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n'
@printf '$(GOLD) ******* * ******* $(END)\n'
@printf '$(GOLD) ****** ******* $(END)\n'
@printf '$(GOLD) ******* ******* $(END)\n'
@printf '$(GOLD) ******************* ******* * $(END)\n'
@printf '$(GOLD) ******************* ******* *** $(END)\n'
@printf '$(GOLD) ****** ******* ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GREY) Made by rparodi$(END)\n\n'
# Footer
footer:
@printf "\n"
@printf "$(GOLD) ,_ _,$(END)\n"
@printf "$(GOLD) | \\___//|$(END)\n"
@printf "$(GOLD) |=6 6=|$(END)\n"
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
@printf "$(GOLD) ) \` ( ,$(END)\n"
@printf "$(GOLD) / \\ (('$(END)\n"
@printf "$(GOLD) | | ))$(END)\n"
@printf "$(GOLD) /| | | |\\_//$(END)\n"
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
@printf "$(GOLD) '\"' '\"'$(END)\n"
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
clangd:
@echo \
"CompileFlags:\n" \
" Add:\n" \
" - \"-std=c++98 -Wall -Wextra -Werror\"\n" \
" - \"-I"$(shell pwd)"/includes\"\n" \
" - \"-xc++\"\n" \
> .clangd
# Phony
.PHONY: all bonus clean fclean re clangd
-include ${OBJ:.o=.d}

103
cpp02/ex02/main.cpp Normal file
View file

@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 14:41:58 by rparodi #+# #+# */
/* Updated: 2025/02/15 18:57:30 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <ostream>
int main( void ) {
Fixed a;
Fixed const b( -42 );
Fixed const c( 42.42f );
Fixed const d( b );
a = Fixed( 42 );
std::cout << CLR_YELLOW << "\t[ Simple " << CLR_LIGHT_BLACK << "( overload '<<' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
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 << std::endl;
std::cout << CLR_YELLOW << "\t[ Integer " << CLR_LIGHT_BLACK << "( method toInt() )" << CLR_YELLOW << " ]" << CLR_RESET << 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;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Floating " << CLR_LIGHT_BLACK << "( method toFloat() )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a is " << a.toFloat() << " as float" << std::endl;
std::cout << "b is " << b.toFloat() << " as float" << std::endl;
std::cout << "c is " << c.toFloat() << " as float" << std::endl;
std::cout << "d is " << d.toFloat() << " as float" << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Bits " << CLR_LIGHT_BLACK << "( method getRawBits() )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a is " << a.getRawBits() << " as raw" << std::endl;
std::cout << "b is " << b.getRawBits() << " as raw" << std::endl;
std::cout << "c is " << c.getRawBits() << " as raw" << std::endl;
std::cout << "d is " << d.getRawBits() << " as raw" << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator + " << CLR_LIGHT_BLACK << "( Overload '+' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") + b (" << b.toFloat() << ") is " << (a + b).toFloat() << std::endl;
std::cout << "c (" << c.toFloat() << ") + d (" << d.toFloat() << ") is " << (c + d).toFloat() << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator - " << CLR_LIGHT_BLACK << "( Overload '-' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") - b (" << b.toFloat() << ") is " << (a - b).toFloat() << std::endl;
std::cout << "c (" << c.toFloat() << ") - d (" << d.toFloat() << ") is " << (c - d).toFloat() << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator * " << CLR_LIGHT_BLACK << "( Overload '*' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") * b (" << b.toFloat() << ") is " << (a * b).toFloat() << std::endl;
std::cout << "c (" << c.toFloat() << ") * d (" << d.toFloat() << ") is " << (c * d).toFloat() << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator / " << CLR_LIGHT_BLACK << "( Overload '/' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") / b (" << b.toFloat() << ") is " << (a / b).toFloat() << std::endl;
std::cout << "c (" << c.toFloat() << ") / d (" << d.toFloat() << ") is " << (c / d).toFloat() << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator == " << CLR_LIGHT_BLACK << "( Overload '==' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") == b (" << b.toFloat() << ") is " << (a == b ? "true" : "false") << std::endl;
std::cout << "c (" << c.toFloat() << ") == d (" << d.toFloat() << ") is " << (c == d ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator != " << CLR_LIGHT_BLACK << "( Overload '!=' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") != b (" << b.toFloat() << ") is " << (a != b ? "true" : "false") << std::endl;
std::cout << "c (" << c.toFloat() << ") != d (" << d.toFloat() << ") is " << (c != d ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator < " << CLR_LIGHT_BLACK << "( Overload '<' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") < b (" << b.toFloat() << ") is " << (a<b ? "true" : "false") << std::endl;
std::cout << "c (" << c.toFloat() << ") < d (" << d.toFloat() << ") is " << (c<d ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator > " << CLR_LIGHT_BLACK << "( Overload '>' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") > b (" << b.toFloat() << ") is " << (a>b ? "true" : "false") << std::endl;
std::cout << "c (" << c.toFloat() << ") > d (" << d.toFloat() << ") is " << (c>d ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator >= " << CLR_LIGHT_BLACK << "( Overload '>=' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") >= b (" << b.toFloat() << ") is " << (a>=b ? "true" : "false") << std::endl;
std::cout << "c (" << c.toFloat() << ") >= d (" << d.toFloat() << ") is " << (c>=d ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Operator <= " << CLR_LIGHT_BLACK << "( Overload '<=' )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
std::cout << "a (" << a.toFloat() << ") <= b (" << b.toFloat() << ") is " << (a<=b ? "true" : "false") << std::endl;
std::cout << "c (" << c.toFloat() << ") <= d (" << d.toFloat() << ") is " << (c<=d ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << CLR_YELLOW << "\t[ Destructor " << CLR_LIGHT_BLACK << "( ~Fixed() )" << CLR_YELLOW << " ]" << CLR_RESET << std::endl;
}