Advertisement
ithoran

Struk Lab 6

May 23rd, 2016
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. // matrica susedstva.h
  2. #pragma once
  3.  
  4. class MatricaSusedstva {
  5.     int ***mat;
  6.     int n;
  7. public:
  8.     MatricaSusedstva(int x);
  9.     ~MatricaSusedstva();
  10.     void insertEdge(int c1, int c2, int x);
  11.     void deleteEdge(int c1, int c2);
  12.     int** findNode(int x);
  13.     int* findEdge(int x);
  14.     void stampaj();
  15. };
  16.  
  17. // .cpp
  18. #pragma once
  19. #include <iostream>
  20. #include "MatSusedstva.h"
  21. using namespace std;
  22.  
  23. MatricaSusedstva::MatricaSusedstva(int x) {
  24.     n = x;
  25.     mat = new int**[n];
  26.     for (int i = 0; i < n; i++)
  27.         mat[i] = new int*[n]();
  28. }
  29.  
  30. MatricaSusedstva::~MatricaSusedstva() {
  31.     if (mat != 0) {
  32.         for (int i = 0; i < n; i++) {
  33.             for (int j = 0; j < n; j++)
  34.                 if (mat[i][j] != 0)
  35.                     delete mat[i][j];
  36.             delete[] mat[i];
  37.         }
  38.         delete[] mat;
  39.     }
  40. }
  41.  
  42. void MatricaSusedstva::insertEdge(int c1, int c2, int x) {
  43.     if (c1 < 0 || c2 < 0 || c1 >= n || c2 >= n) {
  44.         cout << "Nevalidna vrednost cvora!\n";
  45.         return;
  46.     }
  47.     mat[c1][c2] = new int(x);
  48. }
  49.  
  50. void MatricaSusedstva::deleteEdge(int c1, int c2) {
  51.     if (c1 < 0 || c2 < 0 || c1 >= n || c2 >= n) {
  52.         cout << "Nevalidna vrednost cvora!\n";
  53.         return;
  54.     }
  55.     if (mat[c1][c2] == 0) {
  56.         cout << "Taj poteg ne postoji!\n";
  57.         return;
  58.     }
  59.     delete mat[c1][c2];
  60.     mat[c1][c2] = 0;
  61. }
  62.  
  63. int** MatricaSusedstva::findNode(int x) {
  64.     if (x >= n || x < 0) {
  65.         cout << "Taj cvor ne postoji!\n";
  66.         return 0;
  67.     }
  68.     return mat[x];
  69. }
  70.  
  71. int* MatricaSusedstva::findEdge(int x) {
  72.     for (int i = 0; i < n; i++) {
  73.         for (int j = 0; j < n; j++) {
  74.             if (*mat[i][j] == x)
  75.                 return mat[i][j];
  76.         }
  77.     }
  78.     cout << "Taj poteg ne postoji!\n";
  79.     return 0;
  80. }
  81.  
  82. void MatricaSusedstva::stampaj() {
  83.     for (int i = 0; i < n; i++) {
  84.         for (int j = 0; j < n; j++) {
  85.             if (mat[i][j] != 0)
  86.                 cout << 1 << "\t";
  87.             else
  88.                 cout << 0 << "\t";
  89.         }
  90.         cout << endl;
  91.     }
  92. }
  93.  
  94. // mejn
  95. #pragma once
  96. #include "MatSusedstva.h"
  97. #include <iostream>
  98. using namespace std;
  99.  
  100. void main() {
  101.     MatricaSusedstva mat(4);
  102.     mat.stampaj();
  103.     cout << "\n";
  104.     mat.insertEdge(0, 2, -9);
  105.     mat.insertEdge(1, 3, 10);
  106.     mat.stampaj();
  107.     cout << endl;
  108.     mat.deleteEdge(0, 2);
  109.     mat.stampaj();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement