diff --git a/Second/count_elements.c b/Second/count_elements.c new file mode 100644 index 0000000..02fbe54 --- /dev/null +++ b/Second/count_elements.c @@ -0,0 +1,18 @@ +#include "count_elements.h" + +int countElements(FILE * file) { + int current, count; + + count = 0; + + if (fscanf(file, "%d", ¤t) != 1) + { + printf("File is empty!\n"); + return -1; + } + do { + if (current <= 5 && current >= 1) count++; + } while (fscanf(file, "%d", ¤t) == 1); + + return count; +} \ No newline at end of file diff --git a/Second/count_elements.h b/Second/count_elements.h new file mode 100644 index 0000000..855d916 --- /dev/null +++ b/Second/count_elements.h @@ -0,0 +1,8 @@ +#ifndef COUNT_ELEMENTS +#define COUNT_ELEMENTS + +#include + +int countElements(FILE * file); + +#endif \ No newline at end of file diff --git a/Second/count_func.c b/Second/count_func.c deleted file mode 100644 index 5acb4d0..0000000 --- a/Second/count_func.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "count_func.h" - -int locatedIn(char symb) { - char numbers[] = NUMBERS; - - for (int i = 0; i < 5; i++) { - if (symb == numbers[i]) { - return 1; - } - } - - return 0; -} - -int countWords(FILE * file) { - int located, now, count; - char current; - - located = IN; - now = OUT; - count = 0; - - if (fscanf(file, "%c", ¤t) != 1) { - printf("File is empty\n"); - return -1; - } - do { - if (current == '\n' || current == '\t' || current == ' ') { - if (now == IN && located == IN) { - count += 1; - } - now = OUT; - } else { - if (now == OUT) { - now = IN; - located = IN; - } - if (locatedIn(current) == 0) { - located = OUT; - } - } - } while (fscanf(file, "%c", ¤t) == 1); - - return count; -} diff --git a/Second/count_func.h b/Second/count_func.h deleted file mode 100644 index 76ca349..0000000 --- a/Second/count_func.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef COUNT_FUNC -#define COUNT_FUNC - -#include - -#define NUMBERS {'1', '2', '3', '4', '5'} -#define IN 1 -#define OUT 0 - -int locatedIn(char symb); - -int countWords(FILE * file); - -#endif diff --git a/Second/get_file.c b/Second/get_file.c new file mode 100644 index 0000000..5c5e915 --- /dev/null +++ b/Second/get_file.c @@ -0,0 +1,23 @@ +#include "get_file.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; + } +} diff --git a/Second/get_file.h b/Second/get_file.h new file mode 100644 index 0000000..d839b95 --- /dev/null +++ b/Second/get_file.h @@ -0,0 +1,8 @@ +#ifndef GET_FILE +#define GET_FILE + +#include + +FILE * getFile(void); + +#endif diff --git a/Second/input.txt b/Second/input.txt index 99fc703..e02a813 100644 --- a/Second/input.txt +++ b/Second/input.txt @@ -1,9 +1,7 @@ -123 -1234 -12345 -123456 -234567 -345678 -456789 -12 -1 \ No newline at end of file +1 +2 +3 +4 +5 +6 +5 \ No newline at end of file diff --git a/Second/main.c b/Second/main.c index 6325db5..e590385 100644 --- a/Second/main.c +++ b/Second/main.c @@ -1,20 +1,12 @@ #include -#include "count_func.h" +#include "get_file.h" +#include "count_elements.h" int main(void) { - FILE * file; - char filename[50]; - - printf("Enter filename: "); - scanf("%s", filename); - - file = fopen(filename, "r"); + FILE * file = getFile(); if (file == NULL) { - printf("Error file!\n"); - return 1; + return -1; } - - printf("Answer: %d", countWords(file)); - + printf("Count of numbers between 1 to 5 is %d", countElements(file)); return 0; } diff --git a/Second/makefile b/Second/makefile deleted file mode 100644 index a9de7c9..0000000 --- a/Second/makefile +++ /dev/null @@ -1,8 +0,0 @@ -all: main.o count_func.o - gcc main.o count_func.o && del *.o - -main.o: main.c - gcc -c main.c - -count_func.o: count_func.c - gcc -c count_func.c \ No newline at end of file