Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize C++ finite State Machine

Shulou Source: shulou.com Published: 2022-06-03 07:36:08 09月30日 Update

This article mainly explains "how to implement C++ finite state machine". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to implement the C++ finite state machine.

Catalogue

Four elements of finite state machine

Pointer implementation of C++ function

Four elements of finite state machine

Present state: current state

Secondary state: the next state to be transferred when the condition is met.

Action: an action performed when an event is satisfied; after the action is completed, it can be transferred to another state or maintained.

Condition: a condition required to transfer a state, which triggers an action or makes a state transition when the condition is met.

Pointer implementation of C++ function

Case study: students' daily life.

Students' daily life includes the following states: get up, go to school, have lunch, do homework, and go to bed.

The transition between each state requires the execution of the corresponding event.

I am divided into the following steps to achieve:

(1) draw the state transition diagram

(2) create a state transition FSMItem class

Enumeration: all state State, all event Event

Member variables: current _ curState, event _ event, secondary _ nextState

Member functions: action function

(3) create the finite state machine FSM class

Member variables: state transition table vector _ fsmTable

Member functions: initialize state transition table, state transition, and perform corresponding actions according to events

(4) Test FSM

(1) draw the state transition diagram

(2) FSMItem class

/ / FSM status item class FSMItem {friend class FSM;private: / / Action function static void getUp () {cout

Tags: State Action event function finite restricted condition member Test content variable Student pointer element Learning Life practical deeper Intermediate interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn macOS Apple Huawei