-
Notifications
You must be signed in to change notification settings - Fork 0
КР №2 #12
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
shknoko
wants to merge
3
commits into
master
Choose a base branch
from
test_2
base: master
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
КР №2 #12
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,18 @@ | ||
| cmake_minimum_required(VERSION 3.10) | ||
| project(Comparing_binary) | ||
|
|
||
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../bin) | ||
| set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) | ||
| set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) | ||
|
|
||
| add_library(compare compare.c) | ||
|
|
||
| add_executable(comparing main.c) | ||
| target_link_libraries(comparing compare) | ||
|
|
||
| add_compile_options(-Wall -Wextra -pedantic) | ||
|
|
||
| enable_testing() | ||
| add_executable(tests ../tests/tests.c) | ||
| target_link_libraries(tests compare) | ||
| add_test(NAME tests COMMAND tests) |
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,33 @@ | ||
| #include "compare.h" | ||
|
|
||
| int compare(int* first, int* second, int n, int k) | ||
| { | ||
| int startFirst = 0; | ||
| while (startFirst < n && first[startFirst] == 0) { | ||
| startFirst++; | ||
| } | ||
|
|
||
| int startSecond = 0; | ||
| while (startSecond < k && second[startSecond] == 0) { | ||
| startSecond++; | ||
| } | ||
|
|
||
| int len1 = n - startFirst; | ||
| int len2 = k - startSecond; | ||
|
|
||
| if (len1 > len2) { | ||
| return 2; | ||
| } else if (len2 > len1) { | ||
| return 1; | ||
| } | ||
|
|
||
| for (int i = 0; i < len1; i++) { | ||
| if (first[startFirst + i] > second[startSecond + i]) { | ||
| return 2; | ||
| } else if (second[startSecond + i] > first[startFirst + i]) { | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| 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,3 @@ | ||
| #pragma once | ||
|
|
||
| int compare(int* first, int* second, int n, int k); | ||
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,54 @@ | ||
| #include <stdbool.h> | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include "compare.h" | ||
|
|
||
| int main(void) | ||
| { | ||
| int n = 0; | ||
| int k = 0; | ||
|
|
||
| if (scanf("%d %d", &n, &k) != 2) { | ||
| return 1; | ||
| } | ||
|
|
||
| if (n < 0 || k < 0) { | ||
| return 1; | ||
| } | ||
|
|
||
| int* first = calloc(n, sizeof(int)); | ||
| int* second = calloc(k, sizeof(int)); | ||
|
|
||
| if (first == NULL || second == NULL) { | ||
| return 1; | ||
| } | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| if (scanf("%d", &first[i]) != 1) { | ||
| free(first); | ||
| free(second); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| for (int i = 0; i < k; i++) { | ||
| if (scanf("%d", &second[i]) != 1) { | ||
| free(first); | ||
| free(second); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| int res = compare(first, second, n, k); | ||
| if (res == 2) { | ||
| printf("первое больше\n"); | ||
| } else if (res == 1) { | ||
| printf("второе больше\n"); | ||
| } else { | ||
| printf("числа равны\n"); | ||
| } | ||
|
|
||
| free(first); | ||
| free(second); | ||
| 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,58 @@ | ||
| #include <assert.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include "../src/compare.h" | ||
|
|
||
| void testEqualNumbers() | ||
| { | ||
| int a[6] = { 1, 0, 1, 1, 1, 0 }; | ||
| int b[6] = { 1, 0, 1, 1, 1, 0 }; | ||
| assert(compare(a, b, 6, 6) == 0); | ||
| } | ||
|
|
||
| void testFirstBigger() | ||
| { | ||
| int a[6] = { 1, 0, 1, 1, 1, 1 }; | ||
| int b[6] = { 1, 0, 1, 1, 1, 0 }; | ||
| assert(compare(a, b, 6, 6) == 2); | ||
| } | ||
|
|
||
| void testSecondBigger() | ||
| { | ||
| int a[6] = { 0, 0, 1, 1, 1, 0 }; | ||
| int b[6] = { 1, 0, 1, 1, 1, 0 }; | ||
| assert(compare(a, b, 6, 6) == 1); | ||
| } | ||
|
|
||
| void testLeadingZeros() | ||
| { | ||
| int a[9] = { 0, 0, 0, 1, 0, 1, 1, 1, 0 }; | ||
| int b[8] = { 0, 0, 1, 0, 1, 1, 1, 0 }; | ||
| assert(compare(a, b, 9, 8) == 0); | ||
| } | ||
|
|
||
| void testZero() | ||
| { | ||
| int a[1] = { 0 }; | ||
| int b[1] = { 0 }; | ||
| assert(compare(a, b, 1, 1) == 0); | ||
| } | ||
|
|
||
| void testNotEqualLen() | ||
| { | ||
| int a[1] = { 0 }; | ||
| int b[6] = { 1, 0, 1, 1, 1, 0 }; | ||
| assert(compare(a, b, 1, 6) == 1); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| testEqualNumbers(); | ||
| testFirstBigger(); | ||
| testSecondBigger(); | ||
| testLeadingZeros(); | ||
| testZero(); | ||
| testNotEqualLen(); | ||
| return 0; | ||
| } |
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.
Было бы неплохо описать, какой код что значит.