1
0

」 feat(Ex02): finished

This commit is contained in:
2024-11-20 12:47:39 +01:00
parent 0d07c2b9b6
commit 05bc62995c
4 changed files with 298 additions and 0 deletions

152
ex02/Fixed.cpp Normal file
View File

@ -0,0 +1,152 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 21:14:55 by adjoly #+# #+# */
/* Updated: 2024/11/20 12:46:18 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <cmath>
#include <iostream>
Fixed::Fixed(void) : _number(0) {
std::cout << "Default constructor called" << std::endl;
}
Fixed::Fixed(const int nbr) {
std::cout << "Int constructor called" << std::endl;
_number = nbr << _fractBit;
}
Fixed::Fixed(const float nbr) {
std::cout << "Float constructor called" << std::endl;
_number = roundf(nbr * (1 << _fractBit));
}
Fixed::Fixed(const Fixed& cpy) {
std::cout << "Copy constructor called" << std::endl;
*this = cpy;
}
Fixed::~Fixed(void) {
std::cout << "Destructor called" << std::endl;
}
int Fixed::toInt(void) const {
return (_number >> _fractBit);
}
float Fixed::toFloat(void) const {
return ((float)_number / (1 << _fractBit));
}
int Fixed::getRawBits(void) const {
return (_number);
}
void Fixed::setRawBits(int const raw) {
_number = raw;
}
std::ostream &operator<<(std::ostream &file, Fixed const &fixed) {
file << fixed.toFloat();
return (file);
}
Fixed &Fixed::operator=(const Fixed& cpy) {
std::cout << "Copy assignment operator called" << std::endl;
if (this != &cpy)
_number = cpy.getRawBits();
return (*this);
}
float Fixed::operator+(const Fixed& cpy) {
return (this->toFloat() + cpy.toFloat());
}
float Fixed::operator-(const Fixed& cpy) {
return (this->toFloat() - cpy.toFloat());
}
float Fixed::operator*(const Fixed& cpy) {
return (this->toFloat() * cpy.toFloat());
}
float Fixed::operator/(const Fixed& cpy) {
return (this->toFloat() / cpy.toFloat());
}
Fixed& Fixed::operator++(void) {
_number++;
return (*this);
}
Fixed Fixed::operator++(int) {
Fixed tmp = *this;
_number++;
return (tmp);
}
Fixed& Fixed::operator--(void) {
_number--;
return (*this);
}
Fixed Fixed::operator--(int) {
Fixed tmp = *this;
_number--;
return (tmp);
}
bool Fixed::operator<(const Fixed& comp) const {
return (getRawBits() < comp.getRawBits());
}
bool Fixed::operator>(const Fixed& comp) const {
return (getRawBits() > comp.getRawBits());
}
bool Fixed::operator<=(const Fixed& comp) const {
return (getRawBits() <= comp.getRawBits());
}
bool Fixed::operator>=(const Fixed& comp) const {
return (getRawBits() >= comp.getRawBits());
}
bool Fixed::operator==(const Fixed& comp) const {
return (getRawBits() == comp.getRawBits());
}
bool Fixed::operator!=(const Fixed& comp) const {
return (getRawBits() != comp.getRawBits());
}
Fixed& max(Fixed& fst, Fixed &scnd) {
if (fst < scnd)
return (scnd);
return (fst);
}
Fixed& min(Fixed& fst, Fixed &scnd) {
if (fst > scnd)
return (scnd);
return (fst);
}
const Fixed& Fixed::max(const Fixed& fst, const Fixed &scnd) {
if (fst < scnd)
return (scnd);
return (fst);
}
const Fixed& min(const Fixed& fst, const Fixed &scnd) {
if (fst > scnd)
return (scnd);
return (fst);
}

60
ex02/Fixed.hpp Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 19:23:08 by adjoly #+# #+# */
/* Updated: 2024/11/20 12:40:10 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
class Fixed {
private:
int _number;
static const int _fractBit = 8;
public:
Fixed(void);
Fixed(const int nbr);
Fixed(const float nbr);
Fixed(const Fixed& cpy);
Fixed& operator=(const Fixed&);
float operator+(const Fixed&);
float operator-(const Fixed&);
float operator*(const Fixed&);
float operator/(const Fixed&);
bool operator<(const Fixed&) const;
bool operator>(const Fixed&) const;
bool operator<=(const Fixed&) const;
bool operator>=(const Fixed&) const;
bool operator==(const Fixed&) const;
bool operator!=(const Fixed&) const;
Fixed& operator--(void);
Fixed operator--(int);
Fixed& operator++(void);
Fixed operator++(int);
static Fixed& min(Fixed& fst, Fixed& scnd);
static Fixed& max(Fixed& fst, Fixed& scnd);
static const Fixed& min(const Fixed& fst, const Fixed& scnd);
static const Fixed& max(const Fixed& fst, const Fixed& scnd);
~Fixed(void);
int toInt(void) const;
float toFloat(void) const;
int getRawBits( void ) const;
void setRawBits( int const raw );
};
std::ostream &operator<<(std::ostream &file, Fixed const &fixed);

55
ex02/Makefile Normal file
View File

@ -0,0 +1,55 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2024/11/12 21:50:23 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = Fixed
CC = c++
OBJSDIR = obj/
SRCS = main.cpp \
Fixed.cpp
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

31
ex02/main.cpp Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 16:32:27 by adjoly #+# #+# */
/* Updated: 2024/11/19 16:40:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <iostream>
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;
}