Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions comparing_binary/src/CMakeLists.txt
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)
33 changes: 33 additions & 0 deletions comparing_binary/src/compare.c
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;
}
3 changes: 3 additions & 0 deletions comparing_binary/src/compare.h
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Было бы неплохо описать, какой код что значит.

54 changes: 54 additions & 0 deletions comparing_binary/src/main.c
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;
}
58 changes: 58 additions & 0 deletions comparing_binary/tests/tests.c
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;
}