Skip to content

Commit

Permalink
added states structure, reworked graph structure, fixed mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiipatov committed Dec 18, 2021
1 parent a6ecc0d commit 7b5da1b
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 145 deletions.
101 changes: 24 additions & 77 deletions states/states/graph.c
Original file line number Diff line number Diff line change
@@ -1,134 +1,81 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "graph.h"

typedef struct Graph
{
List** capitals;
int numberOfCapitals;
int numberOfCities;
int matrix[NUMBER_OF_CITIES][NUMBER_OF_CITIES];
int numberOfNodes;
int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES];
} Graph;

Graph* createGraph(void)
{
Graph* graph = calloc(1, sizeof(Graph));
graph->capitals = calloc(NUMBER_OF_CITIES, sizeof(List*));
if (graph->capitals == NULL)
{
free(graph);
return NULL;
}
graph->numberOfCities = 0;
return graph;
}

void deleteGraph(Graph** graph)
{
int numberOfCapitals = (*graph)->numberOfCapitals;
for (int i = 0; i < numberOfCapitals; i++)
{
deleteList(&((*graph)->capitals[i]));
}
free(*graph);
*graph = NULL;
}

int getEdge(Graph* graph, int start, int end)
int getEdge(Graph* graph, const int start, const int end)
{
return graph->matrix[start][end];
}

void setEdge(Graph* graph, int firstNode, int secondNode, int weight)
void setEdge(Graph* graph, const int firstNode, const int secondNode, const int weight)
{
graph->matrix[firstNode][secondNode] = weight;
graph->matrix[secondNode][firstNode] = weight;
}

Graph* readFile(char* fileName)
Graph* buildGraph(FILE* input)
{
Graph* graph = createGraph();
Graph* graph = calloc(1, sizeof(Graph));
if (graph == NULL)
{
return NULL;
}
FILE* input = fopen(fileName, "r");
int numberOfCities = 0;
graph->numberOfNodes = 0;
int numberOfNodes = 0;
int numberOfRoads = 0;
fscanf_s(input, "%d%*c", &numberOfCities);
graph->numberOfCities = numberOfCities;
fscanf_s(input, "%d%*c", &numberOfNodes);
graph->numberOfNodes = numberOfNodes;
fscanf_s(input, "%d%*c", &numberOfRoads);
for (int index = 0; index < numberOfRoads; index++)
{
int cityIndex1 = 0;
int cityIndex2 = 0;
int nodeIndex1 = 0;
int nodeIndex2 = 0;
int roadLength = 0;
fscanf_s(input, "%d%*c", &cityIndex1);
fscanf_s(input, "%d%*c", &cityIndex2);
fscanf_s(input, "%d%*c", &nodeIndex1);
fscanf_s(input, "%d%*c", &nodeIndex2);
fscanf_s(input, "%d%*c", &roadLength);
if (cityIndex1 >= NUMBER_OF_CITIES || cityIndex2 >= NUMBER_OF_CITIES)
{
printf("City index should be less than %d", NUMBER_OF_CITIES);
deleteGraph(&graph);
fclose(input);
return NULL;
}
setEdge(graph, cityIndex1, cityIndex2, roadLength);
}
int numberOfCapitals = 0;
fscanf_s(input, "%d%*c", &numberOfCapitals);
graph->numberOfCapitals = numberOfCapitals;
for (int index = 0; index < numberOfCapitals; index++)
{
int capitalIndex = 0;
fscanf_s(input, "%d%*c", &capitalIndex);
graph->capitals[index] = createList();
if (!addAtTail(graph->capitals[index], capitalIndex))
if (nodeIndex1 >= NUMBER_OF_NODES || nodeIndex2 >= NUMBER_OF_NODES)
{
printf("City index should be less than %d", NUMBER_OF_NODES);
deleteGraph(&graph);
fclose(input);
return NULL;
}
setEdge(graph, nodeIndex1, nodeIndex2, roadLength);
}
fclose(input);
return graph;
}

void deleteColumn(Graph* graph, int column)
void deleteColumn(Graph* graph, const int column)
{
for (int index = 0; index < NUMBER_OF_CITIES; index++)
for (int index = 0; index < NUMBER_OF_NODES; index++)
{
graph->matrix[index][column] = 0;
}
}

int getNumberOfCapitals(Graph* graph)
{
return graph->numberOfCapitals;
}

int getNumberOfCities(Graph* graph)
{
return graph->numberOfCities;
}

List** getCapitals(Graph* graph)
{
return graph->capitals;
}

void getRelatedCities(Graph* graph, int city, int* cities[NUMBER_OF_CITIES])
int getNumberOfNodes(Graph* graph)
{
*cities = graph->matrix[city];
return graph->numberOfNodes;
}

void getMatrix(Graph* graph, int matrix[NUMBER_OF_CITIES][NUMBER_OF_CITIES])
void getMatrix(Graph* graph, int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES])
{
for (int row = 0; row < NUMBER_OF_CITIES; row++)
for (int row = 0; row < NUMBER_OF_NODES; row++)
{
for (int column = 0; column < NUMBER_OF_CITIES; column++)
for (int column = 0; column < NUMBER_OF_NODES; column++)
{
matrix[row][column] = graph->matrix[row][column];
}
Expand Down
26 changes: 9 additions & 17 deletions states/states/graph.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <stdio.h>
#include "list.h"
#define NUMBER_OF_CITIES 20
#define NUMBER_OF_NODES 20

// graph structure
typedef struct Graph Graph;
Expand All @@ -9,25 +10,16 @@ typedef struct Graph Graph;
void deleteGraph(Graph** graph);

// returns length of edge between two cities
int getEdge(Graph* graph, int firstNode, int secondNode);
int getEdge(Graph* graph, const int start, const int end);

// parses file
Graph* readFile(char* fileName);
Graph* buildGraph(FILE* input);

// deletes whole column
void deleteColumn(Graph* graph, int column);
void deleteColumn(Graph* graph, const int column);

// returns number of capitals
int getNumberOfCapitals(Graph* graph);
// returns number of graph nodes
int getNumberOfNodes(Graph* graph);

// returns number of cities (graph nodes)
int getNumberOfCities(Graph* graph);

// returns array of lists with capitals
List** getCapitals(Graph* graph);

// fills array with cities which have edge with given city
void getRelatedCities(Graph* graph, int city, int* cities[NUMBER_OF_CITIES]);

// copies adjacency matrix to two 2d array
void getMatrix(Graph* graph, int matrix[NUMBER_OF_CITIES][NUMBER_OF_CITIES]);
// copies adjacency matrix to 2d array
void getMatrix(Graph* graph, int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES]);
40 changes: 20 additions & 20 deletions states/states/graphTests.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
#include <stdlib.h>
#include "graphTests.h"
#include "graph.h"
#include "states.h"

bool testCapturing(void)
{
Graph* graph = readFile("inputTest.txt");
if (graph == NULL)
States* states = readFile("inputTest.txt");
if (states == NULL)
{
return false;
}
if (!capture(graph))
if (!capture(states))
{
deleteStates(&states);
return false;
}
List** states = getCapitals(graph);
int numberOfCapitals = getNumberOfCapitals(graph);
List** arrayOfStates = getCapitals(states);
int numberOfCapitals = getNumberOfCapitals(states);
int correctStates[3][4] = { { 6, 3, 0, 7 }, { 4, 5 }, { 2, 1 } };
for (int i = 0; i < numberOfCapitals; i++)
{
Position* position = createPosition();
if (position == NULL)
{
deleteGraph(&graph);
deleteStates(&states);
return false;
}
first(states[i], &position);
first(arrayOfStates[i], &position);
int column = 0;
while (!isNull(position))
{
if (getPositionValue(position) != correctStates[i][column])
{
deletePosition(&position);
deleteGraph(&graph);
deleteStates(&states);
return false;
}
next(&position);
++column;
}
deletePosition(&position);
}
deleteGraph(&graph);
return graph == NULL;
deleteStates(&states);
return states == NULL;
}

bool testParsing(void)
{
Graph* graph = readFile("inputTest.txt");
if (graph == NULL)
States* states = readFile("inputTest.txt");
if (states == NULL)
{
return false;
}
int matrix[NUMBER_OF_CITIES][NUMBER_OF_CITIES] = { 0 };
getMatrix(graph, matrix);
if (!(getNumberOfCities(graph) == 8 && getNumberOfCapitals(graph) == 3))
int matrix[NUMBER_OF_NODES][NUMBER_OF_NODES] = { 0 };
getMatrix(getGraph(states), matrix);
if (!(getNumberOfCities(states) == 8 && getNumberOfCapitals(states) == 3))
{
deleteGraph(&graph);
deleteStates(&states);
return false;
}
if (!(matrix[0][1] == matrix[1][0] && matrix[0][1] == 7
Expand All @@ -72,11 +72,11 @@ bool testParsing(void)
&& matrix[6][4] == matrix[4][6] && matrix[6][4] == 6
&& matrix[6][7] == matrix[7][6] && matrix[6][7] == 34))
{
deleteGraph(&graph);
deleteStates(&states);
return false;
}
deleteGraph(&graph);
return graph == NULL;
deleteStates(&states);
return states == NULL;
}

bool graphPassedTests(void)
Expand Down
12 changes: 6 additions & 6 deletions states/states/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ int main(int argc, char argv[])
{
return 0;
}
Graph* graph = readFile("input.txt");
if (graph == NULL)
States* states = readFile("input.txt");
if (states == NULL)
{
printf("An error occured while parsing");
printf("An error occured while reading file");
return -1;
}
if (!capture(graph))
if (!capture(states))
{
printf("An error occured while capturing world");
return -1;
}
printStates(graph);
deleteGraph(&graph);
printStates(states);
deleteStates(&states);
return 0;
}
Loading

0 comments on commit 7b5da1b

Please sign in to comment.