Добавил выбор между рандомной генерацией и файлом в 6м задании

This commit is contained in:
AZEN-SGG 2024-12-20 23:49:57 +03:00
parent 7e7ec0e12c
commit 610ed3990a
4 changed files with 95 additions and 19 deletions

View file

@ -18,7 +18,7 @@ FILE * getFile(void) {
}
}
points getPoints(FILE * file) {
points * getFilePoints(FILE * file) {
int i, size = 2;
point * array = NULL;
point p;
@ -26,7 +26,7 @@ points getPoints(FILE * file) {
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!\n");
return (points){.array=NULL, 0};
return NULL;
}
array = (point *)malloc(size * sizeof(point));
@ -36,6 +36,7 @@ points getPoints(FILE * file) {
if (++i / 2 >= size) {
size *= 2;
array = (point *)realloc(array, size * sizeof(point));
if (array == NULL) return NULL;
}
if (i % 2 == 1) {
@ -46,9 +47,73 @@ points getPoints(FILE * file) {
if (i == 0) {
printf("Array is empty!\n");
return (points){.array=NULL, 0};
free(array);
return NULL
}
return (points){array, (i + 1) / 2};
return &(points){array, (i + 1) / 2};
}
points * GetPointsFromFile(void) {
FILE * file = getFile();
points * pts = NULL;
if (file == NULL) return NULL;
getFilePoints(file)
fclose(file);
return pts;
}
points * getRandomPoints(void) {
points * pts = &(points){.array=NULL, .length=0};
int length;
printf("Enter the number of points: ");
if (scanf("%d", &length) < 1) return NULL;
pts->length = length;
pts->array = (point *)malloc(length * sizeof(point));
if (pts->array == NULL) return NULL;
generate(pts);
return pts
}
points * getPoints(void) {
int fileOrRandom;
printf("0 - if points from file\n1 - if random generation of points\nYour choice: ");
if (scanf("%d", &fileOrRandom) < 1) {
printf("Wrong enter!\n");
return NULL;
}
if (fileOrRandom == 0) {
return getPointsFromFile();
} else (fileOrRandom == 1) {
return getRandomPoints();
} else {
printf("Wrong enter!\n");
return NULL;
}
}
void printPoints(points pts) {
for (int i = 0; i < ((MAX_PRINT_POINTS < pts.length) ? MAX_PRINT_POINTS : pts.length); ++i) {
printf("%c = (%.4lf, %.4lf)\n", i + ASCII_FIRST_LETTER, pts.array[i].x, pts.array[i].y);
}
}
void generate(points * pts)
{
srand(time(NULL));
for (int i = 0; pts->length; i++)
{
(pts->array[i]).x = (rand() % (2*MAX_RAND_COORD + 1) - MAX_RAND_COORD) * RAND_MULTIPLIER;
(pts->array[i]).y = (rand() % (2*MAX_RAND_COORD + 1) - MAX_RAND_COORD) * RAND_MULTIPLIER;
}
}