From 4e498f2819962b16b0bca0a135ccc9db53a97fd7 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Fri, 1 Nov 2024 07:05:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=2015=D1=8E?= =?UTF-8?q?=20=D0=97=D0=B0=D0=B4=D0=B0=D1=87=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WorkingArrays/15Ex/input.txt | 2 +- WorkingArrays/15Ex/main.c | 10 +- WorkingArrays/15Ex/makefile | 17 ++-- WorkingArrays/15Ex/replace_local_min.c | 104 ++++++++++++-------- WorkingArrays/15Ex/replace_local_min.h | 10 +- WorkingArrays/15Ex/replace_local_min_test.c | 36 +++++++ WorkingArrays/15Ex/tools.c | 7 ++ WorkingArrays/15Ex/tools.h | 1 + 8 files changed, 133 insertions(+), 54 deletions(-) create mode 100644 WorkingArrays/15Ex/replace_local_min_test.c diff --git a/WorkingArrays/15Ex/input.txt b/WorkingArrays/15Ex/input.txt index 7b3976f..5fda71a 100644 --- a/WorkingArrays/15Ex/input.txt +++ b/WorkingArrays/15Ex/input.txt @@ -1 +1 @@ -1 1 2 2 1 1 2 2 1 1 2 2 \ No newline at end of file +1 1 1 2 2 2 4 4 4 3 3 3 4 4 4 3 3 3 4 4 4 \ No newline at end of file diff --git a/WorkingArrays/15Ex/main.c b/WorkingArrays/15Ex/main.c index 7a86f3a..84d8a43 100644 --- a/WorkingArrays/15Ex/main.c +++ b/WorkingArrays/15Ex/main.c @@ -4,12 +4,20 @@ int main(void) { double * numbers; + unsigned length; FILE * file = getFile(); if (file == NULL) return -1; numbers = getList(file); if (numbers == NULL) return 1; - printf("Count elements in file: %d", findAndReplaceLocalMin(numbers)); + length = find_replace_local_min(&numbers[1], (int)numbers[0] - 1); + + for (int i = 1; i < (int)length + 1; ++i) { + printf("%.1lf ", numbers[i]); + } + free(numbers); + + return 0; } diff --git a/WorkingArrays/15Ex/makefile b/WorkingArrays/15Ex/makefile index cb9b5c7..d1b9748 100644 --- a/WorkingArrays/15Ex/makefile +++ b/WorkingArrays/15Ex/makefile @@ -1,11 +1,16 @@ -all: main.o replace_local_min.o tools.o - gcc main.o replace_local_min.o tools.o && del *.o +all: main clean + +main: main.o replace_local_min.o tools.o + gcc -o main main.o replace_local_min.o tools.o -lssp main.o: main.c - gcc -c main.c - + gcc -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -c main.c + replace_local_min.o: replace_local_min.c - gcc -c replace_local_min.c + gcc -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -c replace_local_min.c tools.o: tools.c - gcc -c tools.c \ No newline at end of file + gcc -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -c tools.c + +clean: + del /f /q *.o main 2>nul diff --git a/WorkingArrays/15Ex/replace_local_min.c b/WorkingArrays/15Ex/replace_local_min.c index 79755a6..4dc3d21 100644 --- a/WorkingArrays/15Ex/replace_local_min.c +++ b/WorkingArrays/15Ex/replace_local_min.c @@ -1,63 +1,81 @@ #include "replace_local_min.h" +unsigned find_replace_local_min(double * array, int length) { + int start_length_next_min[3]; + unsigned deleted = 0, have_min = 0; + double min_element = min(array, length); -int findAndReplaceLocalMin(double * numbers) { - double min_element = min(numbers); - int length = (int)numbers[0]; - unsigned start_end_minimum_index[3] = {0}; - do { - unsigned start_index = (start_end_minimum_index[2]) ? start_end_minimum_index[2] : 1; - findLocalMinimum(&numbers[start_index], length - start_index, start_end_minimum_index); - replaceLocalMinimum(&numbers[start_index], start_end_minimum_index, min_element); - } while start_end_minimum_index[0]; + do { + unsigned changed_len; + findLocalMin(&array[have_min + deleted], length - (have_min + deleted), start_length_next_min); + replace(&array[have_min + deleted], length - (have_min + deleted), min_element, start_length_next_min[0], start_length_next_min[1]); - for (int i = 0; i < 3; ++i) { - printf("%d\n", start_end_minimum_index[i]); - } + deleted += have_min; + changed_len = start_length_next_min[1] - 1; + length -= changed_len; + have_min = (start_length_next_min[2]) ? start_length_next_min[2] - changed_len : 0; + } while (have_min); - return 0; + return unbs(length); } +double min(double * array, int length) { + unsigned min_index = 0; -double min(double * numbers) { - unsigned index_min_el = 0; - for (int i = 1; i < (int)numbers[0]; ++i) { - if ((numbers[index_min_el] - numbers[i]) > exp || !index_min_el) index_min_el = i; - } - return numbers[index_min_el]; + for (int i = 1; i < length; ++i) if (array[min_index] > array[i]) min_index = i; + + return array[min_index]; } +unsigned findLocalMin(double * num_array, int length, int * start_length_local_min) { + unsigned index_next_min = 0; + int len_local_min = 1; + bool found = false; -void findLocalMinimum(double * numbers, unsigned length, unsigned *start_end_minimum_index) { - unsigned local_length = 1; + start_length_local_min[0] = 0, start_length_local_min[1] = 0, start_length_local_min[2] = 0; - for (unsigned i = 1; i < length; ++i) { - if (isEqual(numbers[i - 1], numbers[i])) { - if (local_length) ++local_length; - } else if ((numbers[i - 1] - numbers[i]) < exp) { - if (local_length) { - start_end_minimum_index[0] = i - local_length, start_end_minimum_index[1] = i - 1; - local_length = 0; - } - } else { - if (start_end_minimum_index[1]) { - start_end_minimum_index[2] = i; - } - } - } + for (int i = 1; i < length; ++i) { + if (len_local_min) { + if (isEqual(num_array[i - 1], num_array[i])) { + len_local_min++; + } else if ((num_array[i] - num_array[i - 1]) < exp) { + len_local_min = 1; + } else { + start_length_local_min[0] = i - len_local_min; + start_length_local_min[1] = len_local_min; + + found = true; + len_local_min = 0; + } + } else { + if ((num_array[i] - num_array[i - 1]) < -exp) { + if (found) { + start_length_local_min[2] = i; + index_next_min = i; + break; + } + len_local_min = 1; + } + } + } + + if (len_local_min && !found) start_length_local_min[0] = length - len_local_min, start_length_local_min[1] = len_local_min; + return index_next_min; } - bool isEqual(double first, double second) { - return (fabs(first - second) < exp); + if (fabs(first - second) < exp) return true; + return false; } +void replace(double * array, int length, double wherewith, int from, int len_replaceable) { + array[from] = wherewith; -void replaceLocalMinimum(double * numbers, unsigned * start_end_minimum_index, double minimum) { - unsigned start_i = start_end_minimum_index[0], end_i = start_end_minimum_index[1] - numbers[start_i] = minimum; - - for (int i = 0) + for (int i = from + len_replaceable; i < length ; ++i) { + array[i - len_replaceable + 1] = array[i]; + } } - +unsigned unbs(int number) { + return (unsigned int)((number < 0) ? -number : number); +} diff --git a/WorkingArrays/15Ex/replace_local_min.h b/WorkingArrays/15Ex/replace_local_min.h index a9f2001..509e57a 100644 --- a/WorkingArrays/15Ex/replace_local_min.h +++ b/WorkingArrays/15Ex/replace_local_min.h @@ -8,9 +8,13 @@ #define exp 1.e-6 -int findAndReplaceLocalMin(double * numbers); -double min(double * numbers); -void findLocalMinimum(double * numbers, unsigned length, unsigned *start_end_minimum_index); +unsigned findLocalMin(double * num_array, int length, int * start_length_local_min); +void test_findLocalMin(void); bool isEqual(double first, double second); +void replace(double * array, int length, double wherewith, int from, int len_replaceable); +void test_replace(void); +unsigned find_replace_local_min(double * array, int length); +double min(double * array, int length); +unsigned unbs(int number); #endif // REPLACE_LOCAL_MIN diff --git a/WorkingArrays/15Ex/replace_local_min_test.c b/WorkingArrays/15Ex/replace_local_min_test.c new file mode 100644 index 0000000..b1caadc --- /dev/null +++ b/WorkingArrays/15Ex/replace_local_min_test.c @@ -0,0 +1,36 @@ +#include "replace_local_min.h" + +void test_findLocalMin(void) { + int expected_start, expected_length, expected_next, length = 10; + double array[] = {2, 1, 1, 0, 0, 0, 2, 2, 3, 2}; + int start_len_loc_min[3] = {-1, -2, -3}; + + findLocalMin(array, length, start_len_loc_min); + + for (int i = 0; i < length; ++i) printf("%.1lf ", array[i]); + printf("\n"); + for (int i = 0; i < 3; ++i) printf("%d\n", start_len_loc_min[i]); + + expected_start = 3, expected_length = 3, expected_next = 9; + + if (start_len_loc_min[0] != expected_start) printf("expected start: %d, but got: %d\n", expected_start, start_len_loc_min[0]); + if (start_len_loc_min[1] != expected_length) printf("expected length: %d, but got: %d\n", expected_length, start_len_loc_min[1]); + if (start_len_loc_min[2] != expected_next) printf("expected next min: %d, but got: %d\n", expected_next, start_len_loc_min[2]); +} + +void test_replace(void) { + int from, len_replaceable, length = 7; + double array[] = {1, 0, 0, 0, 0, 3, 3}; + double wherewith; + + wherewith = -100; + from = 1; + len_replaceable = 4; + + replace(array, length, wherewith, from, len_replaceable); + + for (int i = 0; i < length; ++i) printf("%.1lf ", array[i]); + printf("\n"); + + if (array[from] != wherewith) printf("Expected replace to %.1lf, but got: %.1lf\n", wherewith, array[from]); +} diff --git a/WorkingArrays/15Ex/tools.c b/WorkingArrays/15Ex/tools.c index f2ebc66..5fee269 100644 --- a/WorkingArrays/15Ex/tools.c +++ b/WorkingArrays/15Ex/tools.c @@ -48,3 +48,10 @@ double * getList(FILE * file) { numbers[0] = i; return numbers; } + +void outputArray(double * array, int length) { + for (int i = 0; i < length; ++i) { + printf("%.1lf\n", array[i]); + } + printf("\n"); +} diff --git a/WorkingArrays/15Ex/tools.h b/WorkingArrays/15Ex/tools.h index 706e31a..9c92e68 100644 --- a/WorkingArrays/15Ex/tools.h +++ b/WorkingArrays/15Ex/tools.h @@ -6,5 +6,6 @@ FILE * getFile(void); double * getList(FILE * file); +void outputArray(double * array, int length); #endif