状态机在project中使用很的频繁,有例如以下常见的三种实现方法:

1. switch-case 实现。适合简单的状态机。

2. 二维状态表state-event实现。逻辑清晰。可是矩阵通常比較稀疏,并且维护麻烦。

3. 用状态转移表stateTransfer Table实现,数组大小等于状体转移边个数,易扩展;

以下用一个样例来进行具体说明,描写叙述的例如以下场景:

描写叙述对象:门

状态:开着、关着、锁着 (这里的关着指关了但未锁的状态)

事件:开门、关门、上锁、解锁

代码实现用枚举来定义状态和事件,操作数据节点转移到目的状态用函数实现。枚举本身默认是从0開始的int类型,利用这个特点将状态转移函数放到函数指针数组中与状态相应起来。方便操作。

核心数据结构例如以下:

状态:枚举类型

事件:枚举类型

状态转移结构体:{当前状态、事件、下个状态},定义一个全局数组来使用

状态变更函数:到下个状态(放到数组中与状态枚举相应起来)

此种实现方法easy扩展,添加状态和事件都比較easy。

假设存在一个状态通过相应事件能够转移到多个状态的情形,则能够扩展状态转移函数。或者在状态转移结构中添加一个推断函数字段。

代码实现例如以下:

#include <iostream>
using namespace std; typedef enum{
OPENED,
CLOSED,
LOCKED,
} State; typedef enum{
OPEN,
CLOSE,
LOCK,
UNLOCK
} Event; typedef struct{
State currentState;
Event event;
State NextState;
} StateTransfer; typedef struct{
State state;
int transferTimes;
}Door; StateTransfer g_stateTransferTable[]{
{OPENED, CLOSE, CLOSED},
{CLOSED, OPEN, OPENED},
{CLOSED, LOCK, LOCKED},
{LOCKED, UNLOCK, CLOSED},
}; void toOpen(Door& door);
void toClose(Door& door);
void toLock(Door& door);
typedef void (*pfToState)(Door& door);
pfToState g_pFun[] = {toOpen, toClose, toLock}; //状态枚举值相应下标 void toOpen(Door& door){
door.state = OPENED;
cout << "open the door!\n";
} void toClose(Door& door){
door.state = CLOSED;
cout << "close the door!\n";
} void toLock(Door& door){
door.state = LOCKED;
cout << "lock the door!\n";
} void transfer(Door& door,const Event event){
for (int i = 0; i < sizeof(g_stateTransferTable)/sizeof(StateTransfer); ++i) {
if(door.state == g_stateTransferTable[i].currentState &&
event == g_stateTransferTable[i].event){
g_pFun[g_stateTransferTable[i].NextState](door);
door.transferTimes++;
cout << "transfer ok!\n";
return;
}
}
cout << "This event cannot transfer current state!!\n";
return;
} void printDoor(const Door& door){
string stateNote[] = {"opened","closed","locked"}; // 下标正好相应状态枚举值
cout << "the door's state is: " << stateNote[door.state] << endl;
cout << "the door transfer times is: " << door.transferTimes << endl;
} int main(){
Door door = {CLOSED, 0};
printDoor(door);
transfer(door, OPEN);
printDoor(door);
transfer(door, LOCK);
printDoor(door);
transfer(door, CLOSE);
printDoor(door);
return 0;
}

执行结果例如以下:

the door’s state is: closed

the door transfer times is: 0

open the door!

transfer ok!

the door’s state is: opened

the door transfer times is: 1

This event cannot transfer current state!!

the door’s state is: opened

the door transfer times is: 1

close the door!

transfer ok!

the door’s state is: closed

the door transfer times is: 2

C/C++用状态转移表联合函数指针数组实现状态机FSM的更多相关文章

  1. C 函数指针 函数指针数组 转移表

    内容来自<c和指针>,整理后方便个人理解 高级声明 cdel程序可以方便的给出声明的释义 指向函数的指针 int ( *f ) ( int n_values, float amount ) ...

  2. c语言.函数指针数组

    函数指针: 一个指向函数的指针.一般用函数名表示. 函数指针数组:元素为函数指针的数组.转移表.c语言中函数不可以定义为数组,只能通过定义函数指针来操作. #include<stdio.h> ...

  3. 转:函数指针数组的妙用(I)

    转自:http://blog.sina.com.cn/s/blog_4c78b35f010008hi.html 笔者在开发某软件过程中遇到这样一个问题,前级模块传给我二进制数据,输入参数为 char* ...

  4. C++基础——函数指针 函数指针数组

    ==================================声明================================== 本文版权归作者所有. 本文原创,转载必须在正文中显要地注明 ...

  5. typedef 函数指针 数组 std::function

    1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA,  *PDATA;  //D ...

  6. C#委托与C语言函数指针及函数指针数组

    C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...

  7. C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组

    #include<stdio.h> #include<stdlib.h> #include<windows.h> /* 举列子说明什么是函数指针 */ //以一个加 ...

  8. C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子

    先来不带参数的回调函数例子 #include <iostream> #include <windows.h> void printFunc() { std::cout<& ...

  9. C 函数指针数组

    名字有点绕口,其实更应该翻译为指针函数数组. 记录下对Head-First C这一节的理解,几乎每天班车上都会咪两眼,几乎每次都是看不懂,敲一敲的时候才有些明白. 通俗点讲,这功能解决的是,具有同种签 ...

随机推荐

  1. Leetcode 407.接雨水

    接雨水 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水. 说明: m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小 ...

  2. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. UITableView滑动动画+FPSLabel

    主要使用了tableView的代理方法 行将要显示的时候 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableView ...

  4. BZOJ 1007 [HNOI2008]水平可见直线 ——计算几何

    用了trinkle的方法,半平面交转凸包. 写了一发,既没有精度误差,也很好写. #include <map> #include <ctime> #include <cm ...

  5. BZOJ 1026: [SCOI2009]windy数 【数位dp】

    Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? In ...

  6. 洛谷P2522 - [HAOI2011]Problem b

    Portal Description 进行\(T(T\leq10^5)\)次询问,每次给出\(x_1,x_2,y_1,y_2\)和\(d\)(均不超过\(10^5\)),求\(\sum_{i=x_1} ...

  7. 刷题总结——鸭舌(ssoi)

    题目: 题目背景 CF 77C 题目描述 小美喜欢吃鸭舌.有一个 n 个点的树,每个节点 i ,第 i 个点上有 ai 个鸭舌.小美一开始处于 x 号点.每次小美可以选择一个与现在的点有边的点而且那个 ...

  8. 刷题总结——电影(ssoi)

    题目: 题目背景 SOURCE:NOIP2014-SXYZ T2 题目描述 小美去看电影,发现这个电影票很神奇,有一个编号 (x,y) 表示为第 x 排第 y 位. 小美是个聪明的女孩子,她有自己的一 ...

  9. 如何快速下载maven依赖jar包

    找到settings.xml文件.在mirrors里面添加下面的代码: <mirror> <id>alimaven</id> <mirrorOf>cen ...

  10. mysql语句优化方案(网上流传)

    关于mysql处理百万级以上的数据时如何提高其查询速度的方法 最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当mysql表的数 ...