add: ex02

This commit is contained in:
Camille Chauvet 2023-08-03 14:25:38 +02:00
parent 9fafccf65f
commit bf594f63b0
5 changed files with 145 additions and 25 deletions

View File

@ -1,25 +1,25 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
NAME = ex02
EXECUTABLE = fixed
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
all: $(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(CXX) $(CXXFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(NAME)
rm -fr $(EXECUTABLE)
re: fclean all

0
ex02/obj/.gitkeep Normal file
View File

View File

@ -10,14 +10,13 @@ Fixed::~Fixed()
Fixed::Fixed()
{
this->_value = 0;
std::cout << "Default constructor called" << std::endl;
}
Fixed::Fixed(const Fixed& src)
{
std::cout << "Copy constructor called" << std::endl;
this->_value = src.getRawBits();
this->_value = src._value;
}
Fixed::Fixed(const int& src)
@ -35,7 +34,7 @@ Fixed::Fixed(const float& src)
Fixed& Fixed::operator=(const Fixed& src)
{
std::cout << "Copy assignment operator called" << std::endl;
this->_value = src.getRawBits();
this->_value = src._value;
return (*this);
}
@ -66,3 +65,107 @@ std::ostream& operator<<(std::ostream &out, const Fixed& fixed)
out << (fixed.toFloat());
return (out);
}
Fixed& Fixed::operator++()
{
this->_value++;
return (*this);
}
Fixed& Fixed::operator--()
{
this->_value--;
return (*this);
}
Fixed Fixed::operator++(int)
{
Fixed tmp(*this);
this->_value++;
return (tmp);
}
Fixed Fixed::operator--(int)
{
Fixed tmp(*this);
this->_value--;
return (tmp);
}
bool Fixed::operator==(const Fixed& src)
{
return (this->_value == src._value);
}
bool Fixed::operator!=(const Fixed& src)
{
return (this->_value != src._value);
}
bool Fixed::operator>=(const Fixed& src)
{
return (this->_value >= src._value);
}
bool Fixed::operator<=(const Fixed& src)
{
return (this->_value <= src._value);
}
bool Fixed::operator>(const Fixed& src)
{
return (this->_value > src._value);
}
bool Fixed::operator<(const Fixed& src)
{
return (this->_value < src._value);
}
Fixed Fixed::operator*(const Fixed &src)
{
return Fixed(this->toFloat() * src.toFloat());
}
Fixed Fixed::operator/(const Fixed &src)
{
return Fixed(this->toFloat() / src.toFloat());
}
Fixed Fixed::operator+(const Fixed &src)
{
return Fixed(this->toFloat() + src.toFloat());
}
Fixed Fixed::operator-(const Fixed &src)
{
return Fixed(this->toFloat() - src.toFloat());
}
const Fixed& Fixed::max(const Fixed& f1, const Fixed &f2)
{
if (f1._value > f2._value)
return (f1);
return (f2);
}
Fixed& Fixed::max(Fixed& f1, Fixed &f2)
{
if (f1._value > f2._value)
return (f1);
return (f2);
}
const Fixed& Fixed::min(const Fixed& f1, const Fixed &f2)
{
if (f1._value > f2._value)
return (f1);
return (f2);
}
Fixed& Fixed::min(Fixed& f1, Fixed &f2)
{
if (f1._value > f2._value)
return (f1);
return (f2);
}

View File

@ -18,10 +18,32 @@ class Fixed
Fixed &operator=(const Fixed& src);
bool operator<(const Fixed& src);
bool operator>(const Fixed& src);
bool operator>=(const Fixed& src);
bool operator<=(const Fixed& src);
bool operator==(const Fixed& src);
bool operator!=(const Fixed& src);
Fixed operator-(const Fixed& src);
Fixed operator+(const Fixed& src);
Fixed operator*(const Fixed& src);
Fixed operator/(const Fixed& src);
Fixed &operator++();
Fixed &operator--();
Fixed operator++(int);
Fixed operator--(int);
int getRawBits( void ) const;
void setRawBits( int const raw );
float toFloat(void) const ;
int toInt(void) const ;
static const Fixed& max(const Fixed& f1, const Fixed& f2);
static Fixed& max(Fixed& f1, Fixed& f2);
static const Fixed& min(const Fixed& f1, const Fixed& f2);
static Fixed& min(Fixed& f1, Fixed& f2);
};
std::ostream& operator << (std::ostream &out, const Fixed& fixed);

View File

@ -2,19 +2,14 @@
#include <iostream>
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;
Fixed a(0);
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;
}