diff --git a/sem1/test3/test3.3/file.txt b/sem1/test3/test3.3/file.txt new file mode 100644 index 0000000..3c74f58 --- /dev/null +++ b/sem1/test3/test3.3/file.txt @@ -0,0 +1,7 @@ +7 6 +1 -1 0 0 0 0 0 +0 1 -1 0 0 0 0 +-1 0 1 1 0 0 0 +0 0 0 -1 -1 -1 1 +0 0 0 0 1 0 0 +0 0 0 0 0 1 -1 \ No newline at end of file diff --git a/sem1/test3/test3.3/graph.cpp b/sem1/test3/test3.3/graph.cpp new file mode 100644 index 0000000..d65bf59 --- /dev/null +++ b/sem1/test3/test3.3/graph.cpp @@ -0,0 +1,161 @@ +#include +#include +#include "graph.h" +using namespace std; + +struct Graph +{ + int ways; + int vertexes; + bool *select; + int **graph; +}; + +int readNumber(ifstream &file, char &symbol) +{ + int result = 0; + int sign = 1; + if (symbol == '-') + { + sign = -sign; + file.get(symbol); + } + while (symbol != ' ' && symbol != '\n' && !file.eof()) + { + result = result * 10 + (symbol - '0'); + file.get(symbol); + } + file.get(symbol); + return result * sign; +} + +Graph *createGraph(const char *nameOfFile) +{ + Graph *graph = new Graph {}; + ifstream file(nameOfFile); + char symbol = '\0'; + file.get(symbol); + graph->ways = readNumber(file, symbol); + graph->vertexes = readNumber(file, symbol); + graph->select = new bool[graph->vertexes] {}; + graph->graph = new int *[graph->ways] {}; + for (int i = 0; i < graph->ways; i++) + { + graph->graph[i] = new int[graph->vertexes] {}; + } + file.close(); + return graph; +} + +void fillGraph(Graph *graph, const char *nameOfFile) +{ + ifstream file(nameOfFile); + char symbol = '\0'; + file.get(symbol); + while (symbol != '\n') + { + file.get(symbol); + } + file.get(symbol); + for (int i = 0; i < graph->vertexes; i++) + { + for (int j = 0; j < graph->ways; j++) + { + graph->graph[j][i] = readNumber(file, symbol); + } + } + file.close(); +} + +void findVertex(Graph *graph, int indexOfVertex, bool *visited) +{ + visited[indexOfVertex] = true; + for (int i = 0; i < graph->ways; i++) + { + if (graph->graph[i][indexOfVertex] == -1) + { + for (int j = 0; j < graph->vertexes; j++) + { + if (graph->graph[i][j] == 1 && !visited[j]) + { + findVertex(graph, j, visited); + } + } + } + } +} + +void nullVisited(bool *visited, int length) +{ + for (int i = 0; i < length; i++) + { + visited[i] = false; + } +} + +bool allVisited(bool *visited, int length) +{ + for (int i = 0; i < length; i++) + { + if (!visited[i]) + { + return false; + } + } + return true; +} + +void findVertexes(Graph *graph) +{ + bool *visited = new bool[graph->vertexes] {}; + for (int i = 0; i < graph->vertexes; i++) + { + nullVisited(visited, graph->vertexes); + findVertex(graph, i, visited); + if (allVisited(visited, graph->vertexes)) + { + graph->select[i] = true; + } + } + delete[] visited; +} + +void displayVertexes(Graph *graph) +{ + bool exists = false; + for (int i = 0; i < graph->vertexes; i++) + { + if (graph->select[i]) + { + exists = true; + cout << i << endl; + } + } + if (!exists) + { + cout << "Not found"; + } +} + +void deleteGraph(Graph *graph) +{ + for (int i = 0; i < graph->ways; i++) + { + delete[] graph->graph[i]; + } + delete[] graph->graph; + delete[] graph->select; + delete graph; +} + +void displayGraph(Graph *graph) +{ + for (int i = 0; i < graph->vertexes; i++) + { + for (int j = 0; j < graph->ways; j++) + { + cout << graph->graph[j][i] << " "; + } + cout << endl; + } +} diff --git a/sem1/test3/test3.3/graph.h b/sem1/test3/test3.3/graph.h new file mode 100644 index 0000000..341b777 --- /dev/null +++ b/sem1/test3/test3.3/graph.h @@ -0,0 +1,12 @@ +#pragma once + +struct Graph; + +Graph *createGraph(const char *nameOfFile); +void deleteGraph(Graph *graph); + +void fillGraph(Graph *graph, const char *nameOfFile); +void findVertexes(Graph *graph); +void displayVertexes(Graph *graph); + +void displayGraph(Graph *graph); diff --git a/sem1/test3/test3.3/main.cpp b/sem1/test3/test3.3/main.cpp new file mode 100644 index 0000000..006d30b --- /dev/null +++ b/sem1/test3/test3.3/main.cpp @@ -0,0 +1,14 @@ +#include +#include "graph.h" +using namespace std; + +int main() +{ + const char *nameOfFile = "file.txt"; + Graph *graph = createGraph(nameOfFile); + fillGraph(graph, nameOfFile); + findVertexes(graph); + cout << "Result:" << endl; + displayVertexes(graph); + deleteGraph(graph); +}