TD5 finished.

This commit is contained in:
Yohan Boujon 2023-10-04 14:39:30 +02:00
parent 0b57df1fe5
commit 784e1f6ff0
3 changed files with 63 additions and 2 deletions

13
td/td5/include/matrice.h Normal file
View file

@ -0,0 +1,13 @@
#include "vector.h"
constexpr size_t MAX_MAT = 10;
class Matrice {
public:
Matrice();
~Matrice();
Vector<float>*& operator[](const int indice);
friend std::ostream& operator<<(std::ostream& stream, const Matrice& mat);
private:
Vector<float>* _vectors[MAX_MAT];
};

View file

@ -1,8 +1,8 @@
#include "../include/vector.h" #include "../include/vector.h"
#include "../include/smart_vector.h" #include "../include/smart_vector.h"
#include "../include/deque.h" #include "../include/deque.h"
// Joker : slice.h #include "../include/matrice.h"
// Joker : vector_iteraor.h
#include <iostream> #include <iostream>
int main(void) int main(void)
@ -10,5 +10,22 @@ int main(void)
Vector<float> floatVect(4,3.5f); Vector<float> floatVect(4,3.5f);
std::cout << floatVect * 9.0f << std::endl; std::cout << floatVect * 9.0f << std::endl;
std::cout << 9.0f * floatVect << std::endl; std::cout << 9.0f * floatVect << std::endl;
Matrice mat;
mat[0] = new Vector<float>(6,30.0f);
//mat[1] = &floatVect;
// This leads to pointer error when calling the ~Matrice() func.
mat[2] = new Smart_Vector<float>(7, 20.0f);
/*
auto multipleVect = new Vector<float>[2];
for(size_t i = 0; i< 2 ; i++)
multipleVect[i].PushBack(1.9);
mat[3] = multipleVect;
*/
// This leads to an error because each Vector should not be an array but only a pointer.
std::cout << mat << std::endl;
return 0; return 0;
} }

31
td/td5/src/matrice.cpp Normal file
View file

@ -0,0 +1,31 @@
#include "../include/matrice.h"
#include <iostream>
Matrice::Matrice()
{
for(size_t i=0; i<MAX_MAT; i++)
_vectors[i] = NULL;
}
Matrice::~Matrice()
{
for(size_t i=0; i<MAX_MAT; i++)
delete _vectors[i];
}
Vector<float>*& Matrice::operator[](const int indice)
{
if((indice > 10) || (indice < 0))
throw "A Matrix is composed of 10 arrays of vectors.";
return _vectors[indice];
}
std::ostream& operator<<(std::ostream& stream,const Matrice& mat)
{
for(size_t i=0; i<MAX_MAT; i++)
{
if(mat._vectors[i] != NULL)
stream << "Mat[" << i << "]: {" << *(mat._vectors[i]) << "}\t";
}
return stream;
}