TP 1, Exercice 2 (version longue)
This commit is contained in:
parent
7db3257e4d
commit
81982350e8
3 changed files with 147 additions and 3 deletions
121
tp/include/file.h
Normal file
121
tp/include/file.h
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
#ifndef HEADER_FILE
|
||||||
|
#define HEADER_FILE
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class File;
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class Cellule {
|
||||||
|
public:
|
||||||
|
friend File<T>;
|
||||||
|
Cellule(T value)
|
||||||
|
: _value(value)
|
||||||
|
, _next(nullptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Cellule(T value, Cellule* next)
|
||||||
|
: _value(value)
|
||||||
|
, _next(next)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
T _value;
|
||||||
|
Cellule* _next;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class Vide : Cellule<T> {
|
||||||
|
public:
|
||||||
|
friend File<T>;
|
||||||
|
Vide()
|
||||||
|
: Cellule<T>(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class File {
|
||||||
|
public:
|
||||||
|
File()
|
||||||
|
: _size(0)
|
||||||
|
, _front(nullptr)
|
||||||
|
, _end(nullptr)
|
||||||
|
, _vide() {};
|
||||||
|
~File()
|
||||||
|
{
|
||||||
|
auto next = _front;
|
||||||
|
auto front = _front;
|
||||||
|
|
||||||
|
while (next != nullptr) {
|
||||||
|
next = front->_next;
|
||||||
|
delete front;
|
||||||
|
front = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size_t getSize() { return _size; }
|
||||||
|
bool isEmpty() { return _size < 1; }
|
||||||
|
void push_back(T value)
|
||||||
|
{
|
||||||
|
auto newCell = new Cellule<T>(value, nullptr);
|
||||||
|
if (_front == nullptr && _end == nullptr) {
|
||||||
|
_front = newCell;
|
||||||
|
_end = newCell;
|
||||||
|
} else {
|
||||||
|
_end->_next = newCell;
|
||||||
|
_end = newCell;
|
||||||
|
}
|
||||||
|
_size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& operator[](size_t index)
|
||||||
|
{
|
||||||
|
auto cell = getCell(index);
|
||||||
|
if (cell.has_value())
|
||||||
|
return cell.value()->_value;
|
||||||
|
else
|
||||||
|
return _vide._value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(size_t index)
|
||||||
|
{
|
||||||
|
auto actualCell = getCell(index);
|
||||||
|
if (!actualCell.has_value())
|
||||||
|
throw std::string("Cannot remove from an index superior to the File size.");
|
||||||
|
// if not _front -> get previous index and set next to the next of this removed cell
|
||||||
|
if (index > 0)
|
||||||
|
getCell(index - 1).value()->_next = actualCell.value()->_next;
|
||||||
|
// else, the front becomes this cell
|
||||||
|
else
|
||||||
|
_front = actualCell.value()->_next;
|
||||||
|
// deleting the cell and decrementing the size
|
||||||
|
delete actualCell.value();
|
||||||
|
_size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& getFront() { return _front->_value; }
|
||||||
|
T& getEnd() { return _front->_value; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::optional<Cellule<T>*> getCell(size_t index)
|
||||||
|
{
|
||||||
|
auto returnCell = _front;
|
||||||
|
if (index >= _size)
|
||||||
|
return {};
|
||||||
|
for (size_t i = 0; i < index; i++) {
|
||||||
|
returnCell = returnCell->_next;
|
||||||
|
}
|
||||||
|
return returnCell;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t _size;
|
||||||
|
Cellule<T>* _front;
|
||||||
|
Cellule<T>* _end;
|
||||||
|
Vide<T> _vide;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // HEADER_FILE
|
|
@ -1,13 +1,37 @@
|
||||||
|
#include "../include/file.h"
|
||||||
#include "../include/polygone.h"
|
#include "../include/polygone.h"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Fonction de test utilisées pour les question 1 à 5
|
* @brief Fonction de test utilisées pour les question 1 à 5
|
||||||
*/
|
*/
|
||||||
void test();
|
void test();
|
||||||
|
/**
|
||||||
|
* @brief Fonction pour l'exo 1
|
||||||
|
*/
|
||||||
|
void exo1();
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
{
|
||||||
|
File<Polygone*> polyFile;
|
||||||
|
polyFile.push_back(new Carre(2));
|
||||||
|
polyFile.push_back(new Cercle(5, "rouge"));
|
||||||
|
polyFile.push_back(new TriangleEquilateral(3));
|
||||||
|
for (size_t i = 0; i < polyFile.getSize(); i++) {
|
||||||
|
std::cout << polyFile[i]->afficherCaracteristiques() << "\n";
|
||||||
|
std::cout << "Perimetre: " << polyFile[i]->perimetre() << "\n";
|
||||||
|
// ! Expression with side effect (clang-tidy)
|
||||||
|
if (typeid(*polyFile[i]) == typeid(Cercle)) {
|
||||||
|
auto cercle = static_cast<Cercle*>(polyFile[i]);
|
||||||
|
std::cout << "Couleur du cercle: " << cercle->getCouleur() << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void exo1()
|
||||||
{
|
{
|
||||||
// Le polymorphisme ne fonctionne qu'avec des pointeurs
|
// Le polymorphisme ne fonctionne qu'avec des pointeurs
|
||||||
Polygone* polyTab[3] = { new Carre(2), new Cercle(5, "rouge"), new TriangleEquilateral(3) };
|
Polygone* polyTab[3] = { new Carre(2), new Cercle(5, "rouge"), new TriangleEquilateral(3) };
|
||||||
|
@ -23,8 +47,7 @@ int main(void)
|
||||||
// Suppression de chaque Polygone dans le tableau
|
// Suppression de chaque Polygone dans le tableau
|
||||||
while (Polygone::getPolyCount() > 0)
|
while (Polygone::getPolyCount() > 0)
|
||||||
delete polyTab[Polygone::getPolyCount() - 1];
|
delete polyTab[Polygone::getPolyCount() - 1];
|
||||||
// ! WARNING: Potential memory leak (je ne sais pas pourquoi...)
|
// ! Potential memory leak (clang-tidy)
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test()
|
void test()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "../include/polygone.h"
|
#include "../include/polygone.h"
|
||||||
#include "coloriable.h"
|
#include "../include/coloriable.h"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
Loading…
Add table
Reference in a new issue