diff --git a/comparing_binary/src/CMakeLists.txt b/comparing_binary/src/CMakeLists.txt new file mode 100644 index 0000000..4e455a5 --- /dev/null +++ b/comparing_binary/src/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/comparing_binary/src/compare.c b/comparing_binary/src/compare.c new file mode 100644 index 0000000..4917b08 --- /dev/null +++ b/comparing_binary/src/compare.c @@ -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; +} \ No newline at end of file diff --git a/comparing_binary/src/compare.h b/comparing_binary/src/compare.h new file mode 100644 index 0000000..26b041d --- /dev/null +++ b/comparing_binary/src/compare.h @@ -0,0 +1,3 @@ +#pragma once + +int compare(int* first, int* second, int n, int k); \ No newline at end of file diff --git a/comparing_binary/src/main.c b/comparing_binary/src/main.c new file mode 100644 index 0000000..27dfdd3 --- /dev/null +++ b/comparing_binary/src/main.c @@ -0,0 +1,54 @@ +#include +#include +#include +#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; +} \ No newline at end of file diff --git a/comparing_binary/tests/tests.c b/comparing_binary/tests/tests.c new file mode 100644 index 0000000..3e3ae20 --- /dev/null +++ b/comparing_binary/tests/tests.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#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; +}