Сделал задание номер 3 (35)
This commit is contained in:
parent
05ce9de374
commit
f6f440fef3
12 changed files with 138 additions and 0 deletions
45
Second/count_func.c
Normal file
45
Second/count_func.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#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;
|
||||
}
|
||||
14
Second/count_func.h
Normal file
14
Second/count_func.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#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
|
||||
9
Second/input.txt
Normal file
9
Second/input.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
123
|
||||
1234
|
||||
12345
|
||||
123456
|
||||
234567
|
||||
345678
|
||||
456789
|
||||
12
|
||||
1
|
||||
20
Second/main.c
Normal file
20
Second/main.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#include "count_func.h"
|
||||
|
||||
int main(void) {
|
||||
FILE * file;
|
||||
char filename[50];
|
||||
|
||||
printf("Enter filename: ");
|
||||
scanf("%s", filename);
|
||||
|
||||
file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
printf("Error file!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Answer: %d", countWords(file));
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
Second/makefile
Normal file
8
Second/makefile
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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