Сделал 22 Задание Массивов
This commit is contained in:
parent
c3182c8c24
commit
5852a9f034
23 changed files with 99 additions and 31 deletions
30
22Ex/main.c
30
22Ex/main.c
|
@ -1,30 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include "tools.h"
|
||||
|
||||
int transposition(double * numbers);
|
||||
|
||||
int transposition(double * numbers) {
|
||||
unsigned length = (int)numbers[0];
|
||||
|
||||
|
||||
int last_index = -1;
|
||||
unsigned last_length = 0;
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
double * numbers;
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) return -1;
|
||||
|
||||
numbers = getList(file);
|
||||
if (numbers == NULL) return -1;
|
||||
|
||||
if (transposition(numbers) != 0) return -1;
|
||||
|
||||
for (int i = 1; i < numbers[0]; ++i) printf("%d ", numbers[i]);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
}
|
1
WorkingArrays/22Ex/input.txt
Normal file
1
WorkingArrays/22Ex/input.txt
Normal file
|
@ -0,0 +1 @@
|
|||
1 1 2 3 2 2 2 3 3 3 4 4 4 0 0 0 9 9 9 7 7 7 8 8 8 5 5
|
56
WorkingArrays/22Ex/local_minimum.c
Normal file
56
WorkingArrays/22Ex/local_minimum.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
#include "local_minimum.h"
|
||||
|
||||
int transposition(double * numbers, unsigned * start_end)
|
||||
{
|
||||
unsigned f_start = start_end[0], f_end = start_end[1], s_start = start_end[2], s_end = start_end[3];
|
||||
unsigned f_len = f_end - f_start + 1, s_len = s_end - s_start + 1, betw_len = s_start - f_end - 1;
|
||||
|
||||
double buffer[start_end[3] - start_end[0] + 1];
|
||||
|
||||
for (unsigned i = 0; i < s_len; ++i) buffer[i] = numbers[s_start + i];
|
||||
for (unsigned i = 0; i < betw_len; ++i) buffer[i + s_len] = numbers[f_end + 1 + i];
|
||||
for (unsigned i = 0; i < f_len; ++i) buffer[i + s_len + betw_len] = numbers[f_start + i];
|
||||
for (unsigned i = f_start; i <= s_end; ++i) numbers[i] = buffer[i - f_start];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int searching_loc_min(double * numbers)
|
||||
{
|
||||
unsigned length = (int)numbers[0];
|
||||
unsigned start_end[4];
|
||||
int curr_len = 1;
|
||||
|
||||
bool min_num = false;
|
||||
|
||||
|
||||
for (unsigned i = 2; i < length; ++i) {
|
||||
if (numbers[i] == numbers[i - 1]) {
|
||||
if (curr_len) ++curr_len;
|
||||
} else if (numbers[i] > numbers[i - 1]) {
|
||||
if (curr_len) {
|
||||
// It is important to remember that numbers[0] is equal to the length of the list!
|
||||
start_end[0 + (min_num * 2)] = (i - curr_len), start_end[1 + (min_num * 2)] = i - 1;
|
||||
|
||||
if (min_num) {
|
||||
if (transposition(numbers, start_end)) return -1;
|
||||
memset(start_end, 0, sizeof(start_end));
|
||||
}
|
||||
|
||||
min_num = !min_num;
|
||||
curr_len = 0;
|
||||
}
|
||||
} else {
|
||||
curr_len = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (curr_len) {
|
||||
start_end[0 + (min_num * 2)] = (length - curr_len), start_end[1 + (min_num * 2)] = length - 1;
|
||||
|
||||
if (min_num && transposition(numbers, start_end)) return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
10
WorkingArrays/22Ex/local_minimum.h
Normal file
10
WorkingArrays/22Ex/local_minimum.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef LOCAL_MINIMUM
|
||||
#define LOCAL_MINIMUM
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int transposition(double * numbers, unsigned * start_end);
|
||||
int searching_loc_min(double * numbers);
|
||||
|
||||
#endif // LOCAL_MINIMUM
|
19
WorkingArrays/22Ex/main.c
Normal file
19
WorkingArrays/22Ex/main.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
#include "tools.h"
|
||||
#include "local_minimum.h"
|
||||
|
||||
int main(void) {
|
||||
double * numbers;
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) return -1;
|
||||
|
||||
numbers = getList(file);
|
||||
if (numbers == NULL) return -1;
|
||||
|
||||
if (searching_loc_min(numbers)) return -1;
|
||||
|
||||
for (int i = 1; i < numbers[0]; ++i) printf("%.1lf ", numbers[i]);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
}
|
11
WorkingArrays/22Ex/makefile
Normal file
11
WorkingArrays/22Ex/makefile
Normal file
|
@ -0,0 +1,11 @@
|
|||
all: main.o local_minimum.o tools.o
|
||||
gcc main.o local_minimum.o tools.o && del *.o
|
||||
|
||||
main.o: main.c
|
||||
gcc -c main.c
|
||||
|
||||
local_minimum.o: local_minimum.c
|
||||
gcc -c local_minimum.c
|
||||
|
||||
tools.o: tools.c
|
||||
gcc -c tools.c
|
|
@ -22,7 +22,8 @@ FILE * getFile(void)
|
|||
}
|
||||
}
|
||||
|
||||
double * getList(FILE * file) {
|
||||
double * getList(FILE * file)
|
||||
{
|
||||
double current;
|
||||
int i, length = 2;
|
||||
double * numbers = NULL;
|
Loading…
Add table
Add a link
Reference in a new issue