Сделал 15 Задание с массивами

This commit is contained in:
AZEN-SGG 2024-10-07 16:04:27 +03:00
parent 632a718df1
commit 427a6611df
9 changed files with 112 additions and 0 deletions

Binary file not shown.

View file

@ -13,5 +13,7 @@ int main(void) {
if (equal(numbers)) printf("They are different");
else printf("They are the same");
free(numbers);
return 0;
}

1
15Ex/input.txt Normal file
View file

@ -0,0 +1 @@
1 1 2 2 1 1 2 2 1 1 2 2

14
15Ex/main.c Normal file
View file

@ -0,0 +1,14 @@
#include <stdio.h>
#include "tools.h"
#include "replace_local_min.h"
int main(void) {
double * numbers;
FILE * file = getFile();
numbers = getList(file);
if (numbers == NULL) return 1;
printf("Count elements in file: %d", replaceLocalMin(numbers));
free(numbers);
}

11
15Ex/makefile Normal file
View file

@ -0,0 +1,11 @@
all: main.o replace_local_min.o tools.o
gcc main.o replace_local_min.o tools.o && del *.o
main.o: main.c
gcc -c main.c
replace_local_min.o: replace_local_min.c
gcc -c replace_local_min.c
tools.o: tools.c
gcc -c tools.c

18
15Ex/replace_local_min.c Normal file
View file

@ -0,0 +1,18 @@
#include "replace_local_min.h"
int replaceLocalMin(double * numbers) {
int length = numbers[0];
unsigned final_num = 1;
unsigned local_length = 1;
for (int i = 2; i < length; ++i, ++final_num) {
if (numbers[i - 1] == numbers[i]) { if (local_length) ++local_length;
} else if (numbers[i - 1] < numbers[i]) {
if (local_length) final_num -= (local_length - 1), local_length = 0;
} else local_length = 1;
}
if ((numbers[length - 1] == numbers[length - 2]) && (local_length)) final_num -= (local_length - 1);
return final_num;
}

6
15Ex/replace_local_min.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef REPLACE_LOCAL_MIN
#define REPLACE_LOCAL_MIN
int replaceLocalMin(double * numbers);
#endif // REPLACE_LOCAL_MIN

50
15Ex/tools.c Normal file
View file

@ -0,0 +1,50 @@
#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;
}

10
15Ex/tools.h Normal file
View file

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