- 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
Kr2.1 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Kr2.1 #25
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #include <stdlib.h> | ||
| #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
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть strdup  | 
||
| 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); | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо комментарии к функциям из заголовочного файла  | 
||
| char *copyString(char const * const string); | ||
| 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 | 
| 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
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лол. 
        Suggested change
       
    
 А то 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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не скомпилится, аргументы не передаются :)  | 
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (componentsTable == NULL) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||||||||||||||||||||||||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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); | ||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше так: 
        Suggested change
       
    
 Чтобы потом иметь возможность присвоить NULL в graph и не оставлять у вызывающего указатель на удалённую область памяти.  | 
||||||
| 
     | 
||||||
| 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
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут короче без временной переменной: 
        Suggested change
       
    
  | 
||||||||||
| } | ||||||||||
| 
     | 
||||||||||
| ListErrorCode append(List *const list, int const value) | ||||||||||
| { | ||||||||||
| Node **currentNode = list->head; | ||||||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это тоже вызывает ругань компилятора, list->head имеет тип   | 
||||||||||
| 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; | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #pragma once | ||
| 
     | 
||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не компилится, тут точки с запятой не хватает. Причём тут дело даже не в различиях между компиляторами, так и в gcc, и в Visual Studio, и везде компилироваться не будет  | 
||
| 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; | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Общие замечания: