summaryrefslogtreecommitdiff
path: root/sem_4/Algo/TP4
diff options
context:
space:
mode:
authorGaspard Coulet <gaspard.coulet@mines-ales.org>2021-04-28 23:05:53 +0200
committerGaspard Coulet <gaspard.coulet@mines-ales.org>2021-04-28 23:05:53 +0200
commit9fe033ea88c2f705ec18c232873d056e0c229d72 (patch)
tree0647dc8c51610c7336c88c04de2068ea14b21e17 /sem_4/Algo/TP4
Initial commit
Diffstat (limited to 'sem_4/Algo/TP4')
-rw-r--r--sem_4/Algo/TP4/AB.cpp72
-rw-r--r--sem_4/Algo/TP4/AB.h44
-rw-r--r--sem_4/Algo/TP4/SortieLatex.cpp110
-rw-r--r--sem_4/Algo/TP4/TPArborescencesBinaires.pdfbin0 -> 278512 bytes
-rw-r--r--sem_4/Algo/TP4/main.cpp24
-rw-r--r--sem_4/Algo/TP4/progbin0 -> 14039 bytes
-rw-r--r--sem_4/Algo/TP4/testbin0 -> 14039 bytes
7 files changed, 250 insertions, 0 deletions
diff --git a/sem_4/Algo/TP4/AB.cpp b/sem_4/Algo/TP4/AB.cpp
new file mode 100644
index 0000000..379387e
--- /dev/null
+++ b/sem_4/Algo/TP4/AB.cpp
@@ -0,0 +1,72 @@
+//AB.cpp
+
+#include "AB.h"
+
+
+Sommet::Sommet(Valeur v){
+ racine = v;
+ SAG = NULL;
+ SAD = NULL;
+ Pere = NULL;
+}
+
+Sommet::Sommet(Sommet& s){
+ racine = s.racine;
+ SAG = s.SAG;
+ SAD = s.SAD;
+
+}
+
+
+bool Sommet::FeuilleP(){
+ if (SAG == SAD && SAG == NULL){
+ return true;
+ }
+ return false;
+}
+
+
+void Sommet::SupprimerSAG(){
+ if (!FeuilleP()){
+ SAG->SupprimerSAG();
+ delete SAG;
+ }
+}
+
+
+void Sommet::SupprimerSAD(){
+ if (!FeuilleP()){
+ SAD->SupprimerSAD();
+ delete SAD;
+ }
+}
+
+
+void Sommet::GrefferSAG(AB g){
+ SupprimerSAG();
+ SAG = g;
+ g->Pere = this;
+ g->FGP= true;
+ }
+
+void Sommet::GrefferSAD(AB d){
+ SupprimerSAD();
+ SAD=d;
+ d->Pere=this;
+ d->FGP=false;
+ }
+
+
+void Sommet::RemplacerPourLePerePar(AB Ar){
+ //le pere existe
+ if ( FGP ){
+ Pere->GrefferSAG(Ar);
+}
+else {
+ Pere->GrefferSAD(Ar);
+}
+}
+
+
+/*Question 1 : L'etiquette d'un sommet est dans "racine", on voit que l'arbo est binaire car chaque sommet ne peut avoir que 2 fils au maximum.
+*/
diff --git a/sem_4/Algo/TP4/AB.h b/sem_4/Algo/TP4/AB.h
new file mode 100644
index 0000000..436d639
--- /dev/null
+++ b/sem_4/Algo/TP4/AB.h
@@ -0,0 +1,44 @@
+//AB.h
+
+#ifndef AB_H
+#define AB_H
+
+#include <iostream>
+#include <sstream>
+
+typedef int Valeur;
+
+class Sommet;
+
+typedef Sommet* AB;
+void SortieLatex(AB Ar, std::string filepath);
+ std::string* TikzRecursAB(int ligne,int gauche, int droite, int numeroPere, int typeFils, AB Ar);
+
+class Sommet {
+ public:
+ Valeur racine;
+ AB Pere,SAG, SAD;
+ bool FGP;
+
+ int hauteur,balanceGmoinsD;
+
+
+ Sommet(Valeur v);
+ Sommet(Sommet& s);
+
+ void GrefferSAG(AB g);
+ void GrefferSAD(AB d);
+
+ void SupprimerSAG();
+ void SupprimerSAD();
+
+ bool FeuilleP();
+
+ void RemplacerPourLePerePar(AB);
+
+ std::string* TikzRecursAB(int ligne,int gauche, int droite, int numeroPere, int typeFils, AB Ar);
+};
+
+
+
+#endif
diff --git a/sem_4/Algo/TP4/SortieLatex.cpp b/sem_4/Algo/TP4/SortieLatex.cpp
new file mode 100644
index 0000000..b400d1a
--- /dev/null
+++ b/sem_4/Algo/TP4/SortieLatex.cpp
@@ -0,0 +1,110 @@
+//SortieLatex.cpp
+
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <cstdlib>
+#include <stdlib.h>
+
+typedef int Valeur;
+
+class Sommet;
+
+typedef Sommet* AB;
+
+void SortieLatex(AB Ar, std::string filepath);
+
+class Sommet {
+ protected:
+ Valeur racine;
+ AB Pere,SAG,SAD;
+ bool FGP;
+
+ // Unused:
+ // int hauteur,balanceGmoinsD;
+
+ public:
+ Sommet(Valeur v);
+ Sommet(Sommet& s);
+
+ AB remonterToutEnHaut();
+
+ void GrefferSAG(AB g);
+ void GrefferSAD(AB d);
+
+ void SupprimerSAG();
+ void SupprimerSAD();
+
+ bool FeuilleP();
+
+ void RemplacerPourLePerePar(AB);
+
+ friend std::string* TikzRecursAB(int ligne,int gauche, int droite, int numeroPere, int typeFils, AB Ar);
+
+
+};
+
+std::string* TikzRecursAB(int ligne,int gauche, int droite, int numeroPere, int typeFils, AB Ar);
+
+
+
+std::string * TikzRecursAB(int ligne,int gauche, int droite, int numeroPere, int typeFils, AB Ar){
+ std::ostringstream ossnum, osslign,osscol,ossnumPere, ossbal, ossnum2Pere,ossnumRac;
+
+ std::string stres("");
+
+ if (Ar) {
+ ossnumPere<<numeroPere;
+ ossnumRac<<"(\\textcolor{red}{" << Ar->racine << "})\\\\this=\\textcolor{red}{" <<Ar <<"}\\\\Pere=\\textcolor{red}{"<<Ar->Pere << "} (FGP=\\textcolor{red}{" << (Ar->FGP?"Gauche":"Droit") <<"})";
+
+ if (Ar->Pere )ossnum2Pere<<Ar->Pere->racine; else ossnum2Pere<<0;
+
+ int numero;
+ if (typeFils==-1) { numero=1; } else { numero= 2*numeroPere + typeFils; }
+ ossnum<<numero;
+ osslign<<ligne;
+ int mil = (gauche + droite)/2;
+
+ osscol<<mil;
+
+ stres="\\node[draw, color=black, rounded corners=5pt, text width=3cm, text centered] (SZ" + ossnum.str() + ") at " +
+ "(" + osscol.str() + ", " + osslign.str() + ") " +
+ "{ " + ossnumRac.str() + "};\n";
+
+ if (typeFils!=-1) stres+="\\draw[->, >=latex, color=blue] (SZ"+ossnumPere.str()+") -- (SZ"+ossnum.str() +");\n";
+
+ if (Ar->SAG) stres+=*TikzRecursAB(ligne-3,gauche,mil-13,numero,0,Ar->SAG);
+ if (Ar->SAD) stres+=*TikzRecursAB(ligne-3,mil+13,droite,numero,1,Ar->SAD);
+ }
+ return new std::string(stres);
+}
+
+std::string * TikzAB(AB Ar){
+ return TikzRecursAB(1,1,10,1,-1,Ar);
+}
+
+ void SortieLatex(AB Ar, std::string filepath){ //don't insert garbage in filepath, its std::system-ised.
+ std::ofstream fichier(filepath.c_str(), std::ios::out | std::ios::trunc);
+ std::string preamb ("\\documentclass{article} \n \\usepackage{tikz} \n \\begin{document} \n \\resizebox{300pt}{!}{\n \\begin{tikzpicture}\n");
+ std::cout<<preamb<<"\n";
+std::string post("\\end{tikzpicture}\n } \\end{document} \n"); //rsz box end?
+ std::cout<<post<<"\n";
+ std::cout<<*TikzAB(Ar)<<"\n";
+std::string res1(preamb + *TikzAB(Ar));
+ std::string res(res1 + post);
+ //std::cout<<res1<<"\n";
+ fichier <<res<<"\n";
+ fichier.close();
+
+ std::ostringstream system_CARE;
+ // /dev/null 2>&1 isnt enough to mute pdflatex...
+ system_CARE << "mkdir pdflatex_temp > /dev/null 2>&1;"
+ << "pdflatex -output-directory=\"./pdflatex_temp\" -interaction=nonstopmode \"" << filepath << "\" >/dev/null 2>&1;"
+ << "mv ./pdflatex_temp/*.pdf ./ > /dev/null 2>&1;";
+ std::system(system_CARE.str().c_str());
+ return;
+}
+
+
+
+// g++ -c SortieLatex.cpp
diff --git a/sem_4/Algo/TP4/TPArborescencesBinaires.pdf b/sem_4/Algo/TP4/TPArborescencesBinaires.pdf
new file mode 100644
index 0000000..fdf4ff1
--- /dev/null
+++ b/sem_4/Algo/TP4/TPArborescencesBinaires.pdf
Binary files differ
diff --git a/sem_4/Algo/TP4/main.cpp b/sem_4/Algo/TP4/main.cpp
new file mode 100644
index 0000000..6afaf71
--- /dev/null
+++ b/sem_4/Algo/TP4/main.cpp
@@ -0,0 +1,24 @@
+#include<iostream>
+#include<cstdlib>
+#include<ctime>
+#include"AB.h"
+
+int main ( int argc, char ** argv){
+ AB s1 = new Sommet(2);
+ AB s2 = new Sommet(4);
+ AB s3 = new Sommet(2);
+ AB s4 = new Sommet(4);
+ AB s5 = new Sommet(0);
+ AB s6 = new Sommet(0);
+ AB s7 = new Sommet(6);
+
+ s5->GrefferSAG(s1);
+ s5->GrefferSAD(s2);
+ s6->GrefferSAG(s3);
+ s6->GrefferSAD(s4);
+ s7->GrefferSAG(s5);
+ s7->GrefferSAD(s6);
+ SortieLatex(s7, "test");
+ return 0;
+}
+// La methode estFeuille est testé lors de la suppresion, donc lors des greffes.
diff --git a/sem_4/Algo/TP4/prog b/sem_4/Algo/TP4/prog
new file mode 100644
index 0000000..62c1884
--- /dev/null
+++ b/sem_4/Algo/TP4/prog
Binary files differ
diff --git a/sem_4/Algo/TP4/test b/sem_4/Algo/TP4/test
new file mode 100644
index 0000000..57bc262
--- /dev/null
+++ b/sem_4/Algo/TP4/test
Binary files differ