Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <mm_malloc.h>
  4.  
  5. /* Exceptions */
  6. const char* namesOfExceptions[] = {
  7.         "bad number of vertices",
  8.         "bad number of edges",
  9.         "bad vertex",
  10.         "bad number of lines"
  11. };
  12.  
  13. typedef enum {
  14.     BAD_NUMBER_VERTICES = 1,
  15.     BAD_NUMBER_EDGES,
  16.     BAD_INDEX_OF_VERTICE,
  17.     BAD_INPUT
  18. } Exceptions;
  19.  
  20. short flagOfException = 0;
  21.  
  22. /* Definition of struct Graph and it's methods */
  23.  
  24. typedef struct {
  25.     bool** connectivityTable;
  26.     unsigned int numberOfVertices;
  27.     unsigned int numberOfEdges;
  28. } Graph;
  29.  
  30. void checkQuantities(unsigned int n, unsigned int m) {
  31.     if (n < 0 || n > 1000) {
  32.         printf("%s", namesOfExceptions[0]);
  33.         flagOfException = BAD_NUMBER_VERTICES;
  34.         return;
  35.     } else if (m < 0 || m > (n * (n + 1) / 2)) {
  36.         printf("%s", namesOfExceptions[1]);
  37.         flagOfException = BAD_NUMBER_EDGES;
  38.         return;
  39.     }
  40. }
  41.  
  42. void checkIndexes(int n, unsigned int v1, unsigned int v2) {
  43.     if (v1 < 1 || v1 > n) {
  44.         printf("%s", namesOfExceptions[2]);
  45.         flagOfException = BAD_INDEX_OF_VERTICE;
  46.         return;
  47.     } else if (v2 < 1 || v2 > n) {
  48.         printf("%s", namesOfExceptions[2]);
  49.         flagOfException = BAD_INDEX_OF_VERTICE;
  50.         return;
  51.     }
  52. }
  53.  
  54. void getConnectivityTable(Graph* graph, unsigned int n, unsigned int m) {
  55.     graph -> connectivityTable = (bool**)calloc(n, sizeof(bool*));
  56.     for (unsigned int i = 0; i < n; i++) {
  57.         graph -> connectivityTable[i] = (bool*)calloc(n, sizeof(bool));
  58.     }
  59.     for (unsigned int i = 0; i < n; i++) {
  60.         for (unsigned int j = 0; j < n; j++) {
  61.             graph -> connectivityTable[i][j] = false;
  62.         }
  63.     }
  64.     for (unsigned int i = 0; i < m; i++) {
  65.         unsigned int verticeFrom, verticeTo;
  66.         if (scanf("%d%d", &verticeFrom, &verticeTo) != 2) {
  67.             printf("%s", namesOfExceptions[3]);
  68.             flagOfException = BAD_INPUT;
  69.             return;
  70.         } else {
  71.             checkIndexes(n, verticeFrom, verticeTo);
  72.             if (flagOfException == 0) {
  73.                 graph -> connectivityTable[verticeFrom - 1][verticeTo - 1] = true;
  74.             } else return;
  75.         }
  76.     }
  77. }
  78.  
  79. void createGraph(Graph* graph) {
  80.     unsigned int n, m;
  81.     if (scanf("%d", &n) == 0) {
  82.         printf("%s", namesOfExceptions[3]);
  83.         flagOfException = BAD_INPUT;
  84.         return;
  85.     }
  86.     if (scanf("%d", &m) == 0) {
  87.         printf("%s", namesOfExceptions[3]);
  88.         flagOfException = BAD_INPUT;
  89.         return;
  90.     }
  91.     checkQuantities(n, m);
  92.     if (flagOfException == 0) {
  93.         graph -> numberOfVertices = n;
  94.         graph -> numberOfEdges = m;
  95.         getConnectivityTable(graph, n, m);
  96.     }
  97. }
  98.  
  99. void topologicSort(Graph* graph) {
  100.    
  101. }
  102.  
  103. int main() {
  104.     Graph* graph = calloc(1, sizeof(Graph));
  105.     createGraph(graph);
  106.     if (flagOfException > 0) {
  107.         free(graph);
  108.         return flagOfException;
  109.     }
  110.     topologicSort(graph);
  111.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement