Написал две задачи к Автотесту
This commit is contained in:
parent
b4cb5098c2
commit
19e0da8dc6
9 changed files with 295 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -3,3 +3,6 @@
|
||||||
*~
|
*~
|
||||||
*.out
|
*.out
|
||||||
.*.sw*
|
.*.sw*
|
||||||
|
*.zip
|
||||||
|
*.rar
|
||||||
|
*output*
|
1
Session/1Ex/ina.txt
Normal file
1
Session/1Ex/ina.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1 2 3 4 5 6 7
|
1
Session/1Ex/inb.txt
Normal file
1
Session/1Ex/inb.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
2 3 4 6 7
|
117
Session/1Ex/main.c
Normal file
117
Session/1Ex/main.c
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double * arr;
|
||||||
|
int len;
|
||||||
|
} arr;
|
||||||
|
|
||||||
|
#define exp 1.e-6
|
||||||
|
|
||||||
|
FILE * getFile(const char * name);
|
||||||
|
arr getArray(FILE * file);
|
||||||
|
bool compare(arr first, arr second);
|
||||||
|
int max(int f, int s);
|
||||||
|
int min(int f, int s);
|
||||||
|
int write(const char * str);
|
||||||
|
|
||||||
|
|
||||||
|
FILE * getFile(const char * name) {
|
||||||
|
FILE * file = fopen(name, "r");
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
arr getArray(FILE * file) {
|
||||||
|
int size = 2, len = 0;
|
||||||
|
double * array = (double *)malloc(size * sizeof(double));
|
||||||
|
double current;
|
||||||
|
|
||||||
|
if (fscanf(file, "%lf", ¤t) != 1) return (arr){NULL, -1};
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (++len > size) {
|
||||||
|
size *= 2;
|
||||||
|
array = (double *)realloc(array, size * sizeof(double));
|
||||||
|
}
|
||||||
|
|
||||||
|
array[len - 1] = current;
|
||||||
|
} while (fscanf(file, "%lf", ¤t) == 1);
|
||||||
|
|
||||||
|
return (arr){array, len};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool compare(arr first, arr second) {
|
||||||
|
int len = min(first.len, second.len);
|
||||||
|
double maximf = 0, maxims = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; ++i) {
|
||||||
|
if (first.arr[i] - second.arr[i] > -exp) return false;
|
||||||
|
maximf = max(maximf, first.arr[i]);
|
||||||
|
maxims = max(maxims, second.arr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first.len > second.len) {
|
||||||
|
for (int i = len; i < first.len; ++i) {
|
||||||
|
if (first.arr[i] - maxims > -exp) return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int i = len; i < second.len; ++i) {
|
||||||
|
if (maximf - second.arr[i] > -exp) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int min(int f, int s) {
|
||||||
|
if (f > s) return s;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int max(int f, int s) {
|
||||||
|
if (f < s) return s;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int write(const char * str) {
|
||||||
|
FILE * file = fopen("output.txt", "w");
|
||||||
|
if (file == NULL) return -1;
|
||||||
|
|
||||||
|
if (fprintf(file, "%s", str) < 1) {
|
||||||
|
fclose(file);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fclose(file) != 0) return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
arr arr1, arr2;
|
||||||
|
FILE * file;
|
||||||
|
bool answer;
|
||||||
|
|
||||||
|
file = getFile("ina.txt");
|
||||||
|
if (file == NULL) return -1;
|
||||||
|
arr1 = getArray(file);
|
||||||
|
if (fclose(file) != 0) return -1;
|
||||||
|
if (arr1.arr == NULL) return -1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
file = getFile("inb.txt");
|
||||||
|
if (file == NULL) return -1;
|
||||||
|
arr2 = getArray(file);
|
||||||
|
if (fclose(file) != 0) return -1;
|
||||||
|
if (arr2.arr == NULL) return -1;
|
||||||
|
|
||||||
|
answer = compare(arr1, arr2);
|
||||||
|
|
||||||
|
if (answer) return write("YES");
|
||||||
|
else return write("NO");
|
||||||
|
}
|
||||||
|
|
37
Session/1Ex/makefile
Normal file
37
Session/1Ex/makefile
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
CFLAGS = -mfpmath=sse \
|
||||||
|
-fstack-protector-all \
|
||||||
|
-W \
|
||||||
|
-Wall \
|
||||||
|
-Wextra \
|
||||||
|
-Wunused \
|
||||||
|
-Wcast-align \
|
||||||
|
-Werror \
|
||||||
|
-pedantic \
|
||||||
|
-pedantic-errors \
|
||||||
|
-Wfloat-equal \
|
||||||
|
-Wpointer-arith \
|
||||||
|
-Wformat-security \
|
||||||
|
-Wmissing-format-attribute \
|
||||||
|
-Wformat=1 \
|
||||||
|
-Wwrite-strings \
|
||||||
|
-Wcast-align \
|
||||||
|
-Wno-long-long \
|
||||||
|
-std=gnu99 \
|
||||||
|
-Wstrict-prototypes \
|
||||||
|
-Wmissing-prototypes \
|
||||||
|
-Wmissing-declarations \
|
||||||
|
-Wold-style-definition \
|
||||||
|
-Wdeclaration-after-statement \
|
||||||
|
-Wbad-function-cast \
|
||||||
|
-Wnested-externs \
|
||||||
|
-O2 \
|
||||||
|
-D_DEBUG -g \
|
||||||
|
-c
|
||||||
|
|
||||||
|
all: main.o
|
||||||
|
gcc main.o -lssp && del *.o
|
||||||
|
a.exe
|
||||||
|
|
||||||
|
main.o: main.c
|
||||||
|
gcc $(CFLAGS) main.c
|
||||||
|
|
1
Session/2Ex/ina.txt
Normal file
1
Session/2Ex/ina.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1 2 3 4 5
|
1
Session/2Ex/inb.txt
Normal file
1
Session/2Ex/inb.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
1 2 3 5 6
|
96
Session/2Ex/main.c
Normal file
96
Session/2Ex/main.c
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define exp 1.e-6
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double * arr;
|
||||||
|
int len;
|
||||||
|
} array;
|
||||||
|
|
||||||
|
int min(int f, int s);
|
||||||
|
FILE * getFile(const char * name);
|
||||||
|
array getArray(FILE * file);
|
||||||
|
bool compare(array first, array second);
|
||||||
|
int write(const char * str);
|
||||||
|
|
||||||
|
int min(int f, int s) {
|
||||||
|
if (f > s) return s;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE * getFile(const char * name) {
|
||||||
|
FILE * file = fopen(name, "r");
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
array getArray(FILE * file) {
|
||||||
|
int size = 2, len = 0;
|
||||||
|
double * arr = (double *)malloc(size * sizeof(double));
|
||||||
|
double current;
|
||||||
|
|
||||||
|
if (fscanf(file, "%lf", ¤t) != 1) return (array){NULL, -1};
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (++len > size) {
|
||||||
|
size *= 2;
|
||||||
|
arr = (double *)realloc(arr, size * sizeof(double));
|
||||||
|
}
|
||||||
|
|
||||||
|
arr[len - 1] = current;
|
||||||
|
} while (fscanf(file, "%lf", ¤t) == 1);
|
||||||
|
|
||||||
|
return (array){arr, len};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool compare(array first, array second) {
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < min(first.len, second.len); ++i) {
|
||||||
|
if (first.arr[i] - second.arr[i] > -exp) --count;
|
||||||
|
else ++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count > 0) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int write(const char * str) {
|
||||||
|
FILE * file = fopen("output.txt", "w");
|
||||||
|
|
||||||
|
if (file == NULL) return -1;
|
||||||
|
|
||||||
|
if (fprintf(file, "%s", str) < 1) {
|
||||||
|
fclose(file);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fclose(file) != 0) return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
array arr1, arr2;
|
||||||
|
FILE * file;
|
||||||
|
bool answer;
|
||||||
|
|
||||||
|
file = getFile("ina.txt");
|
||||||
|
if (file == NULL) return -1;
|
||||||
|
arr1 = getArray(file);
|
||||||
|
if (fclose(file) != 0) return -1;
|
||||||
|
if (arr1.arr == NULL) return -1;
|
||||||
|
|
||||||
|
file = getFile("inb.txt");
|
||||||
|
if (file == NULL) return -1;
|
||||||
|
arr2 = getArray(file);
|
||||||
|
if (fclose(file) != 0) return -1;
|
||||||
|
if (arr2.arr == NULL) return -1;
|
||||||
|
|
||||||
|
answer = compare(arr1, arr2);
|
||||||
|
|
||||||
|
if (answer) return write("YES");
|
||||||
|
else return write("NO");
|
||||||
|
}
|
37
Session/2Ex/makefile
Normal file
37
Session/2Ex/makefile
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
CFLAGS = -mfpmath=sse \
|
||||||
|
-fstack-protector-all \
|
||||||
|
-W \
|
||||||
|
-Wall \
|
||||||
|
-Wextra \
|
||||||
|
-Wunused \
|
||||||
|
-Wcast-align \
|
||||||
|
-Werror \
|
||||||
|
-pedantic \
|
||||||
|
-pedantic-errors \
|
||||||
|
-Wfloat-equal \
|
||||||
|
-Wpointer-arith \
|
||||||
|
-Wformat-security \
|
||||||
|
-Wmissing-format-attribute \
|
||||||
|
-Wformat=1 \
|
||||||
|
-Wwrite-strings \
|
||||||
|
-Wcast-align \
|
||||||
|
-Wno-long-long \
|
||||||
|
-std=gnu99 \
|
||||||
|
-Wstrict-prototypes \
|
||||||
|
-Wmissing-prototypes \
|
||||||
|
-Wmissing-declarations \
|
||||||
|
-Wold-style-definition \
|
||||||
|
-Wdeclaration-after-statement \
|
||||||
|
-Wbad-function-cast \
|
||||||
|
-Wnested-externs \
|
||||||
|
-O2 \
|
||||||
|
-D_DEBUG -g \
|
||||||
|
-c
|
||||||
|
|
||||||
|
all: main.o
|
||||||
|
gcc main.o -lssp && del *.o
|
||||||
|
a.exe
|
||||||
|
|
||||||
|
main.o: main.c
|
||||||
|
gcc $(CFLAGS) main.c
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue