From 8d3b9973a6bc2d10194fb1a3516d1b646b6cb04e Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 28 Oct 2023 01:34:58 +0200 Subject: [PATCH] fix issues --- TODO.md | 4 ++-- includes/Projet.hpp | 5 ++++- includes/ProtoProjet.hpp | 1 - includes/Tache.hpp | 2 +- src/Projet.cpp | 19 +++++++++++-------- src/ProtoProjet.cpp | 11 +++-------- src/Tache.cpp | 11 ++++++----- src/main.cpp | 9 ++++----- 8 files changed, 31 insertions(+), 31 deletions(-) diff --git a/TODO.md b/TODO.md index 662fefe..33286b2 100644 --- a/TODO.md +++ b/TODO.md @@ -39,12 +39,12 @@ TODO avant rendu : - [x] Tâche `début` - [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` - [x] `bool ajoute(nom, durée)` : sélectionne au hasard 2 tâches déjà enregistrer et **ajoute** la nouvelle tâche entres-elles - - [x] `bool ajoute(nom, durée, id)` : **déplace** une tâche qui doit se réaliser + - [x] `bool ajoute(nom, durée, id)` : **ajoute** 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** - [x] `bool ajoute(nom, durée, id1, id2)` : **ajoute** une tâche entre les 2 tâches qui ont l'identifiant `id1` et `id2` - [x] Surcharge de `<<` diff --git a/includes/Projet.hpp b/includes/Projet.hpp index 41f0d76..b1eb71d 100644 --- a/includes/Projet.hpp +++ b/includes/Projet.hpp @@ -43,7 +43,10 @@ protected: const std::vector consult_tasks() const; // Corrige les éventuelles anomalies du vector de tâches - std::vector topologicalSort(); + void topologicalSort(); + + // Remet tous les marquages à leur valeur initiale + virtual void cleanMarks() const = 0; }; #endif diff --git a/includes/ProtoProjet.hpp b/includes/ProtoProjet.hpp index 8e09515..c3aa9c4 100644 --- a/includes/ProtoProjet.hpp +++ b/includes/ProtoProjet.hpp @@ -14,7 +14,6 @@ public: ProtoProjet(const ProtoProjet &); // copy constructor const ProtoProjet &operator=(const ProtoProjet &); // copy assignement - // Remet tous les marquages à leur valeur initiale void cleanMarks() const; // Insère une nouvelle tâche en la plaçant entre deux tâches au hasard diff --git a/includes/Tache.hpp b/includes/Tache.hpp index f2dd9db..e5ef796 100644 --- a/includes/Tache.hpp +++ b/includes/Tache.hpp @@ -51,7 +51,7 @@ public: int dureeParal() const; // Parcours en profondeur - void PP_postfixe(std::vector); + void PP_postfixe(std::vector &); }; #endif diff --git a/src/Projet.cpp b/src/Projet.cpp index 6e132b1..3c3b13d 100644 --- a/src/Projet.cpp +++ b/src/Projet.cpp @@ -1,7 +1,7 @@ #include "../includes/Projet.hpp" Projet::Projet() : debut(new Tache("Début", 0)), fin(new Tache("Fin", 0)) { - fin->depends_from(*debut); + fin->ajouteDependance(*debut); taches.push_back(fin); taches.push_back(debut); @@ -13,7 +13,8 @@ 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)); + Tache *tache = new Tache(*t); + taches.push_back(tache); } } @@ -40,7 +41,7 @@ const Projet &Projet::operator=(const Projet &src) { std::ostream &operator<<(std::ostream &out, const Projet &data) { for (Tache *t : data.taches) { - out << *t << " "; + out << *t << "\n"; } return out; @@ -55,8 +56,7 @@ const std::pair Projet::pick_two_random_tasks() const { Tache *tache1 = this->taches[rand1]; Tache *tache2 = this->taches[rand2]; - // - tache2 ne doit pas dépendre transitivement de tache1 - // - permet aussi de vérifier si tache1 == tache2 + // tache2 ne doit pas dépendre transitivement de tache1 if (tache2->depends_from(*tache1)) { return pick_two_random_tasks(); } @@ -88,7 +88,7 @@ Tache *Projet::contains(const std::string name) const { const std::vector Projet::consult_tasks() const { return taches; } -std::vector Projet::topologicalSort() { +void Projet::topologicalSort() { // Construction de la pile std::vector pile; for (Tache *it : taches) { @@ -97,9 +97,12 @@ std::vector Projet::topologicalSort() { } } + // Nettoyage des marques + cleanMarks(); + // Reverse de la pile std::reverse(pile.begin(), pile.end()); - // Renvoie la liste de tâches topologiquement triée - return pile; + // Modifie la liste des tâches topologiquement triée + taches = pile; } diff --git a/src/ProtoProjet.cpp b/src/ProtoProjet.cpp index aa51671..b68246b 100644 --- a/src/ProtoProjet.cpp +++ b/src/ProtoProjet.cpp @@ -16,7 +16,7 @@ const ProtoProjet &ProtoProjet::operator=(const ProtoProjet &src) { std::ostream &operator<<(std::ostream &out, const ProtoProjet &data) { for (Tache *t : data.taches) { - out << *t << " "; + out << *t << "\n"; } return out; @@ -53,12 +53,7 @@ bool ProtoProjet::ajoute(const std::string nom, const int duree, const int id1, // 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)) { + if (!tache1->ajouteDependance(*tache) && !tache->ajouteDependance(*tache2)) { // Problème de dépendance delete tache; return false; @@ -66,6 +61,6 @@ bool ProtoProjet::ajoute(const std::string nom, const int duree, const int id1, // Mise à jour du vecteur avec tri topologique taches.push_back(tache); - taches = topologicalSort(); + topologicalSort(); return true; } diff --git a/src/Tache.cpp b/src/Tache.cpp index f457d89..070fb9d 100644 --- a/src/Tache.cpp +++ b/src/Tache.cpp @@ -4,7 +4,7 @@ int Tache::counter_id = 0; Tache::Tache(const std::string n, const int duree) : duree_total(duree), etat(EnAttente), dependances(std::vector()), - unique_id(++counter_id), name(n) {} + unique_id(counter_id++), name(n), visite(false) {} Tache::~Tache() {} @@ -17,8 +17,8 @@ void Tache::_copy(const Tache &src) { } Tache::Tache(const Tache &src) - : duree_total(src.duree_total), etat(src.etat), unique_id(++counter_id), - name(src.name) { + : duree_total(src.duree_total), etat(src.etat), unique_id(counter_id++), + name(src.name), visite(src.visite) { _copy(src); } @@ -29,6 +29,7 @@ const Tache &Tache::operator=(const Tache &src) { duree_total = src.duree_total; etat = src.etat; name = src.name; + visite = src.visite; _copy(src); return *this; } @@ -86,7 +87,7 @@ bool Tache::depends_from(const Tache &x) const { bool Tache::ajouteDependance(Tache &x) { // On ne créer pas de dépendances cyclique aka 2 tâches // qui dépendent l'une de l'autre - if (depends_from(x)) { + if (x.depends_from(*this)) { return false; } @@ -114,7 +115,7 @@ int Tache::dureeParal() const { return ret_value + max_duree; } -void Tache::PP_postfixe(std::vector pile) { +void Tache::PP_postfixe(std::vector &pile) { visite = true; for (Tache *it : dependances) { if (!it->visite) { diff --git a/src/main.cpp b/src/main.cpp index 4cff930..26a57a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,18 +5,17 @@ using namespace std; int main() { - - /* ProtoProjet pp; + ProtoProjet pp; pp.ajoute("a", 10); // probablement numero 2 cout << pp; // avec ses 3 taches - cout << "----------" << endl; + cout << "\n----------" << endl; pp.ajoute("b", 15, 0, 1); // en supposant que 0: fin et 1: début cout << pp; - cout << "----------" << endl; + cout << "\n----------" << endl; - RunProjet rp{pp}; + /* RunProjet rp{pp}; cout << "----- verification : ProtoProjet vide " << endl; cout << pp << endl;