Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. //Macierz oraz lista sąsiedztwa grafu nieskierowanego
  2. #include<iostream>
  3. #include<fstream>
  4. #include<cstdlib>
  5. #include<iomanip> //umożliwia użycie funkcji setw()
  6. using namespace std;
  7. const int size = 30;
  8. int main()
  9. {
  10. cout << "Podaj nazwe pliku z ktorego zostana pobrane dane: " << endl;
  11. char filename[size];
  12. cin.getline(filename, size);
  13. ifstream inFile(filename);
  14. if (!inFile.is_open())
  15. {
  16. cout << "Otwarcie pliku " << filename << " nie powiodlo sie." << endl;
  17. cout << "Program zostanie zakonczony." << endl;
  18. exit(EXIT_FAILURE);
  19. }
  20. int n,max;
  21. inFile >> n;
  22. for (int i = 0; i < n; i++)
  23. {
  24. int a;
  25. inFile >> a;
  26. int b;
  27. inFile >> b;
  28. if (a>b)
  29. {
  30. max = a;
  31. }
  32. else max = b;
  33. }
  34. int **table_array = new int *[max]; //alokacja pamięci
  35. for (int i = 0; i < max; i++)
  36. {
  37. table_array[i] = new int[max];
  38. }
  39. //wypełnianie zerami macierzy
  40. for (int x = 0; x < max; x++)
  41. {
  42. for (int y = 0; y < max; y++)
  43. {
  44. table_array[x][y] = 0;
  45. }
  46. }
  47. inFile.close();
  48. inFile.open(filename);
  49. int m;
  50. inFile >> m;
  51. for (int i = 0; i < n; i++)
  52. {
  53. int a;
  54. inFile >> a;
  55. int b;
  56. inFile >> b;
  57. table_array[a - 1][b - 1] = 1;
  58. table_array[b - 1][a - 1] = 1;
  59. }
  60. for (int i = 0; i < max; i++)
  61. {
  62. cout << endl << setw(2);
  63. for (int j = 0; j < max; j++) cout << setw(2) << table_array[i][j];
  64. }
  65. cout << endl << endl;
  66. // Usuwamy macierz z pamięci komputera
  67. for (int i = 0; i < max; i++) delete[] table_array[i];
  68. delete[] table_array;
  69. inFile.close();
  70. inFile.open(filename);
  71. //tworzenie listy sasiedztwa
  72. int x;
  73. inFile >> x;
  74. for (int i = 0; i < x; i++)
  75. {
  76. int a;
  77. inFile >> a;
  78. int b;
  79. inFile >> b;
  80. int k;
  81. if (table_array[a][b] == 0) cout << "Drogi nie ma\n";
  82. else
  83. {
  84. cout << a << endl;
  85. k = a;
  86. while (k != b)
  87. {
  88. k = table_array[k][b];
  89. cout << k << endl;
  90. }
  91. }
  92. cout << endl;
  93. }
  94. inFile.close();
  95. cin.get();
  96. cin.get();
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement