From b2137f34e684e3c54c3c96702b303cf2abb353d3 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 13 Dec 2018 01:45:06 +0300 Subject: [PATCH 1/4] Added first version of test 3.3 --- sem1/test3/test3.3/file.txt | 6 ++ sem1/test3/test3.3/graph.cpp | 149 +++++++++++++++++++++++++++++++++++ sem1/test3/test3.3/graph.h | 10 +++ sem1/test3/test3.3/main.cpp | 14 ++++ 4 files changed, 179 insertions(+) create mode 100644 sem1/test3/test3.3/file.txt create mode 100644 sem1/test3/test3.3/graph.cpp create mode 100644 sem1/test3/test3.3/graph.h create mode 100644 sem1/test3/test3.3/main.cpp diff --git a/sem1/test3/test3.3/file.txt b/sem1/test3/test3.3/file.txt new file mode 100644 index 0000000..a54a632 --- /dev/null +++ b/sem1/test3/test3.3/file.txt @@ -0,0 +1,6 @@ +6 5 +-1 1 -1 0 -1 0 +1 0 0 -1 0 0 +0 -1 0 0 0 1 +0 0 1 0 0 -1 +0 0 0 1 1 0 \ 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..31706ca --- /dev/null +++ b/sem1/test3/test3.3/graph.cpp @@ -0,0 +1,149 @@ +#include +#include +#include "graph.h" +using namespace std; + +struct Graph +{ + int ribs; + 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->ribs = readNumber(file, symbol); + graph->vertexes = readNumber(file, symbol); + graph->select = new bool[graph->vertexes] {}; + graph->graph = new int *[graph->vertexes] {}; + for (int i = 0; i < graph->vertexes; i++) + { + graph->graph[i] = new int[graph->ribs] {}; + } + 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->ribs; j++) + { + graph->graph[i][j] = readNumber(file, symbol); + } + } + file.close(); +} + +void findVertex(Graph *graph, int indexOfVertex, bool *visited) +{ + visited[indexOfVertex] = true; + for (int i = 0; i < graph->ribs; i++) + { + if (graph->graph[indexOfVertex][i] == -1) + { + for (int j = 0; j < graph->vertexes; j++) + { + if (graph->graph[j][i] == 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->vertexes; i++) + { + delete[] graph->graph[i]; + } + delete[] graph->graph; + delete[] graph->select; + delete graph; +} diff --git a/sem1/test3/test3.3/graph.h b/sem1/test3/test3.3/graph.h new file mode 100644 index 0000000..b5f9799 --- /dev/null +++ b/sem1/test3/test3.3/graph.h @@ -0,0 +1,10 @@ +#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); 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); +} From d756b57c43a7fa2f14ce69a52e9c45f975edaa42 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 13 Dec 2018 02:01:08 +0300 Subject: [PATCH 2/4] Corrected typos --- sem1/test3/test3.3/graph.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sem1/test3/test3.3/graph.cpp b/sem1/test3/test3.3/graph.cpp index 31706ca..1d9fedf 100644 --- a/sem1/test3/test3.3/graph.cpp +++ b/sem1/test3/test3.3/graph.cpp @@ -5,7 +5,7 @@ using namespace std; struct Graph { - int ribs; + int ways; int vertexes; bool *select; int **graph; @@ -35,13 +35,13 @@ Graph *createGraph(const char *nameOfFile) ifstream file(nameOfFile); char symbol = '\0'; file.get(symbol); - graph->ribs = readNumber(file, symbol); + graph->ways = readNumber(file, symbol); graph->vertexes = readNumber(file, symbol); graph->select = new bool[graph->vertexes] {}; graph->graph = new int *[graph->vertexes] {}; for (int i = 0; i < graph->vertexes; i++) { - graph->graph[i] = new int[graph->ribs] {}; + graph->graph[i] = new int[graph->ways] {}; } file.close(); return graph; @@ -59,7 +59,7 @@ void fillGraph(Graph *graph, const char *nameOfFile) file.get(symbol); for (int i = 0; i < graph->vertexes; i++) { - for (int j = 0; j < graph->ribs; j++) + for (int j = 0; j < graph->ways; j++) { graph->graph[i][j] = readNumber(file, symbol); } @@ -70,7 +70,7 @@ void fillGraph(Graph *graph, const char *nameOfFile) void findVertex(Graph *graph, int indexOfVertex, bool *visited) { visited[indexOfVertex] = true; - for (int i = 0; i < graph->ribs; i++) + for (int i = 0; i < graph->ways; i++) { if (graph->graph[indexOfVertex][i] == -1) { From 561e7078eba8d5db292d67f8b439613b9dbcb9c5 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 13 Dec 2018 02:21:46 +0300 Subject: [PATCH 3/4] Added file --- sem1/test3/test3.3/file.txt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sem1/test3/test3.3/file.txt b/sem1/test3/test3.3/file.txt index a54a632..3c74f58 100644 --- a/sem1/test3/test3.3/file.txt +++ b/sem1/test3/test3.3/file.txt @@ -1,6 +1,7 @@ -6 5 --1 1 -1 0 -1 0 -1 0 0 -1 0 0 -0 -1 0 0 0 1 -0 0 1 0 0 -1 -0 0 0 1 1 0 \ No newline at end of file +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 From dc09dfe0b524f4d7131682c1defc26dd44025464 Mon Sep 17 00:00:00 2001 From: Anton Chernikov Date: Thu, 13 Dec 2018 01:56:16 +0300 Subject: [PATCH 4/4] Updated --- sem1/test3/test3.3/graph.cpp | 26 +++++++++++++++++++------- sem1/test3/test3.3/graph.h | 2 ++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/sem1/test3/test3.3/graph.cpp b/sem1/test3/test3.3/graph.cpp index 1d9fedf..d65bf59 100644 --- a/sem1/test3/test3.3/graph.cpp +++ b/sem1/test3/test3.3/graph.cpp @@ -38,10 +38,10 @@ Graph *createGraph(const char *nameOfFile) graph->ways = readNumber(file, symbol); graph->vertexes = readNumber(file, symbol); graph->select = new bool[graph->vertexes] {}; - graph->graph = new int *[graph->vertexes] {}; - for (int i = 0; i < graph->vertexes; i++) + graph->graph = new int *[graph->ways] {}; + for (int i = 0; i < graph->ways; i++) { - graph->graph[i] = new int[graph->ways] {}; + graph->graph[i] = new int[graph->vertexes] {}; } file.close(); return graph; @@ -61,7 +61,7 @@ void fillGraph(Graph *graph, const char *nameOfFile) { for (int j = 0; j < graph->ways; j++) { - graph->graph[i][j] = readNumber(file, symbol); + graph->graph[j][i] = readNumber(file, symbol); } } file.close(); @@ -72,11 +72,11 @@ void findVertex(Graph *graph, int indexOfVertex, bool *visited) visited[indexOfVertex] = true; for (int i = 0; i < graph->ways; i++) { - if (graph->graph[indexOfVertex][i] == -1) + if (graph->graph[i][indexOfVertex] == -1) { for (int j = 0; j < graph->vertexes; j++) { - if (graph->graph[j][i] == 1 && !visited[j]) + if (graph->graph[i][j] == 1 && !visited[j]) { findVertex(graph, j, visited); } @@ -139,7 +139,7 @@ void displayVertexes(Graph *graph) void deleteGraph(Graph *graph) { - for (int i = 0; i < graph->vertexes; i++) + for (int i = 0; i < graph->ways; i++) { delete[] graph->graph[i]; } @@ -147,3 +147,15 @@ void deleteGraph(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 index b5f9799..341b777 100644 --- a/sem1/test3/test3.3/graph.h +++ b/sem1/test3/test3.3/graph.h @@ -8,3 +8,5 @@ void deleteGraph(Graph *graph); void fillGraph(Graph *graph, const char *nameOfFile); void findVertexes(Graph *graph); void displayVertexes(Graph *graph); + +void displayGraph(Graph *graph);