Сделал 1 Задание со списками
This commit is contained in:
parent
5dbf4901b8
commit
632a718df1
71 changed files with 106 additions and 0 deletions
BIN
13Ex/a.exe
Normal file
BIN
13Ex/a.exe
Normal file
Binary file not shown.
6
13Ex/equal.c
Normal file
6
13Ex/equal.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "equal.h"
|
||||
|
||||
int equal(double * numbers) {
|
||||
for (int i = 2; i < numbers[0]; i++) if (fabs(numbers[i] - numbers[i - 1]) > eps) return 1;
|
||||
return 0;
|
||||
}
|
11
13Ex/equal.h
Normal file
11
13Ex/equal.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef EQUAL
|
||||
#define EQUAL
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#define eps 1.e-6
|
||||
|
||||
int equal(double * numbers);
|
||||
|
||||
#endif // EQUAL
|
1
13Ex/input.txt
Normal file
1
13Ex/input.txt
Normal file
|
@ -0,0 +1 @@
|
|||
1 1 1 1 1 1 1
|
17
13Ex/main.c
Normal file
17
13Ex/main.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <stdio.h>
|
||||
#include "tools.h"
|
||||
#include "equal.h"
|
||||
|
||||
int main(void) {
|
||||
double * numbers;
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) return 1;
|
||||
|
||||
numbers = getList(file);
|
||||
if (numbers == NULL) return 1;
|
||||
|
||||
if (equal(numbers)) printf("They are different");
|
||||
else printf("They are the same");
|
||||
|
||||
return 0;
|
||||
}
|
11
13Ex/makefile
Normal file
11
13Ex/makefile
Normal file
|
@ -0,0 +1,11 @@
|
|||
all: main.o equal.o tools.o
|
||||
gcc main.o equal.o tools.o && del *.o
|
||||
|
||||
main.o: main.c
|
||||
gcc -c main.c
|
||||
|
||||
equal.o: equal.c
|
||||
gcc -c equal.c
|
||||
|
||||
tools.o: tools.c
|
||||
gcc -c tools.c
|
50
13Ex/tools.c
Normal file
50
13Ex/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
13Ex/tools.h
Normal file
10
13Ex/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