Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sem1/test3/test3.3/file.txt
Original file line number Diff line number Diff line change
@@ -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
161 changes: 161 additions & 0 deletions sem1/test3/test3.3/graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include <iostream>
#include <fstream>
#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;
}
}
12 changes: 12 additions & 0 deletions sem1/test3/test3.3/graph.h
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions sem1/test3/test3.3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#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);
}