Questa invece è una versione modificata del programma di Vincenzo1968 che fa uso di un minor numero di stati interni, a patto di introdurre due nuove variabili con funzione di selettore e carattere precedente.
codice:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
typedef enum tagStati
{
S0,
S10,
S11,
S2,
S30,
S31
} Stati;
void Automa()
{
Stati stato = S0;
// s= selettore; prv= carattere precedentemente digitato
int s,prv,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' ) || ( c == 'B' ))
{
prv=c;
stato = S10;
}
else
stato = S0;
break;
case S10:
if (prv=='C')
s=0;
else
s=1;
if ( c == 'A' )
stato = S11;
else if (( c == 'C' ) || ( c == 'B' ))
{
prv=c;
stato = S10;
}
else
stato = S0;
break;
case S11:
if ( c == 'B' )
stato = S2;
else if ( c == 'C' )
{
prv=c;
stato = S10;
}
else
stato = S0;
break;
case S2:
if (( c == 'E' ) && ( s == 0 ))
stato = S30;
else if (( c == 'A') && ( s == 1))
stato = S31;
else if (( c == 'C' ) || ( c == 'B' ))
{
prv=c;
stato = S10;
}
else
stato = S0;
break;
case S30:
if ( c == 'D' )
{
printf("Fermi tutti!!! CABED riconosciuta!\n");
stato = S0;
}
else if (( c == 'C' ) || ( c == 'B' ))
{
prv=c;
stato = S10;
}
else stato = S0;
break;
case S31:
if ( c == 'B' )
{
printf("Fermi tutti!!! BABAB riconosciuta!\n");
stato = S0;
}
else if ( c == 'C' )
{
prv=c;
stato = S10;
}
else stato = S0;
break;
}
}
}
int main()
{
Automa();
return 0;
}
Saluti