From c4927de1eb1c8b6fab19caa2153e08c8ca4b6740 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 27 Oct 2023 23:27:41 +0200 Subject: [PATCH] ajout des taches --- TODO.md | 6 +++--- includes/Projet.hpp | 14 ++++++++---- includes/ProtoProjet.hpp | 2 +- includes/Tache.hpp | 13 ++++-------- src/Consultant.cpp | 2 +- src/Expert.cpp | 2 +- src/Projet.cpp | 36 +++++++++++++++++++++++++------ src/ProtoProjet.cpp | 46 ++++++++++++++++++++++++++++++++++++---- src/Tache.cpp | 6 +++--- 9 files changed, 94 insertions(+), 33 deletions(-) diff --git a/TODO.md b/TODO.md index 8a12115..fbc81a6 100644 --- a/TODO.md +++ b/TODO.md @@ -40,12 +40,12 @@ TODO avant rendu : - [x] Tâche `fin` - Méthodes (cf. le PDF du prof) **⇒ Tout ça avec l'ordre topologique** - Pas de méthode d'ajout d'un objet `Tache` - - [ ] `bool ajoute(nom, durée)` : sélectionne au hasard 2 tâches déjà + - [x] `bool ajoute(nom, durée)` : sélectionne au hasard 2 tâches déjà enregistrer et **ajoute** la nouvelle tâche entres-elles - - [ ] `bool ajoute(nom, durée, id)` : **déplace** une tâche qui doit se réaliser + - [x] `bool ajoute(nom, durée, id)` : **déplace** une tâche qui doit se réaliser **après** la tâche qui à l'`id` correspondant (et avant la tâche finale) **⇒ création d'une dépendance** - - [ ] `bool ajoute(nom, durée, id1, id2)` : **ajoute** une tâche entre les 2 tâches + - [x] `bool ajoute(nom, durée, id1, id2)` : **ajoute** une tâche entre les 2 tâches qui ont l'identifiant `id1` et `id2` - [ ] Surcharge de `<<` diff --git a/includes/Projet.hpp b/includes/Projet.hpp index e8b326f..41f0d76 100644 --- a/includes/Projet.hpp +++ b/includes/Projet.hpp @@ -9,13 +9,19 @@ class Projet { friend std::ostream &operator<<(std::ostream &, const Projet &); + // Auxilliaire pour simplifier les copies + void _copy(const Projet &); + + // Auxiliiaire pour simplifier la libération de mémoire des tâches + void _free(); + protected: Tache // Première tâche qui sera exécutée - debut, + *debut, // Source du graphe aka la dernière à être exécutée - fin; + *fin; Projet(); // constructor virtual ~Projet(); // destructor @@ -30,8 +36,8 @@ protected: const std::pair pick_two_random_tasks() const; // Indique pour une tâche si elle fait partie du projet - const Tache *contains(const int id) const; - const Tache *contains(const std::string name) const; + Tache *contains(const int id) const; + Tache *contains(const std::string name) const; // Donne une version du vecteur de tâches non modifiable const std::vector consult_tasks() const; diff --git a/includes/ProtoProjet.hpp b/includes/ProtoProjet.hpp index 476b8dc..aff697e 100644 --- a/includes/ProtoProjet.hpp +++ b/includes/ProtoProjet.hpp @@ -15,7 +15,7 @@ public: const ProtoProjet &operator=(const ProtoProjet &); // copy assignement // Remet tous les marquages à leur valeur initiale - void cleanMarks(); + void cleanMarks() const; // Insère une nouvelle tâche en la plaçant entre deux tâches au hasard bool ajoute(const std::string nom, const int duree); diff --git a/includes/Tache.hpp b/includes/Tache.hpp index a220bdd..f2dd9db 100644 --- a/includes/Tache.hpp +++ b/includes/Tache.hpp @@ -4,8 +4,6 @@ #include #include -struct Projet; - class Tache final { // Compteur global pour les IDs static int counter_id; @@ -14,9 +12,6 @@ class Tache final { enum Etat { EnAttente, Realisee }; friend std::ostream &operator<<(std::ostream &, const Etat &); - // ID unique de la tâche - const int unique_id; - // Durée totale pour faire la tâche int duree_total; // Etat actuelle de la tâche @@ -24,16 +19,16 @@ class Tache final { // Liste des dépendances std::vector dependances; - // Nom de la tâche - std::string name; - friend std::ostream &operator<<(std::ostream &, const Tache &); - friend Projet; // Auxilliaire pour simplifier les copies void _copy(const Tache &); public: + // ID unique de la tâche + const int unique_id; + // Nom de la tâche + std::string name; // Vrai si la tâche à été visitée par le parcours en profondeur bool visite; diff --git a/src/Consultant.cpp b/src/Consultant.cpp index b5ff8b5..dafe6cf 100644 --- a/src/Consultant.cpp +++ b/src/Consultant.cpp @@ -4,7 +4,7 @@ Consultant::Consultant() { std::cout << "Hello, Consultant!\n"; } Consultant::~Consultant() {} -Consultant::Consultant(const Consultant &) {} +Consultant::Consultant(const Consultant &) : Gestionnaire() {} const Consultant &Consultant::operator=(const Consultant &src) { if (this == &src) { diff --git a/src/Expert.cpp b/src/Expert.cpp index 40ba729..a9fa5d1 100644 --- a/src/Expert.cpp +++ b/src/Expert.cpp @@ -4,7 +4,7 @@ Expert::Expert() { std::cout << "Hello, Expert!\n"; } Expert::~Expert() {} -Expert::Expert(const Expert &) {} +Expert::Expert(const Expert &) : Gestionnaire() {} const Expert &Expert::operator=(const Expert &src) { if (this == &src) { diff --git a/src/Projet.cpp b/src/Projet.cpp index e33ffb0..6e132b1 100644 --- a/src/Projet.cpp +++ b/src/Projet.cpp @@ -1,18 +1,40 @@ #include "../includes/Projet.hpp" -Projet::Projet() : debut(Tache("Début", 0)), fin(Tache("Fin", 0)) { - fin.depends_from(debut); +Projet::Projet() : debut(new Tache("Début", 0)), fin(new Tache("Fin", 0)) { + fin->depends_from(*debut); + + taches.push_back(fin); + taches.push_back(debut); } -Projet::~Projet() {} +Projet::~Projet() { _free(); } -Projet::Projet(const Projet &src) : debut(src.debut), fin(src.fin) {} +void Projet::_copy(const Projet &src) { + // Copie des tâches + taches.reserve(src.taches.size()); + for (Tache *t : src.taches) { + taches.push_back(new Tache(*t)); + } +} + +void Projet::_free() { + for (Tache *it : taches) { + delete it; + } +} + +Projet::Projet(const Projet &src) : debut(src.debut), fin(src.fin) { + _copy(src); +} const Projet &Projet::operator=(const Projet &src) { if (this == &src) { return *this; } - + debut = src.debut; + fin = src.fin; + _free(); + _copy(src); return *this; } @@ -44,7 +66,7 @@ const std::pair Projet::pick_two_random_tasks() const { return std::make_pair(id1, id2); } -const Tache *Projet::contains(const int id) const { +Tache *Projet::contains(const int id) const { for (Tache *t : this->taches) { if (id == t->unique_id) { return t; @@ -54,7 +76,7 @@ const Tache *Projet::contains(const int id) const { return nullptr; } -const Tache *Projet::contains(const std::string name) const { +Tache *Projet::contains(const std::string name) const { for (Tache *t : this->taches) { if (name == t->name) { return t; diff --git a/src/ProtoProjet.cpp b/src/ProtoProjet.cpp index c4b6692..11e969a 100644 --- a/src/ProtoProjet.cpp +++ b/src/ProtoProjet.cpp @@ -14,12 +14,50 @@ const ProtoProjet &ProtoProjet::operator=(const ProtoProjet &src) { return *this; } -void ProtoProjet::cleanMarks() { +void ProtoProjet::cleanMarks() const { for (auto t : taches) { t->visite = false; } }; -/* bool ProtoProjet::ajoute(std::string nom, int duree) { - return false; -} */ +bool ProtoProjet::ajoute(const std::string nom, const int duree) { + // Sélection de 2 tâches au hasard + auto tasks = pick_two_random_tasks(); + + // Ajout des tâches + return ajoute(nom, duree, tasks.first, tasks.second); +} + +bool ProtoProjet::ajoute(const std::string nom, const int duree, const int id) { + // Ajout de la tâche entre l'ID voulu et la tâche finale + return ajoute(nom, duree, id, fin->unique_id); +} + +bool ProtoProjet::ajoute(const std::string nom, const int duree, const int id1, + const int id2) { + Tache *tache1 = contains(id1); + Tache *tache2 = contains(id2); + + // Vérifie que les tâches avec les id1 et id2 existent bien dans la liste + if (!tache1 || !tache2) { + return false; + } + + // Création des dépendances + Tache *tache = new Tache(nom, duree); + if (!tache1->ajouteDependance(*tache)) { + // Problème de dépendance + delete tache; + return false; + } + if (!tache->ajouteDependance(*tache2)) { + // Problème de dépendance + delete tache; + return false; + } + + // Mise à jour du vecteur avec tri topologique + taches.push_back(tache); + taches = topologicalSort(); + return true; +} diff --git a/src/Tache.cpp b/src/Tache.cpp index ee28b72..f457d89 100644 --- a/src/Tache.cpp +++ b/src/Tache.cpp @@ -3,8 +3,8 @@ int Tache::counter_id = 0; Tache::Tache(const std::string n, const int duree) - : unique_id(++counter_id), duree_total(duree), etat(EnAttente), - dependances(std::vector()), name(n) {} + : duree_total(duree), etat(EnAttente), dependances(std::vector()), + unique_id(++counter_id), name(n) {} Tache::~Tache() {} @@ -17,7 +17,7 @@ void Tache::_copy(const Tache &src) { } Tache::Tache(const Tache &src) - : unique_id(++counter_id), duree_total(src.duree_total), etat(src.etat), + : duree_total(src.duree_total), etat(src.etat), unique_id(++counter_id), name(src.name) { _copy(src); }