Advertisement
AdrianMadajewski

WDI Homework

Nov 24th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. // Adrian Madajewski I6.1 145406 Wprowadzenie do Informatyki
  2.  
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <conio.h>
  6.  
  7. class Graph {
  8. private:
  9.     int id;
  10.     int size;
  11.     int **zone;
  12. public:
  13.     Graph(int id, int size) {
  14.         printf("Creating graph instance... (id-%d)\n", id);
  15.         this->size = size;
  16.         this->id = id;
  17.  
  18.         zone = (int**)malloc(size * sizeof(int));
  19.         if (zone == NULL) {
  20.             fprintf(stderr, "out of memory\n");
  21.             exit(0);
  22.         }
  23.  
  24.         for (int i = 0; i < size; ++i) {
  25.             zone[i] = (int*)malloc(size * sizeof(int));
  26.             if (zone[i] == NULL) {
  27.                 fprintf(stderr, "out of memory\n");
  28.                 exit(0);
  29.             }
  30.         }
  31.         // set all to 0s
  32.         for (int i = 0; i < size; ++i)
  33.             for (int j = 0; j < size; ++j)
  34.                 zone[i][j] = 0;
  35.     }
  36.     ~Graph() {
  37.         printf("Deleting graph instance... (id-%d)\n", id);
  38.         for (int i = 0; i < size; ++i) {
  39.             free(zone[i]);
  40.         }
  41.         free(zone);
  42.     }
  43.  
  44.     void addEdge(int pos_y, int pos_x, int value) {
  45.         if (pos_y < size && pos_x < size)
  46.             zone[pos_y][pos_x] = value;
  47.         else
  48.             printf("Position index out of zone size range\n");
  49.            
  50.     }
  51.    
  52.     void printMatrix() {
  53.         for (int i = 0; i < size; ++i) {
  54.             for (int j = 0; j < size; ++j) {
  55.                 printf("%d ", zone[j][i]);
  56.             }
  57.             printf("\n");
  58.         }
  59.     }
  60.  
  61.     int getPathLength(int pos_y, int pos_x) {
  62.         return zone[pos_y][pos_x];
  63.     }
  64.  
  65.     int getId() {
  66.         return id;
  67.     }
  68.  
  69.     // Nie wiem jak to ma dzialac wiec pominie cialo funkcji
  70.     // zeby moc zrobic dalsze czesci zadania
  71.     int getMaxLength(int pos_y, int pos_x) {
  72.         return -1;
  73.     }
  74.  
  75.     // Tak samo tutaj suma wszystkich mozliwych sciezek
  76.     int sumAllPath() {
  77.         return -1;
  78.     }
  79. };
  80.  
  81. typedef struct Node {
  82.     Graph current_graph;
  83.     Node *next_graph;
  84. } Node_t;
  85.  
  86. bool isEmpty(Node_t *node) {
  87.     return node == NULL;
  88. }
  89.  
  90. Node_t *createNode_t(Graph &graph, Node_t *next_graph) {
  91.     Node_t *node = (Node_t *)malloc(sizeof(Node_t));
  92.     node->current_graph = graph;
  93.     node->next_graph = next_graph;
  94.     return node;
  95. }
  96.  
  97. void addGraph(Node_t *head, Graph &graph) {
  98.     Node_t *new_node = createNode_t(graph, NULL);
  99.     Node_t *it = head;
  100.     while (!isEmpty(it->next_graph)) {
  101.         it = it->next_graph;
  102.     }
  103.     it->next_graph = new_node;
  104. }
  105.  
  106. int findMaxLength(Node_t *head, int pos_y, int pos_x) {
  107.     // dla kazdego graphu z tej listy wyszukaj najwieksza dlugosc miedzy
  108.     // kazdym odcinkiem i ja zwroc
  109.     Node_t *it = head;
  110.     int max_length = -1;
  111.     while (!isEmpty(it->next_graph)) {
  112.         it = it->next_graph;
  113.         if (it->current_graph.getMaxLength(pos_y, pos_x) > max_length) {
  114.             max_length = it->current_graph.getMaxLength(pos_y, pos_x);
  115.         }
  116.     }
  117.     return max_length;
  118. }
  119.  
  120. void removeSmallest(Node_t *head) {
  121.     Node_t *it = head;
  122.     int smallest = INT_MAX;
  123.     while (!isEmpty(it)) {
  124.         if (it->current_graph.sumAllPath() < smallest) {
  125.             smallest = it->current_graph.sumAllPath();
  126.         }
  127.     }
  128.  
  129.     // Tutaj powinieniem wyszukac teraz ten graf ktory mial ta najmniejsza sume
  130.     // i usunac go, przesuwajac te elementy kolejne w miejsce wskaznika
  131.     // na ten dany graf, ale nie wiem jak to napisac :/
  132. }
  133.  
  134. void printAllGraphs(Node_t *head) {
  135.     Node_t *it = head;
  136.  
  137.     while (!isEmpty(it)) {
  138.         printf("%d\n", it->current_graph.getId());
  139.         it->current_graph.printMatrix();
  140.         printf("\n\n");
  141.         it = it->next_graph;
  142.     }
  143.     printf("\n");
  144. }
  145.  
  146. int main(void) {
  147.     Graph head(0, 5);
  148.     head.addEdge(2, 3, 20);
  149.     head.addEdge(4, 1, 4);
  150.     head.addEdge(2, 3, 1);
  151.     Graph g1(1, 20);
  152.     g1.addEdge(1, 2, 2);
  153.     g1.addEdge(0, 2, 4);
  154.     Graph g2(2, 3);
  155.     g2.addEdge(1, 2, 3);
  156.     g2.addEdge(0, 1, 40);
  157.     Graph g3(10, 4);
  158.     g3.addEdge(0, 3, 45);
  159.     g3.addEdge(0, 2, 100);
  160.     Node_t *HEAD = createNode_t(head, NULL);
  161.    
  162.     addGraph(HEAD, g1);
  163.     addGraph(HEAD, g2);
  164.     addGraph(HEAD, g3);
  165.    
  166.     printAllGraphs(HEAD);
  167.  
  168.     _getch();
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement