题目:给定一个字符流,使用状态机技术解析其中包含的单词个数,划分单词的分隔符只考虑【逗号】【句号】【空格】,实现获取单词个数接口:int32_t get_word_count(const char* input)。
下面是自己画的一张极丑的状态图,状态图中没有画从Init 到Final的线。
typedef enum _state_t {
INIT,
IN_WORD,
OUT_WORD,
FINAL
}state_t;
typedef enum _event_t {
SEPARATION = 1,
NOT_SEPARATION,
STOP
}event_t;
//动作前输入单词统计个数,动作后返回处理后的单词个数
typedef int32_t (*action_t)(int32_t word_count);
typedef struct _state_transformation_t {
state_t current_state; //当前状态,现态
event_t current_event; //当前发生的事件,条件
state_t next_state; //下个状态,次态
action_t action; //状态迁移时发生的动作
}state_transformation_t;
typedef struct _state_machine_t {
state_t state; //当前状态
int32_t transformation_count; //状态迁移的个数
state_transformation_t* transformation_array; //状态迁移的列表
}state_machine_t;
static state_transformation_t* find_transformation(const state_machine_t* state_machine, const event_t event) {
if (state_machine == NULL) {
return NULL;
}
for (int i = 0; i < state_machine->transformation_count; i++) {
if (state_machine->state == state_machine->transformation_array[i].current_state && event == state_machine->transformation_array[i].current_event) {
return &state_machine->transformation_array[i];
}
}
return NULL;
}
static int32_t count_add_one(int32_t currrent_word_count) {
return currrent_word_count + 1;
}
int32_t count_not_change(int32_t currrent_word_count) {
return currrent_word_count;
}
static event_t current_event(const char* one_char) {
char separate_char[] = { ',', '.', ' ' };
char final_char = '\0';
if (one_char == final_char) {
return STOP;
}
for (int i = 0; i < sizeof(separate_char); i++) {
if (one_char[0] == separate_char[i]) {
return SEPARATION;
}
}
return NOT_SEPARATION;
}
static int32_t get_word_count_ext(state_machine_t* state_machine, const char* words) {
int32_t word_count = 0;
char* one_char = words;
while (strlen(one_char) != 0)
{
state_transformation_t* state_transformation = find_transformation(state_machine, current_event(one_char));
if (state_transformation == NULL) {
return 0;
}
state_machine->state = state_transformation->next_state;
action_t action = state_transformation->action;
if (action != NULL) {
word_count = action(word_count);
}
one_char = one_char++;
}
return word_count;
}
int32_t get_word_count(const char* input) {
state_transformation_t state_transformation_array[] = {
{INIT, NOT_SEPARATION, IN_WORD, count_add_one},
{INIT, SEPARATION, OUT_WORD, count_not_change},
{INIT, STOP, FINAL, count_not_change},
{IN_WORD, STOP, FINAL, count_not_change},
{IN_WORD, NOT_SEPARATION, IN_WORD, count_not_change},
{IN_WORD, SEPARATION, OUT_WORD, count_not_change},
{OUT_WORD, SEPARATION, OUT_WORD, count_not_change},
{OUT_WORD, NOT_SEPARATION, IN_WORD, count_add_one},
{OUT_WORD, STOP, FINAL, count_not_change},
};
//初始化状态机
state_machine_t word_statistic_state_machine;
word_statistic_state_machine.state = INIT;
word_statistic_state_machine.transformation_count = sizeof(state_transformation_array) / sizeof(state_transformation_t);
word_statistic_state_machine.transformation_array = state_transformation_array;
return get_word_count_ext(&word_statistic_state_machine, input);
}
int main(int argc, char* argv[]) {
//26 words
char* input = "There are moments in life, when you miss someolifene so much that you just want to pick them from your dreams and hug them for real.";
//char* input2 = "hello, hi.";
int32_t word_count = get_word_count(input);
printf("words count:%d\n", word_count);
}