Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #pragma once
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #include <fstream>
  5. #include <string>
  6. #include <iostream>
  7. #include <ctime>
  8. #include <cstdlib>
  9. #include <algorithm>
  10. #include <climits>
  11. #include <Windows.h>
  12.  
  13. using namespace std;
  14.  
  15.  
  16.  
  17. class Graph
  18. {
  19. private:
  20.     int N;
  21.     int **matrix;
  22.  
  23. public:
  24.     Graph(string fileName);
  25.     ~Graph();
  26.     void printGraph();
  27.  
  28. };
  29.  
  30.  
  31. #include "Graph.h"
  32.  
  33. Graph::Graph(string fileName)
  34. {
  35.     ifstream file;
  36.     string temp;
  37.     file.open(fileName.c_str());
  38.  
  39.     if (file.is_open()) {
  40.  
  41.         do {
  42.             file >> temp;
  43.         } while (strcmp(temp.c_str(), "DIMENSION:") && strcmp(temp.c_str(), "DIMENSION"));
  44.         file >> temp;
  45.         if (!strcmp(temp.c_str(), ":"))
  46.             file >> this->N;
  47.         else {
  48.             this->N = stoi(temp);
  49.         }
  50.  
  51.         do {
  52.             file >> temp;
  53.         } while (strcmp(temp.c_str(), "EDGE_WEIGHT_SECTION"));
  54.  
  55.  
  56.         this->matrix = new int*[N];
  57.         for (int i = 0; i < N; i++) {
  58.             matrix[i] = new int[N];
  59.         }
  60.         for (int i = 0; i < N; i++) {
  61.             for (int j = 0; j < N; j++) {
  62.                 file >> matrix[i][j];
  63.  
  64.                 if (i == j) matrix[i][j] = -1;
  65.                 if (matrix[i][j] >= 9999 || matrix[i][j] == 0) matrix[i][j] = -1;
  66.             }
  67.         }
  68.     }
  69.     else {
  70.         cout << "Error occured during loading from file";
  71.     }
  72.     cout << "File loaded to matrix" << endl;
  73. }
  74.  
  75.  
  76. Graph::~Graph() {
  77.     for (int i = 0; i < N; i++) {
  78.         delete[] matrix[i];
  79.     }
  80.     delete[] matrix;
  81. }
  82.  
  83. void Graph::printGraph() {
  84.  
  85.     for (int i = 0; i < N; i++) {
  86.         for (int j = 0; j < N; j++) {
  87.             cout.width(4);
  88.             cout.fill(' ');
  89.             cout << matrix[i][j];
  90.         }
  91.         cout << endl;
  92.     }
  93. }
  94.  
  95.  
  96.  
  97.  
  98. // PEA_TSP_BB.cpp : Defines the entry point for the console application.
  99. //
  100.  
  101. #include "stdafx.h"
  102. #include "Graph.h"
  103.  
  104. using namespace std;
  105.  
  106. int main()
  107. {
  108.     string fileName;
  109.     cout << "Provide file name to load graph" << endl;
  110.     cin >> fileName;
  111.  
  112.     Graph *g = new Graph(fileName);
  113.     //g ->printGraph();
  114.  
  115.  
  116.     system("PAUSE");
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement