diff --git a/cpp02/ex01/Fixed.cpp b/cpp02/ex01/Fixed.cpp index f150527..481a676 100644 --- a/cpp02/ex01/Fixed.cpp +++ b/cpp02/ex01/Fixed.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/21 19:52:44 by rparodi #+# #+# */ -/* Updated: 2024/12/27 13:06:30 by rparodi ### ########.fr */ +/* Updated: 2024/12/27 13:42:55 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,13 +29,11 @@ Fixed::Fixed(const Fixed ©) { } Fixed::Fixed(const int &integer) { - this->_integer_value = integer; - this->_floating_value = integer; + this->_fixed_value = integer << _fractionnal; } Fixed::Fixed(const float &floating) { - this->_integer_value = roundf(floating); - this->_floating_value = floating; + this->_fixed_value = (int)(roundf(floating * (1 << this->_fractionnal))); } Fixed::~Fixed() { @@ -62,14 +60,14 @@ void Fixed::setRawBits(const int value) { } int Fixed::toInt() const { - return (std::roundf(this->_floating_value)); + return ((int)this->_fixed_value >> this->_fractionnal); } float Fixed::toFloat() const { - return (this->_floating_value); + return ((float)this->_fixed_value / ( 1 << this->_fractionnal)); } -int Fixed::getRawBits() { +int Fixed::getRawBits() const { std::cout << "getRawBits member function called" << std::endl; return this->_fixed_value; } diff --git a/cpp02/ex01/Fixed.hpp b/cpp02/ex01/Fixed.hpp index 4394bac..41b86af 100644 --- a/cpp02/ex01/Fixed.hpp +++ b/cpp02/ex01/Fixed.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/21 19:30:23 by rparodi #+# #+# */ -/* Updated: 2024/12/23 17:37:34 by rparodi ### ########.fr */ +/* Updated: 2024/12/27 13:50:06 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,12 +22,11 @@ Fixed(const int &integer); Fixed(const float &floating); ~Fixed(); - int getRawBits(); + int getRawBits() const; 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; diff --git a/cpp02/ex01/main.cpp b/cpp02/ex01/main.cpp index 29cf789..441bd3f 100644 --- a/cpp02/ex01/main.cpp +++ b/cpp02/ex01/main.cpp @@ -6,25 +6,39 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 14:41:58 by rparodi #+# #+# */ -/* Updated: 2024/12/27 13:11:37 by rparodi ### ########.fr */ +/* Updated: 2024/12/27 14:01:23 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "Fixed.hpp" +#include int main( void ) { Fixed a; - Fixed const b( 10 ); + Fixed const b( -10 ); Fixed const c( 42.42f ); Fixed const d( b ); a = Fixed( 1234.4321f ); + std::cout << 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 << "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 << "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 << "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; } diff --git a/cpp02/ex02/Fixed.cpp b/cpp02/ex02/Fixed.cpp new file mode 100644 index 0000000..74f0b37 --- /dev/null +++ b/cpp02/ex02/Fixed.cpp @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/21 19:52:44 by rparodi #+# #+# */ +/* Updated: 2024/12/27 14:13:47 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" +#include +#include + +Fixed::Fixed() { + std::cout << "Default constructor called" << std::endl; + _fixed_value = 0; + _floating_value = 0; + _integer_value = 0; +} + +Fixed::Fixed(const Fixed ©) { + 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->_fixed_value = integer << _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=(const Fixed &value){ + std::cout << "Copy assignment operator called" << std::endl; + this->_fixed_value = value._fixed_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 ((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; +} +Fixed & Fixed::operator > (const Fixed &value) { + +} +Fixed & Fixed::operator < (const Fixed &value) { + +} + +Fixed & Fixed::operator >= (const Fixed &value) { + +} + +Fixed & Fixed::operator <= (const Fixed &value) { + +} + +Fixed & Fixed::operator == (const Fixed &value) { + +} + +Fixed & Fixed::operator != (const Fixed &value) { + +} + +Fixed & Fixed::operator + (const Fixed &value) { + +} + +Fixed & Fixed::operator - (const Fixed &value) { + +} + +Fixed & Fixed::operator / (const Fixed &value) { + +} + +Fixed & Fixed::operator * (const Fixed &value) { + +} + +fixed fixed::operator ++ () { + return (*this); +} + +Fixed Fixed::operator -- () { + this->_fixed_value++; + return (*this); +} + + +Fixed & Fixed::operator ++ () { + this->_fixed_value++; + return (*this); +} + +Fixed & Fixed::operator -- () { + this->_fixed_value--; + return (*this); +} diff --git a/cpp02/ex02/Fixed.hpp b/cpp02/ex02/Fixed.hpp new file mode 100644 index 0000000..2412ab2 --- /dev/null +++ b/cpp02/ex02/Fixed.hpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/21 19:30:23 by rparodi #+# #+# */ +/* Updated: 2024/12/27 14:13:47 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FIXED_HPP + #define FIXED_HPP + + #include + + class Fixed { + public: + Fixed(); + Fixed(const Fixed ©); + Fixed(const int &integer); + Fixed(const float &floating); + ~Fixed(); + int getRawBits() const; + void setRawBits(const int value); + int toInt() const; + float toFloat() const; + Fixed & operator = (const Fixed &value); + Fixed & operator > (const Fixed &value); + Fixed & operator < (const Fixed &value); + Fixed & operator >= (const Fixed &value); + Fixed & operator <= (const Fixed &value); + Fixed & operator == (const Fixed &value); + Fixed & operator != (const Fixed &value); + Fixed & operator + (const Fixed &value); + Fixed & operator - (const Fixed &value); + Fixed & operator / (const Fixed &value); + Fixed & operator * (const Fixed &value); + Fixed operator ++ (); + Fixed operator -- (); + Fixed & operator ++ (); + Fixed & operator -- (); + 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 diff --git a/cpp02/ex02/Makefile b/cpp02/ex02/Makefile new file mode 100644 index 0000000..37fb3f5 --- /dev/null +++ b/cpp02/ex02/Makefile @@ -0,0 +1,124 @@ +# **************************************************************************** # + +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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} diff --git a/cpp02/ex02/main.cpp b/cpp02/ex02/main.cpp new file mode 100644 index 0000000..d857d06 --- /dev/null +++ b/cpp02/ex02/main.cpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 14:41:58 by rparodi #+# #+# */ +/* Updated: 2024/12/27 13:33:43 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" +#include + +int main( void ) { + Fixed a; + Fixed const b( 10 ); + Fixed const c( 42.42f ); + Fixed const d( b ); + + a = Fixed( 1234.4321f ); + std::cout << 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 << "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 << "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 << "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; +}