fix: patching the ex01 of the module 02

This commit is contained in:
Raphael 2024-12-27 13:12:44 +01:00
parent 2d6b2ddb83
commit 95fb804997
5 changed files with 269 additions and 0 deletions

75
cpp02/ex01/Fixed.cpp Normal file
View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:52:44 by rparodi #+# #+# */
/* Updated: 2024/12/27 13:06:30 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <cmath>
#include <iostream>
Fixed::Fixed() {
std::cout << "Default constructor called" << std::endl;
_fixed_value = 0;
_floating_value = 0;
_integer_value = 0;
}
Fixed::Fixed(const Fixed &copy) {
std::cout << "Copy constructor called" << std::endl;
this->_fixed_value = copy._fixed_value;
this->_integer_value = copy._integer_value;
this->_floating_value = copy._floating_value;
}
Fixed::Fixed(const int &integer) {
this->_integer_value = integer;
this->_floating_value = integer;
}
Fixed::Fixed(const float &floating) {
this->_integer_value = roundf(floating);
this->_floating_value = floating;
}
Fixed::~Fixed() {
std::cout << "Destructor called" << std::endl;
}
Fixed& Fixed::operator=(const Fixed &value){
std::cout << "Copy assignment operator called" << std::endl;
this->_fixed_value = value._fixed_value;
this->_integer_value = value._integer_value;
this->_floating_value = value._floating_value;
return *this;
}
std::ostream& operator<<(std::ostream &output, const Fixed &value) {
output << value.toFloat();
return (output);
}
void Fixed::setRawBits(const int value) {
std::cout << "setRawBits member function called" << std::endl;
this->_fixed_value = value;
}
int Fixed::toInt() const {
return (std::roundf(this->_floating_value));
}
float Fixed::toFloat() const {
return (this->_floating_value);
}
int Fixed::getRawBits() {
std::cout << "getRawBits member function called" << std::endl;
return this->_fixed_value;
}

39
cpp02/ex01/Fixed.hpp Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/21 19:30:23 by rparodi #+# #+# */
/* Updated: 2024/12/23 17:37:34 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();
void setRawBits(const int value);
int toInt() const;
float toFloat() const;
Fixed & operator = (const Fixed &value);
friend std::ostream& operator<< (std::ostream &output, const Fixed &value);
private:
int _integer_value;
float _floating_value;
int _fixed_value;
static const int _fractionnal = 8;
};
std::ostream & operator << (std::ostream& output, const Fixed &value);
#endif

124
cpp02/ex01/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}

30
cpp02/ex01/main.cpp Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 14:41:58 by rparodi #+# #+# */
/* Updated: 2024/12/27 13:11:37 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#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;
}