From 896caa5d3e0dd5dcbef331451de5be5574a0adb8 Mon Sep 17 00:00:00 2001 From: Bohdan Kapliuk Date: Thu, 14 Nov 2024 14:25:09 +0200 Subject: [PATCH] cv7 --- a2/binary_search.h | 33 --------------------------------- cv7/program.c | 12 ++++-------- 2 files changed, 4 insertions(+), 41 deletions(-) delete mode 100644 a2/binary_search.h diff --git a/a2/binary_search.h b/a2/binary_search.h deleted file mode 100644 index 7589424..0000000 --- a/a2/binary_search.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef BINARY_SEARCH_H -#define BINARY_SEARCH_H - -#include -#include -#include - -const int *binary_search(int value, const int *arr, size_t length) { - if (length == 0) { - return NULL; - } - - size_t left = 0; - size_t right = length - 1; - - while (left <= right) { - size_t middle = left + (right - left) / 2; - - if (arr[middle] == value) { - return &arr[middle]; - } else if (arr[middle] > value) { - if (middle == 0) { - break; - } - right = middle - 1; - } else { - left = middle + 1; - } - } - return NULL; -} - -#endif \ No newline at end of file diff --git a/cv7/program.c b/cv7/program.c index 0cd8271..861bb24 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -14,19 +14,15 @@ struct tree* read_tree(int* counter) { char buffer[SIZE]; memset(buffer, 0, SIZE); char* r = fgets(buffer, SIZE, stdin); + if(r == NULL){ + printf("Chybna databaza\n"); + return 0; + } if (buffer[0] == '\n') { return NULL; } struct tree* tree = calloc(1, sizeof(struct tree)); - if(tree == NULL){ - printf("Chybna databaza\n"); - exit(0); - } tree->value = malloc(strlen(buffer) + 1); - if(tree->value == NULL){ - printf("Chybna databaza\n"); - exit(0); - } strcpy(tree->value, buffer); if (buffer[0] == '*') { (*counter)++;