Advertisement
Agaciora

Untitled

May 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. // Macierz Incydencji.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <fstream>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <stack>
  12.  
  13. using namespace std;
  14.  
  15. int w, k, i, j, v1, v2;
  16. int wierzcholki = 10000;
  17. double **tab = new double *[w];
  18. bool  *visited = new bool[w];
  19. int ile_krawedzi = 0;
  20.  
  21. void macierzIncyd()
  22. {
  23.     string linia;
  24.     fstream plik;
  25.     plik.open("C:\\Users\\Agata\\Desktop\\grafy.txt", ios::in);
  26.     if (plik.good() == true)
  27.     {
  28.  
  29.         while (!plik.eof())
  30.         {
  31.             while ((getline(plik, linia)))
  32.             {
  33.                 ile_krawedzi++;
  34.             }
  35.  
  36.             plik.close();
  37.             cout << "Plik wczytany!" << endl;
  38.         }
  39.  
  40.         cout << "Ilosc krawedzi: " << ile_krawedzi << endl;
  41.  
  42.  
  43.         //alokacja pamieci DO TABLICY 2D i jej zerowanie
  44.         for (int i = 0; i < 7; ++i)
  45.         {
  46.             tab[i] = new double[ile_krawedzi]; //alokacja pamieci
  47.             for (int j = 0; j < ile_krawedzi; ++j) //wpisanie wartosci do tablicy
  48.                 tab[i][j] = 0;
  49.         }
  50.  
  51.  
  52.         //          WYSWIETLANIE TABLICY O MALYCH WARTOSCIACH
  53.         //        cout << "   ";
  54.         //        for(int f = 0; f < ile_krawedzi; f++) cout << setw(3) << i;
  55.         //        cout << endl << endl;
  56.         //        for(int f = 0; f < 10000; f++)
  57.         //        {
  58.         //            cout << setw(3) << i;
  59.         //            for(j = 0; j < ile_krawedzi; j++) cout << setw(3) << (int) tab[i][j];
  60.         //            cout << endl;
  61.         //        }
  62.  
  63.  
  64.         plik.open("C:\\Users\\Agata\\Desktop\\grafy.txt", ios::in);
  65.         if (plik.good() == true)
  66.         {
  67.  
  68.             while (!plik.eof())
  69.             {
  70.                 int found;
  71.                 string halp;
  72.                 int i = 0;
  73.                 while ((getline(plik, linia)))
  74.                 {
  75.                     found = linia.find(" ");
  76.                     halp.insert(0, linia, 0, found); //indeks kto
  77.                     v1 = atoi(halp.c_str());
  78.  
  79.                     halp.clear();
  80.                     halp.insert(0, linia, found + 1, 6);
  81.                     v2 = atoi(halp.c_str());
  82.                     tab[v1][i] = 1;
  83.                     tab[v2][i] = 1;
  84.                     cout << v1 << " " << v2 << endl;
  85.                     halp.clear();
  86.                     i++;
  87.  
  88.  
  89.                 }
  90.  
  91.                 plik.close();
  92.                 cout << "Plik wczytany!" << endl;
  93.             }
  94.  
  95.  
  96.  
  97.             /* cout << endl << "Macierz INCYDENCJI po wypelnieniu" << endl << endl;
  98.             cout << "   ";
  99.             for(i = 0; i < ile_krawedzi; i++) cout << setw(3) << i;
  100.             cout << endl << endl;
  101.             for(i = 0; i < 7; i++)
  102.             {
  103.             cout << setw(3) << i;
  104.             for(j = 0; j < ile_krawedzi; j++) cout << setw(3) << (int) tab[i][j];
  105.             cout << endl;
  106.             }*/
  107.  
  108.  
  109.         }
  110.     }
  111. }
  112.  
  113. void DFSincyd(int v)
  114. {
  115.     visited[v] = true;              // Zaznaczamy wierzchołek jako odwiedzony
  116.  
  117.     cout << v << " ";               // Wypisujemy numer odwiedzonego wierzchołka
  118.  
  119.     for (int i = ile_krawedzi - 1; i >= 0; i--)
  120.         if (tab[v][i])                   // Jeśli v należy do i-tej krawędzi,
  121.             for (int j = 0; j < 7; j++)  // to szukamy jej drugiego końca
  122.                 if (j != v && tab[j][i])
  123.                 {
  124.                     if (!visited[j]) DFSincyd(j); // Jeśli nie odwiedzony, idziemy do niego
  125.                     break;
  126.                 }
  127. }
  128. int main()
  129. {
  130.     macierzIncyd();
  131.     DFSincyd(1);
  132.  
  133.     system("Pause");
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement