From a1a35972d46888fa36c95f68736e912d490f74a6 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Fri, 8 Nov 2024 10:14:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=2025=D1=8E=20=D0=97=D0=B0=D0=B4=D0=B0=D1=87=D1=83?= =?UTF-8?q?=20=D1=81=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=BC=20=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=20=D0=B1=D0=B8=D1=82=D0=BE=D0=B2=D1=8B=D0=B5?= =?UTF-8?q?=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProcessingSequences/Second/count_elements.c | 23 +++++++++++---------- ProcessingSequences/Second/count_elements.h | 6 +++--- ProcessingSequences/Second/input.txt | 2 +- ProcessingSequences/Second/main.c | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/ProcessingSequences/Second/count_elements.c b/ProcessingSequences/Second/count_elements.c index ef3828a..f27c6c2 100644 --- a/ProcessingSequences/Second/count_elements.c +++ b/ProcessingSequences/Second/count_elements.c @@ -1,37 +1,38 @@ #include "count_elements.h" -double detectNumber(double number, double count) { +int detectNumber(double number, int count) { if ((1 - number > INACCURACY) || (number - 5 > INACCURACY)) { return 0; } - for (double i = 1.; i < 6; i++) { - // printf("%lf ? \n", abs(number - i)); - if (fabs(number - i) < INACCURACY) return (1 - (int)(fmod(count, pow(10., i)) / pow(10., i - 1))) * pow(10., i - 1); + for (int i = 1; i < 6; i++) { + if (fabs(number - i) < INACCURACY) return 1 << (i - 1); } return 0; } -double summa(double info_count) { +int summa(int info_count) { int count = 0; - for (int i = 0; i < 5; i++) { - count += fmod(info_count, 10.); - info_count /= 10; + for (;info_count;info_count >>= 1) { + count += info_count & 1; } + return count; } -double countElements(FILE * file) { +int countElements(FILE * file) { double current; - double count = 100000.; + int count; if (fscanf(file, "%lf", ¤t) != 1) { printf("File is empty!\n"); return -1; } + + count = 0; do { - count += detectNumber(current, count); + count |= detectNumber(current, count); } while (fscanf(file, "%lf", ¤t) == 1); return summa(count); diff --git a/ProcessingSequences/Second/count_elements.h b/ProcessingSequences/Second/count_elements.h index 71df543..f93df58 100644 --- a/ProcessingSequences/Second/count_elements.h +++ b/ProcessingSequences/Second/count_elements.h @@ -6,8 +6,8 @@ #define INACCURACY 1.e-10 -double detectNumber(double number, double count); -double summa(double count); -double countElements(FILE * file); +int detectNumber(double number, int count); +int summa(int count); +int countElements(FILE * file); #endif diff --git a/ProcessingSequences/Second/input.txt b/ProcessingSequences/Second/input.txt index 4f017c9..effe434 100644 --- a/ProcessingSequences/Second/input.txt +++ b/ProcessingSequences/Second/input.txt @@ -1 +1 @@ -0.123 -1.12 1 2 1 2 1 2 3.4 5 \ No newline at end of file +4 5 4 5 6 4 5 2.5 6 4 4 4 \ No newline at end of file diff --git a/ProcessingSequences/Second/main.c b/ProcessingSequences/Second/main.c index 42f1101..e590385 100644 --- a/ProcessingSequences/Second/main.c +++ b/ProcessingSequences/Second/main.c @@ -7,6 +7,6 @@ int main(void) { if (file == NULL) { return -1; } - printf("Count of numbers between 1 to 5 is %.0lf", countElements(file)); + printf("Count of numbers between 1 to 5 is %d", countElements(file)); return 0; }