-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafo.cpp
322 lines (302 loc) · 8.85 KB
/
grafo.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include<iostream>
#include<string>
#include<conio.h>
#include<stdio.h>
#define MAXVERTS 100
using namespace std;
typedef int *pint;
//INICIO CLASES ---------------
class vertice{
protected:
string nombre;
int numvert;
public:
vertice(){
}
vertice(string x){
nombre=x;
numvert=-1;
}
vertice(string x,int n){
nombre=x;
numvert=n;
}
int obtNvert(){
return numvert;
}
void ponNvert(int n){
numvert=n;
}
string obtNomVert(){
return nombre;
}
void ponNomVert(string x){
nombre=x;
}
bool igual(vertice x){
return nombre==x.nombre;
}
bool igual(string x){
return nombre==x;
}
};
class GrafoMatrix{
protected:
int maxVerts;
int numVerts;
vertice *verts;
int **matAd;
public:
GrafoMatrix(){
maxVerts=1;
GrafoMatrix(maxVerts);
}
GrafoMatrix(int n){
maxVerts=n;
verts=new vertice[n];
matAd=new pint[n];
numVerts=0;
//Inicializa la matriz
for(int i=0;i<n;i++){
matAd[i]=new int[n];
}
//Se llena de ceros
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
matAd[i][j]=0;
}
}
}
int Onv(){ //Getter de numvert
return numVerts;
}
void Pnv(int n){ //Setter de numvert
numVerts=n;
}
int numVertice(string n){
int i;
bool encontrado=false;
for(i=0;(i<numVerts && !encontrado);){
encontrado=verts[i].igual(n);
if(!encontrado)
i++;
}
if(i<numVerts){
return i;
}else{
return -1;
}
}
void nuevoVertice(string n){
bool esta=numVertice(n)>=0;
if(!esta){
vertice v=vertice(n,numVerts);
verts[numVerts++]=v;
}
}
void nuevoArco(string a,string b){
int va=numVertice(a);
int vb=numVertice(b);
if(va<0||vb<0)
cout<<"Vert No existe!";
else
matAd[va][vb]=1;
}
void nuevoArco(int a,int b){
if((a<0||b<0)&&(a>maxVerts||b>maxVerts))
cout<<"Vert No existe!";
else
matAd[a][b]=1;
}
bool adyacente(string a,string b){ //Dice si dos nodos son adyacentes
int va=numVertice(a);
int vb=numVertice(b);
if(va<0||vb<0)
cout<<"Vert No existe!";
else
return matAd[va][vb]==1;
}
bool adyacente(int a,int b){ //Dice si dos nodos son adyacentes
if((a<0||b<0)&&(a>maxVerts||b>maxVerts))
cout<<"Vert No existe!";
else
return matAd[a][b]==1;
}
int ovalor(string a,string b){ //Devuelve el peso del arco
int va=numVertice(a);
int vb=numVertice(b);
if(va<0||vb<0)
cout<<"Vert No existe!";
else
return matAd[va][vb];
}
int ovalor(int a,int b){ //Devuelve el peso del arco
if((a<0||b<0)&&(a>maxVerts||b>maxVerts))
cout<<"Vert No existe!";
else
return matAd[a][b];
}
void pvalor(string a, string b,int n){ //Setea el peso de un arco
int va=numVertice(a);
int vb=numVertice(b);
if(va<0||vb<0)
cout<<"Vert No existe!";
else
matAd[va][vb]=n;
}
void pvalor(int a,int b,int n){ //Setea el peso de un arco
if((a<0||b<0)&&(a>maxVerts||b>maxVerts))
cout<<"Vert No existe!";
else
matAd[a][b]=n;
}
vertice overtice(int n){
if(n<0||n>maxVerts)
cout<<"Vert No existe!";
else
return verts[n];
}
void overtice(vertice v,int n){
if(n<0||n>maxVerts)
cout<<"pos invalida!";
else
verts[n]=v;
}
void imprimirM(){ //IMprime matriz
for(int i=0;i<numVerts;i++){
for(int j=0;j<numVerts;j++){
cout<<"["<<matAd[i][j]<<"]";
}
cout<<endl;
}
}
void imprimirV(){ //IMprime vertices
for(int i=0;i<numVerts;i++){
cout<<"{"<<verts[i].obtNomVert()<<"}";
}
}
void PrintConjunto(){ // Método para imprimir los vértices y arcos en forma de conjunto
for(int i=0;i<numVerts;i++){
for(int j=0;j<numVerts;j++){
if(matAd[i][j]!=0)
cout<<"{"<<verts[i].obtNomVert()<<", "<<verts[j].obtNomVert()<<", "<<matAd[i][j]<<"}"<<endl;
}
}
}
void GradoSalida(){ // Método que me permite imprimir el grado de salida de un vértice
int _Grado=0;
cout<<"Grados de Salida: "<<endl;
for(int i=0;i<numVerts;i++){
for(int j=0;j<numVerts;j++){
if(matAd[i][j]!=0)
_Grado+=1;
}
cout<<"La ciudad de: "<<verts[i].obtNomVert()<<" tiene un grado de salida: "<<_Grado<<endl;
_Grado=0;
}
}
void GradoEntrada(){ //Método que permite ingresar los grados de entrada de un vértice
int _Grado=0;
cout<<"\nGrados de Entradas: "<<endl;
for(int i=0;i<numVerts;i++){
for(int j=0;j<numVerts;j++){
if(matAd[j][i]!=0)
_Grado+=1;
}
cout<<"La ciudad de: "<<verts[i].obtNomVert()<<" tiene un grado de entrada: "<<_Grado<<endl;
_Grado=0;
}
}
};
// FIN CLASES ----------
//FUNCIONES AUXILIARES ------------------
int menu (void);
// FIN FUNCIONES AUXIALES ---------------
int main(){
GrafoMatrix ObjGM(10); // Creando Objeto de la clase
// -------------------------------------------------------------
string Info;
string Ciudad_origen;
string Ciudad_destino;
int _Distancia;
system("cls");
int sw=1;
int opc;
long opcion;
do{
switch(menu()){
case 1:
cout<<"ingrese una ciudad: ";
getline (cin, Info);
ObjGM.nuevoVertice(Info);
break;
case 2:
cin.clear();
cout<<"Ingrese ciudad de Origen: ";
getline(cin, Ciudad_origen);
if(Ciudad_origen.compare("")==0) // Presentaba un error al momento de volver a ingresar una distancia
getline(cin, Ciudad_origen);
cout<<"Ingrese la ciudad de destino: ";
getline(cin, Ciudad_destino);
cout<<"Ingrese la distada representada en Kilometros: ";
cin>>_Distancia;
if(cin.fail()){
cin.clear();
cin.ignore(256, '\n');
cout << "Datos erroneos" << endl;
system("pause");
return 0;
}
if(_Distancia <0 ){
_Distancia = _Distancia*-1;
}
ObjGM.pvalor(Ciudad_origen, Ciudad_destino, _Distancia);
getch();
break;
case 3:
ObjGM.PrintConjunto();
getch();
break;
case 4:
ObjGM.GradoSalida(); //Se implementaro método para imprimir las salidas de un vertice
ObjGM.GradoEntrada();//Se implementa método para imprimir las entradas de un vértice
getch();
break;
}
}while(sw);
system("Pause>>NULL");
}
int menu (void){
int opcion;
system("CLS");
system ("color 3A");
printf ("\n\n");
printf("\t\t\t?????????????????????????????????????????????????\n");
printf("\t\t\t? IMPLEMENTACION DE UN GRAFO (CIUDADES) ?\n");
printf("\t\t\t?????????????????????????????????????????????????\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? 1 -> Crear Ciudad ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? 2 -> Ingrese distancia entre ciudades(KM) ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? 3 -> Imprimir en forma de conjunto ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? 4 -> Salidas/Entradas de cada ciudad (Grados) ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t? ?\n");
printf("\t\t\t?????????????????????????????????????????????????\n");
printf("\t\t\t? Elija una opcion... ?\n");
printf("\t\t\t?????????????????????????????????????????????????\n");
int sw=1;
do {
opcion=getch();
if (opcion>='0' && opcion <='4'){
sw=0;
}else
printf("\nSe debe ingresar un entero entre 1 y 9\n");
}while(sw);
opcion=opcion-'0';
return opcion;
}