Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <mm_malloc.h>
- /* Exceptions */
- const char* namesOfExceptions[] = {
- "bad number of vertices",
- "bad number of edges",
- "bad vertex",
- "bad number of lines"
- };
- typedef enum {
- BAD_NUMBER_VERTICES = 1,
- BAD_NUMBER_EDGES,
- BAD_INDEX_OF_VERTICE,
- BAD_INPUT
- } Exceptions;
- short flagOfException = 0;
- /* Definition of struct Graph and it's methods */
- typedef struct {
- bool** connectivityTable;
- unsigned int numberOfVertices;
- unsigned int numberOfEdges;
- } Graph;
- void checkQuantities(unsigned int n, unsigned int m) {
- if (n < 0 || n > 1000) {
- printf("%s", namesOfExceptions[0]);
- flagOfException = BAD_NUMBER_VERTICES;
- return;
- } else if (m < 0 || m > (n * (n + 1) / 2)) {
- printf("%s", namesOfExceptions[1]);
- flagOfException = BAD_NUMBER_EDGES;
- return;
- }
- }
- void checkIndexes(int n, unsigned int v1, unsigned int v2) {
- if (v1 < 1 || v1 > n) {
- printf("%s", namesOfExceptions[2]);
- flagOfException = BAD_INDEX_OF_VERTICE;
- return;
- } else if (v2 < 1 || v2 > n) {
- printf("%s", namesOfExceptions[2]);
- flagOfException = BAD_INDEX_OF_VERTICE;
- return;
- }
- }
- void getConnectivityTable(Graph* graph, unsigned int n, unsigned int m) {
- graph -> connectivityTable = (bool**)calloc(n, sizeof(bool*));
- for (unsigned int i = 0; i < n; i++) {
- graph -> connectivityTable[i] = (bool*)calloc(n, sizeof(bool));
- }
- for (unsigned int i = 0; i < n; i++) {
- for (unsigned int j = 0; j < n; j++) {
- graph -> connectivityTable[i][j] = false;
- }
- }
- for (unsigned int i = 0; i < m; i++) {
- unsigned int verticeFrom, verticeTo;
- if (scanf("%d%d", &verticeFrom, &verticeTo) != 2) {
- printf("%s", namesOfExceptions[3]);
- flagOfException = BAD_INPUT;
- return;
- } else {
- checkIndexes(n, verticeFrom, verticeTo);
- if (flagOfException == 0) {
- graph -> connectivityTable[verticeFrom - 1][verticeTo - 1] = true;
- } else return;
- }
- }
- }
- void createGraph(Graph* graph) {
- unsigned int n, m;
- if (scanf("%d", &n) == 0) {
- printf("%s", namesOfExceptions[3]);
- flagOfException = BAD_INPUT;
- return;
- }
- if (scanf("%d", &m) == 0) {
- printf("%s", namesOfExceptions[3]);
- flagOfException = BAD_INPUT;
- return;
- }
- checkQuantities(n, m);
- if (flagOfException == 0) {
- graph -> numberOfVertices = n;
- graph -> numberOfEdges = m;
- getConnectivityTable(graph, n, m);
- }
- }
- void topologicSort(Graph* graph) {
- }
- int main() {
- Graph* graph = calloc(1, sizeof(Graph));
- createGraph(graph);
- if (flagOfException > 0) {
- free(graph);
- return flagOfException;
- }
- topologicSort(graph);
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement