elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / Algoritmo de Floyd Warshall en: 22 Febrero 2020, 02:30 am
tengo este codigo la verdad no estoy seguro de si es correcto en su totalidad, soy algo nuevo en esto.

3 archivos



*******************MatrizQ.cpp******************

 
Código
  1. #include "MatrizQ.h"
  2.  
  3. MatrizQ::MatrizQ(int tamanio)
  4. {
  5. this->tamanio=tamanio;
  6. this->matriz=NULL;
  7. matriz=new int*[tamanio];
  8. for(int i=0;i<tamanio;i++)
  9. {
  10. matriz[i]=new int[tamanio];
  11. }
  12.  
  13. for(int i=0;i<this->tamanio;i++)
  14. {
  15. cout<<endl;
  16. for(int j=0;j<this->tamanio;j++)
  17. {
  18. matriz[i][j]=0;
  19. }
  20. }
  21.  
  22. }
  23.  
  24. MatrizQ::~MatrizQ()
  25. {
  26.  
  27. }
  28. int MatrizQ::dameTuTamanio()
  29. {
  30. cin>>this->tamanio;
  31. return this->tamanio;
  32. }
  33.  
  34. void MatrizQ::verificaTusDatos()
  35. {
  36. if(this->tamanio<0)
  37. {
  38. cout<<"el tamanio de la matriz no puede ser negativo, por favor ingresa otro valor: ";
  39.  
  40. }
  41. }
  42.  
  43. void MatrizQ::pideleAlUsuariosTusDatos()
  44. {
  45. if(matriz!=NULL)
  46. {
  47. for(int i=0;i<this->tamanio;i++)
  48. delete []matriz[i];
  49. delete []matriz;
  50. }
  51.  
  52. cout<<endl<<"Dame el orden de la matriz A"<<endl<<"tamanio: ";
  53.  
  54. do
  55. {
  56. this->dameTuTamanio();
  57. this->verificaTusDatos();
  58.  
  59. }while(this->tamanio<0);
  60.  
  61.  
  62. /**RESERVA DE MEMORIA DINAMICA*/
  63. matriz=new int*[this->tamanio];
  64. for(int i=0;i<=this->tamanio;i++)
  65. {
  66. matriz[i]=new int[this->tamanio];
  67. }
  68.  
  69. /**LECTURA DE LOS DATOS DE LA MATRIZ*/
  70. cout<<"ingresa los datos de la matriz"<<endl;
  71. for(int i=0;i<this->tamanio;i++)
  72. {
  73. for(int j=0;j<this->tamanio;j++)
  74. {
  75. cout<<"ingresa el dato en la posicion ["<<i+1<<"]["<<j+1<<"]:";
  76. cin>>matriz[i][j];
  77. }
  78. }
  79. }
  80.  
  81.  
  82. void MatrizQ::muestraTusDatos()
  83. {
  84.  
  85. for(int i=0;i<this->tamanio;i++)
  86. {
  87. cout<<endl;
  88. for(int j=0;j<this->tamanio;j++)
  89. {
  90. cout<<"["<<matriz[i][j]<<"] ";
  91. }
  92. }
  93. cout<<endl;
  94. }
  95.  
  96. int MatrizQ::dameTuDatoRC(int renglon, int columna)
  97. {
  98. renglon=renglon;
  99. columna=columna;
  100.  
  101. if(renglon<0||columna<0)
  102. {
  103. cout<<"no hay renglones ni columnas negativas"<<endl;
  104.  
  105. }
  106.  
  107. return matriz[renglon][columna];
  108. }
  109.  
  110. void MatrizQ::modificaTuDatoRC(int renglon, int columna, int valor)
  111. {
  112. renglon=renglon;
  113. columna=columna;
  114. this->matriz[renglon][columna]=valor;
  115. }
  116.  
  117.  
  118. MatrizQ& MatrizQ::operator=(MatrizQ Q)
  119. {
  120.  
  121.    if(this!=&Q)
  122.    {
  123.        tamanio=Q.tamanio;
  124.        delete [] this->matriz;
  125.        this->tamanio=Q.tamanio;
  126.  
  127.        this->matriz = new int *[this->tamanio*this->tamanio];
  128.        for(int i=0; i<this->tamanio; i++)
  129.        {
  130.        this->matriz[i] = new int[tamanio];
  131.        for(int j=0; j<this->tamanio; j++)
  132.        this->matriz[i][j] = Q.matriz[i][j];
  133.        }
  134.  
  135.    }
  136.  
  137.    else
  138.        {
  139.            matriz=NULL;
  140.            for(int i=0; i<this->tamanio; i++)
  141.                {
  142.            this->matriz[i] = new int[tamanio];
  143.            for(int j=0; j<this->tamanio; j++)
  144.            this->matriz[i][j] = Q.matriz[i][j];
  145.        }
  146.        }
  147. /**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  148.    for(int i=0;i<this->tamanio;i++)
  149.    {
  150.        for(int j=0;j<this->tamanio;j++)
  151.  
  152.        modificaTuDatoRC(i,j,Q.dameTuDatoRC(i,j));
  153.    }
  154.    return *this;
  155.  
  156. }



/*****************MatrizQ.h**************



Código
  1. #ifndef MATRIZQ_H
  2. #define MATRIZQ_H
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. class MatrizQ
  7. {
  8. public:
  9. MatrizQ(int tamanio = 0);
  10. ~MatrizQ();////////////////////////////////////////////////
  11. void pideleAlUsuariosTusDatos(void);
  12. void muestraTusDatos(void);
  13. int dameTuDatoRC(int renglon, int columna);
  14. int dameTuTamanio(void);
  15. void modificaTuDatoRC(int renglon, int columna, int valor);
  16. MatrizQ& operator=(MatrizQ Q);//////////////////////////////////
  17. friend void cam_min(MatrizQ **W, MatrizQ **Q0 );
  18.  
  19. private:
  20. void verificaTusDatos(void);
  21. void liberaMemoria(void);////////////////////////////
  22. int** matriz;
  23. int tamanio;
  24. };
  25.  
  26. #endif // MATRIZQ_H

en el main no tengo nada porque intente hacer el algoritmo de warshall pero no se como hacerlo o como empezar.


pregunte y solo me dijeron que es una funcion que no pertenece a la clase pero que si utiliza la clase, la verdad no entendí
y tengo que programar este código pero no se como hacerlo , intente con objetos dinamicos, y otras formas pero no se como hacerlo, si me pudieran orientar a como ejecutarlo y en donde se los agradeceria mucho, URGE  tengo que entregar esto antes de las 12:00 am


*************** algoritmo a ejecutar

Algoritmo del camino mínimo
Un dígrafo con pesos G de M nodos está en memoria mediante una matriz de pesos W. Este
algoritmo encuentra la matriz Q tal que [I, J] es la longitud del camino mínimo del nodo VI al nodo
VJ. Infinito es un número muy grande y MIN es la función del valor mínimo.
1. [Iniciar Q]
 Repetir Para I = 1, 2,…, M:
 Repetir Para J = 1, 2,…, M:
 Si W[I, J] = 0 entonces
 Hacer Q[I, J] := Infinito
 Sino
 Hacer Q[I, J] := W[I, J]
 [fin del bucle]
 [fin del bucle]
2. [Actualizar Q:]
Repetir pasos 3 y 4 para K = 1,2,…, M:
3. Repetir paso 4 para I = 1,2,…, M:
4. Repetir para J = 1,2,…, M:
 Hacer Q[I, J] := MIN( Q[I, J], Q[I, K] + Q[K , J])
 [fin del bucle]
 [fin del bucle]
 [fin del bucle]
5. [Salir]




la matriz de pesos es ********************

[ 7 5 0 0 ]
[ 7 0 0 2 ]
[ 0 3 0 0 ]
[ 4 0 1 0 ]

MOD: Etiquetas GeSHi.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines