Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Finite State Machine Program Example (ECE 2534)
// 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; }
run
|
edit
|
history
|
help
0
Binary to Integer (C)
Project 3 Part 1 v2
18BCE2182 ASSESS_3 Q3
K&R/1_3
300
B_141118_Euclides
Code2
B_141202_CTYPE
es collar
MATRIZ DE CADENAS