Compare commits

...

No commits in common. "main" and "master" have entirely different histories.
main ... master

19 changed files with 538 additions and 14 deletions

11
.gitmodules vendored
View File

@ -1,11 +0,0 @@
[submodule "ex00"]
path = ex00
url = git@git.chauvet.pro:starnakin/42_CPP02_00.git
[submodule "ex01"]
path = ex01
url = git@git.chauvet.pro:starnakin/42_CPP02_01.git
[submodule "ex02/"]
url = git@git.chauvet.pro:starnakin/42_CPP02_02.git
[submodule "ex02"]
path = ex02
url = git@git.chauvet.pro:starnakin/42_CPP02_02.git

1
ex00

@ -1 +0,0 @@
Subproject commit 16ceb3d4004350d28de60e0c0b75250fc146debb

2
ex00/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
fixed

25
ex00/Makefile Normal file
View File

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

38
ex00/src/Fixed.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "Fixed.hpp"
#include <iostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
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();
}
Fixed& Fixed::operator=(Fixed& src)
{
std::cout << "Copy assignment operator called" << std::endl;
this->_value = src.getRawBits();
return (*this);
}
int Fixed::getRawBits() const
{
std::cout << "getRawBits member function called" << std::endl;
return (this->_value);
}
void Fixed::setRawBits(const int raw)
{
std::cout << "setRawBits member function called" << std::endl;
this->_value = raw;
}

18
ex00/src/Fixed.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
class Fixed
{
private:
int _value;
static const int _nb_bits = 8;
public:
Fixed();
Fixed(const Fixed& src);
~Fixed();
Fixed &operator=(Fixed& src);
int getRawBits( void ) const;
void setRawBits( int const raw );
};

13
ex00/src/main.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "Fixed.hpp"
#include <iostream>
int main( void ) {
Fixed a;
Fixed b( a );
Fixed c;
c = b;
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
return 0;
}

1
ex01

@ -1 +0,0 @@
Subproject commit 38c07a571b3088f9578003847751a73a36073acf

2
ex01/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
fixed

25
ex01/Makefile Normal file
View File

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

68
ex01/src/Fixed.cpp Normal file
View File

@ -0,0 +1,68 @@
#include "Fixed.hpp"
#include <iostream>
#include <cmath>
#include <ostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
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();
}
Fixed::Fixed(const int& src)
{
std::cout << "Int constructor called" << std::endl;
this->_value = src << this->_nb_bits;
}
Fixed::Fixed(const float& src)
{
std::cout << "Float constructor called" << std::endl;
this->_value = (int) roundf(src * (1 << this->_nb_bits));
}
Fixed& Fixed::operator=(const Fixed& src)
{
std::cout << "Copy assignment operator called" << std::endl;
this->_value = src.getRawBits();
return (*this);
}
int Fixed::getRawBits() const
{
std::cout << "getRawBits member function called" << std::endl;
return (this->_value);
}
void Fixed::setRawBits(const int raw)
{
std::cout << "setRawBits member function called" << std::endl;
this->_value = raw;
}
float Fixed::toFloat(void) const
{
return ((float) this->_value / (1 << this->_nb_bits));
}
int Fixed::toInt() const
{
return ((int) (roundf(this->toFloat())));
}
std::ostream& operator<<(std::ostream &out, const Fixed& fixed)
{
out << (fixed.toFloat());
return (out);
}

26
ex01/src/Fixed.hpp Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <ostream>
class Fixed
{
private:
int _value;
static const int _nb_bits = 8;
public:
Fixed();
Fixed(const Fixed& src);
Fixed(const int &n);
Fixed(const float &f);
~Fixed();
Fixed &operator=(const Fixed& src);
int getRawBits( void ) const;
void setRawBits( int const raw );
float toFloat(void) const ;
int toInt(void) const ;
};
std::ostream& operator << (std::ostream &out, const Fixed& fixed);

20
ex01/src/main.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "Fixed.hpp"
#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;
return 0;
}

1
ex02

@ -1 +0,0 @@
Subproject commit 38c07a571b3088f9578003847751a73a36073acf

2
ex02/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
fixed

25
ex02/Makefile Normal file
View File

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

172
ex02/src/Fixed.cpp Normal file
View File

@ -0,0 +1,172 @@
#include "Fixed.hpp"
#include <iostream>
#include <cmath>
#include <ostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
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._value;
}
Fixed::Fixed(const int& src)
{
std::cout << "Int constructor called" << std::endl;
this->_value = src << this->_nb_bits;
}
Fixed::Fixed(const float& src)
{
std::cout << "Float constructor called" << std::endl;
this->_value = (int) roundf(src * (1 << this->_nb_bits));
}
Fixed& Fixed::operator=(const Fixed& src)
{
std::cout << "Copy assignment operator called" << std::endl;
this->_value = src._value;
return (*this);
}
int Fixed::getRawBits() const
{
std::cout << "getRawBits member function called" << std::endl;
return (this->_value);
}
void Fixed::setRawBits(const int raw)
{
std::cout << "setRawBits member function called" << std::endl;
this->_value = raw;
}
float Fixed::toFloat(void) const
{
return ((float) this->_value / (1 << this->_nb_bits));
}
int Fixed::toInt() const
{
return ((int) (roundf(this->toFloat())));
}
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) const
{
return (this->_value == src._value);
}
bool Fixed::operator!=(const Fixed& src) const
{
return (this->_value != src._value);
}
bool Fixed::operator>=(const Fixed& src) const
{
return (this->_value >= src._value);
}
bool Fixed::operator<=(const Fixed& src) const
{
return (this->_value <= src._value);
}
bool Fixed::operator>(const Fixed& src) const
{
return (this->_value > src._value);
}
bool Fixed::operator<(const Fixed& src) const
{
return (this->_value < src._value);
}
Fixed Fixed::operator*(const Fixed &src) const
{
return Fixed(this->toFloat() * src.toFloat());
}
Fixed Fixed::operator/(const Fixed &src) const
{
return Fixed(this->toFloat() / src.toFloat());
}
Fixed Fixed::operator+(const Fixed &src) const
{
return Fixed(this->toFloat() + src.toFloat());
}
Fixed Fixed::operator-(const Fixed &src) const
{
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);
}

48
ex02/src/Fixed.hpp Normal file
View File

@ -0,0 +1,48 @@
#pragma once
# include <ostream>
class Fixed
{
private:
int _value;
static const int _nb_bits = 8;
public:
Fixed();
Fixed(const Fixed& src);
Fixed(const int &n);
Fixed(const float &f);
~Fixed();
Fixed &operator=(const Fixed& src);
bool operator<(const Fixed& src) const;
bool operator>(const Fixed& src) const;
bool operator>=(const Fixed& src) const;
bool operator<=(const Fixed& src) const;
bool operator==(const Fixed& src) const;
bool operator!=(const Fixed& src) const;
Fixed operator-(const Fixed& src) const;
Fixed operator+(const Fixed& src) const;
Fixed operator*(const Fixed& src) const;
Fixed operator/(const Fixed& src) const;
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);

54
ex02/src/main.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "Fixed.hpp"
#include <iostream>
int main( void ) {
{
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;
}
{
const Fixed a(2.5f);
const Fixed b(1.3f);
std::cout << "b: " << b << std::endl;
std::cout << "a: " << a << std::endl;
Fixed sum = a + b;
std::cout << "Addition: " << sum << std::endl;
Fixed difference = a - b;
std::cout << "Subtraction: " << difference << std::endl;
Fixed product = a * b;
std::cout << "Multiplication: " << product << std::endl;
Fixed quotient = a / b;
std::cout << "Division: " << quotient << std::endl;
bool isEqual = (a == b);
std::cout << "Is a equal to b? " << std::boolalpha << isEqual << std::endl;
bool isNotEqual = (a != b);
std::cout << "Is a not equal to b? " << std::boolalpha << isNotEqual << std::endl;
bool isLessThan = (a < b);
std::cout << "Is a less than b? " << std::boolalpha << isLessThan << std::endl;
bool isGreaterThan = (a > b);
std::cout << "Is a greater than b? " << std::boolalpha << isGreaterThan << std::endl;
bool isLessThanOrEqual = (a <= b);
std::cout << "Is a less than or equal to b? " << std::boolalpha << isLessThanOrEqual << std::endl;
bool isGreaterThanOrEqual = (a >= b);
std::cout << "Is a greater than or equal to b? " << std::boolalpha << isGreaterThanOrEqual << std::endl;
}
return 0;
}