Repository navigation
Hw3 #9
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
Open
EStarikov
wants to merge
9
commits into
main
Choose a base branch
from
hw3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw3 #9
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5540d0c
first realisation
EStarikov 84652d4
fix
EStarikov a4c1b4d
Исправлены ошибки, решены утечки памяти
EStarikov 3591cca
добавлены папки с симэйками чтобы clang-tidy прошел
EStarikov fa580ee
удален лишний файл
EStarikov 3853b8f
Merge pull request #10 from EStarikov/main
EStarikov 34f3b6c
fix for clang tidy
EStarikov dc367e9
добавлены тесты, сделана оптимизация чтобы не добавлять в кучу столиц…
EStarikov e6d7f4a
добавлены комментарии к функциям
EStarikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| add_library(hw3_lib states.c) | ||
|
|
||
| target_include_directories(hw3_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
|
||
| add_executable(hw3_main main.c) | ||
| target_link_libraries(hw3_main PRIVATE hw3_lib) | ||
|
|
||
| if(BUILD_TESTS) | ||
| add_executable(hw3_tests tests.c) | ||
| target_link_libraries(hw3_tests PRIVATE hw3_lib) | ||
| endif() | ||
|
|
||
| configure_file( | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/graph.txt | ||
| ${CMAKE_CURRENT_BINARY_DIR}/graph.txt | ||
| COPYONLY | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| 7 | ||
| 9 | ||
| 0 3 3 | ||
| 0 1 2 | ||
| 1 3 1 | ||
| 1 5 7 | ||
| 3 6 2 | ||
| 3 4 9 | ||
| 5 4 8 | ||
| 4 2 5 | ||
| 6 2 3 | ||
| 3 | ||
| 0 | ||
| 3 | ||
| 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include "states.h" | ||
| #include <stdlib.h> | ||
|
|
||
| int main() | ||
| { | ||
| int* states = NULL; | ||
| unsigned* capitals = NULL; | ||
| size_t n; | ||
| size_t k; | ||
|
|
||
| Heap** graph = readFromFile("graph.txt", &states, &capitals, &n, &k); | ||
| if (graph == NULL) { | ||
| return -1; | ||
| } | ||
| matchCityWithCapital(graph, states, capitals, n, k); | ||
| printStates(states, capitals, n, k); | ||
| freeGraph(&graph, n); | ||
| free(states); | ||
| free(capitals); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| #include "states.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct Road { | ||
| int city; | ||
| size_t len; | ||
| } Road; | ||
|
|
||
| struct Heap { | ||
| Road** roads; | ||
| size_t size; | ||
| }; | ||
|
|
||
| static Heap* initHeap() | ||
| { | ||
| Heap* heap = calloc(1, sizeof(Heap)); | ||
| return heap; | ||
| } | ||
|
|
||
| static void swap(Road* r1, Road* r2) | ||
| { | ||
| Road temp = *r1; | ||
| *r1 = *r2; | ||
| *r2 = temp; | ||
| } | ||
|
|
||
| static void siftDown(Heap* heap, size_t i) | ||
| { | ||
| while (2 * i + 1 < heap->size) { | ||
| size_t left = 2 * i + 1; | ||
| size_t right = 2 * i + 2; | ||
| size_t j = left; | ||
| if (right < heap->size && (heap->roads[right])->len < (heap->roads[left])->len) { | ||
| j = right; | ||
| } | ||
| if ((heap->roads[i])->len <= (heap->roads[j])->len) { | ||
| break; | ||
| } | ||
| swap(heap->roads[i], heap->roads[j]); | ||
| i = j; | ||
| } | ||
| } | ||
|
|
||
| static void siftUp(Heap* heap, size_t i) | ||
| { | ||
| if (i == 0) { | ||
| return; | ||
| } | ||
| while ((heap->roads[i])->len < (heap->roads[(i - 1) / 2])->len) { | ||
| swap(heap->roads[i], heap->roads[(i - 1) / 2]); | ||
| i = (i - 1) / 2; | ||
| if (i == 0) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static Road* extractMin(Heap* heap) | ||
| { | ||
| if (heap->size == 0) { | ||
| return NULL; | ||
| } | ||
| Road* min = heap->roads[0]; | ||
| heap->roads[0] = heap->roads[heap->size - 1]; | ||
| heap->size--; | ||
| siftDown(heap, 0); | ||
| return min; | ||
| } | ||
|
|
||
| static int insert(Heap* heap, int city, size_t len) | ||
| { | ||
| Road* newRoad = malloc(sizeof(Road)); | ||
| if (newRoad == NULL) { | ||
| return -1; | ||
| } | ||
| newRoad->city = city; | ||
| newRoad->len = len; | ||
| ++heap->size; | ||
| Road** temp = realloc(heap->roads, heap->size * sizeof(Road*)); | ||
| if (temp == NULL) { | ||
| free(newRoad); | ||
| free(heap->roads); | ||
| return -1; | ||
| } | ||
| heap->roads = temp; | ||
| heap->roads[heap->size - 1] = newRoad; | ||
| siftUp(heap, heap->size - 1); | ||
| return 0; | ||
| } | ||
|
|
||
| void freeGraph(Heap*** graph, size_t len) | ||
| { | ||
| for (size_t i = 0; i < len; ++i) { | ||
| for (size_t j = 0; j < ((*graph)[i])->size; ++j) { | ||
| free(((*graph)[i])->roads[j]); | ||
| } | ||
| free(((*graph)[i])->roads); | ||
| free((*graph)[i]); | ||
| } | ||
| free(*graph); | ||
| } | ||
|
|
||
| Heap** readFromFile(char* filename, int** states, unsigned** capitals, size_t* n, size_t* k) | ||
| { | ||
| FILE* input = fopen(filename, "r"); | ||
| if (input == NULL) { | ||
| printf("file"); | ||
| return NULL; | ||
| } | ||
|
|
||
| fscanf(input, "%zu", n); | ||
| Heap** graph = malloc(sizeof(Heap*) * (*n)); | ||
| if (graph == NULL) { | ||
| printf("init"); | ||
| fclose(input); | ||
| return NULL; | ||
| } | ||
| *states = malloc(sizeof(int) * (*n)); | ||
| if (states == NULL) { | ||
| free(graph); | ||
| fclose(input); | ||
| printf("states"); | ||
| return NULL; | ||
| } | ||
| for (size_t i = 0; i < (*n); ++i) { | ||
| graph[i] = initHeap(); | ||
| (*states)[i] = -1; | ||
| if (graph[i] == NULL) { | ||
| freeGraph(&graph, i); | ||
| fclose(input); | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
||
| unsigned m; | ||
| fscanf(input, "%u", &m); | ||
| int i, j; | ||
| size_t len; | ||
| int err = 0; | ||
|
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. err можно было и объявлять в месте первого использования, в строке 145. Вне цикла она не нужна |
||
| for (int l = 0; l < m; ++l) { | ||
| fscanf(input, "%d", &i); | ||
| fscanf(input, "%d", &j); | ||
| fscanf(input, "%zu", &len); | ||
| err = insert(graph[i], j, len); | ||
| if (err != 0) { | ||
| freeGraph(&graph, (*n)); | ||
| fclose(input); | ||
| return NULL; | ||
| } | ||
| err = insert(graph[j], i, len); | ||
| if (err != 0) { | ||
| freeGraph(&graph, (*n)); | ||
| fclose(input); | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
||
| int num; | ||
| fscanf(input, "%zu", k); | ||
| *capitals = malloc(sizeof(unsigned) * (*k)); | ||
| if (capitals == NULL) { | ||
| freeGraph(&graph, (*n)); | ||
| fclose(input); | ||
| printf("capitals"); | ||
| return NULL; | ||
| } | ||
| for (size_t l = 0; l < (*k); ++l) { | ||
| fscanf(input, "%d", &num); | ||
| (*states)[num] = num; | ||
| (*capitals)[l] = num; | ||
| } | ||
|
|
||
| fclose(input); | ||
| return graph; | ||
| } | ||
|
|
||
| void matchCityWithCapital(Heap** graph, int* states, const unsigned* capitals, size_t n, size_t k) | ||
| { | ||
| size_t numOfUnmatchedCities = n - k; | ||
| while (numOfUnmatchedCities != 0) { | ||
| for (size_t i = 0; i < k; ++i) { | ||
| unsigned cap = capitals[i]; | ||
| Road* road = extractMin(graph[cap]); | ||
| while (road != NULL) { | ||
| int city = road->city; | ||
| free(road); | ||
| road = NULL; | ||
| if (states[city] == -1) { | ||
| states[city] = (int)cap; | ||
| numOfUnmatchedCities--; | ||
| while ((graph[city])->size != 0) { | ||
| road = extractMin(graph[city]); | ||
| if (states[road->city] == -1) { | ||
| insert(graph[cap], road->city, road->len); | ||
| } | ||
| free(road); | ||
| road = NULL; | ||
| } | ||
| break; | ||
| } else { | ||
| road = extractMin(graph[cap]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void printStates(const int* states, const unsigned* capitals, size_t n, size_t k) | ||
| { | ||
| for (size_t i = 0; i < k; ++i) { | ||
| printf("%u: ", capitals[i]); | ||
| for (size_t j = 0; j < n; ++j) { | ||
| if (states[j] == capitals[i]) { | ||
| printf("%zu ", j); | ||
| } | ||
| } | ||
| printf("\n"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #pragma once | ||
| #include <stddef.h> | ||
|
|
||
| typedef struct Heap Heap; | ||
|
|
||
| // освобождает граф | ||
| void freeGraph(Heap*** graph, size_t len); | ||
|
|
||
| // ситывает граф из файла | ||
| Heap** readFromFile(char* filename, int** states, unsigned** capitals, size_t* n, size_t* k); | ||
|
|
||
| // сопоставляет города с столицами | ||
| void matchCityWithCapital(Heap** graph, int* states, const unsigned* capitals, size_t n, size_t k); | ||
|
|
||
| // печатает государства | ||
| void printStates(const int* states, const unsigned* capitals, size_t n, size_t k); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.