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; }