Advertisement
Guest User

class graph

a guest
May 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. ///////////////     КЛАСС  //////////////////////
  7. class MyGraph
  8. {
  9. private:
  10.     short** matrix;
  11.     unsigned short size;
  12. public:
  13.     MyGraph(unsigned short count_of_tops, bool with_nul);
  14.     ~MyGraph();
  15. public:
  16.     void print_matrix();
  17.     unsigned short count_of_ways();
  18. public:
  19.     short* operator[](unsigned short index);
  20.  
  21. public:
  22.     unsigned short get_size();
  23.     short** get_pointer();
  24. };
  25.  
  26. ///////////////     КОНСТРУКТОР  ///////////////////////
  27. MyGraph::MyGraph(unsigned short count_of_tops, bool with_null = true)
  28. {
  29.     size = count_of_tops;
  30.     matrix = new short*[count_of_tops];
  31.     for (unsigned short i = 0; i < count_of_tops; i++)
  32.         matrix[i] = new short[count_of_tops];
  33.  
  34.     if (with_null)
  35.         for (unsigned short i = 0; i < count_of_tops; i++)
  36.             for (unsigned short j = 0; j < count_of_tops; j++)
  37.                 matrix[i][j] = 0;
  38. }
  39.  
  40.  
  41. ///////////////     ДЕСТРУКТОР    //////////////////////
  42. MyGraph::~MyGraph()
  43. {
  44.  
  45. }
  46.  
  47.  
  48. ///////////////     МЕТОДЫ        //////////////////////
  49. void MyGraph::print_matrix()
  50. {
  51.     for (unsigned short i = 0; i < size; i++)
  52.     {
  53.         for (unsigned short j = 0; j < size; j++)
  54.         {
  55.             cout << '\t' << matrix[i][j];
  56.         }
  57.         cout << endl;
  58.     }
  59. }
  60.  
  61. unsigned short MyGraph::count_of_ways()
  62. {
  63.     unsigned short count = 0;
  64.  
  65.     for (unsigned short i = 0; i < size; i++)
  66.         for (unsigned short j = 0; j < size; j++)
  67.             if (matrix[i][j] != 0)
  68.                 count++;
  69.  
  70.     return count / 2;
  71. }
  72.  
  73. ///////////////     ГЕТТЕРЫ      //////////////////////
  74. unsigned short MyGraph::get_size()
  75. {
  76.     return size;
  77. }
  78.  
  79. short** MyGraph::get_pointer()
  80. {
  81.     return matrix;
  82. }
  83.  
  84.  
  85. ///////////////     ОПЕРАТОРЫ      //////////////////////
  86. short* MyGraph::operator[](unsigned short index)
  87. {
  88.     return matrix[index];
  89. }
  90.  
  91.  
  92. //////////////////////////////////////////////////////////
  93. ////////////////    ФУНКЦИИ      //////////////////////////
  94. void console_read_matrix(MyGraph graph)
  95. {
  96.     for (unsigned short i = 0; i < graph.get_size(); i++)
  97.         for (unsigned short j = 0; j < graph.get_size(); j++)
  98.         {
  99.             cin >> graph[i][j];
  100.         }
  101. }
  102.  
  103. void console_write(MyGraph graph)
  104. {
  105.     for (unsigned short i = 0; i < graph.get_size(); i++)
  106.     {
  107.         for (unsigned short j = 0; j < graph.get_size(); j++)
  108.         {
  109.             cout << graph[i][j] << ' ';
  110.         }
  111.         cout << endl;
  112.     }
  113.     cout << endl;
  114. }
  115.  
  116. void console_read_ways(MyGraph graph, unsigned short count_of_ways, bool oriented = false)
  117. {
  118.     unsigned short v1, v2;
  119.     for (unsigned short i = 0; i < count_of_ways; i++)
  120.     {
  121.         // - 1
  122.         cin >> v1 >> v2;
  123.         graph[v1 - 1][v2 - 1] = 1;
  124.         if (!oriented)
  125.             graph[v2 - 1][v1 - 1] = 1;
  126.            
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement