Bloomberg LP Interview Question
Software Engineer / Developers/* State machine implementation
*/
#include <stdio.h>
#include <stdlib.h>
typedef enum State {S1, S2, S3, S4} State;
void f1(State *stateMachine)
{
printf("State: S1\n");
*stateMachine = S2;
}
void f2(State *stateMachine)
{
printf("State: S2\n");
*stateMachine = S3;
}
void f3(State *stateMachine)
{
printf("State: S3\n");
*stateMachine = S4;
}
void f4(State *stateMachine)
{
printf("State: S4\n");
*stateMachine = S1;
}
void (*func[4])(State *) = {f1, f2, f3, f4};
int main()
{
State stateMachine = S1;
int i = 0;
while (i < 100)
{
switch (stateMachine)
{
case S1:
func[S1](&stateMachine);
break;
case S2:
func[S2](&stateMachine);
break;
case S3:
func[S3](&stateMachine);
break;
case S4:
func[S4](&stateMachine);
break;
default:
break;
}
i++;
}
}
Is this the same as set and reset bits using a hash table for each state of the machine
- Java Coder November 26, 2007