• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            C++分析研究  
            C++
            日歷
            <2013年11月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567
            統計
            • 隨筆 - 92
            • 文章 - 4
            • 評論 - 4
            • 引用 - 0

            導航

            常用鏈接

            留言簿

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             
              一、狀態機實現的要素
             
               首先,分析一下一個普通的狀態機究竟要實現哪些內容。
             
               狀態機存儲從開始時刻到現在的變化,并根據當前輸入,決定下一個狀態。這意味著,狀態機要存儲狀態、獲得輸入(我們把它叫做跳轉條件)、做出響應。托福改分
             
               如上所示,{s1, s2, s3}均為狀態,箭頭c1/a1表示在s1狀態、輸入為c1時,跳轉到s2,并進行a1操作。托福答案
             
               最下方為一組輸入,狀態機應做出如下反應:
             
               當前狀態 輸入 下一個狀態 動作
             
               s1 c1 s2 a1
             
               s2 c2 s3 a2
             
               s3 c1 s2 a3
             
               s2 c2 s3 a2
             
               s3 c1 s2 a3
             
               s2 c1 s_trap a_trap
             
               s_trap c1 s_trap a_trap
             
               當某個狀態遇到不能識別的輸入時,就默認進入陷阱狀態,在陷阱狀態中,不論遇到怎樣的輸入都不能跳出。
             
               為了表達上面這個自動機,我們定義它們的狀態和輸入類型:
             
               typedef int state;
             
               typedef int condition;
             
               #define
             
               STATES 4
             
               #define
             
               STATE1 0
             
               #define
             
               STATE2 1
             
               #define
             
               STATE3 2
             
               #define
             
               STATETRAP 3
             
               #define
             
               CONDITIONS 2
             
               #define
             
               CONDITION1 0
             
               #define
             
               CONDITION2 1
             
               總結一下,我們需要定義的有狀態、輸入、行為(動作+下一個狀態),其中,行為的個數是"狀態數*輸入數量"(其中有一些是重復的);其中動作一般來說可以用一個函數指針來實現。
             
               二、具體設計
             
               在嵌入式環境中,由于存儲空間比較小,因此把它們全部定義成宏。此外,為了降低執行時間的不確定性,我們使用O(1)的跳轉表來模擬狀態的跳轉。
             
               首先定義跳轉類型:
             
               typedef void (*actiontype)(state
             
               mystate, condition condition);
             
               typedef struct
             
               {
             
               state
             
               next;
             
               actiontype
             
               action;
             
               }
             
               trasition, * ptrasition;
             
               然后按照上圖中的跳轉關系,把三個跳轉加一個陷阱跳轉先定義出來:
             
               //
             
               (s1, c1, s2, a1)
             
               trasition
             
               t1 = {
             
               STATE2,
             
               action1
             
               };
             
               //
             
               (s2, c2, s3, a2)
             
               trasition
             
               t2 = {
             
               STATE3,
             
               action2
             
               };
             
               //
             
               (s3, c1, s2, a3)
             
               trasition
             
               t3 = {
             
               STATE2,
             
               action3
             
               };
             
               //
             
               (s, c, trap, a1)
             
               trasition
             
               tt = {
             
               STATETRAP,
             
               actiontrap
             
               };
             
               其中的動作,由用戶自己完成,在這里僅定義一條輸出語句。
             
               void action1(State
             
               state, Condition condition)
             
               {
             
               printf("Action
             
               1 triggered.\n");
             
               }
             
               1
             
               最后定義跳轉表:
             
               asition
             
               transition_table[STATES][CONDITIONS] = {
             
               /*
             
               c1, c2*/
             
               /*
             
               s1 */&t1,
             
               &tt,
             
               /*
             
               s2 */&tt,
             
               &t2,
             
               /*
             
               s3 */&t3,
             
               &tt,
             
               /*
             
               st */&tt,
             
               &tt,
             
               };
             
               即可表達上文中的跳轉關系。
             
               最后定義狀態機,如果不考慮多任務請求,那么狀態機僅需要存儲當前狀態便行了。例如:
             
               typedef struct
             
               {
             
               State
             
               current;
             
               }
             
               StateMachine, * pStateMachine;
             
               State
             
               step(pStateMachine machine, Condition condition)
             
               {
             
               pTrasition
             
               t = transition_table[machine->current][condition];
             
               (*(t->action))(machine->current,
             
               condition);
             
               machine->current
             
               = t->next;
             
               return machine->current;
             
               }
             
               總結:我們現在設計實現好了一個狀態機,然后要給這個狀態機特定的輸入,看看狀態機的運轉情況,以上面圖中的那個狀態機為例,我們輸入的序列是0和1分別代表c1和C2,然后狀態s1,s2分別對應0,1.用程序實現這個內容如下
             
               三、程序實現
             
               程序清單:小型狀態機的實現
             
               [cpp
             
               #include<stdio.h>
             
               #include<unistd.h>
             
               #include<stdlib.h>
             
               typedef int state;
             
               typedef int condition;
             
               #define STATENUM 4
             
               #define STATE1 0
             
               #define STATE2 1
             
               #define STATE3 2
             
               #define STATETRAP 3
             
               #define CONDITIONS 2
             
               #define CONDITION1 0
             
               #define CONDITION2 1
             
               typedef void (* actiontype)(state mystate,condition mycondition);
             
               typedef struct{
             
               state next;
             
               actiontype action;
             
               }trasition, *ptrasition;
             
               void action1(state mystate,condition myconditon);
             
               void action2(state mystate,condition myconditon);
             
               void action3(state mystate,condition myconditon);
             
               void actiontrap(state mystate,condition myconditon);
             
               trasition t1={
             
               STATE2,action1
             
               };
             
               trasition t2={
             
               STATE3,action2
             
               };
             
               trasition t3={
             
               STATE2,action3
             
               };
             
               trasition tt={
             
               STATETRAP,actiontrap
             
               };
             
               void action1(state mystate,condition myconditon){
             
               printf("action1 one triggered\n");
             
               }
             
               void action2(state mystate,condition myconditon){
             
               printf("action2 one triggered\n");
             
               }
             
               void action3(state mystate,condition myconditon){
             
               printf("action3 one triggered\n");
             
               }
             
               void actiontrap(state mystate,condition myconditon){
             
               printf("actiontrap one triggered\n");
             
               }
             
               ptrasition transition_table[STATENUM][CONDITIONS] = {
             
               /* c1, c2*/
             
               /* s1 */&t1, &tt,
             
               /* s2 */&tt, &t2,
             
               /* s3 */&t3, &tt,
             
               /* st */&tt, &tt,
             
               };
             
               typedef struct
             
               {
             
               state current;
             
               } StateMachine, * pStateMachine;
             
               state step(pStateMachine machine, condition mycondition)
             
               {
             
               ptrasition t = transition_table[machine->current][mycondition];
             
               (*(t->action))(machine->current, mycondition);
             
               machine->current = t->next;
             
               printf("the current state is %d\n",t->next );
             
               return machine->current;
             
               }
             
               int main(int argc, char *argv[])
             
               {
             
               StateMachine mymachine;
             
               mymachine.current=STATE1;
             
               int mycon;
             
               char ch;
             
               while(1){
             
               scanf("%d",&mycon);
             
               step(&mymachine,mycon);
             
               }
             
               return 0;
             
               }
             
             
             
            posted on 2013-10-14 15:42 HAOSOLA 閱讀(1838) 評論(0)  編輯 收藏 引用
             
            Copyright © HAOSOLA Powered by: 博客園 模板提供:滬江博客
            PK10開獎 PK10開獎
            99久久综合国产精品免费| 日韩久久久久中文字幕人妻| 国内精品久久久久久麻豆| 国产aⅴ激情无码久久| 九九久久精品国产| 久久精品国产一区二区三区日韩| 久久久久亚洲精品无码蜜桃| 久久精品一区二区三区AV| 狠狠色丁香久久婷婷综合_中 | 99久久精品国产麻豆| 97精品伊人久久久大香线蕉| 亚洲精品tv久久久久| 久久影院亚洲一区| 欧美激情精品久久久久久久| 久久综合给合综合久久| 久久精品国产福利国产琪琪| 久久99精品久久久久久野外| 欧美久久一区二区三区| 久久久久久久97| 久久香蕉超碰97国产精品| 国内精品伊人久久久久| A级毛片无码久久精品免费| 91麻精品国产91久久久久| 人人狠狠综合久久亚洲| 亚洲国产精品一区二区三区久久| 综合久久精品色| 久久久国产乱子伦精品作者| 久久九九全国免费| 欧美日韩中文字幕久久久不卡| 久久这里只有精品首页| 精品久久久久久久| 亚洲性久久久影院| 国产美女久久久| 一级a性色生活片久久无少妇一级婬片免费放 | 久久er99热精品一区二区| 久久777国产线看观看精品| 久久国产成人午夜AV影院| 国产成年无码久久久免费| 久久国产精品免费| 97久久超碰国产精品旧版| 亚洲国产天堂久久综合|