Добавил к седьмому заданию сравнение разных сортировок, сделал 9е задание на массивы

This commit is contained in:
AZEN-SGG 2024-12-04 13:25:05 +03:00
parent c33f7295d5
commit a45af76154
25 changed files with 599 additions and 23 deletions

View file

@ -0,0 +1,58 @@
#include "grouping.h"
void group(double * array, int length) {
negativeToRight(array, length);
zeroesToCenter(array, length);
}
void negativeToRight(double * array, int length) {
double temp;
int negative = 0, last_index = length - 1;
for (int i = 0; i <= last_index - negative; ++i) {
// Так как мы передвигаем каждый отрицательный эл-нт в конец
// То тогда нам не нужно проверять последний эл-нт (т.к. он отрицательный) - поэтому я его пропускаю - -negative
if (array[i] < 0) {
for (int j = length - 2; j >= i; --j) {
// Сначала последний эл-нт копируется в предпоследний, а предпоследний в последний
// Далее каждый следующий j-й элемент заменяется тем, что находится на месте последнего, а последний элемент заменяется на j
// Так шаг за шагов элементы переставляются и наш i-й элемент оказывается на месте последнего (типа ЭП2)
temp = array[j];
array[j] = array[last_index];
array[last_index] = temp;
}
--i, ++negative;
}
}
}
void zeroesToCenter(double * array, int length) {
// Аналогичный способ перемещения эл-тов, но только не в конец, а в место между отрицательными и положительными числами
double temp;
int zeroes = 0, last_index = 0;
while (array[last_index] >= 0 && last_index < length) ++last_index;
// last_index < length - очень важен, так как если нет отрицательных чисел, то цикл уёдет в бесконечность
--last_index; // Так как изначально указывает на первый отрицательный элемент, а нам нужен последний положительный
for (int i = 0; i <= last_index - zeroes; ++i) {
if (fabs(array[i]) < eps) {
for (int j = last_index - 1; j >= i; --j) {
temp = array[j];
array[j] = array[last_index];
array[last_index] = temp;
}
--i, ++zeroes;
}
}
}
void printArray(double * array, int length) {
for (int i = 0; i < length; ++i) {
printf("%.2lf ", array[i]);
}
printf("\n");
}

View file

@ -0,0 +1,14 @@
#ifndef GROUPING
#define GROUPING
#include <stdio.h>
#include <math.h>
#define eps 1.e-6
void group(double * array, int length);
void negativeToRight(double * array, int length);
void zeroesToCenter(double * array, int length);
void printArray(double * array, int length);
#endif

View file

@ -0,0 +1 @@
-1 -2 -3 -4 -5 0 1 2 3 4 5

27
WorkingArrays/9Ex/main.c Normal file
View file

@ -0,0 +1,27 @@
#include "tools.h"
#include "grouping.h"
int main(void) {
FILE * file = getFile();
double * array;
double * orig_arr;
int length;
if (file == NULL) return -1;
array = getArray(file);
if (array == NULL) return -2;
length = (int)array[0] - 1;
orig_arr = array;
array = &array[1];
printArray(array, length);
group(array, length);
printArray(array, length);
free(orig_arr);
return 0;
}

View file

@ -0,0 +1,43 @@
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 \
-O3 \
-D_DEBUG -g \
-c
all: main.o grouping.o tools.o
gcc main.o grouping.o tools.o -lssp && del *.o
a.exe
main.o: main.c
gcc $(CFLAGS) main.c
grouping.o: grouping.c
gcc $(CFLAGS) grouping.c
tools.o: tools.c
gcc $(CFLAGS) tools.c

47
WorkingArrays/9Ex/tools.c Normal file
View file

@ -0,0 +1,47 @@
#include "tools.h"
FILE * getFile(void) {
char filename[50];
printf("Enter file name: ");
if (scanf("%s", filename) == 1) {
FILE * file = fopen(filename, "r");
if (file == NULL) {
printf("Error file!\n)");
return NULL;
} else {
return file;
}
} else {
printf("Empty name!\n");
return NULL;
}
}
double * getArray(FILE * file) {
int i, size = 2;
double * array = NULL;
double current;
if (fscanf(file, "%lf", &current) != 1) {
printf("File if empty!");
return array;
}
array = (double *)malloc(size * sizeof(double));
i = 1;
do {
if (i >= size) {
size *= 2;
array = (double *)realloc(array, sizeof(double) * size);
}
array[i] = current;
++i;
} while (fscanf(file, "%lf", &current) == 1);
array = (double *)realloc(array, sizeof(double) * i);
array[0] = (double)i;
return array;
}

10
WorkingArrays/9Ex/tools.h Normal file
View file

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