Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize C++ finite State Machine

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report