Сделал 15ю Задачу

This commit is contained in:
AZEN-SGG 2024-11-01 07:05:41 +03:00
parent 4c36e1638a
commit 4e498f2819
8 changed files with 133 additions and 54 deletions

View file

@ -1 +1 @@
1 1 2 2 1 1 2 2 1 1 2 2
1 1 1 2 2 2 4 4 4 3 3 3 4 4 4 3 3 3 4 4 4

View file

@ -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;
}

View file

@ -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
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

View file

@ -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);
}

View file

@ -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

View file

@ -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]);
}

View file

@ -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");
}

View file

@ -6,5 +6,6 @@
FILE * getFile(void);
double * getList(FILE * file);
void outputArray(double * array, int length);
#endif