Сделал 22 Задание Массивов

This commit is contained in:
AZEN-SGG 2024-10-09 15:51:25 +03:00
parent c3182c8c24
commit 5852a9f034
23 changed files with 99 additions and 31 deletions

View 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

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

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

View 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

View file

@ -0,0 +1,51 @@
#include "tools.h"
FILE * getFile(void)
{
FILE * file;
char filename[50];
printf("Enter filename: ");
if (scanf("%s", filename) == 1)
{
file = fopen(filename, "r");
if (file == NULL) {
printf("Error file!\n");
return NULL;
} else {
return file;
}
} else
{
printf("Empty name!\n");
return NULL;
}
}
double * getList(FILE * file)
{
double current;
int i, length = 2;
double * numbers = NULL;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!");
return numbers;
}
numbers = (double *)malloc(length * sizeof(double));
i = 1;
do {
if (i >= length) {
length *= 2;
numbers = (double *)realloc(numbers, (length * sizeof(double)));
}
numbers[i] = current;
i++;
} while (fscanf(file, "%lf", &current) == 1);
numbers = (double *)realloc(numbers, i * sizeof(double));
numbers[0] = i;
return numbers;
}

View file

@ -0,0 +1,10 @@
#ifndef TOOLS
#define TOOLS
#include <stdio.h>
#include <stdlib.h>
FILE * getFile(void);
double * getList(FILE * file);
#endif