-
Notifications
You must be signed in to change notification settings - Fork 0
Work branch #1
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: master
Are you sure you want to change the base?
Work branch #1
Changes from all commits
f4eeabb
af9ffcd
6f05478
370af04
1d746b7
0396a9b
8fb82a6
63f5f85
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,32 @@ | ||
| language: c | ||
|
|
||
| before_install: | ||
| - sudo apt update | ||
| - sudo apt install cmake | ||
| - sudo apt-get install valgrind | ||
|
|
||
| before_script: | ||
| - pip3 install --user cpplint | ||
|
|
||
|
|
||
| script: | ||
| - cppcheck main.c | ||
| - cppcheck Resource.h | ||
| - cpplint main.c | ||
| - cpplint Resource.h | ||
| - g++ -g -Wall -Wextra main.c -o main | ||
| - mkdir build | ||
| - cd build | ||
| - cmake .. | ||
| - cmake --build . | ||
|
|
||
| after_script: | ||
| - make clean | ||
|
|
||
| addons: | ||
| apt: | ||
| packages: | ||
| - "python3" | ||
| - "python3-pip" | ||
| - "cppcheck" | ||
| update: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| cmake_minimum_required(VERSION 3.12) | ||
| project(HomeworkC C) | ||
|
|
||
| set(CMAKE_C_STANDARD 99) | ||
|
|
||
| add_executable(first_homerok_c main.c) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * "Copyright 2019 Sergei Petrenko" | ||
| */ | ||
| #ifndef RESOURCE_H_ | ||
| #define RESOURCE_H_ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <assert.h> | ||
|
Collaborator
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. include-ы стоит располагать в алфавитном порядке |
||
|
|
||
| typedef struct Resource Resource; | ||
|
|
||
| struct Resource { | ||
| char* name; | ||
| char* callnumber; | ||
| char* measure; | ||
| char* category; | ||
| int count; | ||
| }; | ||
| void resource_init(Resource* resource, char* name, char* callnumber, | ||
|
Collaborator
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* measure, char* category, int count) { | ||
|
Collaborator
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. если какие-то параметры не меняются, то нужно это помечать через const |
||
| resource->name = name; | ||
| resource->callnumber = callnumber; | ||
| resource->measure = measure; | ||
| resource->category = category; | ||
| resource->count = count; | ||
| } | ||
|
|
||
| int compare(const void *resource1, const void *resource2) { | ||
|
Collaborator
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. тут void * уже не имел смысла, так как функция работает только для Resource |
||
| Resource *res1 = (Resource *)resource1; // NOLINT | ||
| Resource *res2 = (Resource *)resource2; // NOLINT | ||
| int result_comp = strcmp(res1->category, res2->category); | ||
| if (result_comp == 0) { | ||
| result_comp = strcmp(res2->measure, res1->measure); | ||
| if (result_comp == 0) { | ||
| return res1->count > res2->count; | ||
| } else { | ||
|
Collaborator
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. else не нужен |
||
| return result_comp; | ||
| } | ||
| } else { | ||
|
Collaborator
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. else не нужен |
||
| return result_comp; | ||
| } | ||
| } | ||
|
|
||
| void my_swap(void* v1, void* v2, int size) { | ||
| char *buffer[size]; | ||
| memcpy(buffer, v1, size); | ||
| memcpy(v1, v2, size); | ||
| memcpy(v2, buffer, size); | ||
| } | ||
|
|
||
| void my_quicksort(void* v, size_t size, int left, int right, | ||
| int (*comp)(const void*, const void*)) { | ||
| void *vt, *v3; | ||
| size_t i, last, mid = (left + right) / 2; | ||
| if (left >= right) | ||
| return; | ||
| void* vl = (char*)(v + (left * size)); // NOLINT | ||
| void* vr = (char*)(v + (mid * size)); // NOLINT | ||
| my_swap(vl, vr, size); | ||
| last = left; | ||
| for (i = left + 1; i <= right; i++) { | ||
| vt = (char*)(v + (i * size)); // NOLINT | ||
| if ((*comp)(vl, vt) > 0) { | ||
| ++last; | ||
| v3 = (char*)(v + (last * size)); // NOLINT | ||
| my_swap(vt, v3, size); | ||
| } | ||
| } | ||
| v3 = (char*)(v + (last * size)); // NOLINT | ||
| my_swap(vl, v3, size); | ||
| my_quicksort(v, size, left, last - 1, comp); | ||
| my_quicksort(v, size, last + 1, right, comp); | ||
| } | ||
|
|
||
| void print_grouped_resources(Resource *resources, int size) { //NOLINT | ||
| my_quicksort(resources, sizeof(Resource), 0, size-1, compare); // NOLINT | ||
| for (size_t i = 0; i < size; ++i) { | ||
| printf("%s %s %s %d %s %s", resources[i].category, resources[i].name, | ||
| resources[i].callnumber, resources[i].count, | ||
| resources[i].measure, "\n"); | ||
| } | ||
| } | ||
| int get_measure_statistics(Resource* resources, size_t size, | ||
| char* measure, char* category) { | ||
| int result = 0; | ||
| for (size_t i = 0; i < size; i++) { | ||
| if ( strcmp(resources[i].measure, measure) == 0 | ||
| && strcmp(resources[i].category, category) == 0 ) { | ||
| result+=resources[i].count; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| void add_some_resources(Resource* resources) { | ||
| resource_init(&resources[0] , "Coal", "CE01", "kg" | ||
| , "Energy", 600); | ||
| resource_init(&resources[1] , "Oil", "O11S", | ||
| "l", "Energy", 100); | ||
| resource_init(&resources[2] , "Silver", "Slv01", | ||
| "kg", "Metal", 400); | ||
| resource_init(&resources[3] , "Аcid", "AC02E", | ||
| "l", "Liquid", 300); | ||
| resource_init(&resources[4] , "Gold", "Gld01", | ||
| "kg", "Metal", 500); | ||
| resource_init(&resources[5] , "Antifreeze", "AF01W", | ||
| "l", "Liquid", 200); | ||
| resource_init(&resources[6], "Bla", "Bla", | ||
| "Bla", "Bla", 100000); | ||
| } | ||
|
|
||
| #endif // RESOURCE_H_" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * "Copyright 2019 Sergei Petrenko" | ||
| */ | ||
| #include <stdio.h> | ||
| #include "Resource.h" | ||
| int main() { | ||
| size_t size = 7; | ||
| Resource *resources = (Resource*)malloc( // NOLINT | ||
| sizeof(struct Resource)*size); // NOLINT | ||
| add_some_resources(resources); | ||
| print_grouped_resources(&*resources, size); | ||
| printf("%d %s", get_measure_statistics(resources, size, "l", | ||
| "Liquid") , "l of liquid\n"); | ||
| printf("%d %s", get_measure_statistics(resources, size, | ||
| "kg", "Metal"), "kg of metal]\n"); | ||
| free(resources); | ||
| return 0; | ||
| } |
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.
почему для c программы используется g++?