以下是轉載內容:
☆─────────────────────────傳說中的分隔字元───────────────────────────────────────☆
來源1:http://www.cnblogs.com/swingboat/archive/2005/07/27/201488.html
【轉載1】有限狀態機器的實現
有限狀態機器(Finite State Machine或者Finite State Automata)是軟體領域中一種重要的工具,很多東西的模型實際上就是有限狀態機器。
最近看了一些遊戲編程AI的材料,感覺遊戲中的AI,第一要說的就是有限狀態機器來實現精靈的AI,然後才是A*尋路,其他學術界討論比較多的神經網路、模糊控制等問題還不是很熱。
FSM的實現方式:
1) switch/case或者if/else
這無意是最直觀的方式,使用一堆條件判斷,會編程的人都可以做到,對簡單小巧的狀態機器來說最合適,但是毫無疑問,這樣的方式比較原始,對龐大的狀態機器難以維護。
2) 狀態表
維護一個二維狀態表,橫座標表示目前狀態,縱座標表示輸入,表中一個元素儲存下一個狀態和對應的操作。這一招易於維護,但是已耗用時間和儲存空間的代價較大。
3) 使用State Pattern
使用State Pattern使得代碼的維護比switch/case方式稍好,效能上也不會有很多的影響,但是也不是100%完美。不過Robert C. Martin做了兩個自動產生FSM代碼的工具,for java和for C++各一個,在http://www.objectmentor.com/resources/index上有免費下載,這個工具的輸入是純文字的狀態機器描述,自動產生符合State Pattern的代碼,這樣developer的工作只需要維護狀態機器的文本描述,每必要冒引入bug的風險去維護code。
4) 使用宏定義描述狀態機器
一般來說,C++編程中應該避免使用#define,但是這主要是因為如果用宏來定義函數的話,很容易產生這樣那樣的問題,但是巧妙的使用,還是能夠產生奇妙的效果。MFC就是使用宏定義來實現大的架構的。
在實現FSM的時候,可以把一些繁瑣無比的if/else還有花括弧的組合放在宏中,這樣,在代碼中可以3)中狀態機器描述文本一樣寫,通過編譯器的先行編譯處理產生1)一樣的效果,我見過產生C代碼的宏,如果要產生C++代碼,己軟MFC可以,那麼理論上也是可行的。
【評】:狀態表的實現方法,《C專家編程》第8章有具體說明,轉載【6】
☆──────────────────────傳說中的分隔字元──────────────────────────────☆
來源2:http://hi.baidu.com/juneshine/blog/item/6bff718bd5902f13c9fc7a14.html
【轉載2】有限狀態機器的c實現
2007-05-11 15:12
網絡上可以搜尋到很多有限狀態機的代碼和理論分析,這兒僅僅是做一個簡單的例子,僅供入門參考。
這兒以四位密碼校正作為狀態機器的例子,連續輸入2479就可以通過密碼測試。一個非常簡單的例子,在實際的狀態機器執行個體中,狀態跳躍表要更復雜一些,不過方式非常類似。在狀態查詢的地方可以做優化,同時對於輸入量也可以做有效性優化。具體代碼如下:
view plaincopy to clipboardprint?
c.h
typedef enum{
STATE1 = 1,
STATE2,
STATE3,
STATE4,
STATE5,//password pass
//...ADD here
}STATE;
typedef enum{
INPUT1 = '2',
INPUT2 = '4',
INPUT3 = '7',
INPUT4 = '9',
}INPUT;
typedef struct
{
STATE cur_state;
INPUT input;
STATE next_state;
}STATE_TRANS;
c.h
typedef enum{
STATE1 = 1,
STATE2,
STATE3,
STATE4,
STATE5,//password pass
//...ADD here
}STATE;
typedef enum{
INPUT1 = '2',
INPUT2 = '4',
INPUT3 = '7',
INPUT4 = '9',
}INPUT;
typedef struct
{
STATE cur_state;
INPUT input;
STATE next_state;
}STATE_TRANS;
c.c
#include
#include "c.h"
STATE_TRANS state_trans_arry[] =
{
{STATE1,INPUT1,STATE2},
{STATE2,INPUT2,STATE3},
{STATE3,INPUT3,STATE4},
{STATE4,INPUT4,STATE5},
};
#define STATE_TRANS_CNT (sizeof(state_trans_arry)/sizeof(state_trans_arry[0]))
int main()
{
int i;
char ch;
STATE state_machine = STATE1;
while(ch != 'e')
{
ch = getchar();
if((ch >= '0') && (ch
#include "c.h"
STATE_TRANS state_trans_arry[] =
{
{STATE1,INPUT1,STATE2},
{STATE2,INPUT2,STATE3},
{STATE3,INPUT3,STATE4},
{STATE4,INPUT4,STATE5},
};
#define STATE_TRANS_CNT (sizeof(state_trans_arry)/sizeof(state_trans_arry[0]))
int main()
{
int i;
char ch;
STATE state_machine = STATE1;
while(ch != 'e')
{
ch = getchar();
if((ch >= '0') && (ch
#include
#include
//Finite state machine declaration
//state declaration
#define IDLE 0 //idle state in rx mode
#define M_BROADCAST 1 //broadcast state in tx mode,broadcast to be a master point
#define M_WAIT_BROADCAST_ACK 2 //wait for broadcast ack state in rx mode,wait for the point ack in a specific time window
#define M_WAIT_COMMAND 3 //wait for command state,wait for PC command via UART
#define M_BROADCAST_CANCEL 4 //broadcast cancel state,broadcast to cancel master point
#define S_BROADCAST_ACK 5 //slave mode,send back self physical address
#define S_WAIT_COMMAND 6 //slave mode, wait for command from the master point
//state transition trig
//used in master mode
int isReqBeMaster = 0;//Is PC request the point to be master?
int isTimeout = 0;//Is time out?
int isReqCancelMaster = 0;//Is request to cancel master?
//used in slave mode
int isRxBroadcast = 0;//Is there a point broadcast to be master?
int isRxBroadcastCancel = 0;//Is receive broadcast cancel master?
typedef struct fsmtag
{
int state; //state
int timeouttime; //time out time in milliseconds
}fsm;
//function prototype
int main()
{
fsm f;
f.state = IDLE;
f.timeouttime = 0;
while(1)
{
switch(f.state)
{
case IDLE:
puts("IDLE/nWait for isReqBeMaster(1/0) isRxBroadcast(1/0):");
scanf("%d %d",&isReqBeMaster,&isRxBroadcast);
if(isReqBeMaster)
{
f.state = M_BROADCAST;
break;
}
else if(isRxBroadcast)
{
f.state = S_BROADCAST_ACK;
break;
}
else
break;
case M_BROADCAST:
puts("M_BROADCAST/nBroadcasting.../n");
f.state = M_WAIT_BROADCAST_ACK;
case M_WAIT_BROADCAST_ACK:
puts("M_WAIT_BROADCAST_ACK/nWaiting for isTimeout(1/0):");
scanf("%d",&isTimeout);
if(isTimeout)
{
f.state = M_WAIT_COMMAND;
break;
}
else
break;
case M_WAIT_COMMAND:
puts("M_WAIT_COMMAND/nWaiting for isReqCancelMaster(1/0):");
scanf("%d",&isReqCancelMaster);
if(isReqCancelMaster)
{
f.state = IDLE;
break;
}
else
break;
//Slave mode routine
case S_BROADCAST_ACK:
puts("S_BROADCAST_ACK/nAcking.../n");
f.state = S_WAIT_COMMAND;
break;
case S_WAIT_COMMAND:
puts("S_WAIT_COMMAND/nWaiting for isRxBroadcastCancel(1/0):");
scanf("%d",&isRxBroadcastCancel);
if(isRxBroadcastCancel)
{
f.state = IDLE;
break;
}
else
break;
default:
puts("default");
printf("%d/n",rand());
f.state = IDLE;
}
}
return 0;
}
#include
#include
#include
//Finite state machine declaration
//state declaration
#define IDLE 0 //idle state in rx mode
#define M_BROADCAST 1 //broadcast state in tx mode,broadcast to be a master point
#define M_WAIT_BROADCAST_ACK 2 //wait for broadcast ack state in rx mode,wait for the point ack in a specific time window
#define M_WAIT_COMMAND 3 //wait for command state,wait for PC command via UART
#define M_BROADCAST_CANCEL 4 //broadcast cancel state,broadcast to cancel master point
#define S_BROADCAST_ACK 5 //slave mode,send back self physical address
#define S_WAIT_COMMAND 6 //slave mode, wait for command from the master point
//state transition trig
//used in master mode
int isReqBeMaster = 0;//Is PC request the point to be master?
int isTimeout = 0;//Is time out?
int isReqCancelMaster = 0;//Is request to cancel master?
//used in slave mode
int isRxBroadcast = 0;//Is there a point broadcast to be master?
int isRxBroadcastCancel = 0;//Is receive broadcast cancel master?
typedef struct fsmtag
{
int state; //state
int timeouttime; //time out time in milliseconds
}fsm;
//function prototype
int main()
{
fsm f;
f.state = IDLE;
f.timeouttime = 0;
while(1)
{
switch(f.state)
{
case IDLE:
puts("IDLE/nWait for isReqBeMaster(1/0) isRxBroadcast(1/0):");
scanf("%d %d",&isReqBeMaster,&isRxBroadcast);
if(isReqBeMaster)
{
f.state = M_BROADCAST;
break;
}
else if(isRxBroadcast)
{
f.state = S_BROADCAST_ACK;
break;
}
else
break;
case M_BROADCAST:
puts("M_BROADCAST/nBroadcasting.../n");
f.state = M_WAIT_BROADCAST_ACK;
case M_WAIT_BROADCAST_ACK:
puts("M_WAIT_BROADCAST_ACK/nWaiting for isTimeout(1/0):");
scanf("%d",&isTimeout);
if(isTimeout)
{
f.state = M_WAIT_COMMAND;
break;
}
else
break;
case M_WAIT_COMMAND:
puts("M_WAIT_COMMAND/nWaiting for isReqCancelMaster(1/0):");
scanf("%d",&isReqCancelMaster);
if(isReqCancelMaster)
{
f.state = IDLE;
break;
}
else
break;
//Slave mode routine
case S_BROADCAST_ACK:
puts("S_BROADCAST_ACK/nAcking.../n");
f.state = S_WAIT_COMMAND;
break;
case S_WAIT_COMMAND:
puts("S_WAIT_COMMAND/nWaiting for isRxBroadcastCancel(1/0):");
scanf("%d",&isRxBroadcastCancel);
if(isRxBroadcastCancel)
{
f.state = IDLE;
break;
}
else
break;
default:
puts("default");
printf("%d/n",rand());
f.state = IDLE;
}
}
return 0;
}
【評】:很實用的一個狀態機器程式
☆──────────────────────傳說中的分隔字元──────────────────────────────☆
來源5:http://redbug.21ic.org/user1/349/archives/2007/44609.html
【轉載5】狀態機器的兩種寫法
2004/12/26 www.armecos.com asdjf@163.com
yy20041226-1v1
有限狀態機器FSM思想廣泛應用於硬體控制電路設計,也是軟體上常用的一種處理方法(軟體上稱為FMM--有限訊息機)。它把複雜的控制邏輯分解成有限個穩定點,在每個狀態上判斷事件,變連續處理為離散數文書處理,符合電腦的工作特點。同時,因為有限狀態機器具有有限個狀態,所以可以在實際的工程上實現。但這並不意味著其只能進行有限次的處理,相反,有限狀態機器是閉環系統,有限無窮,可以用有限的狀態,處理無窮的事務。
有限狀態機器的工作原理1所示,發生事件(event)後,根據目前狀態(cur_state),決定執行的動作(action),並設定下一個狀態號(nxt_state)。
-------------
| |-------->執行動作action
發生事件event ----->| cur_state |
| |-------->設定下一狀態號nxt_state
-------------
目前狀態
圖1 有限狀態機器工作原理
e0/a0
--->--
| |
-------->----------
e0/a0 | | S0 |-----
| -
#include
#include
typedef enum{
STATE0 = 0,
STATE1,
STATE2,
STATE3,
STATE4,//password pass
//...ADD here
}STATE;
typedef enum{
INPUT1 = '2',
INPUT2 = '4',
INPUT3 = '7',
INPUT4 = '9',
}INPUT;
int main()
{
char ch;
STATE current_state = STATE0;
while(1){
printf("請輸入數字進行解碼:");
while((ch = getchar()) != '/n'){
if((ch '9')){
printf("非數字,請重新輸入!/n");
break;
}
switch(current_state){
case STATE0:
if(ch == '2') current_state = STATE1;
break;
case STATE1:
if(ch == '4') current_state = STATE2;
break;
case STATE2:
if(ch == '7') current_state = STATE3;
break;
case STATE3:
if(ch == '9') current_state = STATE4;
break;
default:
current_state = STATE0;
break;
}
}
if(current_state == STATE4){
printf("Correct, lock is open!/n");
current_state = STATE0;
}else
{
printf("Wrong, unlocked!/n");
current_state = STATE0;
}
return 0;
}