Advertisement
wojtas626

wczytanie grafu z pliku

Mar 13th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. void makeArrayOfArray(int ***newArray, int x, int y)
  9. {
  10.     int *arrPointer;
  11.  
  12.     *newArray = new int*[x];
  13.     arrPointer = **newArray;
  14.  
  15.     for (int i = 0; i < x; i++)
  16.     {
  17.         arrPointer = new int[y];
  18.         (*newArray)[i] = arrPointer;
  19.         arrPointer++;
  20.     }
  21. }
  22.  
  23.  
  24.  
  25. void fillArray(int **arrayToFill, int x, int y)
  26. {
  27.     for (int i = 0; i < x; i++)
  28.     {
  29.         for (int j = 0; j < y; j++)
  30.         {
  31.             arrayToFill[i][j] = -1;
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37. void showArray(int **arrayToShow, int x, int y)
  38. {
  39.     for (int i = 0; i < x; i++)
  40.     {
  41.         for (int j = 0; j < y; j++)
  42.         {
  43.             cout << arrayToShow[i][j] << " ";
  44.         }
  45.         cout << endl;
  46.     }
  47. }
  48.  
  49.  
  50. void deleteArray(int ***arrayToDelete, int x)
  51. {
  52.     for (int i = 0; i < x; i++)
  53.     {
  54.         delete [] (*arrayToDelete)[i];
  55.     }
  56.     delete [] (*arrayToDelete);
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62.     ifstream myFile("network.txt");
  63.  
  64.     if(!myFile)
  65.         cout << "Nie mo¿na otworzyæ pliku!" << endl;
  66.     else
  67.     {
  68.         int **matrix;
  69.         string input;
  70.         int size;
  71.  
  72.         int weight;
  73.  
  74.         myFile >> size;
  75.         makeArrayOfArray(&matrix, size, size);
  76.         fillArray(matrix, size, size);
  77.  
  78.  
  79.         weight = 0;
  80.  
  81.         int j = 0;
  82.         int k = 0;
  83.         int stringLength;
  84.  
  85.         for (int i = 0; i < size; i++)
  86.         {
  87.  
  88.             myFile >> input;
  89.             stringLength = input.length();
  90.  
  91.  
  92.             k = 0;
  93.             while (true)
  94.             {
  95.                 if (k == stringLength)
  96.                 {
  97.                     matrix[i][j] = weight;
  98.                     j = 0;
  99.                     weight = 0;
  100.                     break;
  101.                 }
  102.  
  103.                 if ( j == size)
  104.                 {
  105.                     j = 0;
  106.                     break;
  107.                 }
  108.  
  109.                 if (input[k] == '-')
  110.                 {
  111.                     matrix[i][j] = -1;
  112.                     weight = 0;
  113.                 }
  114.                 else if (input[k] != ',')
  115.                 {
  116.                     weight = weight*10 + input[k] - 48;
  117.                 }
  118.                 else
  119.                 {
  120.                     matrix[i][j] = weight;
  121.                     j++;
  122.                     weight = 0;
  123.                 }
  124.                 k++;
  125.             }
  126.  
  127.         }
  128.  
  129.         cout << endl << endl << endl;
  130.         showArray(matrix, size, size);
  131.  
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement