确定有穷自动机测试代码

来源:互联网 发布:安卓app源码 编辑:程序博客网 时间:2024/05/23 11:47
#include "stdio.h"


/*
Q:state set ;
E:input table;
state transition function;
q0;
F:accept state set;
*/
#define False ((char)0)
#define True  (!False)


int IS_WORD_CHAR(char c)
{
    printf("c=%c\r\n", c);
    if('a'<=c && 'z'>=c)
    {
        return True;
    }
    else if('A'<=c && 'Z'>=c)
    {
        return True;
    }
    else
    {
        printf("xxxxxxxxxxxxxxxxxxxxxxxx\r\n");
        return False;
    }
}




int count_word(const char *text)
{
    enum _State
    {
        STAT_INIT,
        STAT_IN_WORD,
        STAT_OUT_WORD,
    }state = STAT_INIT;


    int count  = 0;
    const char *p = text;


    for (p = text; *p != '\0'; p++)
    {
        switch(state)
        {
            case   STAT_INIT:
            {
                if(IS_WORD_CHAR(*p))
                {
                    state = STAT_IN_WORD;
                }
                else
                {
                    state = STAT_OUT_WORD;
                }
                break;
            }
            case   STAT_IN_WORD:
            {
                if(!IS_WORD_CHAR(*p))
                {
                    count++;
                    state = STAT_OUT_WORD;
                }
                break;
            }
            case   STAT_OUT_WORD:
            {
                if(IS_WORD_CHAR(*p))
                {
                    state = STAT_IN_WORD;
                }


                break;
            }
            default:
                    break;
       }
    }


    if(STAT_IN_WORD == state)
    {
        count++;
    }


    return (count);
}




const char *data = " abc a. hello girls,  code-error fix! something todo end";
int main(void)
{
    int num = count_word(data);


    printf("count_word=%d\r\n", num);

}
















0 0
原创粉丝点击