This commit is contained in:
Camille Chauvet 2023-08-03 14:01:36 +02:00
commit 0008b7624a
18 changed files with 379 additions and 0 deletions

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++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
EXECUTABLE = fixed
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(EXECUTABLE)
re: fclean all

0
ex00/obj/.gitkeep Normal file
View File

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

@ -0,0 +1,37 @@
#include "Fixed.hpp"
#include <iostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
Fixed::Fixed()
{
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;
}

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

@ -0,0 +1,16 @@
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;
}

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++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
EXECUTABLE = fixed
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(EXECUTABLE)
re: fclean all

0
ex01/obj/.gitkeep Normal file
View File

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

@ -0,0 +1,67 @@
#include "Fixed.hpp"
#include <iostream>
#include <cmath>
#include <ostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
Fixed::Fixed()
{
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 << "Copy constructor called" << std::endl;
this->_value = src << this->_nb_bits;
}
Fixed::Fixed(const float& src)
{
std::cout << "Copy 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);
}

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

@ -0,0 +1,29 @@
#ifndef FIXED_H
# define FIXED_H
# 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);
#endif

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;
}

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++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
EXECUTABLE = fixed
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(EXECUTABLE)
re: fclean all

0
ex02/obj/.gitkeep Normal file
View File

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

@ -0,0 +1,67 @@
#include "Fixed.hpp"
#include <iostream>
#include <cmath>
#include <ostream>
Fixed::~Fixed()
{
std::cout << "Destructor called" << std::endl;
}
Fixed::Fixed()
{
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 << "Copy constructor called" << std::endl;
this->_value = src << this->_nb_bits;
}
Fixed::Fixed(const float& src)
{
std::cout << "Copy 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);
}

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

@ -0,0 +1,29 @@
#ifndef FIXED_H
# define FIXED_H
# 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);
#endif

20
ex02/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;
}