From 81982350e8e37fdd88c75db65f7924abd551831e Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Wed, 6 Dec 2023 19:42:54 +0100 Subject: [PATCH] TP 1, Exercice 2 (version longue) --- tp/include/file.h | 121 ++++++++++++++++++++++++++++++++++++++++++++ tp/src/main.cpp | 27 +++++++++- tp/src/polygone.cpp | 2 +- 3 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 tp/include/file.h diff --git a/tp/include/file.h b/tp/include/file.h new file mode 100644 index 0000000..cb7eab0 --- /dev/null +++ b/tp/include/file.h @@ -0,0 +1,121 @@ +#ifndef HEADER_FILE +#define HEADER_FILE + +#include +#include +#include + +template +class File; + +template +class Cellule { +public: + friend File; + Cellule(T value) + : _value(value) + , _next(nullptr) + { + } + Cellule(T value, Cellule* next) + : _value(value) + , _next(next) + { + } + +private: + T _value; + Cellule* _next; +}; + +template +class Vide : Cellule { +public: + friend File; + Vide() + : Cellule(0) + { + } +}; + +template +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(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*> 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* _front; + Cellule* _end; + Vide _vide; +}; + +#endif // HEADER_FILE \ No newline at end of file diff --git a/tp/src/main.cpp b/tp/src/main.cpp index ef30535..fa4e450 100644 --- a/tp/src/main.cpp +++ b/tp/src/main.cpp @@ -1,13 +1,37 @@ +#include "../include/file.h" #include "../include/polygone.h" #include #include +#include /** * @brief Fonction de test utilisées pour les question 1 à 5 */ void test(); +/** + * @brief Fonction pour l'exo 1 + */ +void exo1(); int main(void) +{ + File 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(polyFile[i]); + std::cout << "Couleur du cercle: " << cercle->getCouleur() << std::endl; + } + std::cout << "\n"; + } +} + +void exo1() { // Le polymorphisme ne fonctionne qu'avec des pointeurs 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 while (Polygone::getPolyCount() > 0) delete polyTab[Polygone::getPolyCount() - 1]; - // ! WARNING: Potential memory leak (je ne sais pas pourquoi...) - return 0; + // ! Potential memory leak (clang-tidy) } void test() diff --git a/tp/src/polygone.cpp b/tp/src/polygone.cpp index e8460a4..d70d97f 100644 --- a/tp/src/polygone.cpp +++ b/tp/src/polygone.cpp @@ -1,5 +1,5 @@ #include "../include/polygone.h" -#include "coloriable.h" +#include "../include/coloriable.h" #include #include