Write simples state machine on c++ programming language
时间: 2023-12-25 12:04:23 浏览: 38
Here's an example of a simple state machine in C programming language:
```
#include <stdio.h>
// Define the states
enum State {
STATE_A,
STATE_B,
STATE_C
};
int main() {
// Initialize the current state to STATE_A
enum State current_state = STATE_A;
// Run the state machine
while (1) {
switch (current_state) {
case STATE_A:
printf("Currently in STATE_A\n");
// Transition to STATE_B
current_state = STATE_B;
break;
case STATE_B:
printf("Currently in STATE_B\n");
// Transition to STATE_C
current_state = STATE_C;
break;
case STATE_C:
printf("Currently in STATE_C\n");
// Transition back to STATE_A
current_state = STATE_A;
break;
default:
printf("Invalid state!\n");
return 1;
}
}
return 0;
}
```
This state machine consists of three states: STATE_A, STATE_B, and STATE_C. It starts in STATE_A and transitions to STATE_B, then to STATE_C, and finally back to STATE_A. The state machine runs indefinitely in a while loop, and each state is printed to the console as it is entered.