状态机实现文件单词统计


状态机实现文件单词统计


原理:
数据结构:
在这里插入图片描述
参考:

#include <stdio.h>
#define OUT	0
#define IN	1

#define INIT 	OUT

int splite(char c) {
	if ((' ' == c) || ('\n' == c) || ('\t' == c) ||
			('\"' == c) || ('\'' == c) || ('+' == c) ||
			(',' == c))
		return 1;
	else
		return 0;
}

int count_word(char *filename) {

	int status = INIT;
	int word = 0;

	FILE *fp = fopen(filename, "r");
	if (fp == NULL) return -1;

	char c;
	while ((c = fgetc(fp)) != EOF) {

		if (splite(c)) {
			status = OUT;
		}else if(OUT == status){
			status = IN;
			word ++;
		}
		
	}

	return word;
}

int main(int argc, char *argv[]) {

	if (argc < 2) return -1;
	printf("about word: %d\n", count_word(argv[1]));

	return 0;
}

运行结果:
在这里插入图片描述
英语单词(随便找的):
Book III Lesson 1
A puma at largePumas are large, cat-like animals which are found in America.
When reports came into London Zoo that a wild puma had been spotted forty-five miles south of London, they were not taken seriously.
However, as the evidence began to accumulate, experts from the Zoo felt obliged to investigate, for the descriptions given by people who claimed to have seen the puma were extraordinarily similar.
The hunt for the puma began in a small village where a woman picking blackberries saw ‘a large cat’ only five yards away from her.
It immediately ran away when she saw it, and experts confirmed that a puma will not attack a human being unless it is cornered.
The search proved difficult, for the puma was often observed at one place in the morning and at another place twenty miles away in the evening.
Wherever it went, it left behind it a trail of dead deer and small animals like rabbits.
Paw prints were seen in a number of places and puma fur was found clinging to bushes.
Several people complained of ‘cat-like noises’ at night and a businessman on a fishing trip saw the puma up a tree.

发布了33 篇原创文章 · 获赞 33 · 访问量 242

猜你喜欢

转载自blog.csdn.net/m0_45867846/article/details/105573756
今日推荐