d02 ex00 ok

This commit is contained in:
Hugo LAMY
2022-02-10 13:57:07 +01:00
parent 57663f5285
commit c287ea7c97
4 changed files with 116 additions and 18 deletions

41
d02/ex00/Fixed.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include "Fixed.hpp"
// default/parametric constructor
Fixed::Fixed( void ) : _value( 0 ) {
std::cout << "Default constructor called" << '\n';
return;
}
// copy constructor
Fixed::Fixed( Fixed const & src ) {
std::cout << "Copy constructor called" << '\n';
*this = src;
return;
}
// destructor
Fixed::~Fixed( void ) {
std::cout << "Destructor called" << '\n';
return;
}
// assignement operator
Fixed & Fixed::operator=( Fixed const & rhs ) {
std::cout << "Copy assignment operator called" << '\n';
if ( this != &rhs )
this->_value = rhs.getRawBits();
return *this;
}
int Fixed::getRawBits( void ) const {
std::cout << "getRawBits member function called" << '\n';
return this->_value;
}
void Fixed::setRawBits( int const raw ) {
this->_value = raw;
}

View File

@@ -4,23 +4,30 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
class NameFile { class Fixed {
public: public:
Fixed( void ); // default/parametric constructor Fixed( void ); // default/parametric constructor
Fixed( Sample const & src ); // copy constructor Fixed( Fixed const & src ); // copy constructor
~Fixed( void ); // destructor ~Fixed( void ); // destructor
Fixed & operator=( Namefile const & rhs ); // assignement operator Fixed & operator=( Fixed const & rhs ); // assignement operator
int getRawBits( void ) const;
void setRawBits( int const raw );
private: private:
int _value;
static int const _frac = 8;
}; };
#endif #endif
// //
// /// DECIMALE VS BINARY //////////////////////////////////////
// //
// 11010101 // 11010101
// //
@@ -73,6 +80,8 @@ private:
// //
// //
// //
// /// FLOATS VS FIXED /////////////////////////////////////////
//
// 1.....................23 // 1.....................23
// 8......1 // 8......1
// seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning // seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning
@@ -80,25 +89,37 @@ private:
// //
// 2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2 // 2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2
// //
//
// -------------------------
//
// 5.75 // 5.75
//
// 5 : // 5 :
// 5 / 2 = 2 -> 1 // 5 / 2 = 2 -> 1
// 2 / 2 = 1 -> 0 // 2 / 2 = 1 -> 0
// 1 / 2 = 0 -> 1 // 1 / 2 = 0 -> 1
// 75 : // 75 :
// .75 *2^2 = 2,88 ~= 3 // .75 *2^2 = 2,88 ~= 3
// (*2^x parce que c le moyen dont fait "bouger" la virgule
// en base 2 (*10^x) en base 10)
// 3 / 2 = 1 -> 1 // 3 / 2 = 1 -> 1
// 1 / 2 = 0 -> 1 // 1 / 2 = 0 -> 1
// 101.11 = 2^2 + 2^0 + 2^-1 + 2^-2 // 101.11 = 2^2 + 2^0 + 2^-1 + 2^-2
// = 4 + 1 + 0.5 + 0.25 // = 4 + 1 + 0.5 + 0.25
// //
// -------------------------
//
// 0.875 // 0.875
//
// .875 * 2^3 = 7 -> 0.111 * 2^3 = 111.0 // .875 * 2^3 = 7 -> 0.111 * 2^3 = 111.0
// 7 / 2 = 3 -> 1 // 7 / 2 = 3 -> 1
// 3 / 2 = 1 -> 1 // 3 / 2 = 1 -> 1
// 1 / 1 = 0 -> 1 // 1 / 1 = 0 -> 1
// 0.111 // 0.111
// //
// -------------------------
//
// 0.1875
// -1 1875 // -1 1875
// 0.1875 = 1.875 * 10^-1 = 01 11101010011 // 0.1875 = 1.875 * 10^-1 = 01 11101010011
// 0.1875 = 0011 // 0.1875 = 0011
@@ -106,7 +127,10 @@ private:
// 0 + 0 + .125 + .0625 = .1875 // 0 + 0 + .125 + .0625 = .1875
// //
// //
// -------------------------
//
// -43.625 // -43.625
//
// -101011.101 // -101011.101
// fixed : // fixed :
// 1 101011 101 // 1 101011 101
@@ -119,7 +143,10 @@ private:
// .5 + .125 = .625 // .5 + .125 = .625
// //
// //
// -------------------------
//
// -53.5 --> -110101.1 // -53.5 --> -110101.1
//
// [ fixed : ] // [ fixed : ]
// 1 110101 1 // 1 110101 1
// - // -
@@ -144,9 +171,10 @@ private:
// ->1101011 // ->1101011
// //
// //
// // -------------------------
// //
// .85 // .85
//
// .85 * 2 = 1.7 [1] [.7] // .85 * 2 = 1.7 [1] [.7]
// .7 * 2 = 1.4 [1] [.4] // .7 * 2 = 1.4 [1] [.4]
// .4 * 2 = 0.8 [0] [.8] // .4 * 2 = 0.8 [0] [.8]
@@ -186,6 +214,10 @@ private:
// .8499755859375 // .8499755859375
// //
// //
// -------------------------
//
// .453125
//
// .453125 *2 = 0.90625 [o] [.90625] // .453125 *2 = 0.90625 [o] [.90625]
// .90625 *2 = 1.8125 [1] [.8125] // .90625 *2 = 1.8125 [1] [.8125]
// .8125 *2 = 1.625 [1] [.625] // .8125 *2 = 1.625 [1] [.625]

View File

@@ -4,7 +4,7 @@
# . value . or .cpp # # . value . or .cpp #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = zombie NAME = fixed
CC = clang++ CC = clang++
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
@@ -17,12 +17,10 @@ INCLUDES = -I$(D_HEADERS)
D_SRCS = . D_SRCS = .
SRCS = main.cpp \ SRCS = main.cpp \
Zombie.cpp \ Fixed.cpp
newZombie.cpp \
randomChump.cpp
D_HEADERS = . D_HEADERS = .
HEADERS = Zombie.hpp HEADERS = Fixed.hpp
D_OBJS = builds D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o) OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)

View File

@@ -1,9 +1,36 @@
#include "Fixed.hpp" #include "Fixed.hpp"
#include <iostream>
int main() { 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; return 0;
} }
// $> ./a.out
// Default constructor called
// Copy constructor called
// Copy assignment operator called // <-- This line may be missing depending on your implementation
// getRawBits member function called
// Default constructor called
// Copy assignment operator called
// getRawBits member function called
// getRawBits member function called
// 0
// getRawBits member function called
// 0
// getRawBits member function called
// 0
// Destructor called
// Destructor called
// Destructor called
// $>