Сделал задание 25 по ЭВМ

This commit is contained in:
AZEN-SGG 2024-09-18 09:54:54 +03:00
commit 05ce9de374
3 changed files with 79 additions and 0 deletions

45
count_func.c Normal file
View 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", &current) != 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", &current) == 1);
return count;
}

14
count_func.h Normal file
View 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

20
main.c Normal file
View 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;
}