Сделал 15ю Задачу
This commit is contained in:
parent
4c36e1638a
commit
4e498f2819
8 changed files with 133 additions and 54 deletions
|
@ -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
|
|
@ -4,12 +4,20 @@
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
double * numbers;
|
double * numbers;
|
||||||
|
unsigned length;
|
||||||
FILE * file = getFile();
|
FILE * file = getFile();
|
||||||
if (file == NULL) return -1;
|
if (file == NULL) return -1;
|
||||||
|
|
||||||
numbers = getList(file);
|
numbers = getList(file);
|
||||||
if (numbers == NULL) return 1;
|
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);
|
free(numbers);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
all: main.o replace_local_min.o tools.o
|
all: main clean
|
||||||
gcc main.o replace_local_min.o tools.o && del *.o
|
|
||||||
|
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
|
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
|
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
|
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
|
||||||
|
|
|
@ -1,63 +1,81 @@
|
||||||
#include "replace_local_min.h"
|
#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) {
|
do {
|
||||||
double min_element = min(numbers);
|
unsigned changed_len;
|
||||||
int length = (int)numbers[0];
|
findLocalMin(&array[have_min + deleted], length - (have_min + deleted), start_length_next_min);
|
||||||
unsigned start_end_minimum_index[3] = {0};
|
replace(&array[have_min + deleted], length - (have_min + deleted), min_element, start_length_next_min[0], start_length_next_min[1]);
|
||||||
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];
|
|
||||||
|
|
||||||
for (int i = 0; i < 3; ++i) {
|
deleted += have_min;
|
||||||
printf("%d\n", start_end_minimum_index[i]);
|
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) {
|
for (int i = 1; i < length; ++i) if (array[min_index] > array[i]) min_index = i;
|
||||||
unsigned index_min_el = 0;
|
|
||||||
for (int i = 1; i < (int)numbers[0]; ++i) {
|
return array[min_index];
|
||||||
if ((numbers[index_min_el] - numbers[i]) > exp || !index_min_el) index_min_el = i;
|
|
||||||
}
|
|
||||||
return numbers[index_min_el];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
start_length_local_min[0] = 0, start_length_local_min[1] = 0, start_length_local_min[2] = 0;
|
||||||
unsigned local_length = 1;
|
|
||||||
|
|
||||||
for (unsigned i = 1; i < length; ++i) {
|
for (int i = 1; i < length; ++i) {
|
||||||
if (isEqual(numbers[i - 1], numbers[i])) {
|
if (len_local_min) {
|
||||||
if (local_length) ++local_length;
|
if (isEqual(num_array[i - 1], num_array[i])) {
|
||||||
} else if ((numbers[i - 1] - numbers[i]) < exp) {
|
len_local_min++;
|
||||||
if (local_length) {
|
} else if ((num_array[i] - num_array[i - 1]) < exp) {
|
||||||
start_end_minimum_index[0] = i - local_length, start_end_minimum_index[1] = i - 1;
|
len_local_min = 1;
|
||||||
local_length = 0;
|
} else {
|
||||||
}
|
start_length_local_min[0] = i - len_local_min;
|
||||||
} else {
|
start_length_local_min[1] = len_local_min;
|
||||||
if (start_end_minimum_index[1]) {
|
|
||||||
start_end_minimum_index[2] = i;
|
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) {
|
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) {
|
for (int i = from + len_replaceable; i < length ; ++i) {
|
||||||
unsigned start_i = start_end_minimum_index[0], end_i = start_end_minimum_index[1]
|
array[i - len_replaceable + 1] = array[i];
|
||||||
numbers[start_i] = minimum;
|
}
|
||||||
|
|
||||||
for (int i = 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned unbs(int number) {
|
||||||
|
return (unsigned int)((number < 0) ? -number : number);
|
||||||
|
}
|
||||||
|
|
|
@ -8,9 +8,13 @@
|
||||||
|
|
||||||
#define exp 1.e-6
|
#define exp 1.e-6
|
||||||
|
|
||||||
int findAndReplaceLocalMin(double * numbers);
|
unsigned findLocalMin(double * num_array, int length, int * start_length_local_min);
|
||||||
double min(double * numbers);
|
void test_findLocalMin(void);
|
||||||
void findLocalMinimum(double * numbers, unsigned length, unsigned *start_end_minimum_index);
|
|
||||||
bool isEqual(double first, double second);
|
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
|
#endif // REPLACE_LOCAL_MIN
|
||||||
|
|
36
WorkingArrays/15Ex/replace_local_min_test.c
Normal file
36
WorkingArrays/15Ex/replace_local_min_test.c
Normal 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]);
|
||||||
|
}
|
|
@ -48,3 +48,10 @@ double * getList(FILE * file) {
|
||||||
numbers[0] = i;
|
numbers[0] = i;
|
||||||
return numbers;
|
return numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void outputArray(double * array, int length) {
|
||||||
|
for (int i = 0; i < length; ++i) {
|
||||||
|
printf("%.1lf\n", array[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
|
@ -6,5 +6,6 @@
|
||||||
|
|
||||||
FILE * getFile(void);
|
FILE * getFile(void);
|
||||||
double * getList(FILE * file);
|
double * getList(FILE * file);
|
||||||
|
void outputArray(double * array, int length);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue