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
48 changes: 48 additions & 0 deletions semester_1/kr2.1/task2/String/String.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdlib.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Общие замечания:

  • Комментарии к коммитам ужасны
  • Надо проектные файлы. Тут вообще исходники раскиданы по папкам, собирать вручную — очень такое себе развлечение.

#include <stdio.h>
#include <string.h>

#include "String.h"

char *getString(size_t * const len, FILE * file, char const endOfLine)
{
*len = 0;
size_t capacity = 1;
char *s = (char *)malloc(sizeof(char));
if (s == NULL)
{
return NULL;
}

for (char c = fgetc(file); c != endOfLine && c != EOF; c = fgetc(file))
{
s[(*len)++] = c;

if (*len >= capacity)
{
capacity *= 2;
char *tmp = (char *)realloc(s, capacity * sizeof(char));
if (tmp == NULL)
{
free(s);
return NULL;
}
s = tmp;
}
}

s[*len] = '\0';

return s;
}

char *copyString(char const * const string)
{
const size_t len = strlen(string);
char *copy = (char *)malloc(len * sizeof(char));
if (copy != NULL)
{
strcpy(copy, string);
}
return copy;
}
Comment on lines +39 to +48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть strdup

8 changes: 8 additions & 0 deletions semester_1/kr2.1/task2/String/String.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdbool.h>
#include <string.h>
#include <stdio.h>

char *getString(size_t * const len, FILE * file, char const endOfLine);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо комментарии к функциям из заголовочного файла

char *copyString(char const * const string);
7 changes: 7 additions & 0 deletions semester_1/kr2.1/task2/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
5
6
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
111 changes: 111 additions & 0 deletions semester_1/kr2.1/task2/graph/graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdbool.h>

#include "graph.h"
#include "../list/list.h"

typedef struct Edge
{
size_t firstNodeNumber;
size_t secondNodeNumber;
} Edge;

typedef struct Graph
{
size_t edgeAmount;
Edge **edges;
} Graph;

Graph *createGraph(const size_t nodeAmount, const size_t edgeAmount)
{
Graph *graph = calloc(1, sizeof(Graph));
if (graph == NULL)
{
return NULL;
}

Edge **edges = calloc(edges, sizeof(Edge *));
if (edges == NULL)
{
free(graph);
return NULL;
}
graph->edgeAmount = edgeAmount;
graph->edges = edges;
}

void connectNode(Graph *graph, size_t index, size_t nodeNumber)
{
Edge *edge = graph->edges[index];
switch (edge->firstNodeNumber == 0)
{
case false:
{
edge->firstNodeNumber = nodeNumber;
break;
}

case true:
{
edge->secondNodeNumber = nodeNumber;
break;
}

default:
break;
}
Comment on lines +39 to +55

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лол.

Suggested change
switch (edge->firstNodeNumber == 0)
{
case false:
{
edge->firstNodeNumber = nodeNumber;
break;
}
case true:
{
edge->secondNodeNumber = nodeNumber;
break;
}
default:
break;
}
if (edge->firstNodeNumber == 0)
{
edge->secondNodeNumber = nodeNumber;
}
else
{
edge->firstNodeNumber = nodeNumber;
}

А то default для булевого выражения выглядит очень мило, особенно с break, который ничего не делает.

}

static List **createComponentsTable(Graph *graph, const size_t nodeAmount)
{
List **table = calloc(nodeAmount, sizeof(List *));
if (table == NULL)
{
return NULL;
}

ListErrorCode listErrorCode = okList;
for (size_t i = 0; i < graph->edgeAmount; i++)
{
Edge *currentEdge = graph->edges[i];
for (size_t j = 0; j < nodeAmount; ++j)
{
if (!(checkValue(table[j], currentEdge->firstNodeNumber) || checkValue(table[j], currentEdge->secondNodeNumber)))
{
listErrorCode = append(table[i], currentEdge->firstNodeNumber);
listErrorCode = append(table[i], currentEdge->secondNodeNumber);
break;
}
}
}
return table;
}

void printComponentsTable(Graph *graph, const size_t nodeAmount)
{
List ** componentsTable = createComponentsTable;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это не скомпилится, аргументы не передаются :)

if (componentsTable == NULL)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишняя пустая строчка

return;
}

for (size_t i = 0; i < nodeAmount; ++i)
{
printList(componentsTable[i]);
}

for (size_t i = 0; i < nodeAmount; ++i)
{
deleteList(componentsTable[i]);
}
free(componentsTable);
}

void deleteGraph(Graph * const graph)
{
for (size_t i = 0; i < graph->edgeAmount; ++i)
{
free(graph->edges[i]);
}
free(graph);
}
10 changes: 10 additions & 0 deletions semester_1/kr2.1/task2/graph/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <stdlib.h>

typedef struct Graph Graph;

Graph *createGraph(const size_t nodeAmount, const size_t edgeAmount);
void connectNode(Graph *graph, size_t index, size_t nodeNumber);
void printComponentsTable(Graph *graph, const size_t nodeAmount);
void deleteGraph(Graph * const graph);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше так:

Suggested change
void deleteGraph(Graph * const graph);
void deleteGraph(Graph ** const graph);

Чтобы потом иметь возможность присвоить NULL в graph и не оставлять у вызывающего указатель на удалённую область памяти.


132 changes: 132 additions & 0 deletions semester_1/kr2.1/task2/list/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <stdlib.h>
#include <stdio.h>

#include "list.h"

typedef struct Node
{
int value;
struct Node *next;
} Node;

typedef struct List
{
Node *head;
} List;

void printList(List const *const list)
{
printf("\n[");
for (Node *currentNode = list->head; currentNode != NULL; currentNode = currentNode->next)
{
printf("%d", currentNode->value);
if (currentNode->next != NULL)
{
printf(", ");
}
}
printf("]\n");
}

List *createList(void)
{
List *newList = (List *)calloc(1, sizeof(List));

return newList;
Comment on lines +33 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут короче без временной переменной:

Suggested change
List *newList = (List *)calloc(1, sizeof(List));
return newList;
return (List *)calloc(1, sizeof(List));

}

ListErrorCode append(List *const list, int const value)
{
Node **currentNode = list->head;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это тоже вызывает ругань компилятора, list->head имеет тип Node*, а мы его присваиваем в Node **. Типы не сходятся.

for (; *currentNode != NULL; currentNode = &((*currentNode)->next))
{
if ((*currentNode)->value == value)
{
return okList;
}
}

Node *newNode = (Node *)calloc(1, sizeof(Node));
if (newNode == NULL)
{
return memoryErrorList;
}

newNode->value = value;
*currentNode = newNode;
return okList;
}

void deleteList(List *const list)
{
while (list->head != NULL)
{
Node *currentNode = list->head;
list->head = currentNode->next;
free(currentNode);
}
free(list);
}

int getValueByIndex(List *list, size_t index, ListErrorCode *listErrorCode)
{
size_t currentIndex = 0;
for (Node *currentNode = list->head; currentNode != NULL; currentNode = currentNode->next)
{
if (currentIndex == index)
{
*listErrorCode = okList;
return currentNode->value;
}

++currentIndex;
}
*listErrorCode = indexErrorList;
return -1;
}

const bool checkValue(List *list, int value)
{
for (Node *currentNode = list->head; currentNode != NULL; currentNode = currentNode->next)
{
if (currentNode->value == value)
{
return true;
}
}
return false;
}

ListErrorCode popByIndex(List *const list, size_t const index)
{
Node *temp = list->head;
Node *previous = NULL;
size_t i = 0;
for (Node *currentNode = temp; currentNode != NULL; currentNode = currentNode->next)
{
if (i == index)
{
if (i == 0)
{
list->head = currentNode->next;
}
else
{
temp->next = currentNode->next;
}
free(currentNode);
return okList;
}
++i;
previous = temp;
temp = currentNode;
}

if (index == -1)
{
free(temp);
previous->next = NULL;
return okList;
}
return indexErrorList;
}
35 changes: 35 additions & 0 deletions semester_1/kr2.1/task2/list/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <stdlib.h>
#include <stdio.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если хедер не используется непосредственно в .h-нике, его не надо в .h-ник.

#include <stdbool.h>

typedef enum ListErrorCode
{
okList,
memoryErrorList,
indexErrorList
} ListErrorCode;

typedef struct List List;

//creates list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо пробел после //

List *createList(void);

//prints list to console
void printList(List const * const list);

//appends int value to list
ListErrorCode append(List *const list, int const value);

//deletes list
void deleteList(List * const list);

//deletes element
ListErrorCode popByIndex(List *const list, size_t const index);

//gets value from list by index
int getValueByIndex(List *list, size_t index, ListErrorCode *listErrorCode);

//checks if value is already in the list
const bool checkValue(List *list, int value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не компилится, тут точки с запятой не хватает. Причём тут дело даже не в различиях между компиляторами, так и в gcc, и в Visual Studio, и везде компилироваться не будет

23 changes: 23 additions & 0 deletions semester_1/kr2.1/task2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

#include "workWithFile/workWithFile.h"
#include "graph/graph.h"

#define PROGRAM_FINISHED_CORRECTLY 0
#define ERROR_OCCURED 1

#define FILENAME "file.txt"

int main()
{
Graph *graph = NULL;
size_t nodeAmount = 0;
WorkWithFileErrorCode errorCode = readTableFromFile(FILENAME, &graph, &nodeAmount);
if (errorCode != okFile)
{
return ERROR_OCCURED;
}

printComponentsTable(graph, nodeAmount);
return PROGRAM_FINISHED_CORRECTLY;
}
Loading