Сделал 15 Задание с массивами
This commit is contained in:
parent
632a718df1
commit
427a6611df
9 changed files with 112 additions and 0 deletions
BIN
13Ex/a.exe
BIN
13Ex/a.exe
Binary file not shown.
|
@ -13,5 +13,7 @@ int main(void) {
|
||||||
if (equal(numbers)) printf("They are different");
|
if (equal(numbers)) printf("They are different");
|
||||||
else printf("They are the same");
|
else printf("They are the same");
|
||||||
|
|
||||||
|
free(numbers);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
1
15Ex/input.txt
Normal file
1
15Ex/input.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1 1 2 2 1 1 2 2 1 1 2 2
|
14
15Ex/main.c
Normal file
14
15Ex/main.c
Normal 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
11
15Ex/makefile
Normal 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
18
15Ex/replace_local_min.c
Normal 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
6
15Ex/replace_local_min.h
Normal 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
50
15Ex/tools.c
Normal 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", ¤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;
|
||||||
|
}
|
10
15Ex/tools.h
Normal file
10
15Ex/tools.h
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef TOOLS
|
||||||
|
#define TOOLS
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
FILE * getFile(void);
|
||||||
|
double * getList(FILE * file);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Add a link
Reference in a new issue