Сделал 13 усовершенствованную с иным заданием
This commit is contained in:
parent
5852a9f034
commit
3f5c6299fc
11 changed files with 146 additions and 5 deletions
23
WorkingArrays/13ExDeluxe/equal.c
Normal file
23
WorkingArrays/13ExDeluxe/equal.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "equal.h"
|
||||
|
||||
int canFixArray(double * numbers) {
|
||||
unsigned short indexNonEqualNumber = 0, indexSecondNonEqualNumber = 0, numberDiffernce = 0, length = (int)numbers[0];
|
||||
|
||||
for (int i = 2; i < length; ++i) {
|
||||
if (!isEqual(numbers[i - 1], numbers[i])) {
|
||||
numberDiffernce++;
|
||||
if (!indexNonEqualNumber) indexNonEqualNumber = i;
|
||||
else if (!indexSecondNonEqualNumber) indexSecondNonEqualNumber = i - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (numberDiffernce > 2) return -1;
|
||||
else if (numberDiffernce == 2 && (!isEqual(numbers[indexNonEqualNumber - 1], numbers[length - 1]) || indexNonEqualNumber != indexSecondNonEqualNumber)) return -2;
|
||||
else if (numberDiffernce == 1 && ((indexNonEqualNumber != 2) && (indexNonEqualNumber != (length - 1)))) return -3;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isEqual(double first, double second) {
|
||||
return (fabs(first - second) < eps);
|
||||
}
|
13
WorkingArrays/13ExDeluxe/equal.h
Normal file
13
WorkingArrays/13ExDeluxe/equal.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef EQUAL
|
||||
#define EQUAL
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define eps 1.e-6
|
||||
|
||||
int canFixArray(double * numbers);
|
||||
bool isEqual(double first, double second);
|
||||
|
||||
#endif // EQUAL
|
1
WorkingArrays/13ExDeluxe/input.txt
Normal file
1
WorkingArrays/13ExDeluxe/input.txt
Normal file
|
@ -0,0 +1 @@
|
|||
1 1 1 2 2 2
|
19
WorkingArrays/13ExDeluxe/main.c
Normal file
19
WorkingArrays/13ExDeluxe/main.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
#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 (canFixArray(numbers)) printf("It can't be fixed!");
|
||||
else printf("It is possible to make them equal!");
|
||||
|
||||
free(numbers);
|
||||
|
||||
return 0;
|
||||
}
|
11
WorkingArrays/13ExDeluxe/makefile
Normal file
11
WorkingArrays/13ExDeluxe/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
WorkingArrays/13ExDeluxe/tools.c
Normal file
50
WorkingArrays/13ExDeluxe/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
WorkingArrays/13ExDeluxe/tools.h
Normal file
10
WorkingArrays/13ExDeluxe/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
|
|
@ -1,18 +1,25 @@
|
|||
#include "replace_local_min.h"
|
||||
|
||||
double min(double * numbers) {
|
||||
|
||||
for (int i = 1; i < length; ++i) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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 (fabs(numbers[i - 1] - numbers[i]) < exp) { if (local_length) ++local_length;
|
||||
} else if ((numbers[i] - numbers[i - 1]) > exp) {
|
||||
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);
|
||||
if ((fabs(numbers[length - 1] - numbers[length - 2]) < exp) && (local_length)) final_num -= (local_length - 1);
|
||||
|
||||
return final_num;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
#ifndef REPLACE_LOCAL_MIN
|
||||
#define REPLACE_LOCAL_MIN
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define exp 1.e-6
|
||||
|
||||
int replaceLocalMin(double * numbers);
|
||||
|
||||
#endif // REPLACE_LOCAL_MIN
|
||||
|
|
|
@ -26,9 +26,9 @@ int searching_loc_min(double * numbers)
|
|||
|
||||
|
||||
for (unsigned i = 2; i < length; ++i) {
|
||||
if (numbers[i] == numbers[i - 1]) {
|
||||
if (fabs(numbers[i] - numbers[i - 1]) < exp) {
|
||||
if (curr_len) ++curr_len;
|
||||
} else if (numbers[i] > numbers[i - 1]) {
|
||||
} else if ((numbers[i] - numbers[i - 1]) > exp) {
|
||||
if (curr_len) {
|
||||
// It is important to remember that numbers[0] is equal to the length of the list!
|
||||
start_end[0 + (min_num * 2)] = (i - curr_len), start_end[1 + (min_num * 2)] = i - 1;
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#ifndef LOCAL_MINIMUM
|
||||
#define LOCAL_MINIMUM
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define exp 1.e-6
|
||||
|
||||
int transposition(double * numbers, unsigned * start_end);
|
||||
int searching_loc_min(double * numbers);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue