相关资料来自Finite-state machine
可以看做一个抽象的机器,一次只能在一个状态。如果被一个触发事件或者条件所发起,可以从一个状态变到另外一个状态,这个过程叫做转变。一个特定的Finite-state machine(FSM)有有限个状态,每一种转变有一个特定的触发条件。
但是只有有限个状态,所以FSM得能力不如其他的计算模型,例如图灵机Turing machine。
Event-driven finite-state machine
下面的程序就是一个车上的播放器的简单表示,总共两个状态:radio模式和CD模式。
If (mode change)
radio to CD back and forth;
if (go to next)
next preset for radio or next CD track;
用到了枚举和switch语句。
/********************************************************************/
#include <stdio.h>
/********************************************************************/
typedef enum {
ST_RADIO,
ST_CD
} STATES;
typedef enum {
EVT_MODE,
EVT_NEXT
} EVENTS;
EVENTS readEventFromMessageQueue(void);
/********************************************************************/
int main(void)
{
/* Default state is radio */
STATES state = ST_RADIO;
int stationNumber = 0;
int trackNumber = 0;
/* Infinite loop */
while(1)
{
/* Read the next incoming event. Usually this is a blocking function. */
EVENTS event = readEventFromMessageQueue();
/* Switch the state and the event to execute the right transition. */
switch(state)
{
case ST_RADIO:
switch(event)
{
case EVT_MODE:
/* Change the state */
state = ST_CD;
break;
case EVT_NEXT:
/* Increase the station number */
stationNumber++;
break;
}
break;
case ST_CD:
switch(event)
{
case EVT_MODE:
/* C

本文探讨了有限状态机(FSM)的概念,它是一种只能处于有限数量状态的抽象机器,根据触发事件或条件进行状态转换。讨论了事件驱动的FSM和基于自动机的编程,特别是独立的自动机步骤程序和显式状态转换表的使用。通过状态转换表详细解释了FSM如何根据输入字符进行状态变化和输出决策。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



