-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphe.cpp
196 lines (166 loc) · 4.53 KB
/
Graphe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*************************************************************************
Graphe - Structure de Données
-------------------
début : 15/01/2019
copyright : (C) 2019 par Jacquot Pierre
Villenave Sophie
*************************************************************************/
//------- Réalisation de la classe <Graphe> (fichier Graphe.cpp) ---------
//---------------------------------------------------------------- INCLUDE
//-------------------------------------------------------- Include système
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//------------------------------------------------------ Include personnel
#include "Graphe.h"
//------------------------------------------------------------- Constantes
//----------------------------------------------------------------- PUBLIC
//----------------------------------------------------- Méthodes publiques
void Graphe::Afficher ( ) const
{
for ( catArc::const_iterator it = mapArc.begin ( ); it != mapArc.end ( ); ++it )
{
cout << *( it->first ) << " : " << it->second.nbVisites << endl;
for ( catNode::const_iterator itRef = it->second.donneesNoeud.begin ( );
itRef != it->second.donneesNoeud.end ( ); ++itRef )
{
cout << " Référence : " << *( itRef->first ) << " : " << itRef->second << endl;
}
}
}
void Graphe::Ajouter ( string * cible , string * ref )
{
cible = InsererString ( cible );
ref = InsererString ( ref );
catArc::iterator it = mapArc.find ( cible );
if ( it == mapArc.end ( ) )
{
noeud n;
n.nbVisites = 1;
n.donneesNoeud.insert ( pair<string * , int> ( ref , 1 ) );
pair<string * , noeud> p ( cible , n );
mapArc.insert ( p );
}
else
{
it->second.nbVisites++;
catNode::iterator itRef = it->second.donneesNoeud.find ( ref );
if ( itRef == it->second.donneesNoeud.end ( ) )
{
pair<string * , int> pRef ( ref , 1 );
it->second.donneesNoeud.insert ( pRef );
}
else
{
itRef->second++;
}
}
}
void Graphe::afficherTop ( )
{
catTop top;
int size = 10;
for ( auto&& item : mapArc )
{
top.emplace ( item.second.nbVisites , item.first );
}
if ( top.size ( ) < 10 )
{
size = top.size ( );
}
cout << "Top " << size << " : " << endl;
int i = 0;
for ( catTop::iterator it = top.end ( ); i < size; ++i )
{
--it;
cout << *( it->second ) << " : " << it->first << endl;
}
}
void ecrireDonneesNoeud ( ofstream & fdot , const catArc::const_iterator & it , const catNode & donneesNoeud )
{
for ( catNode::const_iterator itNode = donneesNoeud.begin ( ); itNode != donneesNoeud.end ( ); ++itNode )
{
fdot << "\t\""
<< *( itNode->first )
<< "\" -> \""
<< *( it->first )
<< "\" [label=\""
<< itNode->second
<< "\"];\n";
}
}
bool Graphe::GenererFichierGraphe ( string nomFichier ) const
{
bool reussite;
ofstream fdot ( nomFichier , ofstream::out );
ofstream ftmp ( "tmp" , ofstream::out );
if ( fdot )
{
fdot << "digraph {" << endl;
for ( catArc::const_iterator it = mapArc.begin ( ); it != mapArc.end ( ); ++it )
{
fdot << "\t\"" << *( it->first ) << "\";\n";
ecrireDonneesNoeud ( ftmp , it , ( it->second ).donneesNoeud );
}
ftmp.close ( );
ifstream itmp ( "tmp" );
string tampon;
while ( itmp )
{
getline ( itmp , tampon );
fdot << tampon << "\n";
}
fdot << "}";
fdot.close ( );
remove ( "tmp" );
reussite = true;
}
else
{
reussite = false;
fdot.close ( );
ftmp.close ( );
remove ( "tmp" );
}
return reussite;
}
//------------------------------------------------- Surcharge d'opérateurs
//-------------------------------------------- Constructeurs - destructeur
Graphe::Graphe ( )
{
#ifdef MAP
cout << "Appel au constructeur de <Graphe>" << endl;
#endif
} //----- Fin de Graphe
Graphe::~Graphe ( )
// Algorithme :
//
{
#ifdef MAP
cout << "Appel au destructeur de <Graphe>" << endl;
#endif
for ( catAdresse::iterator it = adresses.begin ( ); it != adresses.end ( ); ++it )
{
delete ( it->second );
}
} //----- Fin de ~Graphe
//------------------------------------------------------------------ PRIVE
//----------------------------------------------------- Méthodes protégées
string * Graphe::InsererString ( string * value )
{
string * pointer;
catAdresse::iterator it = adresses.find ( *value );
if ( it == adresses.end ( ) )
{
pair<string , string *> p ( *value , value );
adresses.insert ( p );
pointer = value;
}
else
{
delete value;
pointer = it->second;
}
return pointer;
}