Переделал вторую задачу
This commit is contained in:
parent
f6f440fef3
commit
8a0409478a
9 changed files with 69 additions and 89 deletions
18
Second/count_elements.c
Normal file
18
Second/count_elements.c
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
8
Second/count_elements.h
Normal file
8
Second/count_elements.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef COUNT_ELEMENTS
|
||||||
|
#define COUNT_ELEMENTS
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int countElements(FILE * file);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
#ifndef COUNT_FUNC
|
|
||||||
#define COUNT_FUNC
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define NUMBERS {'1', '2', '3', '4', '5'}
|
|
||||||
#define IN 1
|
|
||||||
#define OUT 0
|
|
||||||
|
|
||||||
int locatedIn(char symb);
|
|
||||||
|
|
||||||
int countWords(FILE * file);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
23
Second/get_file.c
Normal file
23
Second/get_file.c
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Second/get_file.h
Normal file
8
Second/get_file.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef GET_FILE
|
||||||
|
#define GET_FILE
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
FILE * getFile(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
123
|
1
|
||||||
1234
|
2
|
||||||
12345
|
3
|
||||||
123456
|
4
|
||||||
234567
|
5
|
||||||
345678
|
6
|
||||||
456789
|
5
|
||||||
12
|
|
||||||
1
|
|
||||||
|
|
@ -1,20 +1,12 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "count_func.h"
|
#include "get_file.h"
|
||||||
|
#include "count_elements.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
FILE * file;
|
FILE * file = getFile();
|
||||||
char filename[50];
|
|
||||||
|
|
||||||
printf("Enter filename: ");
|
|
||||||
scanf("%s", filename);
|
|
||||||
|
|
||||||
file = fopen(filename, "r");
|
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
printf("Error file!\n");
|
return -1;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
printf("Count of numbers between 1 to 5 is %d", countElements(file));
|
||||||
printf("Answer: %d", countWords(file));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue