Сделал 43ю Задачу с Массивами
This commit is contained in:
parent
4e498f2819
commit
baa0509a01
7 changed files with 144 additions and 0 deletions
1
WorkingArrays/43Ex/input.txt
Normal file
1
WorkingArrays/43Ex/input.txt
Normal file
|
@ -0,0 +1 @@
|
|||
3 3 3 3 3 3
|
23
WorkingArrays/43Ex/main.c
Normal file
23
WorkingArrays/43Ex/main.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include "tools.h"
|
||||
#include "max_distance.h"
|
||||
|
||||
int main(void) {
|
||||
double * numbers;
|
||||
int max_distance;
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) return -1;
|
||||
|
||||
numbers = getList(file);
|
||||
if (numbers == NULL) return 1;
|
||||
|
||||
max_distance = maxDistance(&numbers[1], (int)numbers[0] - 1);
|
||||
|
||||
if (max_distance == NO_LOCAL_MINIMA) printf("There are no local minima in the file");
|
||||
else if (max_distance == ONE_LOCAL_MINIMUM) printf("Only one local minimum");
|
||||
else printf("Max distance between local minimum is %d\n", max_distance);
|
||||
|
||||
free(numbers);
|
||||
|
||||
return 0;
|
||||
}
|
11
WorkingArrays/43Ex/makefile
Normal file
11
WorkingArrays/43Ex/makefile
Normal file
|
@ -0,0 +1,11 @@
|
|||
all: main.o max_distance.o tools.o
|
||||
gcc main.o max_distance.o tools.o && del *.o
|
||||
|
||||
main.o: main.c
|
||||
gcc -c main.c
|
||||
|
||||
max_distance.o: max_distance.c
|
||||
gcc -c max_distance.c
|
||||
|
||||
tools.o: tools.c
|
||||
gcc -c tools.c
|
30
WorkingArrays/43Ex/max_distance.c
Normal file
30
WorkingArrays/43Ex/max_distance.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "max_distance.h"
|
||||
|
||||
int maxDistance(double * array, int length) {
|
||||
int end_first_min, start_last_min, len_local;
|
||||
|
||||
end_first_min = 0;
|
||||
start_last_min = 0;
|
||||
len_local = 1;
|
||||
|
||||
for (int i = 1; i < length; ++i) {
|
||||
if (len_local != OUT) {
|
||||
if ((array[i] - array[i - 1]) < -exp) {
|
||||
len_local = 1;
|
||||
} else if ((array[i] - array[i - 1]) > exp) {
|
||||
if (end_first_min == OUT) end_first_min = i;
|
||||
else if (start_last_min == OUT) start_last_min = i - len_local;
|
||||
len_local = 0;
|
||||
} else {++len_local;}
|
||||
} else {
|
||||
if ((array[i] - array[i - 1]) < -exp) len_local = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (len_local != OUT) start_last_min = length - len_local;
|
||||
|
||||
if (end_first_min == 0) return NO_LOCAL_MINIMA;
|
||||
else if (start_last_min == 0) return ONE_LOCAL_MINIMUM;
|
||||
else return start_last_min - end_first_min;
|
||||
}
|
||||
|
11
WorkingArrays/43Ex/max_distance.h
Normal file
11
WorkingArrays/43Ex/max_distance.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef MAX_DISTANCE
|
||||
#define MAX_DISTANCE
|
||||
|
||||
#define exp 1.e-6
|
||||
#define OUT 0
|
||||
#define NO_LOCAL_MINIMA -1
|
||||
#define ONE_LOCAL_MINIMUM -2
|
||||
|
||||
int maxDistance(double * array, int length);
|
||||
|
||||
#endif
|
57
WorkingArrays/43Ex/tools.c
Normal file
57
WorkingArrays/43Ex/tools.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#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", ¤t) != 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", ¤t) == 1);
|
||||
|
||||
numbers = (double *)realloc(numbers, i * sizeof(double));
|
||||
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");
|
||||
}
|
11
WorkingArrays/43Ex/tools.h
Normal file
11
WorkingArrays/43Ex/tools.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef TOOLS
|
||||
#define TOOLS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
FILE * getFile(void);
|
||||
double * getList(FILE * file);
|
||||
void outputArray(double * array, int length);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue