Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Finite State Machine Program Example (ECE 2534)
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
// Example Finite State Machine (FSM) program example #include <stdio.h> // When turning in your code for HW 3, only include from here... //----------------- START CUT HERE ----------------------------- // My states typedef enum states {STATE_INIT, STATE_A, STATE_B, STATE_EXIT} State; // My transitiion functions State computeTransitionFromStateA(int value) { State returnState = STATE_A; if (value==1) { returnState = STATE_B; } return(returnState); } State computeTransitionFromStateB(int value) { State returnState = STATE_B; if (value==2) { returnState = STATE_A; } else if (value==(-1)) { returnState = STATE_EXIT; } return(returnState); } // My finite state machine State finiteStateMachine(int num) { static State systemState = STATE_INIT; switch (systemState) { case STATE_INIT: systemState = STATE_A; break; case STATE_A: systemState = computeTransitionFromStateA(num); break; case STATE_B: systemState = computeTransitionFromStateB(num); break; default: printf("Should never get here!\n"); exit(0); } return(systemState); } //----------------- END CUT HERE ----------------------------- // When turning in your code for HW 3, end code you include here... int main(void) { unsigned int inputNumber; // number that we read in from stdin State returnState; // new state returned by FSM int returnValueScanf; printf("\n*** Finite state machine program example ***\n\n"); while(1) { // Read in next integer returnValueScanf = scanf("%d\n", &inputNumber); // check to see if input ended if (returnValueScanf < 1) { printf("Exiting because of EOF\n"); exit(-1); } // print out the input number printf("Read in the value %d. ", inputNumber); // call the FSM returnState = finiteStateMachine(inputNumber); // Print out the result printf("Returned FSM state is %d.\n", returnState); // exit if STATE_D if (returnState == STATE_EXIT) { printf("Exiting\n"); exit(0); } } return 0; }
gcc
1 1 1 1 2 -1 1 0 2 1 -1 2
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 0.12 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0,35 sec
edit mode
|
history
|
discussion