Devi implementare la funzione di transizione in modo da passare da uno stato all'altro in base allo stato e al carattere corrente. Quando ti trovi in uno stato finale, stampi la stringa di riconoscimento.
El còdigo fuente:
codice:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
typedef enum tagStati
{
S0,
S1,
S2,
S3,
S4,
S5,
S6,
S7,
S8
} Stati;
void Automa()
{
Stati stato = S0;
int c = 0;
while ( c != 'X' )
{
//c = toupper(getch());
c = toupper(_getch());
if ( c == 'X' )
{
printf("digitato X\n");
break;
}
else if ( c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' )
{
printf("digitato %c\n", c);
}
switch(stato)
{
case S0:
if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
case S1:
if ( c == 'A' )
stato = S2;
else if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
case S2:
if ( c == 'B' )
stato = S3;
else if ( c == 'C' )
stato = S1;
break;
case S3:
if ( c == 'E' )
stato = S4;
else if ( c == 'A' )
stato = S6;
else if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
case S4:
if ( c == 'D' )
{
printf("Fermi tutti!!! CABED riconosciuta!\n");
stato = S0;
}
else if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
case S5:
if ( c == 'A' )
stato = S6;
else if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
case S6:
if ( c == 'B' )
stato = S7;
else if ( c == 'C' )
stato = S1;
break;
case S7:
if ( c == 'A' )
stato = S8;
else if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
case S8:
if ( c == 'B' )
{
printf("Fermi tutti!!! BABAB riconosciuta!\n");
stato = S0;
}
else if ( c == 'C' )
stato = S1;
else if ( c == 'B' )
stato = S5;
break;
}
}
}
int main()
{
Automa();
return 0;
}