Description: For this week\'s lab you will program a simple finite state machine
ID: 3814750 • Letter: D
Question
Description: For this week's lab you will program a simple finite state machine using structures. The FSM will model a rudimentary vending machine algorithm which will add nickels and dimes up to $0.20. Lask Specifics: The Moore type finite state machine described in Table-1 is to be implemented in a Cprogramusing structures and pointers. We will assume that it willbe impossible for A and B to both be 1 at the same time. Present State Present Output inputs Next State Table-1: Finite State Machine Table. The following structure is to be used to store data foreach state. struct FSM state unsigned int id; state number //output variable z unsigned int z; struct FSM state next: //pointer to next state The program should start in state 0.The program should ask the user for the input values A and B (A represents a nickel, Brepresents a dime). The program should then go to the next state based on the inputs, display the current state number as well as the current output value ofZ(which represents the current total), then wait for the next input values. Use a pointer to keep track of the current state. Allow the user to enter a 2 for input A to exit out ofthe program. EE 107- Spring 2017Explanation / Answer
#include <stdio.h>
struct FSM_state {
unsigned int id; //state number
unsigned int z; //output variable Z
struct FSM_state *next; // pointer to next state
};
int main()
{
char A ;
char B ;
int T = 0;
int ci = 0;
struct FSM_state * current = NULL;
current = malloc(sizeof(struct FSM_state));
current->id = 0 ;
current->z = 0 ;
ci = current->id ;
T = current->z ;
printf("The start state is %d and output value is %d",current->id,current->z);
printf(" Please enter binary values for A,B without space or 2 for A to exit:");
A= getchar();
B= getchar();
while(A!=2&¤t->z<20)
{
if(A=='0'&&B=='0')
{
printf("The current state is %d and output value is %d",current->id,current->z);
printf(" Please enter binary values for A,B without space or 2 for A to exit:");
A= getchar();
B= getchar();
continue;
}
else if(A=='1'&&B=='0'&&ci<4)
{
current->next = malloc(sizeof(struct FSM_state));
current = current->next ;
current->id = ++ci ;
T += 5 ;
current->z = T ;
printf("The current state is %d and output value is %d",current->id,current->z);
printf(" Please enter binary values for A,B without space or 2 for A to exit:");
A= getchar();
B= getchar();
continue;
}
else if((A=='1'&&B=='0'&&ci==4)||(A=='0'&&B=='1'&&(ci==4||ci==2||ci==3)))
{
current->next = malloc(sizeof(struct FSM_state));
current = current->next ;
current->id = 0 ;
current->z = T ;
printf("The current state is %d and output value is %d",current->id,current->z);
printf(" Please enter binary values for A,B without space or 2 for A to exit:");
A= getchar();
B= getchar();
continue;
}
else if(A=='0'&&B=='1')
{
current->next = malloc(sizeof(struct FSM_state));
current = current->next ;
ci +=2 ;
current->id = ci ;
T += 10 ;
current->z = T ;
printf("The current state is %d and output value is %d",current->id,current->z);
printf(" Please enter binary values for A,B without space or 2 for A to exit:");
A= getchar();
B= getchar();
continue;
}
}
return 0;
}